1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "AsmParser.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetAsmParser.h"
26 void AsmParser::Warning(SMLoc L, const char *Msg) {
27 Lexer.PrintMessage(L, Msg, "warning");
30 bool AsmParser::Error(SMLoc L, const char *Msg) {
31 Lexer.PrintMessage(L, Msg, "error");
35 bool AsmParser::TokError(const char *Msg) {
36 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
40 bool AsmParser::Run() {
44 bool HadError = false;
46 // While we have input, parse each statement.
47 while (Lexer.isNot(asmtok::Eof)) {
48 if (!ParseStatement()) continue;
50 // If we had an error, remember it and recover by skipping to the next line.
52 EatToEndOfStatement();
58 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
59 void AsmParser::EatToEndOfStatement() {
60 while (Lexer.isNot(asmtok::EndOfStatement) &&
61 Lexer.isNot(asmtok::Eof))
65 if (Lexer.is(asmtok::EndOfStatement))
70 /// ParseParenExpr - Parse a paren expression and return it.
71 /// NOTE: This assumes the leading '(' has already been consumed.
73 /// parenexpr ::= expr)
75 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
76 if (ParseExpression(Res)) return true;
77 if (Lexer.isNot(asmtok::RParen))
78 return TokError("expected ')' in parentheses expression");
83 /// ParsePrimaryExpr - Parse a primary expression and return it.
84 /// primaryexpr ::= (parenexpr
85 /// primaryexpr ::= symbol
86 /// primaryexpr ::= number
87 /// primaryexpr ::= ~,+,- primaryexpr
88 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
89 switch (Lexer.getKind()) {
91 return TokError("unknown token in expression");
93 Lexer.Lex(); // Eat the operator.
94 if (ParsePrimaryExpr(Res))
96 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
98 case asmtok::Identifier: {
99 // This is a label, this should be parsed as part of an expression, to
100 // handle things like LFOO+4.
101 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
103 // If this is use of an undefined symbol then mark it external.
104 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
105 Sym->setExternal(true);
107 Res = new AsmSymbolRefExpr(Sym);
108 Lexer.Lex(); // Eat identifier.
112 Res = new AsmConstantExpr(Lexer.getCurIntVal());
113 Lexer.Lex(); // Eat identifier.
116 Lexer.Lex(); // Eat the '('.
117 return ParseParenExpr(Res);
119 Lexer.Lex(); // Eat the operator.
120 if (ParsePrimaryExpr(Res))
122 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
125 Lexer.Lex(); // Eat the operator.
126 if (ParsePrimaryExpr(Res))
128 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
131 Lexer.Lex(); // Eat the operator.
132 if (ParsePrimaryExpr(Res))
134 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
139 /// ParseExpression - Parse an expression and return it.
141 /// expr ::= expr +,- expr -> lowest.
142 /// expr ::= expr |,^,&,! expr -> middle.
143 /// expr ::= expr *,/,%,<<,>> expr -> highest.
144 /// expr ::= primaryexpr
146 bool AsmParser::ParseExpression(AsmExpr *&Res) {
148 return ParsePrimaryExpr(Res) ||
149 ParseBinOpRHS(1, Res);
152 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
155 SMLoc StartLoc = Lexer.getLoc();
156 if (ParseExpression(Expr))
159 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
160 return Error(StartLoc, "expected absolute expression");
165 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
168 SMLoc StartLoc = Lexer.getLoc();
169 if (ParseExpression(Expr))
172 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
173 return Error(StartLoc, "expected relocatable expression");
178 bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
181 SMLoc StartLoc = Lexer.getLoc();
182 if (ParseParenExpr(Expr))
185 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
186 return Error(StartLoc, "expected relocatable expression");
191 static unsigned getBinOpPrecedence(asmtok::TokKind K,
192 AsmBinaryExpr::Opcode &Kind) {
194 default: return 0; // not a binop.
196 // Lowest Precedence: &&, ||
198 Kind = AsmBinaryExpr::LAnd;
200 case asmtok::PipePipe:
201 Kind = AsmBinaryExpr::LOr;
204 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
206 Kind = AsmBinaryExpr::Add;
209 Kind = AsmBinaryExpr::Sub;
211 case asmtok::EqualEqual:
212 Kind = AsmBinaryExpr::EQ;
214 case asmtok::ExclaimEqual:
215 case asmtok::LessGreater:
216 Kind = AsmBinaryExpr::NE;
219 Kind = AsmBinaryExpr::LT;
221 case asmtok::LessEqual:
222 Kind = AsmBinaryExpr::LTE;
224 case asmtok::Greater:
225 Kind = AsmBinaryExpr::GT;
227 case asmtok::GreaterEqual:
228 Kind = AsmBinaryExpr::GTE;
231 // Intermediate Precedence: |, &, ^
233 // FIXME: gas seems to support '!' as an infix operator?
235 Kind = AsmBinaryExpr::Or;
238 Kind = AsmBinaryExpr::Xor;
241 Kind = AsmBinaryExpr::And;
244 // Highest Precedence: *, /, %, <<, >>
246 Kind = AsmBinaryExpr::Mul;
249 Kind = AsmBinaryExpr::Div;
251 case asmtok::Percent:
252 Kind = AsmBinaryExpr::Mod;
254 case asmtok::LessLess:
255 Kind = AsmBinaryExpr::Shl;
257 case asmtok::GreaterGreater:
258 Kind = AsmBinaryExpr::Shr;
264 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
265 /// Res contains the LHS of the expression on input.
266 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
268 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
269 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
271 // If the next token is lower precedence than we are allowed to eat, return
272 // successfully with what we ate already.
273 if (TokPrec < Precedence)
278 // Eat the next primary expression.
280 if (ParsePrimaryExpr(RHS)) return true;
282 // If BinOp binds less tightly with RHS than the operator after RHS, let
283 // the pending operator take RHS as its LHS.
284 AsmBinaryExpr::Opcode Dummy;
285 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
286 if (TokPrec < NextTokPrec) {
287 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
290 // Merge LHS and RHS according to operator.
291 Res = new AsmBinaryExpr(Kind, Res, RHS);
299 /// ::= EndOfStatement
300 /// ::= Label* Directive ...Operands... EndOfStatement
301 /// ::= Label* Identifier OperandList* EndOfStatement
302 bool AsmParser::ParseStatement() {
303 switch (Lexer.getKind()) {
305 return TokError("unexpected token at start of statement");
306 case asmtok::EndOfStatement:
309 case asmtok::Identifier:
311 // TODO: Recurse on local labels etc.
314 // If we have an identifier, handle it as the key symbol.
315 SMLoc IDLoc = Lexer.getLoc();
316 const char *IDVal = Lexer.getCurStrVal();
318 // Consume the identifier, see what is after it.
319 switch (Lexer.Lex()) {
320 case asmtok::Colon: {
321 // identifier ':' -> Label.
324 // Diagnose attempt to use a variable as a label.
326 // FIXME: Diagnostics. Note the location of the definition as a label.
327 // FIXME: This doesn't diagnose assignment to a symbol which has been
328 // implicitly marked as external.
329 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
330 if (Sym->getSection())
331 return Error(IDLoc, "invalid symbol redefinition");
332 if (Ctx.GetSymbolValue(Sym))
333 return Error(IDLoc, "symbol already used as assembler variable");
335 // Since we saw a label, create a symbol and emit it.
336 // FIXME: If the label starts with L it is an assembler temporary label.
337 // Why does the client of this api need to know this?
340 return ParseStatement();
344 // identifier '=' ... -> assignment statement
347 return ParseAssignment(IDVal, false);
349 default: // Normal instruction or directive.
353 // Otherwise, we have a normal instruction or directive.
354 if (IDVal[0] == '.') {
355 // FIXME: This should be driven based on a hash lookup and callback.
356 if (!strcmp(IDVal, ".section"))
357 return ParseDirectiveDarwinSection();
358 if (!strcmp(IDVal, ".text"))
359 // FIXME: This changes behavior based on the -static flag to the
361 return ParseDirectiveSectionSwitch("__TEXT,__text",
362 "regular,pure_instructions");
363 if (!strcmp(IDVal, ".const"))
364 return ParseDirectiveSectionSwitch("__TEXT,__const");
365 if (!strcmp(IDVal, ".static_const"))
366 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
367 if (!strcmp(IDVal, ".cstring"))
368 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
370 if (!strcmp(IDVal, ".literal4"))
371 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
372 if (!strcmp(IDVal, ".literal8"))
373 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
374 if (!strcmp(IDVal, ".literal16"))
375 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
377 if (!strcmp(IDVal, ".constructor"))
378 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
379 if (!strcmp(IDVal, ".destructor"))
380 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
381 if (!strcmp(IDVal, ".fvmlib_init0"))
382 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
383 if (!strcmp(IDVal, ".fvmlib_init1"))
384 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
385 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
386 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
387 "self_modifying_code+pure_instructions,5");
388 // FIXME: .picsymbol_stub on PPC.
389 if (!strcmp(IDVal, ".data"))
390 return ParseDirectiveSectionSwitch("__DATA,__data");
391 if (!strcmp(IDVal, ".static_data"))
392 return ParseDirectiveSectionSwitch("__DATA,__static_data");
393 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
394 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
395 "non_lazy_symbol_pointers");
396 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
397 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
398 "lazy_symbol_pointers");
399 if (!strcmp(IDVal, ".dyld"))
400 return ParseDirectiveSectionSwitch("__DATA,__dyld");
401 if (!strcmp(IDVal, ".mod_init_func"))
402 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
404 if (!strcmp(IDVal, ".mod_term_func"))
405 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
407 if (!strcmp(IDVal, ".const_data"))
408 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
411 // FIXME: Verify attributes on sections.
412 if (!strcmp(IDVal, ".objc_class"))
413 return ParseDirectiveSectionSwitch("__OBJC,__class");
414 if (!strcmp(IDVal, ".objc_meta_class"))
415 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
416 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
417 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
418 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
419 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
420 if (!strcmp(IDVal, ".objc_protocol"))
421 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
422 if (!strcmp(IDVal, ".objc_string_object"))
423 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
424 if (!strcmp(IDVal, ".objc_cls_meth"))
425 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
426 if (!strcmp(IDVal, ".objc_inst_meth"))
427 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
428 if (!strcmp(IDVal, ".objc_cls_refs"))
429 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
430 if (!strcmp(IDVal, ".objc_message_refs"))
431 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
432 if (!strcmp(IDVal, ".objc_symbols"))
433 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
434 if (!strcmp(IDVal, ".objc_category"))
435 return ParseDirectiveSectionSwitch("__OBJC,__category");
436 if (!strcmp(IDVal, ".objc_class_vars"))
437 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
438 if (!strcmp(IDVal, ".objc_instance_vars"))
439 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
440 if (!strcmp(IDVal, ".objc_module_info"))
441 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
442 if (!strcmp(IDVal, ".objc_class_names"))
443 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
444 if (!strcmp(IDVal, ".objc_meth_var_types"))
445 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
446 if (!strcmp(IDVal, ".objc_meth_var_names"))
447 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
448 if (!strcmp(IDVal, ".objc_selector_strs"))
449 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
451 // Assembler features
452 if (!strcmp(IDVal, ".set"))
453 return ParseDirectiveSet();
457 if (!strcmp(IDVal, ".ascii"))
458 return ParseDirectiveAscii(false);
459 if (!strcmp(IDVal, ".asciz"))
460 return ParseDirectiveAscii(true);
462 // FIXME: Target hooks for size? Also for "word", "hword".
463 if (!strcmp(IDVal, ".byte"))
464 return ParseDirectiveValue(1);
465 if (!strcmp(IDVal, ".short"))
466 return ParseDirectiveValue(2);
467 if (!strcmp(IDVal, ".long"))
468 return ParseDirectiveValue(4);
469 if (!strcmp(IDVal, ".quad"))
470 return ParseDirectiveValue(8);
472 // FIXME: Target hooks for IsPow2.
473 if (!strcmp(IDVal, ".align"))
474 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
475 if (!strcmp(IDVal, ".align32"))
476 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
477 if (!strcmp(IDVal, ".balign"))
478 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
479 if (!strcmp(IDVal, ".balignw"))
480 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
481 if (!strcmp(IDVal, ".balignl"))
482 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
483 if (!strcmp(IDVal, ".p2align"))
484 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
485 if (!strcmp(IDVal, ".p2alignw"))
486 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
487 if (!strcmp(IDVal, ".p2alignl"))
488 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
490 if (!strcmp(IDVal, ".org"))
491 return ParseDirectiveOrg();
493 if (!strcmp(IDVal, ".fill"))
494 return ParseDirectiveFill();
495 if (!strcmp(IDVal, ".space"))
496 return ParseDirectiveSpace();
498 // Symbol attribute directives
499 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
500 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
501 if (!strcmp(IDVal, ".hidden"))
502 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
503 if (!strcmp(IDVal, ".indirect_symbol"))
504 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
505 if (!strcmp(IDVal, ".internal"))
506 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
507 if (!strcmp(IDVal, ".lazy_reference"))
508 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
509 if (!strcmp(IDVal, ".no_dead_strip"))
510 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
511 if (!strcmp(IDVal, ".private_extern"))
512 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
513 if (!strcmp(IDVal, ".protected"))
514 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
515 if (!strcmp(IDVal, ".reference"))
516 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
517 if (!strcmp(IDVal, ".weak"))
518 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
519 if (!strcmp(IDVal, ".weak_definition"))
520 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
521 if (!strcmp(IDVal, ".weak_reference"))
522 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
524 if (!strcmp(IDVal, ".comm"))
525 return ParseDirectiveComm(/*IsLocal=*/false);
526 if (!strcmp(IDVal, ".lcomm"))
527 return ParseDirectiveComm(/*IsLocal=*/true);
528 if (!strcmp(IDVal, ".zerofill"))
529 return ParseDirectiveDarwinZerofill();
530 if (!strcmp(IDVal, ".desc"))
531 return ParseDirectiveDarwinSymbolDesc();
532 if (!strcmp(IDVal, ".lsym"))
533 return ParseDirectiveDarwinLsym();
535 if (!strcmp(IDVal, ".subsections_via_symbols"))
536 return ParseDirectiveDarwinSubsectionsViaSymbols();
537 if (!strcmp(IDVal, ".abort"))
538 return ParseDirectiveAbort();
539 if (!strcmp(IDVal, ".include"))
540 return ParseDirectiveInclude();
541 if (!strcmp(IDVal, ".dump"))
542 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
543 if (!strcmp(IDVal, ".load"))
544 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
546 Warning(IDLoc, "ignoring directive for now");
547 EatToEndOfStatement();
552 if (ParseX86InstOperands(IDVal, Inst) &&
553 getTargetParser().ParseInstruction(*this, IDVal, Inst))
556 if (Lexer.isNot(asmtok::EndOfStatement))
557 return TokError("unexpected token in argument list");
559 // Eat the end of statement marker.
562 // Instruction is good, process it.
563 Out.EmitInstruction(Inst);
565 // Skip to end of line for now.
569 bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
570 // FIXME: Use better location, we should use proper tokens.
571 SMLoc EqualLoc = Lexer.getLoc();
574 if (ParseRelocatableExpression(Value))
577 if (Lexer.isNot(asmtok::EndOfStatement))
578 return TokError("unexpected token in assignment");
580 // Eat the end of statement marker.
583 // Diagnose assignment to a label.
585 // FIXME: Diagnostics. Note the location of the definition as a label.
586 // FIXME: This doesn't diagnose assignment to a symbol which has been
587 // implicitly marked as external.
588 // FIXME: Handle '.'.
589 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
590 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
591 if (Sym->getSection())
592 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
593 if (Sym->isExternal())
594 return Error(EqualLoc, "invalid assignment to external symbol");
596 // Do the assignment.
597 Out.EmitAssignment(Sym, Value, IsDotSet);
602 /// ParseDirectiveSet:
603 /// ::= .set identifier ',' expression
604 bool AsmParser::ParseDirectiveSet() {
605 if (Lexer.isNot(asmtok::Identifier))
606 return TokError("expected identifier after '.set' directive");
608 const char *Name = Lexer.getCurStrVal();
610 if (Lexer.Lex() != asmtok::Comma)
611 return TokError("unexpected token in '.set'");
614 return ParseAssignment(Name, true);
617 /// ParseDirectiveSection:
618 /// ::= .section identifier (',' identifier)*
619 /// FIXME: This should actually parse out the segment, section, attributes and
620 /// sizeof_stub fields.
621 bool AsmParser::ParseDirectiveDarwinSection() {
622 if (Lexer.isNot(asmtok::Identifier))
623 return TokError("expected identifier after '.section' directive");
625 std::string Section = Lexer.getCurStrVal();
628 // Accept a comma separated list of modifiers.
629 while (Lexer.is(asmtok::Comma)) {
632 if (Lexer.isNot(asmtok::Identifier))
633 return TokError("expected identifier in '.section' directive");
635 Section += Lexer.getCurStrVal();
639 if (Lexer.isNot(asmtok::EndOfStatement))
640 return TokError("unexpected token in '.section' directive");
643 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
647 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
648 const char *Directives) {
649 if (Lexer.isNot(asmtok::EndOfStatement))
650 return TokError("unexpected token in section switching directive");
653 std::string SectionStr = Section;
654 if (Directives && Directives[0]) {
656 SectionStr += Directives;
659 Out.SwitchSection(Ctx.GetSection(Section));
663 /// ParseDirectiveAscii:
664 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
665 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
666 if (Lexer.isNot(asmtok::EndOfStatement)) {
668 if (Lexer.isNot(asmtok::String))
669 return TokError("expected string in '.ascii' or '.asciz' directive");
671 // FIXME: This shouldn't use a const char* + strlen, the string could have
673 // FIXME: Should have accessor for getting string contents.
674 const char *Str = Lexer.getCurStrVal();
675 Out.EmitBytes(Str + 1, strlen(Str) - 2);
677 Out.EmitBytes("\0", 1);
681 if (Lexer.is(asmtok::EndOfStatement))
684 if (Lexer.isNot(asmtok::Comma))
685 return TokError("unexpected token in '.ascii' or '.asciz' directive");
694 /// ParseDirectiveValue
695 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
696 bool AsmParser::ParseDirectiveValue(unsigned Size) {
697 if (Lexer.isNot(asmtok::EndOfStatement)) {
700 if (ParseRelocatableExpression(Expr))
703 Out.EmitValue(Expr, Size);
705 if (Lexer.is(asmtok::EndOfStatement))
708 // FIXME: Improve diagnostic.
709 if (Lexer.isNot(asmtok::Comma))
710 return TokError("unexpected token in directive");
719 /// ParseDirectiveSpace
720 /// ::= .space expression [ , expression ]
721 bool AsmParser::ParseDirectiveSpace() {
723 if (ParseAbsoluteExpression(NumBytes))
726 int64_t FillExpr = 0;
727 bool HasFillExpr = false;
728 if (Lexer.isNot(asmtok::EndOfStatement)) {
729 if (Lexer.isNot(asmtok::Comma))
730 return TokError("unexpected token in '.space' directive");
733 if (ParseAbsoluteExpression(FillExpr))
738 if (Lexer.isNot(asmtok::EndOfStatement))
739 return TokError("unexpected token in '.space' directive");
745 return TokError("invalid number of bytes in '.space' directive");
747 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
748 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
749 Out.EmitValue(MCValue::get(FillExpr), 1);
754 /// ParseDirectiveFill
755 /// ::= .fill expression , expression , expression
756 bool AsmParser::ParseDirectiveFill() {
758 if (ParseAbsoluteExpression(NumValues))
761 if (Lexer.isNot(asmtok::Comma))
762 return TokError("unexpected token in '.fill' directive");
766 if (ParseAbsoluteExpression(FillSize))
769 if (Lexer.isNot(asmtok::Comma))
770 return TokError("unexpected token in '.fill' directive");
774 if (ParseAbsoluteExpression(FillExpr))
777 if (Lexer.isNot(asmtok::EndOfStatement))
778 return TokError("unexpected token in '.fill' directive");
782 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
783 return TokError("invalid '.fill' size, expected 1, 2, or 4");
785 for (uint64_t i = 0, e = NumValues; i != e; ++i)
786 Out.EmitValue(MCValue::get(FillExpr), FillSize);
791 /// ParseDirectiveOrg
792 /// ::= .org expression [ , expression ]
793 bool AsmParser::ParseDirectiveOrg() {
795 if (ParseRelocatableExpression(Offset))
798 // Parse optional fill expression.
799 int64_t FillExpr = 0;
800 if (Lexer.isNot(asmtok::EndOfStatement)) {
801 if (Lexer.isNot(asmtok::Comma))
802 return TokError("unexpected token in '.org' directive");
805 if (ParseAbsoluteExpression(FillExpr))
808 if (Lexer.isNot(asmtok::EndOfStatement))
809 return TokError("unexpected token in '.org' directive");
814 // FIXME: Only limited forms of relocatable expressions are accepted here, it
815 // has to be relative to the current section.
816 Out.EmitValueToOffset(Offset, FillExpr);
821 /// ParseDirectiveAlign
822 /// ::= {.align, ...} expression [ , expression [ , expression ]]
823 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
825 if (ParseAbsoluteExpression(Alignment))
829 bool HasFillExpr = false;
830 int64_t FillExpr = 0;
831 int64_t MaxBytesToFill = 0;
832 if (Lexer.isNot(asmtok::EndOfStatement)) {
833 if (Lexer.isNot(asmtok::Comma))
834 return TokError("unexpected token in directive");
837 // The fill expression can be omitted while specifying a maximum number of
838 // alignment bytes, e.g:
840 if (Lexer.isNot(asmtok::Comma)) {
842 if (ParseAbsoluteExpression(FillExpr))
846 if (Lexer.isNot(asmtok::EndOfStatement)) {
847 if (Lexer.isNot(asmtok::Comma))
848 return TokError("unexpected token in directive");
851 MaxBytesLoc = Lexer.getLoc();
852 if (ParseAbsoluteExpression(MaxBytesToFill))
855 if (Lexer.isNot(asmtok::EndOfStatement))
856 return TokError("unexpected token in directive");
863 // FIXME: Sometimes fill with nop.
867 // Compute alignment in bytes.
869 // FIXME: Diagnose overflow.
870 Alignment = 1LL << Alignment;
873 // Diagnose non-sensical max bytes to fill.
874 if (MaxBytesLoc.isValid()) {
875 if (MaxBytesToFill < 1) {
876 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
877 "many bytes, ignoring");
881 if (MaxBytesToFill >= Alignment) {
882 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
888 // FIXME: Target specific behavior about how the "extra" bytes are filled.
889 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
894 /// ParseDirectiveSymbolAttribute
895 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
896 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
897 if (Lexer.isNot(asmtok::EndOfStatement)) {
899 if (Lexer.isNot(asmtok::Identifier))
900 return TokError("expected identifier in directive");
902 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
905 // If this is use of an undefined symbol then mark it external.
906 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
907 Sym->setExternal(true);
909 Out.EmitSymbolAttribute(Sym, Attr);
911 if (Lexer.is(asmtok::EndOfStatement))
914 if (Lexer.isNot(asmtok::Comma))
915 return TokError("unexpected token in directive");
924 /// ParseDirectiveDarwinSymbolDesc
925 /// ::= .desc identifier , expression
926 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
927 if (Lexer.isNot(asmtok::Identifier))
928 return TokError("expected identifier in directive");
930 // handle the identifier as the key symbol.
931 SMLoc IDLoc = Lexer.getLoc();
932 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
935 if (Lexer.isNot(asmtok::Comma))
936 return TokError("unexpected token in '.desc' directive");
939 SMLoc DescLoc = Lexer.getLoc();
941 if (ParseAbsoluteExpression(DescValue))
944 if (Lexer.isNot(asmtok::EndOfStatement))
945 return TokError("unexpected token in '.desc' directive");
949 // Set the n_desc field of this Symbol to this DescValue
950 Out.EmitSymbolDesc(Sym, DescValue);
955 /// ParseDirectiveComm
956 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
957 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
958 if (Lexer.isNot(asmtok::Identifier))
959 return TokError("expected identifier in directive");
961 // handle the identifier as the key symbol.
962 SMLoc IDLoc = Lexer.getLoc();
963 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
966 if (Lexer.isNot(asmtok::Comma))
967 return TokError("unexpected token in directive");
971 SMLoc SizeLoc = Lexer.getLoc();
972 if (ParseAbsoluteExpression(Size))
975 int64_t Pow2Alignment = 0;
976 SMLoc Pow2AlignmentLoc;
977 if (Lexer.is(asmtok::Comma)) {
979 Pow2AlignmentLoc = Lexer.getLoc();
980 if (ParseAbsoluteExpression(Pow2Alignment))
984 if (Lexer.isNot(asmtok::EndOfStatement))
985 return TokError("unexpected token in '.comm' or '.lcomm' directive");
989 // NOTE: a size of zero for a .comm should create a undefined symbol
990 // but a size of .lcomm creates a bss symbol of size zero.
992 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
993 "be less than zero");
995 // NOTE: The alignment in the directive is a power of 2 value, the assember
996 // may internally end up wanting an alignment in bytes.
997 // FIXME: Diagnose overflow.
998 if (Pow2Alignment < 0)
999 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1000 "alignment, can't be less than zero");
1002 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1003 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1004 return Error(IDLoc, "invalid symbol redefinition");
1006 // Create the Symbol as a common or local common with Size and Pow2Alignment
1007 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
1012 /// ParseDirectiveDarwinZerofill
1013 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1014 /// , align_expression ]]
1015 bool AsmParser::ParseDirectiveDarwinZerofill() {
1016 if (Lexer.isNot(asmtok::Identifier))
1017 return TokError("expected segment name after '.zerofill' directive");
1018 std::string Section = Lexer.getCurStrVal();
1021 if (Lexer.isNot(asmtok::Comma))
1022 return TokError("unexpected token in directive");
1026 if (Lexer.isNot(asmtok::Identifier))
1027 return TokError("expected section name after comma in '.zerofill' "
1029 Section += Lexer.getCurStrVal();
1032 // FIXME: we will need to tell GetSection() that this is to be created with or
1033 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1034 // below could be done but for now it is not as EmitZerofill() does not know
1035 // how to deal with a section type in the section name like
1036 // ParseDirectiveDarwinSection() allows.
1038 // Section += "zerofill";
1040 // If this is the end of the line all that was wanted was to create the
1041 // the section but with no symbol.
1042 if (Lexer.is(asmtok::EndOfStatement)) {
1043 // Create the zerofill section but no symbol
1044 Out.EmitZerofill(Ctx.GetSection(Section.c_str()));
1048 if (Lexer.isNot(asmtok::Comma))
1049 return TokError("unexpected token in directive");
1052 if (Lexer.isNot(asmtok::Identifier))
1053 return TokError("expected identifier in directive");
1055 // handle the identifier as the key symbol.
1056 SMLoc IDLoc = Lexer.getLoc();
1057 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
1060 if (Lexer.isNot(asmtok::Comma))
1061 return TokError("unexpected token in directive");
1065 SMLoc SizeLoc = Lexer.getLoc();
1066 if (ParseAbsoluteExpression(Size))
1069 int64_t Pow2Alignment = 0;
1070 SMLoc Pow2AlignmentLoc;
1071 if (Lexer.is(asmtok::Comma)) {
1073 Pow2AlignmentLoc = Lexer.getLoc();
1074 if (ParseAbsoluteExpression(Pow2Alignment))
1078 if (Lexer.isNot(asmtok::EndOfStatement))
1079 return TokError("unexpected token in '.zerofill' directive");
1084 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1087 // NOTE: The alignment in the directive is a power of 2 value, the assember
1088 // may internally end up wanting an alignment in bytes.
1089 // FIXME: Diagnose overflow.
1090 if (Pow2Alignment < 0)
1091 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1092 "can't be less than zero");
1094 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1095 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1096 return Error(IDLoc, "invalid symbol redefinition");
1098 // Create the zerofill Symbol with Size and Pow2Alignment
1099 Out.EmitZerofill(Ctx.GetSection(Section.c_str()), Sym, Size, Pow2Alignment);
1104 /// ParseDirectiveDarwinSubsectionsViaSymbols
1105 /// ::= .subsections_via_symbols
1106 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1107 if (Lexer.isNot(asmtok::EndOfStatement))
1108 return TokError("unexpected token in '.subsections_via_symbols' directive");
1112 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1117 /// ParseDirectiveAbort
1118 /// ::= .abort [ "abort_string" ]
1119 bool AsmParser::ParseDirectiveAbort() {
1120 const char *Str = NULL;
1121 if (Lexer.isNot(asmtok::EndOfStatement)) {
1122 if (Lexer.isNot(asmtok::String))
1123 return TokError("expected string in '.abort' directive");
1125 Str = Lexer.getCurStrVal();
1130 if (Lexer.isNot(asmtok::EndOfStatement))
1131 return TokError("unexpected token in '.abort' directive");
1135 Out.AbortAssembly(Str);
1140 /// ParseDirectiveLsym
1141 /// ::= .lsym identifier , expression
1142 bool AsmParser::ParseDirectiveDarwinLsym() {
1143 if (Lexer.isNot(asmtok::Identifier))
1144 return TokError("expected identifier in directive");
1146 // handle the identifier as the key symbol.
1147 SMLoc IDLoc = Lexer.getLoc();
1148 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
1151 if (Lexer.isNot(asmtok::Comma))
1152 return TokError("unexpected token in '.lsym' directive");
1156 if (ParseRelocatableExpression(Expr))
1159 if (Lexer.isNot(asmtok::EndOfStatement))
1160 return TokError("unexpected token in '.lsym' directive");
1164 // Create the Sym with the value of the Expr
1165 Out.EmitLocalSymbol(Sym, Expr);
1170 /// ParseDirectiveInclude
1171 /// ::= .include "filename"
1172 bool AsmParser::ParseDirectiveInclude() {
1173 if (Lexer.isNot(asmtok::String))
1174 return TokError("expected string in '.include' directive");
1176 std::string Filename = Lexer.getCurStrVal();
1177 SMLoc IncludeLoc = Lexer.getLoc();
1180 if (Lexer.isNot(asmtok::EndOfStatement))
1181 return TokError("unexpected token in '.include' directive");
1183 // Strip the quotes.
1184 Filename = Filename.substr(1, Filename.size()-2);
1186 // Attempt to switch the lexer to the included file before consuming the end
1187 // of statement to avoid losing it when we switch.
1188 if (Lexer.EnterIncludeFile(Filename)) {
1189 Lexer.PrintMessage(IncludeLoc,
1190 "Could not find include file '" + Filename + "'",
1198 /// ParseDirectiveDarwinDumpOrLoad
1199 /// ::= ( .dump | .load ) "filename"
1200 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1201 if (Lexer.isNot(asmtok::String))
1202 return TokError("expected string in '.dump' or '.load' directive");
1204 Lexer.getCurStrVal();
1208 if (Lexer.isNot(asmtok::EndOfStatement))
1209 return TokError("unexpected token in '.dump' or '.load' directive");
1213 // FIXME: If/when .dump and .load are implemented they will be done in the
1214 // the assembly parser and not have any need for an MCStreamer API.
1216 Warning(IDLoc, "ignoring directive .dump for now");
1218 Warning(IDLoc, "ignoring directive .load for now");