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/ADT/Twine.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetAsmParser.h"
27 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
28 Lexer.PrintMessage(L, Msg.str(), "warning");
31 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
32 Lexer.PrintMessage(L, Msg.str(), "error");
36 bool AsmParser::TokError(const char *Msg) {
37 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
41 bool AsmParser::Run() {
45 bool HadError = false;
47 // While we have input, parse each statement.
48 while (Lexer.isNot(AsmToken::Eof)) {
49 if (!ParseStatement()) continue;
51 // If we had an error, remember it and recover by skipping to the next line.
53 EatToEndOfStatement();
59 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
60 void AsmParser::EatToEndOfStatement() {
61 while (Lexer.isNot(AsmToken::EndOfStatement) &&
62 Lexer.isNot(AsmToken::Eof))
66 if (Lexer.is(AsmToken::EndOfStatement))
71 /// ParseParenExpr - Parse a paren expression and return it.
72 /// NOTE: This assumes the leading '(' has already been consumed.
74 /// parenexpr ::= expr)
76 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
77 if (ParseExpression(Res)) return true;
78 if (Lexer.isNot(AsmToken::RParen))
79 return TokError("expected ')' in parentheses expression");
84 /// ParsePrimaryExpr - Parse a primary expression and return it.
85 /// primaryexpr ::= (parenexpr
86 /// primaryexpr ::= symbol
87 /// primaryexpr ::= number
88 /// primaryexpr ::= ~,+,- primaryexpr
89 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
90 switch (Lexer.getKind()) {
92 return TokError("unknown token in expression");
93 case AsmToken::Exclaim:
94 Lexer.Lex(); // Eat the operator.
95 if (ParsePrimaryExpr(Res))
97 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
99 case AsmToken::Identifier: {
100 // This is a label, this should be parsed as part of an expression, to
101 // handle things like LFOO+4.
102 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
104 // If this is use of an undefined symbol then mark it external.
105 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
106 Sym->setExternal(true);
108 Res = new AsmSymbolRefExpr(Sym);
109 Lexer.Lex(); // Eat identifier.
112 case AsmToken::Integer:
113 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
114 Lexer.Lex(); // Eat identifier.
116 case AsmToken::LParen:
117 Lexer.Lex(); // Eat the '('.
118 return ParseParenExpr(Res);
119 case AsmToken::Minus:
120 Lexer.Lex(); // Eat the operator.
121 if (ParsePrimaryExpr(Res))
123 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
126 Lexer.Lex(); // Eat the operator.
127 if (ParsePrimaryExpr(Res))
129 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
131 case AsmToken::Tilde:
132 Lexer.Lex(); // Eat the operator.
133 if (ParsePrimaryExpr(Res))
135 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
140 /// ParseExpression - Parse an expression and return it.
142 /// expr ::= expr +,- expr -> lowest.
143 /// expr ::= expr |,^,&,! expr -> middle.
144 /// expr ::= expr *,/,%,<<,>> expr -> highest.
145 /// expr ::= primaryexpr
147 bool AsmParser::ParseExpression(AsmExpr *&Res) {
149 return ParsePrimaryExpr(Res) ||
150 ParseBinOpRHS(1, Res);
153 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
156 SMLoc StartLoc = Lexer.getLoc();
157 if (ParseExpression(Expr))
160 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
161 return Error(StartLoc, "expected absolute expression");
166 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
169 SMLoc StartLoc = Lexer.getLoc();
170 if (ParseExpression(Expr))
173 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
174 return Error(StartLoc, "expected relocatable expression");
179 bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
182 SMLoc StartLoc = Lexer.getLoc();
183 if (ParseParenExpr(Expr))
186 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
187 return Error(StartLoc, "expected relocatable expression");
192 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
193 AsmBinaryExpr::Opcode &Kind) {
195 default: return 0; // not a binop.
197 // Lowest Precedence: &&, ||
198 case AsmToken::AmpAmp:
199 Kind = AsmBinaryExpr::LAnd;
201 case AsmToken::PipePipe:
202 Kind = AsmBinaryExpr::LOr;
205 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
207 Kind = AsmBinaryExpr::Add;
209 case AsmToken::Minus:
210 Kind = AsmBinaryExpr::Sub;
212 case AsmToken::EqualEqual:
213 Kind = AsmBinaryExpr::EQ;
215 case AsmToken::ExclaimEqual:
216 case AsmToken::LessGreater:
217 Kind = AsmBinaryExpr::NE;
220 Kind = AsmBinaryExpr::LT;
222 case AsmToken::LessEqual:
223 Kind = AsmBinaryExpr::LTE;
225 case AsmToken::Greater:
226 Kind = AsmBinaryExpr::GT;
228 case AsmToken::GreaterEqual:
229 Kind = AsmBinaryExpr::GTE;
232 // Intermediate Precedence: |, &, ^
234 // FIXME: gas seems to support '!' as an infix operator?
236 Kind = AsmBinaryExpr::Or;
238 case AsmToken::Caret:
239 Kind = AsmBinaryExpr::Xor;
242 Kind = AsmBinaryExpr::And;
245 // Highest Precedence: *, /, %, <<, >>
247 Kind = AsmBinaryExpr::Mul;
249 case AsmToken::Slash:
250 Kind = AsmBinaryExpr::Div;
252 case AsmToken::Percent:
253 Kind = AsmBinaryExpr::Mod;
255 case AsmToken::LessLess:
256 Kind = AsmBinaryExpr::Shl;
258 case AsmToken::GreaterGreater:
259 Kind = AsmBinaryExpr::Shr;
265 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
266 /// Res contains the LHS of the expression on input.
267 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
269 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
270 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
272 // If the next token is lower precedence than we are allowed to eat, return
273 // successfully with what we ate already.
274 if (TokPrec < Precedence)
279 // Eat the next primary expression.
281 if (ParsePrimaryExpr(RHS)) return true;
283 // If BinOp binds less tightly with RHS than the operator after RHS, let
284 // the pending operator take RHS as its LHS.
285 AsmBinaryExpr::Opcode Dummy;
286 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
287 if (TokPrec < NextTokPrec) {
288 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
291 // Merge LHS and RHS according to operator.
292 Res = new AsmBinaryExpr(Kind, Res, RHS);
300 /// ::= EndOfStatement
301 /// ::= Label* Directive ...Operands... EndOfStatement
302 /// ::= Label* Identifier OperandList* EndOfStatement
303 bool AsmParser::ParseStatement() {
304 switch (Lexer.getKind()) {
306 return TokError("unexpected token at start of statement");
307 case AsmToken::EndOfStatement:
310 case AsmToken::Identifier:
312 // TODO: Recurse on local labels etc.
315 // If we have an identifier, handle it as the key symbol.
316 AsmToken ID = Lexer.getTok();
317 SMLoc IDLoc = ID.getLoc();
318 StringRef IDVal = ID.getString();
320 // Consume the identifier, see what is after it.
321 switch (Lexer.Lex().getKind()) {
322 case AsmToken::Colon: {
323 // identifier ':' -> Label.
326 // Diagnose attempt to use a variable as a label.
328 // FIXME: Diagnostics. Note the location of the definition as a label.
329 // FIXME: This doesn't diagnose assignment to a symbol which has been
330 // implicitly marked as external.
331 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
332 if (Sym->getSection())
333 return Error(IDLoc, "invalid symbol redefinition");
334 if (Ctx.GetSymbolValue(Sym))
335 return Error(IDLoc, "symbol already used as assembler variable");
337 // Since we saw a label, create a symbol and emit it.
338 // FIXME: If the label starts with L it is an assembler temporary label.
339 // Why does the client of this api need to know this?
342 return ParseStatement();
345 case AsmToken::Equal:
346 // identifier '=' ... -> assignment statement
349 return ParseAssignment(IDVal, false);
351 default: // Normal instruction or directive.
355 // Otherwise, we have a normal instruction or directive.
356 if (IDVal[0] == '.') {
357 // FIXME: This should be driven based on a hash lookup and callback.
358 if (IDVal == ".section")
359 return ParseDirectiveDarwinSection();
360 if (IDVal == ".text")
361 // FIXME: This changes behavior based on the -static flag to the
363 return ParseDirectiveSectionSwitch("__TEXT,__text",
364 "regular,pure_instructions");
365 if (IDVal == ".const")
366 return ParseDirectiveSectionSwitch("__TEXT,__const");
367 if (IDVal == ".static_const")
368 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
369 if (IDVal == ".cstring")
370 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
372 if (IDVal == ".literal4")
373 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
374 if (IDVal == ".literal8")
375 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
376 if (IDVal == ".literal16")
377 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
379 if (IDVal == ".constructor")
380 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
381 if (IDVal == ".destructor")
382 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
383 if (IDVal == ".fvmlib_init0")
384 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
385 if (IDVal == ".fvmlib_init1")
386 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
387 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
388 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
389 "self_modifying_code+pure_instructions,5");
390 // FIXME: .picsymbol_stub on PPC.
391 if (IDVal == ".data")
392 return ParseDirectiveSectionSwitch("__DATA,__data");
393 if (IDVal == ".static_data")
394 return ParseDirectiveSectionSwitch("__DATA,__static_data");
395 if (IDVal == ".non_lazy_symbol_pointer")
396 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
397 "non_lazy_symbol_pointers");
398 if (IDVal == ".lazy_symbol_pointer")
399 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
400 "lazy_symbol_pointers");
401 if (IDVal == ".dyld")
402 return ParseDirectiveSectionSwitch("__DATA,__dyld");
403 if (IDVal == ".mod_init_func")
404 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
406 if (IDVal == ".mod_term_func")
407 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
409 if (IDVal == ".const_data")
410 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
413 // FIXME: Verify attributes on sections.
414 if (IDVal == ".objc_class")
415 return ParseDirectiveSectionSwitch("__OBJC,__class");
416 if (IDVal == ".objc_meta_class")
417 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
418 if (IDVal == ".objc_cat_cls_meth")
419 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
420 if (IDVal == ".objc_cat_inst_meth")
421 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
422 if (IDVal == ".objc_protocol")
423 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
424 if (IDVal == ".objc_string_object")
425 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
426 if (IDVal == ".objc_cls_meth")
427 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
428 if (IDVal == ".objc_inst_meth")
429 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
430 if (IDVal == ".objc_cls_refs")
431 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
432 if (IDVal == ".objc_message_refs")
433 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
434 if (IDVal == ".objc_symbols")
435 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
436 if (IDVal == ".objc_category")
437 return ParseDirectiveSectionSwitch("__OBJC,__category");
438 if (IDVal == ".objc_class_vars")
439 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
440 if (IDVal == ".objc_instance_vars")
441 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
442 if (IDVal == ".objc_module_info")
443 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
444 if (IDVal == ".objc_class_names")
445 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
446 if (IDVal == ".objc_meth_var_types")
447 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
448 if (IDVal == ".objc_meth_var_names")
449 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
450 if (IDVal == ".objc_selector_strs")
451 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
453 // Assembler features
455 return ParseDirectiveSet();
459 if (IDVal == ".ascii")
460 return ParseDirectiveAscii(false);
461 if (IDVal == ".asciz")
462 return ParseDirectiveAscii(true);
464 // FIXME: Target hooks for size? Also for "word", "hword".
465 if (IDVal == ".byte")
466 return ParseDirectiveValue(1);
467 if (IDVal == ".short")
468 return ParseDirectiveValue(2);
469 if (IDVal == ".long")
470 return ParseDirectiveValue(4);
471 if (IDVal == ".quad")
472 return ParseDirectiveValue(8);
474 // FIXME: Target hooks for IsPow2.
475 if (IDVal == ".align")
476 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
477 if (IDVal == ".align32")
478 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
479 if (IDVal == ".balign")
480 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
481 if (IDVal == ".balignw")
482 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
483 if (IDVal == ".balignl")
484 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
485 if (IDVal == ".p2align")
486 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
487 if (IDVal == ".p2alignw")
488 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
489 if (IDVal == ".p2alignl")
490 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
493 return ParseDirectiveOrg();
495 if (IDVal == ".fill")
496 return ParseDirectiveFill();
497 if (IDVal == ".space")
498 return ParseDirectiveSpace();
500 // Symbol attribute directives
501 if (IDVal == ".globl" || IDVal == ".global")
502 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
503 if (IDVal == ".hidden")
504 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
505 if (IDVal == ".indirect_symbol")
506 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
507 if (IDVal == ".internal")
508 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
509 if (IDVal == ".lazy_reference")
510 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
511 if (IDVal == ".no_dead_strip")
512 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
513 if (IDVal == ".private_extern")
514 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
515 if (IDVal == ".protected")
516 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
517 if (IDVal == ".reference")
518 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
519 if (IDVal == ".weak")
520 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
521 if (IDVal == ".weak_definition")
522 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
523 if (IDVal == ".weak_reference")
524 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
526 if (IDVal == ".comm")
527 return ParseDirectiveComm(/*IsLocal=*/false);
528 if (IDVal == ".lcomm")
529 return ParseDirectiveComm(/*IsLocal=*/true);
530 if (IDVal == ".zerofill")
531 return ParseDirectiveDarwinZerofill();
532 if (IDVal == ".desc")
533 return ParseDirectiveDarwinSymbolDesc();
534 if (IDVal == ".lsym")
535 return ParseDirectiveDarwinLsym();
537 if (IDVal == ".subsections_via_symbols")
538 return ParseDirectiveDarwinSubsectionsViaSymbols();
539 if (IDVal == ".abort")
540 return ParseDirectiveAbort();
541 if (IDVal == ".include")
542 return ParseDirectiveInclude();
543 if (IDVal == ".dump")
544 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
545 if (IDVal == ".load")
546 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
548 Warning(IDLoc, "ignoring directive for now");
549 EatToEndOfStatement();
554 if (ParseX86InstOperands(IDVal, Inst) &&
555 getTargetParser().ParseInstruction(*this, IDVal, Inst))
558 if (Lexer.isNot(AsmToken::EndOfStatement))
559 return TokError("unexpected token in argument list");
561 // Eat the end of statement marker.
564 // Instruction is good, process it.
565 Out.EmitInstruction(Inst);
567 // Skip to end of line for now.
571 bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
572 // FIXME: Use better location, we should use proper tokens.
573 SMLoc EqualLoc = Lexer.getLoc();
576 if (ParseRelocatableExpression(Value))
579 if (Lexer.isNot(AsmToken::EndOfStatement))
580 return TokError("unexpected token in assignment");
582 // Eat the end of statement marker.
585 // Diagnose assignment to a label.
587 // FIXME: Diagnostics. Note the location of the definition as a label.
588 // FIXME: This doesn't diagnose assignment to a symbol which has been
589 // implicitly marked as external.
590 // FIXME: Handle '.'.
591 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
592 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
593 if (Sym->getSection())
594 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
595 if (Sym->isExternal())
596 return Error(EqualLoc, "invalid assignment to external symbol");
598 // Do the assignment.
599 Out.EmitAssignment(Sym, Value, IsDotSet);
604 /// ParseDirectiveSet:
605 /// ::= .set identifier ',' expression
606 bool AsmParser::ParseDirectiveSet() {
607 if (Lexer.isNot(AsmToken::Identifier))
608 return TokError("expected identifier after '.set' directive");
610 StringRef Name = Lexer.getTok().getString();
612 if (Lexer.Lex().isNot(AsmToken::Comma))
613 return TokError("unexpected token in '.set'");
616 return ParseAssignment(Name, true);
619 /// ParseDirectiveSection:
620 /// ::= .section identifier (',' identifier)*
621 /// FIXME: This should actually parse out the segment, section, attributes and
622 /// sizeof_stub fields.
623 bool AsmParser::ParseDirectiveDarwinSection() {
624 if (Lexer.isNot(AsmToken::Identifier))
625 return TokError("expected identifier after '.section' directive");
627 std::string Section = Lexer.getTok().getString();
630 // Accept a comma separated list of modifiers.
631 while (Lexer.is(AsmToken::Comma)) {
634 if (Lexer.isNot(AsmToken::Identifier))
635 return TokError("expected identifier in '.section' directive");
637 Section += Lexer.getTok().getString().str();
641 if (Lexer.isNot(AsmToken::EndOfStatement))
642 return TokError("unexpected token in '.section' directive");
645 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
649 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
650 const char *Directives) {
651 if (Lexer.isNot(AsmToken::EndOfStatement))
652 return TokError("unexpected token in section switching directive");
655 std::string SectionStr = Section;
656 if (Directives && Directives[0]) {
658 SectionStr += Directives;
661 Out.SwitchSection(Ctx.GetSection(Section));
665 /// ParseDirectiveAscii:
666 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
667 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
668 if (Lexer.isNot(AsmToken::EndOfStatement)) {
670 if (Lexer.isNot(AsmToken::String))
671 return TokError("expected string in '.ascii' or '.asciz' directive");
673 // FIXME: This shouldn't use a const char* + strlen, the string could have
675 // FIXME: Should have accessor for getting string contents.
676 StringRef Str = Lexer.getTok().getString();
677 Out.EmitBytes(Str.substr(1, Str.size() - 2));
679 Out.EmitBytes(StringRef("\0", 1));
683 if (Lexer.is(AsmToken::EndOfStatement))
686 if (Lexer.isNot(AsmToken::Comma))
687 return TokError("unexpected token in '.ascii' or '.asciz' directive");
696 /// ParseDirectiveValue
697 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
698 bool AsmParser::ParseDirectiveValue(unsigned Size) {
699 if (Lexer.isNot(AsmToken::EndOfStatement)) {
702 if (ParseRelocatableExpression(Expr))
705 Out.EmitValue(Expr, Size);
707 if (Lexer.is(AsmToken::EndOfStatement))
710 // FIXME: Improve diagnostic.
711 if (Lexer.isNot(AsmToken::Comma))
712 return TokError("unexpected token in directive");
721 /// ParseDirectiveSpace
722 /// ::= .space expression [ , expression ]
723 bool AsmParser::ParseDirectiveSpace() {
725 if (ParseAbsoluteExpression(NumBytes))
728 int64_t FillExpr = 0;
729 bool HasFillExpr = false;
730 if (Lexer.isNot(AsmToken::EndOfStatement)) {
731 if (Lexer.isNot(AsmToken::Comma))
732 return TokError("unexpected token in '.space' directive");
735 if (ParseAbsoluteExpression(FillExpr))
740 if (Lexer.isNot(AsmToken::EndOfStatement))
741 return TokError("unexpected token in '.space' directive");
747 return TokError("invalid number of bytes in '.space' directive");
749 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
750 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
751 Out.EmitValue(MCValue::get(FillExpr), 1);
756 /// ParseDirectiveFill
757 /// ::= .fill expression , expression , expression
758 bool AsmParser::ParseDirectiveFill() {
760 if (ParseAbsoluteExpression(NumValues))
763 if (Lexer.isNot(AsmToken::Comma))
764 return TokError("unexpected token in '.fill' directive");
768 if (ParseAbsoluteExpression(FillSize))
771 if (Lexer.isNot(AsmToken::Comma))
772 return TokError("unexpected token in '.fill' directive");
776 if (ParseAbsoluteExpression(FillExpr))
779 if (Lexer.isNot(AsmToken::EndOfStatement))
780 return TokError("unexpected token in '.fill' directive");
784 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
785 return TokError("invalid '.fill' size, expected 1, 2, or 4");
787 for (uint64_t i = 0, e = NumValues; i != e; ++i)
788 Out.EmitValue(MCValue::get(FillExpr), FillSize);
793 /// ParseDirectiveOrg
794 /// ::= .org expression [ , expression ]
795 bool AsmParser::ParseDirectiveOrg() {
797 if (ParseRelocatableExpression(Offset))
800 // Parse optional fill expression.
801 int64_t FillExpr = 0;
802 if (Lexer.isNot(AsmToken::EndOfStatement)) {
803 if (Lexer.isNot(AsmToken::Comma))
804 return TokError("unexpected token in '.org' directive");
807 if (ParseAbsoluteExpression(FillExpr))
810 if (Lexer.isNot(AsmToken::EndOfStatement))
811 return TokError("unexpected token in '.org' directive");
816 // FIXME: Only limited forms of relocatable expressions are accepted here, it
817 // has to be relative to the current section.
818 Out.EmitValueToOffset(Offset, FillExpr);
823 /// ParseDirectiveAlign
824 /// ::= {.align, ...} expression [ , expression [ , expression ]]
825 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
827 if (ParseAbsoluteExpression(Alignment))
831 bool HasFillExpr = false;
832 int64_t FillExpr = 0;
833 int64_t MaxBytesToFill = 0;
834 if (Lexer.isNot(AsmToken::EndOfStatement)) {
835 if (Lexer.isNot(AsmToken::Comma))
836 return TokError("unexpected token in directive");
839 // The fill expression can be omitted while specifying a maximum number of
840 // alignment bytes, e.g:
842 if (Lexer.isNot(AsmToken::Comma)) {
844 if (ParseAbsoluteExpression(FillExpr))
848 if (Lexer.isNot(AsmToken::EndOfStatement)) {
849 if (Lexer.isNot(AsmToken::Comma))
850 return TokError("unexpected token in directive");
853 MaxBytesLoc = Lexer.getLoc();
854 if (ParseAbsoluteExpression(MaxBytesToFill))
857 if (Lexer.isNot(AsmToken::EndOfStatement))
858 return TokError("unexpected token in directive");
865 // FIXME: Sometimes fill with nop.
869 // Compute alignment in bytes.
871 // FIXME: Diagnose overflow.
872 Alignment = 1LL << Alignment;
875 // Diagnose non-sensical max bytes to fill.
876 if (MaxBytesLoc.isValid()) {
877 if (MaxBytesToFill < 1) {
878 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
879 "many bytes, ignoring");
883 if (MaxBytesToFill >= Alignment) {
884 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
890 // FIXME: Target specific behavior about how the "extra" bytes are filled.
891 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
896 /// ParseDirectiveSymbolAttribute
897 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
898 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
899 if (Lexer.isNot(AsmToken::EndOfStatement)) {
901 if (Lexer.isNot(AsmToken::Identifier))
902 return TokError("expected identifier in directive");
904 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
907 // If this is use of an undefined symbol then mark it external.
908 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
909 Sym->setExternal(true);
911 Out.EmitSymbolAttribute(Sym, Attr);
913 if (Lexer.is(AsmToken::EndOfStatement))
916 if (Lexer.isNot(AsmToken::Comma))
917 return TokError("unexpected token in directive");
926 /// ParseDirectiveDarwinSymbolDesc
927 /// ::= .desc identifier , expression
928 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
929 if (Lexer.isNot(AsmToken::Identifier))
930 return TokError("expected identifier in directive");
932 // handle the identifier as the key symbol.
933 SMLoc IDLoc = Lexer.getLoc();
934 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
937 if (Lexer.isNot(AsmToken::Comma))
938 return TokError("unexpected token in '.desc' directive");
941 SMLoc DescLoc = Lexer.getLoc();
943 if (ParseAbsoluteExpression(DescValue))
946 if (Lexer.isNot(AsmToken::EndOfStatement))
947 return TokError("unexpected token in '.desc' directive");
951 // Set the n_desc field of this Symbol to this DescValue
952 Out.EmitSymbolDesc(Sym, DescValue);
957 /// ParseDirectiveComm
958 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
959 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
960 if (Lexer.isNot(AsmToken::Identifier))
961 return TokError("expected identifier in directive");
963 // handle the identifier as the key symbol.
964 SMLoc IDLoc = Lexer.getLoc();
965 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
968 if (Lexer.isNot(AsmToken::Comma))
969 return TokError("unexpected token in directive");
973 SMLoc SizeLoc = Lexer.getLoc();
974 if (ParseAbsoluteExpression(Size))
977 int64_t Pow2Alignment = 0;
978 SMLoc Pow2AlignmentLoc;
979 if (Lexer.is(AsmToken::Comma)) {
981 Pow2AlignmentLoc = Lexer.getLoc();
982 if (ParseAbsoluteExpression(Pow2Alignment))
986 if (Lexer.isNot(AsmToken::EndOfStatement))
987 return TokError("unexpected token in '.comm' or '.lcomm' directive");
991 // NOTE: a size of zero for a .comm should create a undefined symbol
992 // but a size of .lcomm creates a bss symbol of size zero.
994 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
995 "be less than zero");
997 // NOTE: The alignment in the directive is a power of 2 value, the assember
998 // may internally end up wanting an alignment in bytes.
999 // FIXME: Diagnose overflow.
1000 if (Pow2Alignment < 0)
1001 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1002 "alignment, can't be less than zero");
1004 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1005 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1006 return Error(IDLoc, "invalid symbol redefinition");
1008 // Create the Symbol as a common or local common with Size and Pow2Alignment
1009 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
1014 /// ParseDirectiveDarwinZerofill
1015 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1016 /// , align_expression ]]
1017 bool AsmParser::ParseDirectiveDarwinZerofill() {
1018 if (Lexer.isNot(AsmToken::Identifier))
1019 return TokError("expected segment name after '.zerofill' directive");
1020 std::string Section = Lexer.getTok().getString();
1023 if (Lexer.isNot(AsmToken::Comma))
1024 return TokError("unexpected token in directive");
1028 if (Lexer.isNot(AsmToken::Identifier))
1029 return TokError("expected section name after comma in '.zerofill' "
1031 Section += Lexer.getTok().getString().str();
1034 // FIXME: we will need to tell GetSection() that this is to be created with or
1035 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1036 // below could be done but for now it is not as EmitZerofill() does not know
1037 // how to deal with a section type in the section name like
1038 // ParseDirectiveDarwinSection() allows.
1040 // Section += "zerofill";
1042 // If this is the end of the line all that was wanted was to create the
1043 // the section but with no symbol.
1044 if (Lexer.is(AsmToken::EndOfStatement)) {
1045 // Create the zerofill section but no symbol
1046 Out.EmitZerofill(Ctx.GetSection(Section.c_str()));
1050 if (Lexer.isNot(AsmToken::Comma))
1051 return TokError("unexpected token in directive");
1054 if (Lexer.isNot(AsmToken::Identifier))
1055 return TokError("expected identifier in directive");
1057 // handle the identifier as the key symbol.
1058 SMLoc IDLoc = Lexer.getLoc();
1059 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
1062 if (Lexer.isNot(AsmToken::Comma))
1063 return TokError("unexpected token in directive");
1067 SMLoc SizeLoc = Lexer.getLoc();
1068 if (ParseAbsoluteExpression(Size))
1071 int64_t Pow2Alignment = 0;
1072 SMLoc Pow2AlignmentLoc;
1073 if (Lexer.is(AsmToken::Comma)) {
1075 Pow2AlignmentLoc = Lexer.getLoc();
1076 if (ParseAbsoluteExpression(Pow2Alignment))
1080 if (Lexer.isNot(AsmToken::EndOfStatement))
1081 return TokError("unexpected token in '.zerofill' directive");
1086 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1089 // NOTE: The alignment in the directive is a power of 2 value, the assember
1090 // may internally end up wanting an alignment in bytes.
1091 // FIXME: Diagnose overflow.
1092 if (Pow2Alignment < 0)
1093 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1094 "can't be less than zero");
1096 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1097 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1098 return Error(IDLoc, "invalid symbol redefinition");
1100 // Create the zerofill Symbol with Size and Pow2Alignment
1101 Out.EmitZerofill(Ctx.GetSection(Section.c_str()), Sym, Size, Pow2Alignment);
1106 /// ParseDirectiveDarwinSubsectionsViaSymbols
1107 /// ::= .subsections_via_symbols
1108 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1109 if (Lexer.isNot(AsmToken::EndOfStatement))
1110 return TokError("unexpected token in '.subsections_via_symbols' directive");
1114 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1119 /// ParseDirectiveAbort
1120 /// ::= .abort [ "abort_string" ]
1121 bool AsmParser::ParseDirectiveAbort() {
1122 // FIXME: Use loc from directive.
1123 SMLoc Loc = Lexer.getLoc();
1126 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1127 if (Lexer.isNot(AsmToken::String))
1128 return TokError("expected string in '.abort' directive");
1130 Str = Lexer.getTok().getString();
1135 if (Lexer.isNot(AsmToken::EndOfStatement))
1136 return TokError("unexpected token in '.abort' directive");
1140 // FIXME: Handle here.
1142 Error(Loc, ".abort detected. Assembly stopping.");
1144 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1149 /// ParseDirectiveLsym
1150 /// ::= .lsym identifier , expression
1151 bool AsmParser::ParseDirectiveDarwinLsym() {
1152 if (Lexer.isNot(AsmToken::Identifier))
1153 return TokError("expected identifier in directive");
1155 // handle the identifier as the key symbol.
1156 SMLoc IDLoc = Lexer.getLoc();
1157 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
1160 if (Lexer.isNot(AsmToken::Comma))
1161 return TokError("unexpected token in '.lsym' directive");
1165 if (ParseRelocatableExpression(Expr))
1168 if (Lexer.isNot(AsmToken::EndOfStatement))
1169 return TokError("unexpected token in '.lsym' directive");
1173 // Create the Sym with the value of the Expr
1174 Out.EmitLocalSymbol(Sym, Expr);
1179 /// ParseDirectiveInclude
1180 /// ::= .include "filename"
1181 bool AsmParser::ParseDirectiveInclude() {
1182 if (Lexer.isNot(AsmToken::String))
1183 return TokError("expected string in '.include' directive");
1185 std::string Filename = Lexer.getTok().getString();
1186 SMLoc IncludeLoc = Lexer.getLoc();
1189 if (Lexer.isNot(AsmToken::EndOfStatement))
1190 return TokError("unexpected token in '.include' directive");
1192 // Strip the quotes.
1193 Filename = Filename.substr(1, Filename.size()-2);
1195 // Attempt to switch the lexer to the included file before consuming the end
1196 // of statement to avoid losing it when we switch.
1197 if (Lexer.EnterIncludeFile(Filename)) {
1198 Lexer.PrintMessage(IncludeLoc,
1199 "Could not find include file '" + Filename + "'",
1207 /// ParseDirectiveDarwinDumpOrLoad
1208 /// ::= ( .dump | .load ) "filename"
1209 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1210 if (Lexer.isNot(AsmToken::String))
1211 return TokError("expected string in '.dump' or '.load' directive");
1215 if (Lexer.isNot(AsmToken::EndOfStatement))
1216 return TokError("unexpected token in '.dump' or '.load' directive");
1220 // FIXME: If/when .dump and .load are implemented they will be done in the
1221 // the assembly parser and not have any need for an MCStreamer API.
1223 Warning(IDLoc, "ignoring directive .dump for now");
1225 Warning(IDLoc, "ignoring directive .load for now");