blob: 9121797e814fce136716b9fdf33a4719ed122503 [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"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000020#include "llvm/Support/SourceMgr.h"
21#include "llvm/Support/raw_ostream.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000022using namespace llvm;
23
Chris Lattner14ee48a2009-06-21 21:22:11 +000024bool AsmParser::Error(SMLoc L, const char *Msg) {
25 Lexer.PrintMessage(L, Msg);
26 return true;
27}
28
29bool AsmParser::TokError(const char *Msg) {
30 Lexer.PrintMessage(Lexer.getLoc(), Msg);
31 return true;
32}
33
Chris Lattner27aa7d22009-06-21 20:16:42 +000034bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000035 // Prime the lexer.
36 Lexer.Lex();
37
38 while (Lexer.isNot(asmtok::Eof))
39 if (ParseStatement())
40 return true;
41
42 return false;
43}
44
Chris Lattner2cf5f142009-06-22 01:29:09 +000045/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
46void AsmParser::EatToEndOfStatement() {
47 while (Lexer.isNot(asmtok::EndOfStatement) &&
48 Lexer.isNot(asmtok::Eof))
49 Lexer.Lex();
50
51 // Eat EOL.
52 if (Lexer.is(asmtok::EndOfStatement))
53 Lexer.Lex();
54}
55
Chris Lattnerc4193832009-06-22 05:51:26 +000056
Chris Lattner74ec1a32009-06-22 06:32:03 +000057/// ParseParenExpr - Parse a paren expression and return it.
58/// NOTE: This assumes the leading '(' has already been consumed.
59///
60/// parenexpr ::= expr)
61///
Daniel Dunbar475839e2009-06-29 20:37:27 +000062bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +000063 if (ParseExpression(Res)) return true;
64 if (Lexer.isNot(asmtok::RParen))
65 return TokError("expected ')' in parentheses expression");
66 Lexer.Lex();
67 return false;
68}
Chris Lattnerc4193832009-06-22 05:51:26 +000069
Chris Lattner74ec1a32009-06-22 06:32:03 +000070/// ParsePrimaryExpr - Parse a primary expression and return it.
71/// primaryexpr ::= (parenexpr
72/// primaryexpr ::= symbol
73/// primaryexpr ::= number
74/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +000075bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +000076 switch (Lexer.getKind()) {
77 default:
78 return TokError("unknown token in expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +000079 case asmtok::Exclaim:
80 Lexer.Lex(); // Eat the operator.
81 if (ParsePrimaryExpr(Res))
82 return true;
83 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
84 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +000085 case asmtok::Identifier:
86 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +000087 // handle things like LFOO+4.
88 Res = new AsmSymbolRefExpr(Ctx.GetOrCreateSymbol(Lexer.getCurStrVal()));
Chris Lattnerc4193832009-06-22 05:51:26 +000089 Lexer.Lex(); // Eat identifier.
90 return false;
91 case asmtok::IntVal:
Daniel Dunbar475839e2009-06-29 20:37:27 +000092 Res = new AsmConstantExpr(Lexer.getCurIntVal());
Chris Lattnerc4193832009-06-22 05:51:26 +000093 Lexer.Lex(); // Eat identifier.
94 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +000095 case asmtok::LParen:
96 Lexer.Lex(); // Eat the '('.
97 return ParseParenExpr(Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +000098 case asmtok::Minus:
99 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000100 if (ParsePrimaryExpr(Res))
101 return true;
102 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
103 return false;
104 case asmtok::Plus:
105 Lexer.Lex(); // Eat the operator.
106 if (ParsePrimaryExpr(Res))
107 return true;
108 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
109 return false;
110 case asmtok::Tilde:
111 Lexer.Lex(); // Eat the operator.
112 if (ParsePrimaryExpr(Res))
113 return true;
114 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
115 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000116 }
117}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000118
119/// ParseExpression - Parse an expression and return it.
120///
121/// expr ::= expr +,- expr -> lowest.
122/// expr ::= expr |,^,&,! expr -> middle.
123/// expr ::= expr *,/,%,<<,>> expr -> highest.
124/// expr ::= primaryexpr
125///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000126bool AsmParser::ParseExpression(AsmExpr *&Res) {
127 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000128 return ParsePrimaryExpr(Res) ||
129 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000130}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000131
Daniel Dunbar475839e2009-06-29 20:37:27 +0000132bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
133 AsmExpr *Expr;
134
135 if (ParseExpression(Expr))
136 return true;
137
138 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
139 return TokError("expected absolute expression");
140
141 return false;
142}
143
144static unsigned getBinOpPrecedence(asmtok::TokKind K,
145 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000146 switch (K) {
147 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000148
149 // Lowest Precedence: &&, ||
150 case asmtok::AmpAmp:
151 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000152 return 1;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000153 case asmtok::PipePipe:
154 Kind = AsmBinaryExpr::LOr;
155 return 1;
156
157 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
158 case asmtok::Plus:
159 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000160 return 2;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000161 case asmtok::Minus:
162 Kind = AsmBinaryExpr::Sub;
163 return 2;
164 case asmtok::EqualEqual:
165 Kind = AsmBinaryExpr::EQ;
166 return 2;
167 case asmtok::ExclaimEqual:
168 case asmtok::LessGreater:
169 Kind = AsmBinaryExpr::NE;
170 return 2;
171 case asmtok::Less:
172 Kind = AsmBinaryExpr::LT;
173 return 2;
174 case asmtok::LessEqual:
175 Kind = AsmBinaryExpr::LTE;
176 return 2;
177 case asmtok::Greater:
178 Kind = AsmBinaryExpr::GT;
179 return 2;
180 case asmtok::GreaterEqual:
181 Kind = AsmBinaryExpr::GTE;
182 return 2;
183
184 // Intermediate Precedence: |, &, ^
185 //
186 // FIXME: gas seems to support '!' as an infix operator?
187 case asmtok::Pipe:
188 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000189 return 3;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000190 case asmtok::Caret:
191 Kind = AsmBinaryExpr::Xor;
192 return 3;
193 case asmtok::Amp:
194 Kind = AsmBinaryExpr::And;
195 return 3;
196
197 // Highest Precedence: *, /, %, <<, >>
198 case asmtok::Star:
199 Kind = AsmBinaryExpr::Mul;
200 return 4;
201 case asmtok::Slash:
202 Kind = AsmBinaryExpr::Div;
203 return 4;
204 case asmtok::Percent:
205 Kind = AsmBinaryExpr::Mod;
206 return 4;
207 case asmtok::LessLess:
208 Kind = AsmBinaryExpr::Shl;
209 return 4;
210 case asmtok::GreaterGreater:
211 Kind = AsmBinaryExpr::Shr;
212 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000213 }
214}
215
216
217/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
218/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000219bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000220 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000221 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000222 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000223
224 // If the next token is lower precedence than we are allowed to eat, return
225 // successfully with what we ate already.
226 if (TokPrec < Precedence)
227 return false;
228
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000229 Lexer.Lex();
230
231 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000232 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000233 if (ParsePrimaryExpr(RHS)) return true;
234
235 // If BinOp binds less tightly with RHS than the operator after RHS, let
236 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000237 AsmBinaryExpr::Opcode Dummy;
238 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000239 if (TokPrec < NextTokPrec) {
240 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
241 }
242
Daniel Dunbar475839e2009-06-29 20:37:27 +0000243 // Merge LHS and RHS according to operator.
244 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000245 }
246}
247
Chris Lattnerc4193832009-06-22 05:51:26 +0000248
249
250
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000251/// ParseStatement:
252/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000253/// ::= Label* Directive ...Operands... EndOfStatement
254/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000255bool AsmParser::ParseStatement() {
256 switch (Lexer.getKind()) {
257 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000258 return TokError("unexpected token at start of statement");
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000259 case asmtok::EndOfStatement:
260 Lexer.Lex();
261 return false;
262 case asmtok::Identifier:
263 break;
264 // TODO: Recurse on local labels etc.
265 }
266
267 // If we have an identifier, handle it as the key symbol.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000268 SMLoc IDLoc = Lexer.getLoc();
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000269 const char *IDVal = Lexer.getCurStrVal();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000270
271 // Consume the identifier, see what is after it.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000272 switch (Lexer.Lex()) {
273 case asmtok::Colon:
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000274 // identifier ':' -> Label.
275 Lexer.Lex();
Chris Lattnerc69485e2009-06-24 04:31:49 +0000276
277 // Since we saw a label, create a symbol and emit it.
278 // FIXME: If the label starts with L it is an assembler temporary label.
279 // Why does the client of this api need to know this?
280 Out.EmitLabel(Ctx.GetOrCreateSymbol(IDVal));
281
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000282 return ParseStatement();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000283
284 case asmtok::Equal:
285 // identifier '=' ... -> assignment statement
286 Lexer.Lex();
287
288 return ParseAssignment(IDVal, false);
289
290 default: // Normal instruction or directive.
291 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000292 }
293
294 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000295 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000296 // FIXME: This should be driven based on a hash lookup and callback.
Chris Lattner9a023f72009-06-24 04:43:34 +0000297 if (!strcmp(IDVal, ".section"))
Chris Lattner529fb542009-06-24 05:13:15 +0000298 return ParseDirectiveDarwinSection();
299 if (!strcmp(IDVal, ".text"))
300 // FIXME: This changes behavior based on the -static flag to the
301 // assembler.
302 return ParseDirectiveSectionSwitch("__TEXT,__text",
303 "regular,pure_instructions");
304 if (!strcmp(IDVal, ".const"))
305 return ParseDirectiveSectionSwitch("__TEXT,__const");
306 if (!strcmp(IDVal, ".static_const"))
307 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
308 if (!strcmp(IDVal, ".cstring"))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000309 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
310 "cstring_literals");
Chris Lattner529fb542009-06-24 05:13:15 +0000311 if (!strcmp(IDVal, ".literal4"))
312 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
313 if (!strcmp(IDVal, ".literal8"))
314 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
315 if (!strcmp(IDVal, ".literal16"))
316 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
317 "16byte_literals");
318 if (!strcmp(IDVal, ".constructor"))
319 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
320 if (!strcmp(IDVal, ".destructor"))
321 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
322 if (!strcmp(IDVal, ".fvmlib_init0"))
323 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
324 if (!strcmp(IDVal, ".fvmlib_init1"))
325 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
326 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
327 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
328 "self_modifying_code+pure_instructions,5");
329 // FIXME: .picsymbol_stub on PPC.
330 if (!strcmp(IDVal, ".data"))
331 return ParseDirectiveSectionSwitch("__DATA,__data");
332 if (!strcmp(IDVal, ".static_data"))
333 return ParseDirectiveSectionSwitch("__DATA,__static_data");
334 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
335 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
336 "non_lazy_symbol_pointers");
337 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
338 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
339 "lazy_symbol_pointers");
340 if (!strcmp(IDVal, ".dyld"))
341 return ParseDirectiveSectionSwitch("__DATA,__dyld");
342 if (!strcmp(IDVal, ".mod_init_func"))
343 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
344 "mod_init_funcs");
345 if (!strcmp(IDVal, ".mod_term_func"))
346 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
347 "mod_term_funcs");
348 if (!strcmp(IDVal, ".const_data"))
349 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
350
351
352 // FIXME: Verify attributes on sections.
353 if (!strcmp(IDVal, ".objc_class"))
354 return ParseDirectiveSectionSwitch("__OBJC,__class");
355 if (!strcmp(IDVal, ".objc_meta_class"))
356 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
357 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
358 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
359 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
360 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
361 if (!strcmp(IDVal, ".objc_protocol"))
362 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
363 if (!strcmp(IDVal, ".objc_string_object"))
364 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
365 if (!strcmp(IDVal, ".objc_cls_meth"))
366 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
367 if (!strcmp(IDVal, ".objc_inst_meth"))
368 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
369 if (!strcmp(IDVal, ".objc_cls_refs"))
370 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
371 if (!strcmp(IDVal, ".objc_message_refs"))
372 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
373 if (!strcmp(IDVal, ".objc_symbols"))
374 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
375 if (!strcmp(IDVal, ".objc_category"))
376 return ParseDirectiveSectionSwitch("__OBJC,__category");
377 if (!strcmp(IDVal, ".objc_class_vars"))
378 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
379 if (!strcmp(IDVal, ".objc_instance_vars"))
380 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
381 if (!strcmp(IDVal, ".objc_module_info"))
382 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
383 if (!strcmp(IDVal, ".objc_class_names"))
384 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
385 if (!strcmp(IDVal, ".objc_meth_var_types"))
386 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
387 if (!strcmp(IDVal, ".objc_meth_var_names"))
388 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
389 if (!strcmp(IDVal, ".objc_selector_strs"))
390 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000391
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000392 // Assembler features
393 if (!strcmp(IDVal, ".set"))
394 return ParseDirectiveSet();
395
Daniel Dunbara0d14262009-06-24 23:30:00 +0000396 // Data directives
397
398 if (!strcmp(IDVal, ".ascii"))
399 return ParseDirectiveAscii(false);
400 if (!strcmp(IDVal, ".asciz"))
401 return ParseDirectiveAscii(true);
402
403 // FIXME: Target hooks for size? Also for "word", "hword".
404 if (!strcmp(IDVal, ".byte"))
405 return ParseDirectiveValue(1);
406 if (!strcmp(IDVal, ".short"))
407 return ParseDirectiveValue(2);
408 if (!strcmp(IDVal, ".long"))
409 return ParseDirectiveValue(4);
410 if (!strcmp(IDVal, ".quad"))
411 return ParseDirectiveValue(8);
412 if (!strcmp(IDVal, ".fill"))
413 return ParseDirectiveFill();
Daniel Dunbarc238b582009-06-25 22:44:51 +0000414 if (!strcmp(IDVal, ".org"))
415 return ParseDirectiveOrg();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000416 if (!strcmp(IDVal, ".space"))
417 return ParseDirectiveSpace();
418
Chris Lattner2cf5f142009-06-22 01:29:09 +0000419 Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now");
420 EatToEndOfStatement();
421 return false;
422 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000423
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000424 MCInst Inst;
425 if (ParseX86InstOperands(Inst))
426 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000427
428 if (Lexer.isNot(asmtok::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000429 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000430
431 // Eat the end of statement marker.
432 Lexer.Lex();
433
434 // Instruction is good, process it.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000435 outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
Chris Lattner2cf5f142009-06-22 01:29:09 +0000436 << " operands.\n";
437
438 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000439 return false;
440}
Chris Lattner9a023f72009-06-24 04:43:34 +0000441
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000442bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
443 int64_t Value;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000444 if (ParseAbsoluteExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000445 return true;
446
447 if (Lexer.isNot(asmtok::EndOfStatement))
448 return TokError("unexpected token in assignment");
449
450 // Eat the end of statement marker.
451 Lexer.Lex();
452
453 // Get the symbol for this name.
454 // FIXME: Handle '.'.
455 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
456 Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
457
458 return false;
459}
460
461/// ParseDirectiveSet:
462/// ::= .set identifier ',' expression
463bool AsmParser::ParseDirectiveSet() {
464 if (Lexer.isNot(asmtok::Identifier))
465 return TokError("expected identifier after '.set' directive");
466
467 const char *Name = Lexer.getCurStrVal();
468
469 if (Lexer.Lex() != asmtok::Comma)
470 return TokError("unexpected token in '.set'");
471 Lexer.Lex();
472
473 return ParseAssignment(Name, true);
474}
475
Chris Lattner9a023f72009-06-24 04:43:34 +0000476/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000477/// ::= .section identifier (',' identifier)*
478/// FIXME: This should actually parse out the segment, section, attributes and
479/// sizeof_stub fields.
480bool AsmParser::ParseDirectiveDarwinSection() {
Chris Lattner9a023f72009-06-24 04:43:34 +0000481 if (Lexer.isNot(asmtok::Identifier))
482 return TokError("expected identifier after '.section' directive");
483
484 std::string Section = Lexer.getCurStrVal();
485 Lexer.Lex();
486
487 // Accept a comma separated list of modifiers.
488 while (Lexer.is(asmtok::Comma)) {
489 Lexer.Lex();
490
491 if (Lexer.isNot(asmtok::Identifier))
492 return TokError("expected identifier in '.section' directive");
493 Section += ',';
494 Section += Lexer.getCurStrVal();
495 Lexer.Lex();
496 }
497
498 if (Lexer.isNot(asmtok::EndOfStatement))
499 return TokError("unexpected token in '.section' directive");
500 Lexer.Lex();
501
502 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
503 return false;
504}
505
Chris Lattner529fb542009-06-24 05:13:15 +0000506bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
507 const char *Directives) {
508 if (Lexer.isNot(asmtok::EndOfStatement))
509 return TokError("unexpected token in section switching directive");
510 Lexer.Lex();
511
512 std::string SectionStr = Section;
513 if (Directives && Directives[0]) {
514 SectionStr += ",";
515 SectionStr += Directives;
516 }
517
518 Out.SwitchSection(Ctx.GetSection(Section));
519 return false;
520}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000521
522/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000523/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000524bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
525 if (Lexer.isNot(asmtok::EndOfStatement)) {
526 for (;;) {
527 if (Lexer.isNot(asmtok::String))
528 return TokError("expected string in '.ascii' or '.asciz' directive");
529
530 // FIXME: This shouldn't use a const char* + strlen, the string could have
531 // embedded nulls.
532 // FIXME: Should have accessor for getting string contents.
533 const char *Str = Lexer.getCurStrVal();
534 Out.EmitBytes(Str + 1, strlen(Str) - 2);
535 if (ZeroTerminated)
536 Out.EmitBytes("\0", 1);
537
538 Lexer.Lex();
539
540 if (Lexer.is(asmtok::EndOfStatement))
541 break;
542
543 if (Lexer.isNot(asmtok::Comma))
544 return TokError("unexpected token in '.ascii' or '.asciz' directive");
545 Lexer.Lex();
546 }
547 }
548
549 Lexer.Lex();
550 return false;
551}
552
553/// ParseDirectiveValue
554/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
555bool AsmParser::ParseDirectiveValue(unsigned Size) {
556 if (Lexer.isNot(asmtok::EndOfStatement)) {
557 for (;;) {
558 int64_t Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000559 if (ParseAbsoluteExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000560 return true;
561
562 Out.EmitValue(MCValue::get(Expr), Size);
563
564 if (Lexer.is(asmtok::EndOfStatement))
565 break;
566
567 // FIXME: Improve diagnostic.
568 if (Lexer.isNot(asmtok::Comma))
569 return TokError("unexpected token in directive");
570 Lexer.Lex();
571 }
572 }
573
574 Lexer.Lex();
575 return false;
576}
577
578/// ParseDirectiveSpace
579/// ::= .space expression [ , expression ]
580bool AsmParser::ParseDirectiveSpace() {
581 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000582 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000583 return true;
584
585 int64_t FillExpr = 0;
586 bool HasFillExpr = false;
587 if (Lexer.isNot(asmtok::EndOfStatement)) {
588 if (Lexer.isNot(asmtok::Comma))
589 return TokError("unexpected token in '.space' directive");
590 Lexer.Lex();
591
Daniel Dunbar475839e2009-06-29 20:37:27 +0000592 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000593 return true;
594
595 HasFillExpr = true;
596
597 if (Lexer.isNot(asmtok::EndOfStatement))
598 return TokError("unexpected token in '.space' directive");
599 }
600
601 Lexer.Lex();
602
603 if (NumBytes <= 0)
604 return TokError("invalid number of bytes in '.space' directive");
605
606 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
607 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
608 Out.EmitValue(MCValue::get(FillExpr), 1);
609
610 return false;
611}
612
613/// ParseDirectiveFill
614/// ::= .fill expression , expression , expression
615bool AsmParser::ParseDirectiveFill() {
616 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000617 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000618 return true;
619
620 if (Lexer.isNot(asmtok::Comma))
621 return TokError("unexpected token in '.fill' directive");
622 Lexer.Lex();
623
624 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000625 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000626 return true;
627
628 if (Lexer.isNot(asmtok::Comma))
629 return TokError("unexpected token in '.fill' directive");
630 Lexer.Lex();
631
632 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000634 return true;
635
636 if (Lexer.isNot(asmtok::EndOfStatement))
637 return TokError("unexpected token in '.fill' directive");
638
639 Lexer.Lex();
640
641 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
642 return TokError("invalid '.fill' size, expected 1, 2, or 4");
643
644 for (uint64_t i = 0, e = NumValues; i != e; ++i)
645 Out.EmitValue(MCValue::get(FillExpr), FillSize);
646
647 return false;
648}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000649
650/// ParseDirectiveOrg
651/// ::= .org expression [ , expression ]
652bool AsmParser::ParseDirectiveOrg() {
653 int64_t Offset;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000654 if (ParseAbsoluteExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000655 return true;
656
657 // Parse optional fill expression.
658 int64_t FillExpr = 0;
659 if (Lexer.isNot(asmtok::EndOfStatement)) {
660 if (Lexer.isNot(asmtok::Comma))
661 return TokError("unexpected token in '.org' directive");
662 Lexer.Lex();
663
Daniel Dunbar475839e2009-06-29 20:37:27 +0000664 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000665 return true;
666
667 if (Lexer.isNot(asmtok::EndOfStatement))
668 return TokError("unexpected token in '.org' directive");
669 }
670
671 Lexer.Lex();
672
673 Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
674
675 return false;
676}