blob: 339a16db8c228aa046ed6761d235e65775a804fd [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 Dunbar2c3f00c2009-07-02 02:09:07 +0000170bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
171 AsmExpr *Expr;
172
173 SMLoc StartLoc = Lexer.getLoc();
174 if (ParseParenExpr(Expr))
175 return true;
176
177 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
178 return Error(StartLoc, "expected relocatable expression");
179
180 return false;
181}
182
Daniel Dunbar475839e2009-06-29 20:37:27 +0000183static unsigned getBinOpPrecedence(asmtok::TokKind K,
184 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000185 switch (K) {
186 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000187
188 // Lowest Precedence: &&, ||
189 case asmtok::AmpAmp:
190 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000191 return 1;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000192 case asmtok::PipePipe:
193 Kind = AsmBinaryExpr::LOr;
194 return 1;
195
196 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
197 case asmtok::Plus:
198 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000199 return 2;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000200 case asmtok::Minus:
201 Kind = AsmBinaryExpr::Sub;
202 return 2;
203 case asmtok::EqualEqual:
204 Kind = AsmBinaryExpr::EQ;
205 return 2;
206 case asmtok::ExclaimEqual:
207 case asmtok::LessGreater:
208 Kind = AsmBinaryExpr::NE;
209 return 2;
210 case asmtok::Less:
211 Kind = AsmBinaryExpr::LT;
212 return 2;
213 case asmtok::LessEqual:
214 Kind = AsmBinaryExpr::LTE;
215 return 2;
216 case asmtok::Greater:
217 Kind = AsmBinaryExpr::GT;
218 return 2;
219 case asmtok::GreaterEqual:
220 Kind = AsmBinaryExpr::GTE;
221 return 2;
222
223 // Intermediate Precedence: |, &, ^
224 //
225 // FIXME: gas seems to support '!' as an infix operator?
226 case asmtok::Pipe:
227 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000228 return 3;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000229 case asmtok::Caret:
230 Kind = AsmBinaryExpr::Xor;
231 return 3;
232 case asmtok::Amp:
233 Kind = AsmBinaryExpr::And;
234 return 3;
235
236 // Highest Precedence: *, /, %, <<, >>
237 case asmtok::Star:
238 Kind = AsmBinaryExpr::Mul;
239 return 4;
240 case asmtok::Slash:
241 Kind = AsmBinaryExpr::Div;
242 return 4;
243 case asmtok::Percent:
244 Kind = AsmBinaryExpr::Mod;
245 return 4;
246 case asmtok::LessLess:
247 Kind = AsmBinaryExpr::Shl;
248 return 4;
249 case asmtok::GreaterGreater:
250 Kind = AsmBinaryExpr::Shr;
251 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000252 }
253}
254
255
256/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
257/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000259 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000260 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000262
263 // If the next token is lower precedence than we are allowed to eat, return
264 // successfully with what we ate already.
265 if (TokPrec < Precedence)
266 return false;
267
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000268 Lexer.Lex();
269
270 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000272 if (ParsePrimaryExpr(RHS)) return true;
273
274 // If BinOp binds less tightly with RHS than the operator after RHS, let
275 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000276 AsmBinaryExpr::Opcode Dummy;
277 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000278 if (TokPrec < NextTokPrec) {
279 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
280 }
281
Daniel Dunbar475839e2009-06-29 20:37:27 +0000282 // Merge LHS and RHS according to operator.
283 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000284 }
285}
286
Chris Lattnerc4193832009-06-22 05:51:26 +0000287
288
289
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000290/// ParseStatement:
291/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000292/// ::= Label* Directive ...Operands... EndOfStatement
293/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000294bool AsmParser::ParseStatement() {
295 switch (Lexer.getKind()) {
296 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000297 return TokError("unexpected token at start of statement");
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000298 case asmtok::EndOfStatement:
299 Lexer.Lex();
300 return false;
301 case asmtok::Identifier:
302 break;
303 // TODO: Recurse on local labels etc.
304 }
305
306 // If we have an identifier, handle it as the key symbol.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000307 SMLoc IDLoc = Lexer.getLoc();
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000308 const char *IDVal = Lexer.getCurStrVal();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000309
310 // Consume the identifier, see what is after it.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000311 switch (Lexer.Lex()) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000312 case asmtok::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000313 // identifier ':' -> Label.
314 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000315
316 // Diagnose attempt to use a variable as a label.
317 //
318 // FIXME: Diagnostics. Note the location of the definition as a label.
319 // FIXME: This doesn't diagnose assignment to a symbol which has been
320 // implicitly marked as external.
321 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
322 if (Sym->getSection())
323 return Error(IDLoc, "invalid symbol redefinition");
324 if (Ctx.GetSymbolValue(Sym))
325 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000326
327 // Since we saw a label, create a symbol and emit it.
328 // FIXME: If the label starts with L it is an assembler temporary label.
329 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000330 Out.EmitLabel(Sym);
331
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000332 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000333 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000334
335 case asmtok::Equal:
336 // identifier '=' ... -> assignment statement
337 Lexer.Lex();
338
339 return ParseAssignment(IDVal, false);
340
341 default: // Normal instruction or directive.
342 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000343 }
344
345 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000346 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000347 // FIXME: This should be driven based on a hash lookup and callback.
Chris Lattner9a023f72009-06-24 04:43:34 +0000348 if (!strcmp(IDVal, ".section"))
Chris Lattner529fb542009-06-24 05:13:15 +0000349 return ParseDirectiveDarwinSection();
350 if (!strcmp(IDVal, ".text"))
351 // FIXME: This changes behavior based on the -static flag to the
352 // assembler.
353 return ParseDirectiveSectionSwitch("__TEXT,__text",
354 "regular,pure_instructions");
355 if (!strcmp(IDVal, ".const"))
356 return ParseDirectiveSectionSwitch("__TEXT,__const");
357 if (!strcmp(IDVal, ".static_const"))
358 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
359 if (!strcmp(IDVal, ".cstring"))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000360 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
361 "cstring_literals");
Chris Lattner529fb542009-06-24 05:13:15 +0000362 if (!strcmp(IDVal, ".literal4"))
363 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
364 if (!strcmp(IDVal, ".literal8"))
365 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
366 if (!strcmp(IDVal, ".literal16"))
367 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
368 "16byte_literals");
369 if (!strcmp(IDVal, ".constructor"))
370 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
371 if (!strcmp(IDVal, ".destructor"))
372 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
373 if (!strcmp(IDVal, ".fvmlib_init0"))
374 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
375 if (!strcmp(IDVal, ".fvmlib_init1"))
376 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
377 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
378 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
379 "self_modifying_code+pure_instructions,5");
380 // FIXME: .picsymbol_stub on PPC.
381 if (!strcmp(IDVal, ".data"))
382 return ParseDirectiveSectionSwitch("__DATA,__data");
383 if (!strcmp(IDVal, ".static_data"))
384 return ParseDirectiveSectionSwitch("__DATA,__static_data");
385 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
386 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
387 "non_lazy_symbol_pointers");
388 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
389 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
390 "lazy_symbol_pointers");
391 if (!strcmp(IDVal, ".dyld"))
392 return ParseDirectiveSectionSwitch("__DATA,__dyld");
393 if (!strcmp(IDVal, ".mod_init_func"))
394 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
395 "mod_init_funcs");
396 if (!strcmp(IDVal, ".mod_term_func"))
397 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
398 "mod_term_funcs");
399 if (!strcmp(IDVal, ".const_data"))
400 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
401
402
403 // FIXME: Verify attributes on sections.
404 if (!strcmp(IDVal, ".objc_class"))
405 return ParseDirectiveSectionSwitch("__OBJC,__class");
406 if (!strcmp(IDVal, ".objc_meta_class"))
407 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
408 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
409 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
410 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
411 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
412 if (!strcmp(IDVal, ".objc_protocol"))
413 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
414 if (!strcmp(IDVal, ".objc_string_object"))
415 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
416 if (!strcmp(IDVal, ".objc_cls_meth"))
417 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
418 if (!strcmp(IDVal, ".objc_inst_meth"))
419 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
420 if (!strcmp(IDVal, ".objc_cls_refs"))
421 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
422 if (!strcmp(IDVal, ".objc_message_refs"))
423 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
424 if (!strcmp(IDVal, ".objc_symbols"))
425 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
426 if (!strcmp(IDVal, ".objc_category"))
427 return ParseDirectiveSectionSwitch("__OBJC,__category");
428 if (!strcmp(IDVal, ".objc_class_vars"))
429 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
430 if (!strcmp(IDVal, ".objc_instance_vars"))
431 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
432 if (!strcmp(IDVal, ".objc_module_info"))
433 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
434 if (!strcmp(IDVal, ".objc_class_names"))
435 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
436 if (!strcmp(IDVal, ".objc_meth_var_types"))
437 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
438 if (!strcmp(IDVal, ".objc_meth_var_names"))
439 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
440 if (!strcmp(IDVal, ".objc_selector_strs"))
441 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000442
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000443 // Assembler features
444 if (!strcmp(IDVal, ".set"))
445 return ParseDirectiveSet();
446
Daniel Dunbara0d14262009-06-24 23:30:00 +0000447 // Data directives
448
449 if (!strcmp(IDVal, ".ascii"))
450 return ParseDirectiveAscii(false);
451 if (!strcmp(IDVal, ".asciz"))
452 return ParseDirectiveAscii(true);
453
454 // FIXME: Target hooks for size? Also for "word", "hword".
455 if (!strcmp(IDVal, ".byte"))
456 return ParseDirectiveValue(1);
457 if (!strcmp(IDVal, ".short"))
458 return ParseDirectiveValue(2);
459 if (!strcmp(IDVal, ".long"))
460 return ParseDirectiveValue(4);
461 if (!strcmp(IDVal, ".quad"))
462 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000463
464 // FIXME: Target hooks for IsPow2.
465 if (!strcmp(IDVal, ".align"))
466 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
467 if (!strcmp(IDVal, ".align32"))
468 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
469 if (!strcmp(IDVal, ".balign"))
470 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
471 if (!strcmp(IDVal, ".balignw"))
472 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
473 if (!strcmp(IDVal, ".balignl"))
474 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
475 if (!strcmp(IDVal, ".p2align"))
476 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
477 if (!strcmp(IDVal, ".p2alignw"))
478 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
479 if (!strcmp(IDVal, ".p2alignl"))
480 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
481
Daniel Dunbarc238b582009-06-25 22:44:51 +0000482 if (!strcmp(IDVal, ".org"))
483 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000484
485 if (!strcmp(IDVal, ".fill"))
486 return ParseDirectiveFill();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000487 if (!strcmp(IDVal, ".space"))
488 return ParseDirectiveSpace();
489
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000490 // Symbol attribute directives
491 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
492 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
493 if (!strcmp(IDVal, ".hidden"))
494 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
495 if (!strcmp(IDVal, ".indirect_symbol"))
496 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
497 if (!strcmp(IDVal, ".internal"))
498 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
499 if (!strcmp(IDVal, ".lazy_reference"))
500 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
501 if (!strcmp(IDVal, ".no_dead_strip"))
502 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
503 if (!strcmp(IDVal, ".private_extern"))
504 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
505 if (!strcmp(IDVal, ".protected"))
506 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
507 if (!strcmp(IDVal, ".reference"))
508 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
509 if (!strcmp(IDVal, ".weak"))
510 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
511 if (!strcmp(IDVal, ".weak_definition"))
512 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
513 if (!strcmp(IDVal, ".weak_reference"))
514 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
515
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000516 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000517 EatToEndOfStatement();
518 return false;
519 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000520
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000521 MCInst Inst;
Daniel Dunbard9627e12009-06-30 23:38:38 +0000522 if (ParseX86InstOperands(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000523 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000524
525 if (Lexer.isNot(asmtok::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000526 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000527
528 // Eat the end of statement marker.
529 Lexer.Lex();
530
531 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000532 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000533
534 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000535 return false;
536}
Chris Lattner9a023f72009-06-24 04:43:34 +0000537
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000538bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000539 // FIXME: Use better location, we should use proper tokens.
540 SMLoc EqualLoc = Lexer.getLoc();
541
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000542 MCValue Value;
543 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000544 return true;
545
546 if (Lexer.isNot(asmtok::EndOfStatement))
547 return TokError("unexpected token in assignment");
548
549 // Eat the end of statement marker.
550 Lexer.Lex();
551
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000552 // Diagnose assignment to a label.
553 //
554 // FIXME: Diagnostics. Note the location of the definition as a label.
555 // FIXME: This doesn't diagnose assignment to a symbol which has been
556 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000557 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000558 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000559 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000560 if (Sym->getSection())
561 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
562 if (Sym->isExternal())
563 return Error(EqualLoc, "invalid assignment to external symbol");
564
565 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000566 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000567
568 return false;
569}
570
571/// ParseDirectiveSet:
572/// ::= .set identifier ',' expression
573bool AsmParser::ParseDirectiveSet() {
574 if (Lexer.isNot(asmtok::Identifier))
575 return TokError("expected identifier after '.set' directive");
576
577 const char *Name = Lexer.getCurStrVal();
578
579 if (Lexer.Lex() != asmtok::Comma)
580 return TokError("unexpected token in '.set'");
581 Lexer.Lex();
582
583 return ParseAssignment(Name, true);
584}
585
Chris Lattner9a023f72009-06-24 04:43:34 +0000586/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000587/// ::= .section identifier (',' identifier)*
588/// FIXME: This should actually parse out the segment, section, attributes and
589/// sizeof_stub fields.
590bool AsmParser::ParseDirectiveDarwinSection() {
Chris Lattner9a023f72009-06-24 04:43:34 +0000591 if (Lexer.isNot(asmtok::Identifier))
592 return TokError("expected identifier after '.section' directive");
593
594 std::string Section = Lexer.getCurStrVal();
595 Lexer.Lex();
596
597 // Accept a comma separated list of modifiers.
598 while (Lexer.is(asmtok::Comma)) {
599 Lexer.Lex();
600
601 if (Lexer.isNot(asmtok::Identifier))
602 return TokError("expected identifier in '.section' directive");
603 Section += ',';
604 Section += Lexer.getCurStrVal();
605 Lexer.Lex();
606 }
607
608 if (Lexer.isNot(asmtok::EndOfStatement))
609 return TokError("unexpected token in '.section' directive");
610 Lexer.Lex();
611
612 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
613 return false;
614}
615
Chris Lattner529fb542009-06-24 05:13:15 +0000616bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
617 const char *Directives) {
618 if (Lexer.isNot(asmtok::EndOfStatement))
619 return TokError("unexpected token in section switching directive");
620 Lexer.Lex();
621
622 std::string SectionStr = Section;
623 if (Directives && Directives[0]) {
624 SectionStr += ",";
625 SectionStr += Directives;
626 }
627
628 Out.SwitchSection(Ctx.GetSection(Section));
629 return false;
630}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000631
632/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000634bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
635 if (Lexer.isNot(asmtok::EndOfStatement)) {
636 for (;;) {
637 if (Lexer.isNot(asmtok::String))
638 return TokError("expected string in '.ascii' or '.asciz' directive");
639
640 // FIXME: This shouldn't use a const char* + strlen, the string could have
641 // embedded nulls.
642 // FIXME: Should have accessor for getting string contents.
643 const char *Str = Lexer.getCurStrVal();
644 Out.EmitBytes(Str + 1, strlen(Str) - 2);
645 if (ZeroTerminated)
646 Out.EmitBytes("\0", 1);
647
648 Lexer.Lex();
649
650 if (Lexer.is(asmtok::EndOfStatement))
651 break;
652
653 if (Lexer.isNot(asmtok::Comma))
654 return TokError("unexpected token in '.ascii' or '.asciz' directive");
655 Lexer.Lex();
656 }
657 }
658
659 Lexer.Lex();
660 return false;
661}
662
663/// ParseDirectiveValue
664/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
665bool AsmParser::ParseDirectiveValue(unsigned Size) {
666 if (Lexer.isNot(asmtok::EndOfStatement)) {
667 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000668 MCValue Expr;
669 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000670 return true;
671
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000672 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000673
674 if (Lexer.is(asmtok::EndOfStatement))
675 break;
676
677 // FIXME: Improve diagnostic.
678 if (Lexer.isNot(asmtok::Comma))
679 return TokError("unexpected token in directive");
680 Lexer.Lex();
681 }
682 }
683
684 Lexer.Lex();
685 return false;
686}
687
688/// ParseDirectiveSpace
689/// ::= .space expression [ , expression ]
690bool AsmParser::ParseDirectiveSpace() {
691 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000692 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000693 return true;
694
695 int64_t FillExpr = 0;
696 bool HasFillExpr = false;
697 if (Lexer.isNot(asmtok::EndOfStatement)) {
698 if (Lexer.isNot(asmtok::Comma))
699 return TokError("unexpected token in '.space' directive");
700 Lexer.Lex();
701
Daniel Dunbar475839e2009-06-29 20:37:27 +0000702 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000703 return true;
704
705 HasFillExpr = true;
706
707 if (Lexer.isNot(asmtok::EndOfStatement))
708 return TokError("unexpected token in '.space' directive");
709 }
710
711 Lexer.Lex();
712
713 if (NumBytes <= 0)
714 return TokError("invalid number of bytes in '.space' directive");
715
716 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
717 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
718 Out.EmitValue(MCValue::get(FillExpr), 1);
719
720 return false;
721}
722
723/// ParseDirectiveFill
724/// ::= .fill expression , expression , expression
725bool AsmParser::ParseDirectiveFill() {
726 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000727 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000728 return true;
729
730 if (Lexer.isNot(asmtok::Comma))
731 return TokError("unexpected token in '.fill' directive");
732 Lexer.Lex();
733
734 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000735 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000736 return true;
737
738 if (Lexer.isNot(asmtok::Comma))
739 return TokError("unexpected token in '.fill' directive");
740 Lexer.Lex();
741
742 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000743 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000744 return true;
745
746 if (Lexer.isNot(asmtok::EndOfStatement))
747 return TokError("unexpected token in '.fill' directive");
748
749 Lexer.Lex();
750
751 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
752 return TokError("invalid '.fill' size, expected 1, 2, or 4");
753
754 for (uint64_t i = 0, e = NumValues; i != e; ++i)
755 Out.EmitValue(MCValue::get(FillExpr), FillSize);
756
757 return false;
758}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000759
760/// ParseDirectiveOrg
761/// ::= .org expression [ , expression ]
762bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000763 MCValue Offset;
764 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000765 return true;
766
767 // Parse optional fill expression.
768 int64_t FillExpr = 0;
769 if (Lexer.isNot(asmtok::EndOfStatement)) {
770 if (Lexer.isNot(asmtok::Comma))
771 return TokError("unexpected token in '.org' directive");
772 Lexer.Lex();
773
Daniel Dunbar475839e2009-06-29 20:37:27 +0000774 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000775 return true;
776
777 if (Lexer.isNot(asmtok::EndOfStatement))
778 return TokError("unexpected token in '.org' directive");
779 }
780
781 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000782
783 // FIXME: Only limited forms of relocatable expressions are accepted here, it
784 // has to be relative to the current section.
785 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000786
787 return false;
788}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000789
790/// ParseDirectiveAlign
791/// ::= {.align, ...} expression [ , expression [ , expression ]]
792bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
793 int64_t Alignment;
794 if (ParseAbsoluteExpression(Alignment))
795 return true;
796
797 SMLoc MaxBytesLoc;
798 bool HasFillExpr = false;
799 int64_t FillExpr = 0;
800 int64_t MaxBytesToFill = 0;
801 if (Lexer.isNot(asmtok::EndOfStatement)) {
802 if (Lexer.isNot(asmtok::Comma))
803 return TokError("unexpected token in directive");
804 Lexer.Lex();
805
806 // The fill expression can be omitted while specifying a maximum number of
807 // alignment bytes, e.g:
808 // .align 3,,4
809 if (Lexer.isNot(asmtok::Comma)) {
810 HasFillExpr = true;
811 if (ParseAbsoluteExpression(FillExpr))
812 return true;
813 }
814
815 if (Lexer.isNot(asmtok::EndOfStatement)) {
816 if (Lexer.isNot(asmtok::Comma))
817 return TokError("unexpected token in directive");
818 Lexer.Lex();
819
820 MaxBytesLoc = Lexer.getLoc();
821 if (ParseAbsoluteExpression(MaxBytesToFill))
822 return true;
823
824 if (Lexer.isNot(asmtok::EndOfStatement))
825 return TokError("unexpected token in directive");
826 }
827 }
828
829 Lexer.Lex();
830
831 if (!HasFillExpr) {
832 // FIXME: Sometimes fill with nop.
833 FillExpr = 0;
834 }
835
836 // Compute alignment in bytes.
837 if (IsPow2) {
838 // FIXME: Diagnose overflow.
839 Alignment = 1 << Alignment;
840 }
841
842 // Diagnose non-sensical max bytes to fill.
843 if (MaxBytesLoc.isValid()) {
844 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000845 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
846 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000847 return false;
848 }
849
850 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000851 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
852 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000853 MaxBytesToFill = 0;
854 }
855 }
856
857 // FIXME: Target specific behavior about how the "extra" bytes are filled.
858 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
859
860 return false;
861}
862
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000863/// ParseDirectiveSymbolAttribute
864/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
865bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
866 if (Lexer.isNot(asmtok::EndOfStatement)) {
867 for (;;) {
868 if (Lexer.isNot(asmtok::Identifier))
869 return TokError("expected identifier in directive");
870
871 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
872 Lexer.Lex();
873
874 // If this is use of an undefined symbol then mark it external.
875 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
876 Sym->setExternal(true);
877
878 Out.EmitSymbolAttribute(Sym, Attr);
879
880 if (Lexer.is(asmtok::EndOfStatement))
881 break;
882
883 if (Lexer.isNot(asmtok::Comma))
884 return TokError("unexpected token in directive");
885 Lexer.Lex();
886 }
887 }
888
889 Lexer.Lex();
890 return false;
891}