blob: 3cd2b8e7540addaad3ec2b951d3c33cda6fb718d [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 Dunbar16cdcb32009-07-28 22:40:46 +0000554 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000555 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000556
Daniel Dunbar3f872332009-07-28 16:08:33 +0000557 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000558 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000559
560 // Eat the end of statement marker.
561 Lexer.Lex();
562
563 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000564 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000565
566 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000567 return false;
568}
Chris Lattner9a023f72009-06-24 04:43:34 +0000569
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000571 // FIXME: Use better location, we should use proper tokens.
572 SMLoc EqualLoc = Lexer.getLoc();
573
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000574 MCValue Value;
575 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000576 return true;
577
Daniel Dunbar3f872332009-07-28 16:08:33 +0000578 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000579 return TokError("unexpected token in assignment");
580
581 // Eat the end of statement marker.
582 Lexer.Lex();
583
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000584 // Diagnose assignment to a label.
585 //
586 // FIXME: Diagnostics. Note the location of the definition as a label.
587 // FIXME: This doesn't diagnose assignment to a symbol which has been
588 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000589 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000590 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000591 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000592 if (Sym->getSection())
593 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
594 if (Sym->isExternal())
595 return Error(EqualLoc, "invalid assignment to external symbol");
596
597 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000598 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000599
600 return false;
601}
602
603/// ParseDirectiveSet:
604/// ::= .set identifier ',' expression
605bool AsmParser::ParseDirectiveSet() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000606 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000607 return TokError("expected identifier after '.set' directive");
608
Daniel Dunbar419aded2009-07-28 16:38:40 +0000609 StringRef Name = Lexer.getTok().getString();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000610
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000611 if (Lexer.Lex().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000612 return TokError("unexpected token in '.set'");
613 Lexer.Lex();
614
615 return ParseAssignment(Name, true);
616}
617
Chris Lattner9a023f72009-06-24 04:43:34 +0000618/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000619/// ::= .section identifier (',' identifier)*
620/// FIXME: This should actually parse out the segment, section, attributes and
621/// sizeof_stub fields.
622bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000623 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000624 return TokError("expected identifier after '.section' directive");
625
Daniel Dunbar419aded2009-07-28 16:38:40 +0000626 std::string Section = Lexer.getTok().getString();
Chris Lattner9a023f72009-06-24 04:43:34 +0000627 Lexer.Lex();
628
629 // Accept a comma separated list of modifiers.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000630 while (Lexer.is(AsmToken::Comma)) {
Chris Lattner9a023f72009-06-24 04:43:34 +0000631 Lexer.Lex();
632
Daniel Dunbar3f872332009-07-28 16:08:33 +0000633 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000634 return TokError("expected identifier in '.section' directive");
635 Section += ',';
Daniel Dunbar419aded2009-07-28 16:38:40 +0000636 Section += Lexer.getTok().getString().str();
Chris Lattner9a023f72009-06-24 04:43:34 +0000637 Lexer.Lex();
638 }
639
Daniel Dunbar3f872332009-07-28 16:08:33 +0000640 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000641 return TokError("unexpected token in '.section' directive");
642 Lexer.Lex();
643
644 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
645 return false;
646}
647
Chris Lattner529fb542009-06-24 05:13:15 +0000648bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
649 const char *Directives) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000650 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000651 return TokError("unexpected token in section switching directive");
652 Lexer.Lex();
653
654 std::string SectionStr = Section;
655 if (Directives && Directives[0]) {
656 SectionStr += ",";
657 SectionStr += Directives;
658 }
659
660 Out.SwitchSection(Ctx.GetSection(Section));
661 return false;
662}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000663
664/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000665/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000666bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000667 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000668 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000669 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000670 return TokError("expected string in '.ascii' or '.asciz' directive");
671
672 // FIXME: This shouldn't use a const char* + strlen, the string could have
673 // embedded nulls.
674 // FIXME: Should have accessor for getting string contents.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000675 StringRef Str = Lexer.getTok().getString();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 Out.EmitBytes(Str.substr(1, Str.size() - 2));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000677 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000679
680 Lexer.Lex();
681
Daniel Dunbar3f872332009-07-28 16:08:33 +0000682 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000683 break;
684
Daniel Dunbar3f872332009-07-28 16:08:33 +0000685 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000686 return TokError("unexpected token in '.ascii' or '.asciz' directive");
687 Lexer.Lex();
688 }
689 }
690
691 Lexer.Lex();
692 return false;
693}
694
695/// ParseDirectiveValue
696/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
697bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000698 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000699 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000700 MCValue Expr;
701 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000702 return true;
703
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000704 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000705
Daniel Dunbar3f872332009-07-28 16:08:33 +0000706 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000707 break;
708
709 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000710 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000711 return TokError("unexpected token in directive");
712 Lexer.Lex();
713 }
714 }
715
716 Lexer.Lex();
717 return false;
718}
719
720/// ParseDirectiveSpace
721/// ::= .space expression [ , expression ]
722bool AsmParser::ParseDirectiveSpace() {
723 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000724 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000725 return true;
726
727 int64_t FillExpr = 0;
728 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000729 if (Lexer.isNot(AsmToken::EndOfStatement)) {
730 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000731 return TokError("unexpected token in '.space' directive");
732 Lexer.Lex();
733
Daniel Dunbar475839e2009-06-29 20:37:27 +0000734 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000735 return true;
736
737 HasFillExpr = true;
738
Daniel Dunbar3f872332009-07-28 16:08:33 +0000739 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000740 return TokError("unexpected token in '.space' directive");
741 }
742
743 Lexer.Lex();
744
745 if (NumBytes <= 0)
746 return TokError("invalid number of bytes in '.space' directive");
747
748 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
749 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
750 Out.EmitValue(MCValue::get(FillExpr), 1);
751
752 return false;
753}
754
755/// ParseDirectiveFill
756/// ::= .fill expression , expression , expression
757bool AsmParser::ParseDirectiveFill() {
758 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000759 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000760 return true;
761
Daniel Dunbar3f872332009-07-28 16:08:33 +0000762 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000763 return TokError("unexpected token in '.fill' directive");
764 Lexer.Lex();
765
766 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000768 return true;
769
Daniel Dunbar3f872332009-07-28 16:08:33 +0000770 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000771 return TokError("unexpected token in '.fill' directive");
772 Lexer.Lex();
773
774 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000775 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000776 return true;
777
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000779 return TokError("unexpected token in '.fill' directive");
780
781 Lexer.Lex();
782
783 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
784 return TokError("invalid '.fill' size, expected 1, 2, or 4");
785
786 for (uint64_t i = 0, e = NumValues; i != e; ++i)
787 Out.EmitValue(MCValue::get(FillExpr), FillSize);
788
789 return false;
790}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000791
792/// ParseDirectiveOrg
793/// ::= .org expression [ , expression ]
794bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000795 MCValue Offset;
796 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000797 return true;
798
799 // Parse optional fill expression.
800 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000801 if (Lexer.isNot(AsmToken::EndOfStatement)) {
802 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000803 return TokError("unexpected token in '.org' directive");
804 Lexer.Lex();
805
Daniel Dunbar475839e2009-06-29 20:37:27 +0000806 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000807 return true;
808
Daniel Dunbar3f872332009-07-28 16:08:33 +0000809 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000810 return TokError("unexpected token in '.org' directive");
811 }
812
813 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000814
815 // FIXME: Only limited forms of relocatable expressions are accepted here, it
816 // has to be relative to the current section.
817 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000818
819 return false;
820}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000821
822/// ParseDirectiveAlign
823/// ::= {.align, ...} expression [ , expression [ , expression ]]
824bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
825 int64_t Alignment;
826 if (ParseAbsoluteExpression(Alignment))
827 return true;
828
829 SMLoc MaxBytesLoc;
830 bool HasFillExpr = false;
831 int64_t FillExpr = 0;
832 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000833 if (Lexer.isNot(AsmToken::EndOfStatement)) {
834 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000835 return TokError("unexpected token in directive");
836 Lexer.Lex();
837
838 // The fill expression can be omitted while specifying a maximum number of
839 // alignment bytes, e.g:
840 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +0000841 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000842 HasFillExpr = true;
843 if (ParseAbsoluteExpression(FillExpr))
844 return true;
845 }
846
Daniel Dunbar3f872332009-07-28 16:08:33 +0000847 if (Lexer.isNot(AsmToken::EndOfStatement)) {
848 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000849 return TokError("unexpected token in directive");
850 Lexer.Lex();
851
852 MaxBytesLoc = Lexer.getLoc();
853 if (ParseAbsoluteExpression(MaxBytesToFill))
854 return true;
855
Daniel Dunbar3f872332009-07-28 16:08:33 +0000856 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000857 return TokError("unexpected token in directive");
858 }
859 }
860
861 Lexer.Lex();
862
863 if (!HasFillExpr) {
864 // FIXME: Sometimes fill with nop.
865 FillExpr = 0;
866 }
867
868 // Compute alignment in bytes.
869 if (IsPow2) {
870 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000871 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000872 }
873
874 // Diagnose non-sensical max bytes to fill.
875 if (MaxBytesLoc.isValid()) {
876 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000877 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
878 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000879 return false;
880 }
881
882 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000883 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
884 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000885 MaxBytesToFill = 0;
886 }
887 }
888
889 // FIXME: Target specific behavior about how the "extra" bytes are filled.
890 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
891
892 return false;
893}
894
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000895/// ParseDirectiveSymbolAttribute
896/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
897bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000898 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000899 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000900 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000901 return TokError("expected identifier in directive");
902
Daniel Dunbar419aded2009-07-28 16:38:40 +0000903 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000904 Lexer.Lex();
905
906 // If this is use of an undefined symbol then mark it external.
907 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
908 Sym->setExternal(true);
909
910 Out.EmitSymbolAttribute(Sym, Attr);
911
Daniel Dunbar3f872332009-07-28 16:08:33 +0000912 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000913 break;
914
Daniel Dunbar3f872332009-07-28 16:08:33 +0000915 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000916 return TokError("unexpected token in directive");
917 Lexer.Lex();
918 }
919 }
920
921 Lexer.Lex();
922 return false;
923}
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000924
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000925/// ParseDirectiveDarwinSymbolDesc
926/// ::= .desc identifier , expression
927bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000928 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000929 return TokError("expected identifier in directive");
930
931 // handle the identifier as the key symbol.
932 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000933 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000934 Lexer.Lex();
935
Daniel Dunbar3f872332009-07-28 16:08:33 +0000936 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000937 return TokError("unexpected token in '.desc' directive");
938 Lexer.Lex();
939
940 SMLoc DescLoc = Lexer.getLoc();
941 int64_t DescValue;
942 if (ParseAbsoluteExpression(DescValue))
943 return true;
944
Daniel Dunbar3f872332009-07-28 16:08:33 +0000945 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000946 return TokError("unexpected token in '.desc' directive");
947
948 Lexer.Lex();
949
950 // Set the n_desc field of this Symbol to this DescValue
951 Out.EmitSymbolDesc(Sym, DescValue);
952
953 return false;
954}
955
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000956/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +0000957/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
958bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000959 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000960 return TokError("expected identifier in directive");
961
962 // handle the identifier as the key symbol.
963 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000964 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000965 Lexer.Lex();
966
Daniel Dunbar3f872332009-07-28 16:08:33 +0000967 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000968 return TokError("unexpected token in directive");
969 Lexer.Lex();
970
971 int64_t Size;
972 SMLoc SizeLoc = Lexer.getLoc();
973 if (ParseAbsoluteExpression(Size))
974 return true;
975
976 int64_t Pow2Alignment = 0;
977 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000978 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000979 Lexer.Lex();
980 Pow2AlignmentLoc = Lexer.getLoc();
981 if (ParseAbsoluteExpression(Pow2Alignment))
982 return true;
983 }
984
Daniel Dunbar3f872332009-07-28 16:08:33 +0000985 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +0000986 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000987
988 Lexer.Lex();
989
Chris Lattner1fc3d752009-07-09 17:25:12 +0000990 // NOTE: a size of zero for a .comm should create a undefined symbol
991 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000992 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +0000993 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
994 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000995
996 // NOTE: The alignment in the directive is a power of 2 value, the assember
997 // may internally end up wanting an alignment in bytes.
998 // FIXME: Diagnose overflow.
999 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001000 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1001 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001002
1003 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1004 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1005 return Error(IDLoc, "invalid symbol redefinition");
1006
Chris Lattner1fc3d752009-07-09 17:25:12 +00001007 // Create the Symbol as a common or local common with Size and Pow2Alignment
1008 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001009
1010 return false;
1011}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001012
1013/// ParseDirectiveDarwinZerofill
1014/// ::= .zerofill segname , sectname [, identifier , size_expression [
1015/// , align_expression ]]
1016bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001017 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001018 return TokError("expected segment name after '.zerofill' directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001019 std::string Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001020 Lexer.Lex();
1021
Daniel Dunbar3f872332009-07-28 16:08:33 +00001022 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001023 return TokError("unexpected token in directive");
1024 Section += ',';
1025 Lexer.Lex();
1026
Daniel Dunbar3f872332009-07-28 16:08:33 +00001027 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001028 return TokError("expected section name after comma in '.zerofill' "
1029 "directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001030 Section += Lexer.getTok().getString().str();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001031 Lexer.Lex();
1032
1033 // FIXME: we will need to tell GetSection() that this is to be created with or
1034 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1035 // below could be done but for now it is not as EmitZerofill() does not know
1036 // how to deal with a section type in the section name like
1037 // ParseDirectiveDarwinSection() allows.
1038 // Section += ',';
1039 // Section += "zerofill";
1040
1041 // If this is the end of the line all that was wanted was to create the
1042 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001043 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001044 // Create the zerofill section but no symbol
1045 Out.EmitZerofill(Ctx.GetSection(Section.c_str()));
1046 return false;
1047 }
1048
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001050 return TokError("unexpected token in directive");
1051 Lexer.Lex();
1052
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001054 return TokError("expected identifier in directive");
1055
1056 // handle the identifier as the key symbol.
1057 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001058 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001059 Lexer.Lex();
1060
Daniel Dunbar3f872332009-07-28 16:08:33 +00001061 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001062 return TokError("unexpected token in directive");
1063 Lexer.Lex();
1064
1065 int64_t Size;
1066 SMLoc SizeLoc = Lexer.getLoc();
1067 if (ParseAbsoluteExpression(Size))
1068 return true;
1069
1070 int64_t Pow2Alignment = 0;
1071 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001072 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001073 Lexer.Lex();
1074 Pow2AlignmentLoc = Lexer.getLoc();
1075 if (ParseAbsoluteExpression(Pow2Alignment))
1076 return true;
1077 }
1078
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001080 return TokError("unexpected token in '.zerofill' directive");
1081
1082 Lexer.Lex();
1083
1084 if (Size < 0)
1085 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1086 "than zero");
1087
1088 // NOTE: The alignment in the directive is a power of 2 value, the assember
1089 // may internally end up wanting an alignment in bytes.
1090 // FIXME: Diagnose overflow.
1091 if (Pow2Alignment < 0)
1092 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1093 "can't be less than zero");
1094
1095 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1096 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1097 return Error(IDLoc, "invalid symbol redefinition");
1098
1099 // Create the zerofill Symbol with Size and Pow2Alignment
1100 Out.EmitZerofill(Ctx.GetSection(Section.c_str()), Sym, Size, Pow2Alignment);
1101
1102 return false;
1103}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001104
1105/// ParseDirectiveDarwinSubsectionsViaSymbols
1106/// ::= .subsections_via_symbols
1107bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001108 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001109 return TokError("unexpected token in '.subsections_via_symbols' directive");
1110
1111 Lexer.Lex();
1112
Kevin Enderbyf96db462009-07-16 17:56:39 +00001113 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001114
1115 return false;
1116}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001117
1118/// ParseDirectiveAbort
1119/// ::= .abort [ "abort_string" ]
1120bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001121 // FIXME: Use loc from directive.
1122 SMLoc Loc = Lexer.getLoc();
1123
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001125 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1126 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001127 return TokError("expected string in '.abort' directive");
1128
Daniel Dunbar419aded2009-07-28 16:38:40 +00001129 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001130
1131 Lexer.Lex();
1132 }
1133
Daniel Dunbar3f872332009-07-28 16:08:33 +00001134 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001135 return TokError("unexpected token in '.abort' directive");
1136
1137 Lexer.Lex();
1138
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001139 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001140 if (Str.empty())
1141 Error(Loc, ".abort detected. Assembly stopping.");
1142 else
1143 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001144
1145 return false;
1146}
Kevin Enderby71148242009-07-14 21:35:03 +00001147
1148/// ParseDirectiveLsym
1149/// ::= .lsym identifier , expression
1150bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001151 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby71148242009-07-14 21:35:03 +00001152 return TokError("expected identifier in directive");
1153
1154 // handle the identifier as the key symbol.
1155 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001156 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby71148242009-07-14 21:35:03 +00001157 Lexer.Lex();
1158
Daniel Dunbar3f872332009-07-28 16:08:33 +00001159 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001160 return TokError("unexpected token in '.lsym' directive");
1161 Lexer.Lex();
1162
1163 MCValue Expr;
1164 if (ParseRelocatableExpression(Expr))
1165 return true;
1166
Daniel Dunbar3f872332009-07-28 16:08:33 +00001167 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001168 return TokError("unexpected token in '.lsym' directive");
1169
1170 Lexer.Lex();
1171
1172 // Create the Sym with the value of the Expr
1173 Out.EmitLocalSymbol(Sym, Expr);
1174
1175 return false;
1176}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001177
1178/// ParseDirectiveInclude
1179/// ::= .include "filename"
1180bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001181 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001182 return TokError("expected string in '.include' directive");
1183
Daniel Dunbar419aded2009-07-28 16:38:40 +00001184 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001185 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001186 Lexer.Lex();
1187
Daniel Dunbar3f872332009-07-28 16:08:33 +00001188 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001189 return TokError("unexpected token in '.include' directive");
1190
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001191 // Strip the quotes.
1192 Filename = Filename.substr(1, Filename.size()-2);
1193
1194 // Attempt to switch the lexer to the included file before consuming the end
1195 // of statement to avoid losing it when we switch.
1196 if (Lexer.EnterIncludeFile(Filename)) {
1197 Lexer.PrintMessage(IncludeLoc,
1198 "Could not find include file '" + Filename + "'",
1199 "error");
1200 return true;
1201 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001202
1203 return false;
1204}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001205
1206/// ParseDirectiveDarwinDumpOrLoad
1207/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001208bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001209 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001210 return TokError("expected string in '.dump' or '.load' directive");
1211
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001212 Lexer.Lex();
1213
Daniel Dunbar3f872332009-07-28 16:08:33 +00001214 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001215 return TokError("unexpected token in '.dump' or '.load' directive");
1216
1217 Lexer.Lex();
1218
Kevin Enderby5026ae42009-07-20 20:25:37 +00001219 // FIXME: If/when .dump and .load are implemented they will be done in the
1220 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001221 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001222 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001223 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001224 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001225
1226 return false;
1227}