blob: 931e460ab16fc05d70b7ad57dcf1c2c60e8aeeb8 [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 Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000018#include "llvm/MC/MCInst.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000020#include "llvm/MC/MCSymbol.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000021#include "llvm/Support/SourceMgr.h"
22#include "llvm/Support/raw_ostream.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000023using namespace llvm;
24
Daniel Dunbar3fb76832009-06-30 00:49:23 +000025void AsmParser::Warning(SMLoc L, const char *Msg) {
26 Lexer.PrintMessage(L, Msg, "warning");
27}
28
Chris Lattner14ee48a2009-06-21 21:22:11 +000029bool AsmParser::Error(SMLoc L, const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000030 Lexer.PrintMessage(L, Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000031 return true;
32}
33
34bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000035 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000036 return true;
37}
38
Chris Lattner27aa7d22009-06-21 20:16:42 +000039bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000040 // Prime the lexer.
41 Lexer.Lex();
42
43 while (Lexer.isNot(asmtok::Eof))
44 if (ParseStatement())
45 return true;
46
47 return false;
48}
49
Chris Lattner2cf5f142009-06-22 01:29:09 +000050/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
51void AsmParser::EatToEndOfStatement() {
52 while (Lexer.isNot(asmtok::EndOfStatement) &&
53 Lexer.isNot(asmtok::Eof))
54 Lexer.Lex();
55
56 // Eat EOL.
57 if (Lexer.is(asmtok::EndOfStatement))
58 Lexer.Lex();
59}
60
Chris Lattnerc4193832009-06-22 05:51:26 +000061
Chris Lattner74ec1a32009-06-22 06:32:03 +000062/// ParseParenExpr - Parse a paren expression and return it.
63/// NOTE: This assumes the leading '(' has already been consumed.
64///
65/// parenexpr ::= expr)
66///
Daniel Dunbar475839e2009-06-29 20:37:27 +000067bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +000068 if (ParseExpression(Res)) return true;
69 if (Lexer.isNot(asmtok::RParen))
70 return TokError("expected ')' in parentheses expression");
71 Lexer.Lex();
72 return false;
73}
Chris Lattnerc4193832009-06-22 05:51:26 +000074
Chris Lattner74ec1a32009-06-22 06:32:03 +000075/// ParsePrimaryExpr - Parse a primary expression and return it.
76/// primaryexpr ::= (parenexpr
77/// primaryexpr ::= symbol
78/// primaryexpr ::= number
79/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +000080bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +000081 switch (Lexer.getKind()) {
82 default:
83 return TokError("unknown token in expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +000084 case asmtok::Exclaim:
85 Lexer.Lex(); // Eat the operator.
86 if (ParsePrimaryExpr(Res))
87 return true;
88 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
89 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000090 case asmtok::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +000091 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +000092 // handle things like LFOO+4.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000093 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
94
95 // If this is use of an undefined symbol then mark it external.
96 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
97 Sym->setExternal(true);
98
99 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000100 Lexer.Lex(); // Eat identifier.
101 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000102 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000103 case asmtok::IntVal:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000104 Res = new AsmConstantExpr(Lexer.getCurIntVal());
Chris Lattnerc4193832009-06-22 05:51:26 +0000105 Lexer.Lex(); // Eat identifier.
106 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000107 case asmtok::LParen:
108 Lexer.Lex(); // Eat the '('.
109 return ParseParenExpr(Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000110 case asmtok::Minus:
111 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000112 if (ParsePrimaryExpr(Res))
113 return true;
114 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
115 return false;
116 case asmtok::Plus:
117 Lexer.Lex(); // Eat the operator.
118 if (ParsePrimaryExpr(Res))
119 return true;
120 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
121 return false;
122 case asmtok::Tilde:
123 Lexer.Lex(); // Eat the operator.
124 if (ParsePrimaryExpr(Res))
125 return true;
126 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
127 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000128 }
129}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000130
131/// ParseExpression - Parse an expression and return it.
132///
133/// expr ::= expr +,- expr -> lowest.
134/// expr ::= expr |,^,&,! expr -> middle.
135/// expr ::= expr *,/,%,<<,>> expr -> highest.
136/// expr ::= primaryexpr
137///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000138bool AsmParser::ParseExpression(AsmExpr *&Res) {
139 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000140 return ParsePrimaryExpr(Res) ||
141 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000142}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000143
Daniel Dunbar475839e2009-06-29 20:37:27 +0000144bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
145 AsmExpr *Expr;
146
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000147 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000148 if (ParseExpression(Expr))
149 return true;
150
151 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000152 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000153
154 return false;
155}
156
Daniel Dunbar15d17072009-06-30 01:49:52 +0000157bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
158 AsmExpr *Expr;
159
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000160 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000161 if (ParseExpression(Expr))
162 return true;
163
164 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000165 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000166
167 return false;
168}
169
Daniel Dunbar475839e2009-06-29 20:37:27 +0000170static unsigned getBinOpPrecedence(asmtok::TokKind K,
171 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000172 switch (K) {
173 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000174
175 // Lowest Precedence: &&, ||
176 case asmtok::AmpAmp:
177 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000178 return 1;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000179 case asmtok::PipePipe:
180 Kind = AsmBinaryExpr::LOr;
181 return 1;
182
183 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
184 case asmtok::Plus:
185 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000186 return 2;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000187 case asmtok::Minus:
188 Kind = AsmBinaryExpr::Sub;
189 return 2;
190 case asmtok::EqualEqual:
191 Kind = AsmBinaryExpr::EQ;
192 return 2;
193 case asmtok::ExclaimEqual:
194 case asmtok::LessGreater:
195 Kind = AsmBinaryExpr::NE;
196 return 2;
197 case asmtok::Less:
198 Kind = AsmBinaryExpr::LT;
199 return 2;
200 case asmtok::LessEqual:
201 Kind = AsmBinaryExpr::LTE;
202 return 2;
203 case asmtok::Greater:
204 Kind = AsmBinaryExpr::GT;
205 return 2;
206 case asmtok::GreaterEqual:
207 Kind = AsmBinaryExpr::GTE;
208 return 2;
209
210 // Intermediate Precedence: |, &, ^
211 //
212 // FIXME: gas seems to support '!' as an infix operator?
213 case asmtok::Pipe:
214 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000215 return 3;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000216 case asmtok::Caret:
217 Kind = AsmBinaryExpr::Xor;
218 return 3;
219 case asmtok::Amp:
220 Kind = AsmBinaryExpr::And;
221 return 3;
222
223 // Highest Precedence: *, /, %, <<, >>
224 case asmtok::Star:
225 Kind = AsmBinaryExpr::Mul;
226 return 4;
227 case asmtok::Slash:
228 Kind = AsmBinaryExpr::Div;
229 return 4;
230 case asmtok::Percent:
231 Kind = AsmBinaryExpr::Mod;
232 return 4;
233 case asmtok::LessLess:
234 Kind = AsmBinaryExpr::Shl;
235 return 4;
236 case asmtok::GreaterGreater:
237 Kind = AsmBinaryExpr::Shr;
238 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000239 }
240}
241
242
243/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
244/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000245bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000246 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000247 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000248 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000249
250 // If the next token is lower precedence than we are allowed to eat, return
251 // successfully with what we ate already.
252 if (TokPrec < Precedence)
253 return false;
254
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000255 Lexer.Lex();
256
257 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000259 if (ParsePrimaryExpr(RHS)) return true;
260
261 // If BinOp binds less tightly with RHS than the operator after RHS, let
262 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 AsmBinaryExpr::Opcode Dummy;
264 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000265 if (TokPrec < NextTokPrec) {
266 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
267 }
268
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 // Merge LHS and RHS according to operator.
270 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000271 }
272}
273
Chris Lattnerc4193832009-06-22 05:51:26 +0000274
275
276
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000277/// ParseStatement:
278/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000279/// ::= Label* Directive ...Operands... EndOfStatement
280/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000281bool AsmParser::ParseStatement() {
282 switch (Lexer.getKind()) {
283 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000284 return TokError("unexpected token at start of statement");
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000285 case asmtok::EndOfStatement:
286 Lexer.Lex();
287 return false;
288 case asmtok::Identifier:
289 break;
290 // TODO: Recurse on local labels etc.
291 }
292
293 // If we have an identifier, handle it as the key symbol.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000294 SMLoc IDLoc = Lexer.getLoc();
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000295 const char *IDVal = Lexer.getCurStrVal();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000296
297 // Consume the identifier, see what is after it.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000298 switch (Lexer.Lex()) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000299 case asmtok::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000300 // identifier ':' -> Label.
301 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000302
303 // Diagnose attempt to use a variable as a label.
304 //
305 // FIXME: Diagnostics. Note the location of the definition as a label.
306 // FIXME: This doesn't diagnose assignment to a symbol which has been
307 // implicitly marked as external.
308 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
309 if (Sym->getSection())
310 return Error(IDLoc, "invalid symbol redefinition");
311 if (Ctx.GetSymbolValue(Sym))
312 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000313
314 // Since we saw a label, create a symbol and emit it.
315 // FIXME: If the label starts with L it is an assembler temporary label.
316 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000317 Out.EmitLabel(Sym);
318
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000319 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000320 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000321
322 case asmtok::Equal:
323 // identifier '=' ... -> assignment statement
324 Lexer.Lex();
325
326 return ParseAssignment(IDVal, false);
327
328 default: // Normal instruction or directive.
329 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000330 }
331
332 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000333 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000334 // FIXME: This should be driven based on a hash lookup and callback.
Chris Lattner9a023f72009-06-24 04:43:34 +0000335 if (!strcmp(IDVal, ".section"))
Chris Lattner529fb542009-06-24 05:13:15 +0000336 return ParseDirectiveDarwinSection();
337 if (!strcmp(IDVal, ".text"))
338 // FIXME: This changes behavior based on the -static flag to the
339 // assembler.
340 return ParseDirectiveSectionSwitch("__TEXT,__text",
341 "regular,pure_instructions");
342 if (!strcmp(IDVal, ".const"))
343 return ParseDirectiveSectionSwitch("__TEXT,__const");
344 if (!strcmp(IDVal, ".static_const"))
345 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
346 if (!strcmp(IDVal, ".cstring"))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000347 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
348 "cstring_literals");
Chris Lattner529fb542009-06-24 05:13:15 +0000349 if (!strcmp(IDVal, ".literal4"))
350 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
351 if (!strcmp(IDVal, ".literal8"))
352 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
353 if (!strcmp(IDVal, ".literal16"))
354 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
355 "16byte_literals");
356 if (!strcmp(IDVal, ".constructor"))
357 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
358 if (!strcmp(IDVal, ".destructor"))
359 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
360 if (!strcmp(IDVal, ".fvmlib_init0"))
361 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
362 if (!strcmp(IDVal, ".fvmlib_init1"))
363 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
364 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
365 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
366 "self_modifying_code+pure_instructions,5");
367 // FIXME: .picsymbol_stub on PPC.
368 if (!strcmp(IDVal, ".data"))
369 return ParseDirectiveSectionSwitch("__DATA,__data");
370 if (!strcmp(IDVal, ".static_data"))
371 return ParseDirectiveSectionSwitch("__DATA,__static_data");
372 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
373 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
374 "non_lazy_symbol_pointers");
375 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
376 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
377 "lazy_symbol_pointers");
378 if (!strcmp(IDVal, ".dyld"))
379 return ParseDirectiveSectionSwitch("__DATA,__dyld");
380 if (!strcmp(IDVal, ".mod_init_func"))
381 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
382 "mod_init_funcs");
383 if (!strcmp(IDVal, ".mod_term_func"))
384 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
385 "mod_term_funcs");
386 if (!strcmp(IDVal, ".const_data"))
387 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
388
389
390 // FIXME: Verify attributes on sections.
391 if (!strcmp(IDVal, ".objc_class"))
392 return ParseDirectiveSectionSwitch("__OBJC,__class");
393 if (!strcmp(IDVal, ".objc_meta_class"))
394 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
395 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
396 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
397 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
398 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
399 if (!strcmp(IDVal, ".objc_protocol"))
400 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
401 if (!strcmp(IDVal, ".objc_string_object"))
402 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
403 if (!strcmp(IDVal, ".objc_cls_meth"))
404 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
405 if (!strcmp(IDVal, ".objc_inst_meth"))
406 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
407 if (!strcmp(IDVal, ".objc_cls_refs"))
408 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
409 if (!strcmp(IDVal, ".objc_message_refs"))
410 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
411 if (!strcmp(IDVal, ".objc_symbols"))
412 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
413 if (!strcmp(IDVal, ".objc_category"))
414 return ParseDirectiveSectionSwitch("__OBJC,__category");
415 if (!strcmp(IDVal, ".objc_class_vars"))
416 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
417 if (!strcmp(IDVal, ".objc_instance_vars"))
418 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
419 if (!strcmp(IDVal, ".objc_module_info"))
420 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
421 if (!strcmp(IDVal, ".objc_class_names"))
422 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
423 if (!strcmp(IDVal, ".objc_meth_var_types"))
424 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
425 if (!strcmp(IDVal, ".objc_meth_var_names"))
426 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
427 if (!strcmp(IDVal, ".objc_selector_strs"))
428 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000429
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000430 // Assembler features
431 if (!strcmp(IDVal, ".set"))
432 return ParseDirectiveSet();
433
Daniel Dunbara0d14262009-06-24 23:30:00 +0000434 // Data directives
435
436 if (!strcmp(IDVal, ".ascii"))
437 return ParseDirectiveAscii(false);
438 if (!strcmp(IDVal, ".asciz"))
439 return ParseDirectiveAscii(true);
440
441 // FIXME: Target hooks for size? Also for "word", "hword".
442 if (!strcmp(IDVal, ".byte"))
443 return ParseDirectiveValue(1);
444 if (!strcmp(IDVal, ".short"))
445 return ParseDirectiveValue(2);
446 if (!strcmp(IDVal, ".long"))
447 return ParseDirectiveValue(4);
448 if (!strcmp(IDVal, ".quad"))
449 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000450
451 // FIXME: Target hooks for IsPow2.
452 if (!strcmp(IDVal, ".align"))
453 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
454 if (!strcmp(IDVal, ".align32"))
455 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
456 if (!strcmp(IDVal, ".balign"))
457 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
458 if (!strcmp(IDVal, ".balignw"))
459 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
460 if (!strcmp(IDVal, ".balignl"))
461 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
462 if (!strcmp(IDVal, ".p2align"))
463 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
464 if (!strcmp(IDVal, ".p2alignw"))
465 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
466 if (!strcmp(IDVal, ".p2alignl"))
467 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
468
Daniel Dunbarc238b582009-06-25 22:44:51 +0000469 if (!strcmp(IDVal, ".org"))
470 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000471
472 if (!strcmp(IDVal, ".fill"))
473 return ParseDirectiveFill();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000474 if (!strcmp(IDVal, ".space"))
475 return ParseDirectiveSpace();
476
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000477 // Symbol attribute directives
478 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
479 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
480 if (!strcmp(IDVal, ".hidden"))
481 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
482 if (!strcmp(IDVal, ".indirect_symbol"))
483 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
484 if (!strcmp(IDVal, ".internal"))
485 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
486 if (!strcmp(IDVal, ".lazy_reference"))
487 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
488 if (!strcmp(IDVal, ".no_dead_strip"))
489 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
490 if (!strcmp(IDVal, ".private_extern"))
491 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
492 if (!strcmp(IDVal, ".protected"))
493 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
494 if (!strcmp(IDVal, ".reference"))
495 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
496 if (!strcmp(IDVal, ".weak"))
497 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
498 if (!strcmp(IDVal, ".weak_definition"))
499 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
500 if (!strcmp(IDVal, ".weak_reference"))
501 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
502
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000503 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000504 EatToEndOfStatement();
505 return false;
506 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000507
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000508 MCInst Inst;
Daniel Dunbard9627e12009-06-30 23:38:38 +0000509 if (ParseX86InstOperands(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000510 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000511
512 if (Lexer.isNot(asmtok::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000513 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000514
515 // Eat the end of statement marker.
516 Lexer.Lex();
517
518 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000519 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000520
521 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000522 return false;
523}
Chris Lattner9a023f72009-06-24 04:43:34 +0000524
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000525bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000526 // FIXME: Use better location, we should use proper tokens.
527 SMLoc EqualLoc = Lexer.getLoc();
528
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000529 MCValue Value;
530 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000531 return true;
532
533 if (Lexer.isNot(asmtok::EndOfStatement))
534 return TokError("unexpected token in assignment");
535
536 // Eat the end of statement marker.
537 Lexer.Lex();
538
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000539 // Diagnose assignment to a label.
540 //
541 // FIXME: Diagnostics. Note the location of the definition as a label.
542 // FIXME: This doesn't diagnose assignment to a symbol which has been
543 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000544 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000545 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000546 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000547 if (Sym->getSection())
548 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
549 if (Sym->isExternal())
550 return Error(EqualLoc, "invalid assignment to external symbol");
551
552 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000553 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000554
555 return false;
556}
557
558/// ParseDirectiveSet:
559/// ::= .set identifier ',' expression
560bool AsmParser::ParseDirectiveSet() {
561 if (Lexer.isNot(asmtok::Identifier))
562 return TokError("expected identifier after '.set' directive");
563
564 const char *Name = Lexer.getCurStrVal();
565
566 if (Lexer.Lex() != asmtok::Comma)
567 return TokError("unexpected token in '.set'");
568 Lexer.Lex();
569
570 return ParseAssignment(Name, true);
571}
572
Chris Lattner9a023f72009-06-24 04:43:34 +0000573/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000574/// ::= .section identifier (',' identifier)*
575/// FIXME: This should actually parse out the segment, section, attributes and
576/// sizeof_stub fields.
577bool AsmParser::ParseDirectiveDarwinSection() {
Chris Lattner9a023f72009-06-24 04:43:34 +0000578 if (Lexer.isNot(asmtok::Identifier))
579 return TokError("expected identifier after '.section' directive");
580
581 std::string Section = Lexer.getCurStrVal();
582 Lexer.Lex();
583
584 // Accept a comma separated list of modifiers.
585 while (Lexer.is(asmtok::Comma)) {
586 Lexer.Lex();
587
588 if (Lexer.isNot(asmtok::Identifier))
589 return TokError("expected identifier in '.section' directive");
590 Section += ',';
591 Section += Lexer.getCurStrVal();
592 Lexer.Lex();
593 }
594
595 if (Lexer.isNot(asmtok::EndOfStatement))
596 return TokError("unexpected token in '.section' directive");
597 Lexer.Lex();
598
599 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
600 return false;
601}
602
Chris Lattner529fb542009-06-24 05:13:15 +0000603bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
604 const char *Directives) {
605 if (Lexer.isNot(asmtok::EndOfStatement))
606 return TokError("unexpected token in section switching directive");
607 Lexer.Lex();
608
609 std::string SectionStr = Section;
610 if (Directives && Directives[0]) {
611 SectionStr += ",";
612 SectionStr += Directives;
613 }
614
615 Out.SwitchSection(Ctx.GetSection(Section));
616 return false;
617}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000618
619/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000620/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000621bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
622 if (Lexer.isNot(asmtok::EndOfStatement)) {
623 for (;;) {
624 if (Lexer.isNot(asmtok::String))
625 return TokError("expected string in '.ascii' or '.asciz' directive");
626
627 // FIXME: This shouldn't use a const char* + strlen, the string could have
628 // embedded nulls.
629 // FIXME: Should have accessor for getting string contents.
630 const char *Str = Lexer.getCurStrVal();
631 Out.EmitBytes(Str + 1, strlen(Str) - 2);
632 if (ZeroTerminated)
633 Out.EmitBytes("\0", 1);
634
635 Lexer.Lex();
636
637 if (Lexer.is(asmtok::EndOfStatement))
638 break;
639
640 if (Lexer.isNot(asmtok::Comma))
641 return TokError("unexpected token in '.ascii' or '.asciz' directive");
642 Lexer.Lex();
643 }
644 }
645
646 Lexer.Lex();
647 return false;
648}
649
650/// ParseDirectiveValue
651/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
652bool AsmParser::ParseDirectiveValue(unsigned Size) {
653 if (Lexer.isNot(asmtok::EndOfStatement)) {
654 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000655 MCValue Expr;
656 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000657 return true;
658
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000659 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000660
661 if (Lexer.is(asmtok::EndOfStatement))
662 break;
663
664 // FIXME: Improve diagnostic.
665 if (Lexer.isNot(asmtok::Comma))
666 return TokError("unexpected token in directive");
667 Lexer.Lex();
668 }
669 }
670
671 Lexer.Lex();
672 return false;
673}
674
675/// ParseDirectiveSpace
676/// ::= .space expression [ , expression ]
677bool AsmParser::ParseDirectiveSpace() {
678 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000680 return true;
681
682 int64_t FillExpr = 0;
683 bool HasFillExpr = false;
684 if (Lexer.isNot(asmtok::EndOfStatement)) {
685 if (Lexer.isNot(asmtok::Comma))
686 return TokError("unexpected token in '.space' directive");
687 Lexer.Lex();
688
Daniel Dunbar475839e2009-06-29 20:37:27 +0000689 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000690 return true;
691
692 HasFillExpr = true;
693
694 if (Lexer.isNot(asmtok::EndOfStatement))
695 return TokError("unexpected token in '.space' directive");
696 }
697
698 Lexer.Lex();
699
700 if (NumBytes <= 0)
701 return TokError("invalid number of bytes in '.space' directive");
702
703 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
704 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
705 Out.EmitValue(MCValue::get(FillExpr), 1);
706
707 return false;
708}
709
710/// ParseDirectiveFill
711/// ::= .fill expression , expression , expression
712bool AsmParser::ParseDirectiveFill() {
713 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000714 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000715 return true;
716
717 if (Lexer.isNot(asmtok::Comma))
718 return TokError("unexpected token in '.fill' directive");
719 Lexer.Lex();
720
721 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000722 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000723 return true;
724
725 if (Lexer.isNot(asmtok::Comma))
726 return TokError("unexpected token in '.fill' directive");
727 Lexer.Lex();
728
729 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000730 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000731 return true;
732
733 if (Lexer.isNot(asmtok::EndOfStatement))
734 return TokError("unexpected token in '.fill' directive");
735
736 Lexer.Lex();
737
738 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
739 return TokError("invalid '.fill' size, expected 1, 2, or 4");
740
741 for (uint64_t i = 0, e = NumValues; i != e; ++i)
742 Out.EmitValue(MCValue::get(FillExpr), FillSize);
743
744 return false;
745}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000746
747/// ParseDirectiveOrg
748/// ::= .org expression [ , expression ]
749bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000750 MCValue Offset;
751 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000752 return true;
753
754 // Parse optional fill expression.
755 int64_t FillExpr = 0;
756 if (Lexer.isNot(asmtok::EndOfStatement)) {
757 if (Lexer.isNot(asmtok::Comma))
758 return TokError("unexpected token in '.org' directive");
759 Lexer.Lex();
760
Daniel Dunbar475839e2009-06-29 20:37:27 +0000761 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000762 return true;
763
764 if (Lexer.isNot(asmtok::EndOfStatement))
765 return TokError("unexpected token in '.org' directive");
766 }
767
768 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000769
770 // FIXME: Only limited forms of relocatable expressions are accepted here, it
771 // has to be relative to the current section.
772 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000773
774 return false;
775}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000776
777/// ParseDirectiveAlign
778/// ::= {.align, ...} expression [ , expression [ , expression ]]
779bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
780 int64_t Alignment;
781 if (ParseAbsoluteExpression(Alignment))
782 return true;
783
784 SMLoc MaxBytesLoc;
785 bool HasFillExpr = false;
786 int64_t FillExpr = 0;
787 int64_t MaxBytesToFill = 0;
788 if (Lexer.isNot(asmtok::EndOfStatement)) {
789 if (Lexer.isNot(asmtok::Comma))
790 return TokError("unexpected token in directive");
791 Lexer.Lex();
792
793 // The fill expression can be omitted while specifying a maximum number of
794 // alignment bytes, e.g:
795 // .align 3,,4
796 if (Lexer.isNot(asmtok::Comma)) {
797 HasFillExpr = true;
798 if (ParseAbsoluteExpression(FillExpr))
799 return true;
800 }
801
802 if (Lexer.isNot(asmtok::EndOfStatement)) {
803 if (Lexer.isNot(asmtok::Comma))
804 return TokError("unexpected token in directive");
805 Lexer.Lex();
806
807 MaxBytesLoc = Lexer.getLoc();
808 if (ParseAbsoluteExpression(MaxBytesToFill))
809 return true;
810
811 if (Lexer.isNot(asmtok::EndOfStatement))
812 return TokError("unexpected token in directive");
813 }
814 }
815
816 Lexer.Lex();
817
818 if (!HasFillExpr) {
819 // FIXME: Sometimes fill with nop.
820 FillExpr = 0;
821 }
822
823 // Compute alignment in bytes.
824 if (IsPow2) {
825 // FIXME: Diagnose overflow.
826 Alignment = 1 << Alignment;
827 }
828
829 // Diagnose non-sensical max bytes to fill.
830 if (MaxBytesLoc.isValid()) {
831 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000832 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
833 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000834 return false;
835 }
836
837 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000838 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
839 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000840 MaxBytesToFill = 0;
841 }
842 }
843
844 // FIXME: Target specific behavior about how the "extra" bytes are filled.
845 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
846
847 return false;
848}
849
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000850/// ParseDirectiveSymbolAttribute
851/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
852bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
853 if (Lexer.isNot(asmtok::EndOfStatement)) {
854 for (;;) {
855 if (Lexer.isNot(asmtok::Identifier))
856 return TokError("expected identifier in directive");
857
858 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
859 Lexer.Lex();
860
861 // If this is use of an undefined symbol then mark it external.
862 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
863 Sym->setExternal(true);
864
865 Out.EmitSymbolAttribute(Sym, Attr);
866
867 if (Lexer.is(asmtok::EndOfStatement))
868 break;
869
870 if (Lexer.isNot(asmtok::Comma))
871 return TokError("unexpected token in directive");
872 Lexer.Lex();
873 }
874 }
875
876 Lexer.Lex();
877 return false;
878}