blob: 9a71139873d77653147a3ec169ec9dccb3c1ab95 [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
Chris Lattner14ee48a2009-06-21 21:22:11 +000025bool AsmParser::Error(SMLoc L, const char *Msg) {
26 Lexer.PrintMessage(L, Msg);
27 return true;
28}
29
30bool AsmParser::TokError(const char *Msg) {
31 Lexer.PrintMessage(Lexer.getLoc(), Msg);
32 return true;
33}
34
Chris Lattner27aa7d22009-06-21 20:16:42 +000035bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036 // Prime the lexer.
37 Lexer.Lex();
38
39 while (Lexer.isNot(asmtok::Eof))
40 if (ParseStatement())
41 return true;
42
43 return false;
44}
45
Chris Lattner2cf5f142009-06-22 01:29:09 +000046/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
47void AsmParser::EatToEndOfStatement() {
48 while (Lexer.isNot(asmtok::EndOfStatement) &&
49 Lexer.isNot(asmtok::Eof))
50 Lexer.Lex();
51
52 // Eat EOL.
53 if (Lexer.is(asmtok::EndOfStatement))
54 Lexer.Lex();
55}
56
Chris Lattnerc4193832009-06-22 05:51:26 +000057
Chris Lattner74ec1a32009-06-22 06:32:03 +000058/// ParseParenExpr - Parse a paren expression and return it.
59/// NOTE: This assumes the leading '(' has already been consumed.
60///
61/// parenexpr ::= expr)
62///
Daniel Dunbar475839e2009-06-29 20:37:27 +000063bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +000064 if (ParseExpression(Res)) return true;
65 if (Lexer.isNot(asmtok::RParen))
66 return TokError("expected ')' in parentheses expression");
67 Lexer.Lex();
68 return false;
69}
Chris Lattnerc4193832009-06-22 05:51:26 +000070
Chris Lattner74ec1a32009-06-22 06:32:03 +000071/// ParsePrimaryExpr - Parse a primary expression and return it.
72/// primaryexpr ::= (parenexpr
73/// primaryexpr ::= symbol
74/// primaryexpr ::= number
75/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +000076bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +000077 switch (Lexer.getKind()) {
78 default:
79 return TokError("unknown token in expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +000080 case asmtok::Exclaim:
81 Lexer.Lex(); // Eat the operator.
82 if (ParsePrimaryExpr(Res))
83 return true;
84 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
85 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000086 case asmtok::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +000087 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +000088 // handle things like LFOO+4.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000089 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
90
91 // If this is use of an undefined symbol then mark it external.
92 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
93 Sym->setExternal(true);
94
95 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +000096 Lexer.Lex(); // Eat identifier.
97 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000098 }
Chris Lattnerc4193832009-06-22 05:51:26 +000099 case asmtok::IntVal:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000100 Res = new AsmConstantExpr(Lexer.getCurIntVal());
Chris Lattnerc4193832009-06-22 05:51:26 +0000101 Lexer.Lex(); // Eat identifier.
102 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000103 case asmtok::LParen:
104 Lexer.Lex(); // Eat the '('.
105 return ParseParenExpr(Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000106 case asmtok::Minus:
107 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000108 if (ParsePrimaryExpr(Res))
109 return true;
110 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
111 return false;
112 case asmtok::Plus:
113 Lexer.Lex(); // Eat the operator.
114 if (ParsePrimaryExpr(Res))
115 return true;
116 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
117 return false;
118 case asmtok::Tilde:
119 Lexer.Lex(); // Eat the operator.
120 if (ParsePrimaryExpr(Res))
121 return true;
122 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
123 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000124 }
125}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000126
127/// ParseExpression - Parse an expression and return it.
128///
129/// expr ::= expr +,- expr -> lowest.
130/// expr ::= expr |,^,&,! expr -> middle.
131/// expr ::= expr *,/,%,<<,>> expr -> highest.
132/// expr ::= primaryexpr
133///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000134bool AsmParser::ParseExpression(AsmExpr *&Res) {
135 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000136 return ParsePrimaryExpr(Res) ||
137 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000138}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000139
Daniel Dunbar475839e2009-06-29 20:37:27 +0000140bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
141 AsmExpr *Expr;
142
143 if (ParseExpression(Expr))
144 return true;
145
146 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
147 return TokError("expected absolute expression");
148
149 return false;
150}
151
152static unsigned getBinOpPrecedence(asmtok::TokKind K,
153 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000154 switch (K) {
155 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000156
157 // Lowest Precedence: &&, ||
158 case asmtok::AmpAmp:
159 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000160 return 1;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000161 case asmtok::PipePipe:
162 Kind = AsmBinaryExpr::LOr;
163 return 1;
164
165 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
166 case asmtok::Plus:
167 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000168 return 2;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000169 case asmtok::Minus:
170 Kind = AsmBinaryExpr::Sub;
171 return 2;
172 case asmtok::EqualEqual:
173 Kind = AsmBinaryExpr::EQ;
174 return 2;
175 case asmtok::ExclaimEqual:
176 case asmtok::LessGreater:
177 Kind = AsmBinaryExpr::NE;
178 return 2;
179 case asmtok::Less:
180 Kind = AsmBinaryExpr::LT;
181 return 2;
182 case asmtok::LessEqual:
183 Kind = AsmBinaryExpr::LTE;
184 return 2;
185 case asmtok::Greater:
186 Kind = AsmBinaryExpr::GT;
187 return 2;
188 case asmtok::GreaterEqual:
189 Kind = AsmBinaryExpr::GTE;
190 return 2;
191
192 // Intermediate Precedence: |, &, ^
193 //
194 // FIXME: gas seems to support '!' as an infix operator?
195 case asmtok::Pipe:
196 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000197 return 3;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000198 case asmtok::Caret:
199 Kind = AsmBinaryExpr::Xor;
200 return 3;
201 case asmtok::Amp:
202 Kind = AsmBinaryExpr::And;
203 return 3;
204
205 // Highest Precedence: *, /, %, <<, >>
206 case asmtok::Star:
207 Kind = AsmBinaryExpr::Mul;
208 return 4;
209 case asmtok::Slash:
210 Kind = AsmBinaryExpr::Div;
211 return 4;
212 case asmtok::Percent:
213 Kind = AsmBinaryExpr::Mod;
214 return 4;
215 case asmtok::LessLess:
216 Kind = AsmBinaryExpr::Shl;
217 return 4;
218 case asmtok::GreaterGreater:
219 Kind = AsmBinaryExpr::Shr;
220 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000221 }
222}
223
224
225/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
226/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000227bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000228 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000229 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000230 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000231
232 // If the next token is lower precedence than we are allowed to eat, return
233 // successfully with what we ate already.
234 if (TokPrec < Precedence)
235 return false;
236
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000237 Lexer.Lex();
238
239 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000240 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000241 if (ParsePrimaryExpr(RHS)) return true;
242
243 // If BinOp binds less tightly with RHS than the operator after RHS, let
244 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000245 AsmBinaryExpr::Opcode Dummy;
246 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000247 if (TokPrec < NextTokPrec) {
248 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
249 }
250
Daniel Dunbar475839e2009-06-29 20:37:27 +0000251 // Merge LHS and RHS according to operator.
252 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000253 }
254}
255
Chris Lattnerc4193832009-06-22 05:51:26 +0000256
257
258
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000259/// ParseStatement:
260/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000261/// ::= Label* Directive ...Operands... EndOfStatement
262/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000263bool AsmParser::ParseStatement() {
264 switch (Lexer.getKind()) {
265 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000266 return TokError("unexpected token at start of statement");
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000267 case asmtok::EndOfStatement:
268 Lexer.Lex();
269 return false;
270 case asmtok::Identifier:
271 break;
272 // TODO: Recurse on local labels etc.
273 }
274
275 // If we have an identifier, handle it as the key symbol.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000276 SMLoc IDLoc = Lexer.getLoc();
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000277 const char *IDVal = Lexer.getCurStrVal();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000278
279 // Consume the identifier, see what is after it.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000280 switch (Lexer.Lex()) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000281 case asmtok::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000282 // identifier ':' -> Label.
283 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000284
285 // Diagnose attempt to use a variable as a label.
286 //
287 // FIXME: Diagnostics. Note the location of the definition as a label.
288 // FIXME: This doesn't diagnose assignment to a symbol which has been
289 // implicitly marked as external.
290 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
291 if (Sym->getSection())
292 return Error(IDLoc, "invalid symbol redefinition");
293 if (Ctx.GetSymbolValue(Sym))
294 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000295
296 // Since we saw a label, create a symbol and emit it.
297 // FIXME: If the label starts with L it is an assembler temporary label.
298 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000299 Out.EmitLabel(Sym);
300
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000301 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000302 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000303
304 case asmtok::Equal:
305 // identifier '=' ... -> assignment statement
306 Lexer.Lex();
307
308 return ParseAssignment(IDVal, false);
309
310 default: // Normal instruction or directive.
311 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000312 }
313
314 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000315 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000316 // FIXME: This should be driven based on a hash lookup and callback.
Chris Lattner9a023f72009-06-24 04:43:34 +0000317 if (!strcmp(IDVal, ".section"))
Chris Lattner529fb542009-06-24 05:13:15 +0000318 return ParseDirectiveDarwinSection();
319 if (!strcmp(IDVal, ".text"))
320 // FIXME: This changes behavior based on the -static flag to the
321 // assembler.
322 return ParseDirectiveSectionSwitch("__TEXT,__text",
323 "regular,pure_instructions");
324 if (!strcmp(IDVal, ".const"))
325 return ParseDirectiveSectionSwitch("__TEXT,__const");
326 if (!strcmp(IDVal, ".static_const"))
327 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
328 if (!strcmp(IDVal, ".cstring"))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000329 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
330 "cstring_literals");
Chris Lattner529fb542009-06-24 05:13:15 +0000331 if (!strcmp(IDVal, ".literal4"))
332 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
333 if (!strcmp(IDVal, ".literal8"))
334 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
335 if (!strcmp(IDVal, ".literal16"))
336 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
337 "16byte_literals");
338 if (!strcmp(IDVal, ".constructor"))
339 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
340 if (!strcmp(IDVal, ".destructor"))
341 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
342 if (!strcmp(IDVal, ".fvmlib_init0"))
343 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
344 if (!strcmp(IDVal, ".fvmlib_init1"))
345 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
346 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
347 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
348 "self_modifying_code+pure_instructions,5");
349 // FIXME: .picsymbol_stub on PPC.
350 if (!strcmp(IDVal, ".data"))
351 return ParseDirectiveSectionSwitch("__DATA,__data");
352 if (!strcmp(IDVal, ".static_data"))
353 return ParseDirectiveSectionSwitch("__DATA,__static_data");
354 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
355 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
356 "non_lazy_symbol_pointers");
357 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
358 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
359 "lazy_symbol_pointers");
360 if (!strcmp(IDVal, ".dyld"))
361 return ParseDirectiveSectionSwitch("__DATA,__dyld");
362 if (!strcmp(IDVal, ".mod_init_func"))
363 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
364 "mod_init_funcs");
365 if (!strcmp(IDVal, ".mod_term_func"))
366 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
367 "mod_term_funcs");
368 if (!strcmp(IDVal, ".const_data"))
369 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
370
371
372 // FIXME: Verify attributes on sections.
373 if (!strcmp(IDVal, ".objc_class"))
374 return ParseDirectiveSectionSwitch("__OBJC,__class");
375 if (!strcmp(IDVal, ".objc_meta_class"))
376 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
377 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
378 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
379 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
380 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
381 if (!strcmp(IDVal, ".objc_protocol"))
382 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
383 if (!strcmp(IDVal, ".objc_string_object"))
384 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
385 if (!strcmp(IDVal, ".objc_cls_meth"))
386 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
387 if (!strcmp(IDVal, ".objc_inst_meth"))
388 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
389 if (!strcmp(IDVal, ".objc_cls_refs"))
390 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
391 if (!strcmp(IDVal, ".objc_message_refs"))
392 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
393 if (!strcmp(IDVal, ".objc_symbols"))
394 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
395 if (!strcmp(IDVal, ".objc_category"))
396 return ParseDirectiveSectionSwitch("__OBJC,__category");
397 if (!strcmp(IDVal, ".objc_class_vars"))
398 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
399 if (!strcmp(IDVal, ".objc_instance_vars"))
400 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
401 if (!strcmp(IDVal, ".objc_module_info"))
402 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
403 if (!strcmp(IDVal, ".objc_class_names"))
404 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
405 if (!strcmp(IDVal, ".objc_meth_var_types"))
406 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
407 if (!strcmp(IDVal, ".objc_meth_var_names"))
408 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
409 if (!strcmp(IDVal, ".objc_selector_strs"))
410 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000411
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000412 // Assembler features
413 if (!strcmp(IDVal, ".set"))
414 return ParseDirectiveSet();
415
Daniel Dunbara0d14262009-06-24 23:30:00 +0000416 // Data directives
417
418 if (!strcmp(IDVal, ".ascii"))
419 return ParseDirectiveAscii(false);
420 if (!strcmp(IDVal, ".asciz"))
421 return ParseDirectiveAscii(true);
422
423 // FIXME: Target hooks for size? Also for "word", "hword".
424 if (!strcmp(IDVal, ".byte"))
425 return ParseDirectiveValue(1);
426 if (!strcmp(IDVal, ".short"))
427 return ParseDirectiveValue(2);
428 if (!strcmp(IDVal, ".long"))
429 return ParseDirectiveValue(4);
430 if (!strcmp(IDVal, ".quad"))
431 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000432
433 // FIXME: Target hooks for IsPow2.
434 if (!strcmp(IDVal, ".align"))
435 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
436 if (!strcmp(IDVal, ".align32"))
437 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
438 if (!strcmp(IDVal, ".balign"))
439 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
440 if (!strcmp(IDVal, ".balignw"))
441 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
442 if (!strcmp(IDVal, ".balignl"))
443 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
444 if (!strcmp(IDVal, ".p2align"))
445 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
446 if (!strcmp(IDVal, ".p2alignw"))
447 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
448 if (!strcmp(IDVal, ".p2alignl"))
449 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
450
Daniel Dunbarc238b582009-06-25 22:44:51 +0000451 if (!strcmp(IDVal, ".org"))
452 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000453
454 if (!strcmp(IDVal, ".fill"))
455 return ParseDirectiveFill();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000456 if (!strcmp(IDVal, ".space"))
457 return ParseDirectiveSpace();
458
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000459 // Symbol attribute directives
460 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
461 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
462 if (!strcmp(IDVal, ".hidden"))
463 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
464 if (!strcmp(IDVal, ".indirect_symbol"))
465 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
466 if (!strcmp(IDVal, ".internal"))
467 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
468 if (!strcmp(IDVal, ".lazy_reference"))
469 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
470 if (!strcmp(IDVal, ".no_dead_strip"))
471 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
472 if (!strcmp(IDVal, ".private_extern"))
473 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
474 if (!strcmp(IDVal, ".protected"))
475 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
476 if (!strcmp(IDVal, ".reference"))
477 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
478 if (!strcmp(IDVal, ".weak"))
479 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
480 if (!strcmp(IDVal, ".weak_definition"))
481 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
482 if (!strcmp(IDVal, ".weak_reference"))
483 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
484
Chris Lattner2cf5f142009-06-22 01:29:09 +0000485 Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now");
486 EatToEndOfStatement();
487 return false;
488 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000489
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000490 MCInst Inst;
491 if (ParseX86InstOperands(Inst))
492 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000493
494 if (Lexer.isNot(asmtok::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000495 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000496
497 // Eat the end of statement marker.
498 Lexer.Lex();
499
500 // Instruction is good, process it.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000501 outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
Chris Lattner2cf5f142009-06-22 01:29:09 +0000502 << " operands.\n";
503
504 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000505 return false;
506}
Chris Lattner9a023f72009-06-24 04:43:34 +0000507
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000508bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000509 // FIXME: Use better location, we should use proper tokens.
510 SMLoc EqualLoc = Lexer.getLoc();
511
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000512 int64_t Value;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000513 if (ParseAbsoluteExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000514 return true;
515
516 if (Lexer.isNot(asmtok::EndOfStatement))
517 return TokError("unexpected token in assignment");
518
519 // Eat the end of statement marker.
520 Lexer.Lex();
521
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000522 // Diagnose assignment to a label.
523 //
524 // FIXME: Diagnostics. Note the location of the definition as a label.
525 // FIXME: This doesn't diagnose assignment to a symbol which has been
526 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000527 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000528 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000529 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000530 if (Sym->getSection())
531 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
532 if (Sym->isExternal())
533 return Error(EqualLoc, "invalid assignment to external symbol");
534
535 // Do the assignment.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000536 Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
537
538 return false;
539}
540
541/// ParseDirectiveSet:
542/// ::= .set identifier ',' expression
543bool AsmParser::ParseDirectiveSet() {
544 if (Lexer.isNot(asmtok::Identifier))
545 return TokError("expected identifier after '.set' directive");
546
547 const char *Name = Lexer.getCurStrVal();
548
549 if (Lexer.Lex() != asmtok::Comma)
550 return TokError("unexpected token in '.set'");
551 Lexer.Lex();
552
553 return ParseAssignment(Name, true);
554}
555
Chris Lattner9a023f72009-06-24 04:43:34 +0000556/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000557/// ::= .section identifier (',' identifier)*
558/// FIXME: This should actually parse out the segment, section, attributes and
559/// sizeof_stub fields.
560bool AsmParser::ParseDirectiveDarwinSection() {
Chris Lattner9a023f72009-06-24 04:43:34 +0000561 if (Lexer.isNot(asmtok::Identifier))
562 return TokError("expected identifier after '.section' directive");
563
564 std::string Section = Lexer.getCurStrVal();
565 Lexer.Lex();
566
567 // Accept a comma separated list of modifiers.
568 while (Lexer.is(asmtok::Comma)) {
569 Lexer.Lex();
570
571 if (Lexer.isNot(asmtok::Identifier))
572 return TokError("expected identifier in '.section' directive");
573 Section += ',';
574 Section += Lexer.getCurStrVal();
575 Lexer.Lex();
576 }
577
578 if (Lexer.isNot(asmtok::EndOfStatement))
579 return TokError("unexpected token in '.section' directive");
580 Lexer.Lex();
581
582 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
583 return false;
584}
585
Chris Lattner529fb542009-06-24 05:13:15 +0000586bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
587 const char *Directives) {
588 if (Lexer.isNot(asmtok::EndOfStatement))
589 return TokError("unexpected token in section switching directive");
590 Lexer.Lex();
591
592 std::string SectionStr = Section;
593 if (Directives && Directives[0]) {
594 SectionStr += ",";
595 SectionStr += Directives;
596 }
597
598 Out.SwitchSection(Ctx.GetSection(Section));
599 return false;
600}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000601
602/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000603/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000604bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
605 if (Lexer.isNot(asmtok::EndOfStatement)) {
606 for (;;) {
607 if (Lexer.isNot(asmtok::String))
608 return TokError("expected string in '.ascii' or '.asciz' directive");
609
610 // FIXME: This shouldn't use a const char* + strlen, the string could have
611 // embedded nulls.
612 // FIXME: Should have accessor for getting string contents.
613 const char *Str = Lexer.getCurStrVal();
614 Out.EmitBytes(Str + 1, strlen(Str) - 2);
615 if (ZeroTerminated)
616 Out.EmitBytes("\0", 1);
617
618 Lexer.Lex();
619
620 if (Lexer.is(asmtok::EndOfStatement))
621 break;
622
623 if (Lexer.isNot(asmtok::Comma))
624 return TokError("unexpected token in '.ascii' or '.asciz' directive");
625 Lexer.Lex();
626 }
627 }
628
629 Lexer.Lex();
630 return false;
631}
632
633/// ParseDirectiveValue
634/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
635bool AsmParser::ParseDirectiveValue(unsigned Size) {
636 if (Lexer.isNot(asmtok::EndOfStatement)) {
637 for (;;) {
638 int64_t Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000639 if (ParseAbsoluteExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000640 return true;
641
642 Out.EmitValue(MCValue::get(Expr), Size);
643
644 if (Lexer.is(asmtok::EndOfStatement))
645 break;
646
647 // FIXME: Improve diagnostic.
648 if (Lexer.isNot(asmtok::Comma))
649 return TokError("unexpected token in directive");
650 Lexer.Lex();
651 }
652 }
653
654 Lexer.Lex();
655 return false;
656}
657
658/// ParseDirectiveSpace
659/// ::= .space expression [ , expression ]
660bool AsmParser::ParseDirectiveSpace() {
661 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000662 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000663 return true;
664
665 int64_t FillExpr = 0;
666 bool HasFillExpr = false;
667 if (Lexer.isNot(asmtok::EndOfStatement)) {
668 if (Lexer.isNot(asmtok::Comma))
669 return TokError("unexpected token in '.space' directive");
670 Lexer.Lex();
671
Daniel Dunbar475839e2009-06-29 20:37:27 +0000672 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000673 return true;
674
675 HasFillExpr = true;
676
677 if (Lexer.isNot(asmtok::EndOfStatement))
678 return TokError("unexpected token in '.space' directive");
679 }
680
681 Lexer.Lex();
682
683 if (NumBytes <= 0)
684 return TokError("invalid number of bytes in '.space' directive");
685
686 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
687 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
688 Out.EmitValue(MCValue::get(FillExpr), 1);
689
690 return false;
691}
692
693/// ParseDirectiveFill
694/// ::= .fill expression , expression , expression
695bool AsmParser::ParseDirectiveFill() {
696 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000697 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000698 return true;
699
700 if (Lexer.isNot(asmtok::Comma))
701 return TokError("unexpected token in '.fill' directive");
702 Lexer.Lex();
703
704 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000705 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000706 return true;
707
708 if (Lexer.isNot(asmtok::Comma))
709 return TokError("unexpected token in '.fill' directive");
710 Lexer.Lex();
711
712 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000713 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000714 return true;
715
716 if (Lexer.isNot(asmtok::EndOfStatement))
717 return TokError("unexpected token in '.fill' directive");
718
719 Lexer.Lex();
720
721 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
722 return TokError("invalid '.fill' size, expected 1, 2, or 4");
723
724 for (uint64_t i = 0, e = NumValues; i != e; ++i)
725 Out.EmitValue(MCValue::get(FillExpr), FillSize);
726
727 return false;
728}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000729
730/// ParseDirectiveOrg
731/// ::= .org expression [ , expression ]
732bool AsmParser::ParseDirectiveOrg() {
733 int64_t Offset;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000734 if (ParseAbsoluteExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000735 return true;
736
737 // Parse optional fill expression.
738 int64_t FillExpr = 0;
739 if (Lexer.isNot(asmtok::EndOfStatement)) {
740 if (Lexer.isNot(asmtok::Comma))
741 return TokError("unexpected token in '.org' directive");
742 Lexer.Lex();
743
Daniel Dunbar475839e2009-06-29 20:37:27 +0000744 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000745 return true;
746
747 if (Lexer.isNot(asmtok::EndOfStatement))
748 return TokError("unexpected token in '.org' directive");
749 }
750
751 Lexer.Lex();
752
753 Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
754
755 return false;
756}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000757
758/// ParseDirectiveAlign
759/// ::= {.align, ...} expression [ , expression [ , expression ]]
760bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
761 int64_t Alignment;
762 if (ParseAbsoluteExpression(Alignment))
763 return true;
764
765 SMLoc MaxBytesLoc;
766 bool HasFillExpr = false;
767 int64_t FillExpr = 0;
768 int64_t MaxBytesToFill = 0;
769 if (Lexer.isNot(asmtok::EndOfStatement)) {
770 if (Lexer.isNot(asmtok::Comma))
771 return TokError("unexpected token in directive");
772 Lexer.Lex();
773
774 // The fill expression can be omitted while specifying a maximum number of
775 // alignment bytes, e.g:
776 // .align 3,,4
777 if (Lexer.isNot(asmtok::Comma)) {
778 HasFillExpr = true;
779 if (ParseAbsoluteExpression(FillExpr))
780 return true;
781 }
782
783 if (Lexer.isNot(asmtok::EndOfStatement)) {
784 if (Lexer.isNot(asmtok::Comma))
785 return TokError("unexpected token in directive");
786 Lexer.Lex();
787
788 MaxBytesLoc = Lexer.getLoc();
789 if (ParseAbsoluteExpression(MaxBytesToFill))
790 return true;
791
792 if (Lexer.isNot(asmtok::EndOfStatement))
793 return TokError("unexpected token in directive");
794 }
795 }
796
797 Lexer.Lex();
798
799 if (!HasFillExpr) {
800 // FIXME: Sometimes fill with nop.
801 FillExpr = 0;
802 }
803
804 // Compute alignment in bytes.
805 if (IsPow2) {
806 // FIXME: Diagnose overflow.
807 Alignment = 1 << Alignment;
808 }
809
810 // Diagnose non-sensical max bytes to fill.
811 if (MaxBytesLoc.isValid()) {
812 if (MaxBytesToFill < 1) {
813 Lexer.PrintMessage(MaxBytesLoc, "warning: alignment directive can never "
814 "be satisfied in this many bytes, ignoring");
815 return false;
816 }
817
818 if (MaxBytesToFill >= Alignment) {
819 Lexer.PrintMessage(MaxBytesLoc, "warning: maximum bytes expression "
820 "exceeds alignment and has no effect");
821 MaxBytesToFill = 0;
822 }
823 }
824
825 // FIXME: Target specific behavior about how the "extra" bytes are filled.
826 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
827
828 return false;
829}
830
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000831/// ParseDirectiveSymbolAttribute
832/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
833bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
834 if (Lexer.isNot(asmtok::EndOfStatement)) {
835 for (;;) {
836 if (Lexer.isNot(asmtok::Identifier))
837 return TokError("expected identifier in directive");
838
839 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
840 Lexer.Lex();
841
842 // If this is use of an undefined symbol then mark it external.
843 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
844 Sym->setExternal(true);
845
846 Out.EmitSymbolAttribute(Sym, Attr);
847
848 if (Lexer.is(asmtok::EndOfStatement))
849 break;
850
851 if (Lexer.isNot(asmtok::Comma))
852 return TokError("unexpected token in directive");
853 Lexer.Lex();
854 }
855 }
856
857 Lexer.Lex();
858 return false;
859}