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/MCSection.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetAsmParser.h"
28 void AsmParser::Warning(SMLoc L
, const Twine
&Msg
) {
29 Lexer
.PrintMessage(L
, Msg
.str(), "warning");
32 bool AsmParser::Error(SMLoc L
, const Twine
&Msg
) {
33 Lexer
.PrintMessage(L
, Msg
.str(), "error");
37 bool AsmParser::TokError(const char *Msg
) {
38 Lexer
.PrintMessage(Lexer
.getLoc(), Msg
, "error");
42 bool AsmParser::Run() {
46 bool HadError
= false;
48 // While we have input, parse each statement.
49 while (Lexer
.isNot(AsmToken::Eof
)) {
50 if (!ParseStatement()) continue;
52 // If we had an error, remember it and recover by skipping to the next line.
54 EatToEndOfStatement();
60 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
61 void AsmParser::EatToEndOfStatement() {
62 while (Lexer
.isNot(AsmToken::EndOfStatement
) &&
63 Lexer
.isNot(AsmToken::Eof
))
67 if (Lexer
.is(AsmToken::EndOfStatement
))
72 /// ParseParenExpr - Parse a paren expression and return it.
73 /// NOTE: This assumes the leading '(' has already been consumed.
75 /// parenexpr ::= expr)
77 bool AsmParser::ParseParenExpr(AsmExpr
*&Res
) {
78 if (ParseExpression(Res
)) return true;
79 if (Lexer
.isNot(AsmToken::RParen
))
80 return TokError("expected ')' in parentheses expression");
85 /// ParsePrimaryExpr - Parse a primary expression and return it.
86 /// primaryexpr ::= (parenexpr
87 /// primaryexpr ::= symbol
88 /// primaryexpr ::= number
89 /// primaryexpr ::= ~,+,- primaryexpr
90 bool AsmParser::ParsePrimaryExpr(AsmExpr
*&Res
) {
91 switch (Lexer
.getKind()) {
93 return TokError("unknown token in expression");
94 case AsmToken::Exclaim
:
95 Lexer
.Lex(); // Eat the operator.
96 if (ParsePrimaryExpr(Res
))
98 Res
= new AsmUnaryExpr(AsmUnaryExpr::LNot
, Res
);
100 case AsmToken::String
:
101 case AsmToken::Identifier
: {
102 // This is a label, this should be parsed as part of an expression, to
103 // handle things like LFOO+4.
104 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getTok().getIdentifier());
106 // If this is use of an undefined symbol then mark it external.
107 if (!Sym
->getSection() && !Ctx
.GetSymbolValue(Sym
))
108 Sym
->setExternal(true);
110 Res
= new AsmSymbolRefExpr(Sym
);
111 Lexer
.Lex(); // Eat identifier.
114 case AsmToken::Integer
:
115 Res
= new AsmConstantExpr(Lexer
.getTok().getIntVal());
116 Lexer
.Lex(); // Eat token.
118 case AsmToken::LParen
:
119 Lexer
.Lex(); // Eat the '('.
120 return ParseParenExpr(Res
);
121 case AsmToken::Minus
:
122 Lexer
.Lex(); // Eat the operator.
123 if (ParsePrimaryExpr(Res
))
125 Res
= new AsmUnaryExpr(AsmUnaryExpr::Minus
, Res
);
128 Lexer
.Lex(); // Eat the operator.
129 if (ParsePrimaryExpr(Res
))
131 Res
= new AsmUnaryExpr(AsmUnaryExpr::Plus
, Res
);
133 case AsmToken::Tilde
:
134 Lexer
.Lex(); // Eat the operator.
135 if (ParsePrimaryExpr(Res
))
137 Res
= new AsmUnaryExpr(AsmUnaryExpr::Not
, Res
);
142 /// ParseExpression - Parse an expression and return it.
144 /// expr ::= expr +,- expr -> lowest.
145 /// expr ::= expr |,^,&,! expr -> middle.
146 /// expr ::= expr *,/,%,<<,>> expr -> highest.
147 /// expr ::= primaryexpr
149 bool AsmParser::ParseExpression(AsmExpr
*&Res
) {
151 return ParsePrimaryExpr(Res
) ||
152 ParseBinOpRHS(1, Res
);
155 bool AsmParser::ParseAbsoluteExpression(int64_t &Res
) {
158 SMLoc StartLoc
= Lexer
.getLoc();
159 if (ParseExpression(Expr
))
162 if (!Expr
->EvaluateAsAbsolute(Ctx
, Res
))
163 return Error(StartLoc
, "expected absolute expression");
168 bool AsmParser::ParseRelocatableExpression(MCValue
&Res
) {
171 SMLoc StartLoc
= Lexer
.getLoc();
172 if (ParseExpression(Expr
))
175 if (!Expr
->EvaluateAsRelocatable(Ctx
, Res
))
176 return Error(StartLoc
, "expected relocatable expression");
181 bool AsmParser::ParseParenRelocatableExpression(MCValue
&Res
) {
184 SMLoc StartLoc
= Lexer
.getLoc();
185 if (ParseParenExpr(Expr
))
188 if (!Expr
->EvaluateAsRelocatable(Ctx
, Res
))
189 return Error(StartLoc
, "expected relocatable expression");
194 static unsigned getBinOpPrecedence(AsmToken::TokenKind K
,
195 AsmBinaryExpr::Opcode
&Kind
) {
197 default: return 0; // not a binop.
199 // Lowest Precedence: &&, ||
200 case AsmToken::AmpAmp
:
201 Kind
= AsmBinaryExpr::LAnd
;
203 case AsmToken::PipePipe
:
204 Kind
= AsmBinaryExpr::LOr
;
207 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
209 Kind
= AsmBinaryExpr::Add
;
211 case AsmToken::Minus
:
212 Kind
= AsmBinaryExpr::Sub
;
214 case AsmToken::EqualEqual
:
215 Kind
= AsmBinaryExpr::EQ
;
217 case AsmToken::ExclaimEqual
:
218 case AsmToken::LessGreater
:
219 Kind
= AsmBinaryExpr::NE
;
222 Kind
= AsmBinaryExpr::LT
;
224 case AsmToken::LessEqual
:
225 Kind
= AsmBinaryExpr::LTE
;
227 case AsmToken::Greater
:
228 Kind
= AsmBinaryExpr::GT
;
230 case AsmToken::GreaterEqual
:
231 Kind
= AsmBinaryExpr::GTE
;
234 // Intermediate Precedence: |, &, ^
236 // FIXME: gas seems to support '!' as an infix operator?
238 Kind
= AsmBinaryExpr::Or
;
240 case AsmToken::Caret
:
241 Kind
= AsmBinaryExpr::Xor
;
244 Kind
= AsmBinaryExpr::And
;
247 // Highest Precedence: *, /, %, <<, >>
249 Kind
= AsmBinaryExpr::Mul
;
251 case AsmToken::Slash
:
252 Kind
= AsmBinaryExpr::Div
;
254 case AsmToken::Percent
:
255 Kind
= AsmBinaryExpr::Mod
;
257 case AsmToken::LessLess
:
258 Kind
= AsmBinaryExpr::Shl
;
260 case AsmToken::GreaterGreater
:
261 Kind
= AsmBinaryExpr::Shr
;
267 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
268 /// Res contains the LHS of the expression on input.
269 bool AsmParser::ParseBinOpRHS(unsigned Precedence
, AsmExpr
*&Res
) {
271 AsmBinaryExpr::Opcode Kind
= AsmBinaryExpr::Add
;
272 unsigned TokPrec
= getBinOpPrecedence(Lexer
.getKind(), Kind
);
274 // If the next token is lower precedence than we are allowed to eat, return
275 // successfully with what we ate already.
276 if (TokPrec
< Precedence
)
281 // Eat the next primary expression.
283 if (ParsePrimaryExpr(RHS
)) return true;
285 // If BinOp binds less tightly with RHS than the operator after RHS, let
286 // the pending operator take RHS as its LHS.
287 AsmBinaryExpr::Opcode Dummy
;
288 unsigned NextTokPrec
= getBinOpPrecedence(Lexer
.getKind(), Dummy
);
289 if (TokPrec
< NextTokPrec
) {
290 if (ParseBinOpRHS(Precedence
+1, RHS
)) return true;
293 // Merge LHS and RHS according to operator.
294 Res
= new AsmBinaryExpr(Kind
, Res
, RHS
);
302 /// ::= EndOfStatement
303 /// ::= Label* Directive ...Operands... EndOfStatement
304 /// ::= Label* Identifier OperandList* EndOfStatement
305 bool AsmParser::ParseStatement() {
306 if (Lexer
.is(AsmToken::EndOfStatement
)) {
311 // Statements always start with an identifier.
312 AsmToken ID
= Lexer
.getTok();
313 SMLoc IDLoc
= ID
.getLoc();
315 if (ParseIdentifier(IDVal
))
316 return TokError("unexpected token at start of statement");
318 // FIXME: Recurse on local labels?
320 // See what kind of statement we have.
321 switch (Lexer
.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 (getTargetParser().ParseInstruction(IDVal
, Inst
))
557 if (Lexer
.isNot(AsmToken::EndOfStatement
))
558 return TokError("unexpected token in argument list");
560 // Eat the end of statement marker.
563 // Instruction is good, process it.
564 Out
.EmitInstruction(Inst
);
566 // Skip to end of line for now.
570 bool AsmParser::ParseAssignment(const StringRef
&Name
, bool IsDotSet
) {
571 // FIXME: Use better location, we should use proper tokens.
572 SMLoc EqualLoc
= Lexer
.getLoc();
575 if (ParseRelocatableExpression(Value
))
578 if (Lexer
.isNot(AsmToken::EndOfStatement
))
579 return TokError("unexpected token in assignment");
581 // Eat the end of statement marker.
584 // Diagnose assignment to a label.
586 // FIXME: Diagnostics. Note the location of the definition as a label.
587 // FIXME: This doesn't diagnose assignment to a symbol which has been
588 // implicitly marked as external.
589 // FIXME: Handle '.'.
590 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
591 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
);
592 if (Sym
->getSection())
593 return Error(EqualLoc
, "invalid assignment to symbol emitted as a label");
594 if (Sym
->isExternal())
595 return Error(EqualLoc
, "invalid assignment to external symbol");
597 // Do the assignment.
598 Out
.EmitAssignment(Sym
, Value
, IsDotSet
);
606 bool AsmParser::ParseIdentifier(StringRef
&Res
) {
607 if (Lexer
.isNot(AsmToken::Identifier
) &&
608 Lexer
.isNot(AsmToken::String
))
611 Res
= Lexer
.getTok().getIdentifier();
613 Lexer
.Lex(); // Consume the identifier token.
618 /// ParseDirectiveSet:
619 /// ::= .set identifier ',' expression
620 bool AsmParser::ParseDirectiveSet() {
623 if (ParseIdentifier(Name
))
624 return TokError("expected identifier after '.set' directive");
626 if (Lexer
.isNot(AsmToken::Comma
))
627 return TokError("unexpected token in '.set'");
630 return ParseAssignment(Name
, true);
633 /// ParseDirectiveSection:
634 /// ::= .section identifier (',' identifier)*
635 /// FIXME: This should actually parse out the segment, section, attributes and
636 /// sizeof_stub fields.
637 bool AsmParser::ParseDirectiveDarwinSection() {
638 StringRef SectionName
;
640 if (ParseIdentifier(SectionName
))
641 return TokError("expected identifier after '.section' directive");
643 std::string Section
= SectionName
;
645 // FIXME: This doesn't work, we lose quoting on things
647 // Accept a comma separated list of modifiers.
648 while (Lexer
.is(AsmToken::Comma
)) {
649 Lexer
.Lex(); // Consume the comma.
651 StringRef ModifierName
;
652 if (ParseIdentifier(ModifierName
))
653 return TokError("expected identifier in '.section' directive");
655 Section
+= ModifierName
;
658 if (Lexer
.isNot(AsmToken::EndOfStatement
))
659 return TokError("unexpected token in '.section' directive");
662 // FIXME: Arch specific.
663 MCSection
*S
= Ctx
.GetSection(Section
);
665 S
= MCSection::Create(Section
, false, SectionKind(), Ctx
);
667 Out
.SwitchSection(S
);
671 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section
,
672 const char *Directives
) {
673 if (Lexer
.isNot(AsmToken::EndOfStatement
))
674 return TokError("unexpected token in section switching directive");
677 std::string SectionStr
= Section
;
678 if (Directives
&& Directives
[0]) {
680 SectionStr
+= Directives
;
683 // FIXME: Arch specific.
684 MCSection
*S
= Ctx
.GetSection(Section
);
686 S
= MCSection::Create(Section
, false, SectionKind(), Ctx
);
688 Out
.SwitchSection(S
);
692 /// ParseDirectiveAscii:
693 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
694 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated
) {
695 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
697 if (Lexer
.isNot(AsmToken::String
))
698 return TokError("expected string in '.ascii' or '.asciz' directive");
700 // FIXME: This shouldn't use a const char* + strlen, the string could have
702 // FIXME: Should have accessor for getting string contents.
703 StringRef Str
= Lexer
.getTok().getString();
704 Out
.EmitBytes(Str
.substr(1, Str
.size() - 2));
706 Out
.EmitBytes(StringRef("\0", 1));
710 if (Lexer
.is(AsmToken::EndOfStatement
))
713 if (Lexer
.isNot(AsmToken::Comma
))
714 return TokError("unexpected token in '.ascii' or '.asciz' directive");
723 /// ParseDirectiveValue
724 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
725 bool AsmParser::ParseDirectiveValue(unsigned Size
) {
726 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
729 if (ParseRelocatableExpression(Expr
))
732 Out
.EmitValue(Expr
, Size
);
734 if (Lexer
.is(AsmToken::EndOfStatement
))
737 // FIXME: Improve diagnostic.
738 if (Lexer
.isNot(AsmToken::Comma
))
739 return TokError("unexpected token in directive");
748 /// ParseDirectiveSpace
749 /// ::= .space expression [ , expression ]
750 bool AsmParser::ParseDirectiveSpace() {
752 if (ParseAbsoluteExpression(NumBytes
))
755 int64_t FillExpr
= 0;
756 bool HasFillExpr
= false;
757 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
758 if (Lexer
.isNot(AsmToken::Comma
))
759 return TokError("unexpected token in '.space' directive");
762 if (ParseAbsoluteExpression(FillExpr
))
767 if (Lexer
.isNot(AsmToken::EndOfStatement
))
768 return TokError("unexpected token in '.space' directive");
774 return TokError("invalid number of bytes in '.space' directive");
776 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
777 for (uint64_t i
= 0, e
= NumBytes
; i
!= e
; ++i
)
778 Out
.EmitValue(MCValue::get(FillExpr
), 1);
783 /// ParseDirectiveFill
784 /// ::= .fill expression , expression , expression
785 bool AsmParser::ParseDirectiveFill() {
787 if (ParseAbsoluteExpression(NumValues
))
790 if (Lexer
.isNot(AsmToken::Comma
))
791 return TokError("unexpected token in '.fill' directive");
795 if (ParseAbsoluteExpression(FillSize
))
798 if (Lexer
.isNot(AsmToken::Comma
))
799 return TokError("unexpected token in '.fill' directive");
803 if (ParseAbsoluteExpression(FillExpr
))
806 if (Lexer
.isNot(AsmToken::EndOfStatement
))
807 return TokError("unexpected token in '.fill' directive");
811 if (FillSize
!= 1 && FillSize
!= 2 && FillSize
!= 4)
812 return TokError("invalid '.fill' size, expected 1, 2, or 4");
814 for (uint64_t i
= 0, e
= NumValues
; i
!= e
; ++i
)
815 Out
.EmitValue(MCValue::get(FillExpr
), FillSize
);
820 /// ParseDirectiveOrg
821 /// ::= .org expression [ , expression ]
822 bool AsmParser::ParseDirectiveOrg() {
824 if (ParseRelocatableExpression(Offset
))
827 // Parse optional fill expression.
828 int64_t FillExpr
= 0;
829 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
830 if (Lexer
.isNot(AsmToken::Comma
))
831 return TokError("unexpected token in '.org' directive");
834 if (ParseAbsoluteExpression(FillExpr
))
837 if (Lexer
.isNot(AsmToken::EndOfStatement
))
838 return TokError("unexpected token in '.org' directive");
843 // FIXME: Only limited forms of relocatable expressions are accepted here, it
844 // has to be relative to the current section.
845 Out
.EmitValueToOffset(Offset
, FillExpr
);
850 /// ParseDirectiveAlign
851 /// ::= {.align, ...} expression [ , expression [ , expression ]]
852 bool AsmParser::ParseDirectiveAlign(bool IsPow2
, unsigned ValueSize
) {
854 if (ParseAbsoluteExpression(Alignment
))
858 bool HasFillExpr
= false;
859 int64_t FillExpr
= 0;
860 int64_t MaxBytesToFill
= 0;
861 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
862 if (Lexer
.isNot(AsmToken::Comma
))
863 return TokError("unexpected token in directive");
866 // The fill expression can be omitted while specifying a maximum number of
867 // alignment bytes, e.g:
869 if (Lexer
.isNot(AsmToken::Comma
)) {
871 if (ParseAbsoluteExpression(FillExpr
))
875 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
876 if (Lexer
.isNot(AsmToken::Comma
))
877 return TokError("unexpected token in directive");
880 MaxBytesLoc
= Lexer
.getLoc();
881 if (ParseAbsoluteExpression(MaxBytesToFill
))
884 if (Lexer
.isNot(AsmToken::EndOfStatement
))
885 return TokError("unexpected token in directive");
892 // FIXME: Sometimes fill with nop.
896 // Compute alignment in bytes.
898 // FIXME: Diagnose overflow.
899 Alignment
= 1LL << Alignment
;
902 // Diagnose non-sensical max bytes to fill.
903 if (MaxBytesLoc
.isValid()) {
904 if (MaxBytesToFill
< 1) {
905 Warning(MaxBytesLoc
, "alignment directive can never be satisfied in this "
906 "many bytes, ignoring");
910 if (MaxBytesToFill
>= Alignment
) {
911 Warning(MaxBytesLoc
, "maximum bytes expression exceeds alignment and "
917 // FIXME: Target specific behavior about how the "extra" bytes are filled.
918 Out
.EmitValueToAlignment(Alignment
, FillExpr
, ValueSize
, MaxBytesToFill
);
923 /// ParseDirectiveSymbolAttribute
924 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
925 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr
) {
926 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
930 if (ParseIdentifier(Name
))
931 return TokError("expected identifier in directive");
933 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
);
935 // If this is use of an undefined symbol then mark it external.
936 if (!Sym
->getSection() && !Ctx
.GetSymbolValue(Sym
))
937 Sym
->setExternal(true);
939 Out
.EmitSymbolAttribute(Sym
, Attr
);
941 if (Lexer
.is(AsmToken::EndOfStatement
))
944 if (Lexer
.isNot(AsmToken::Comma
))
945 return TokError("unexpected token in directive");
954 /// ParseDirectiveDarwinSymbolDesc
955 /// ::= .desc identifier , expression
956 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
958 if (ParseIdentifier(Name
))
959 return TokError("expected identifier in directive");
961 // Handle the identifier as the key symbol.
962 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
);
964 if (Lexer
.isNot(AsmToken::Comma
))
965 return TokError("unexpected token in '.desc' directive");
968 SMLoc DescLoc
= Lexer
.getLoc();
970 if (ParseAbsoluteExpression(DescValue
))
973 if (Lexer
.isNot(AsmToken::EndOfStatement
))
974 return TokError("unexpected token in '.desc' directive");
978 // Set the n_desc field of this Symbol to this DescValue
979 Out
.EmitSymbolDesc(Sym
, DescValue
);
984 /// ParseDirectiveComm
985 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
986 bool AsmParser::ParseDirectiveComm(bool IsLocal
) {
987 SMLoc IDLoc
= Lexer
.getLoc();
989 if (ParseIdentifier(Name
))
990 return TokError("expected identifier in directive");
992 // Handle the identifier as the key symbol.
993 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
);
995 if (Lexer
.isNot(AsmToken::Comma
))
996 return TokError("unexpected token in directive");
1000 SMLoc SizeLoc
= Lexer
.getLoc();
1001 if (ParseAbsoluteExpression(Size
))
1004 int64_t Pow2Alignment
= 0;
1005 SMLoc Pow2AlignmentLoc
;
1006 if (Lexer
.is(AsmToken::Comma
)) {
1008 Pow2AlignmentLoc
= Lexer
.getLoc();
1009 if (ParseAbsoluteExpression(Pow2Alignment
))
1013 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1014 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1018 // NOTE: a size of zero for a .comm should create a undefined symbol
1019 // but a size of .lcomm creates a bss symbol of size zero.
1021 return Error(SizeLoc
, "invalid '.comm' or '.lcomm' directive size, can't "
1022 "be less than zero");
1024 // NOTE: The alignment in the directive is a power of 2 value, the assember
1025 // may internally end up wanting an alignment in bytes.
1026 // FIXME: Diagnose overflow.
1027 if (Pow2Alignment
< 0)
1028 return Error(Pow2AlignmentLoc
, "invalid '.comm' or '.lcomm' directive "
1029 "alignment, can't be less than zero");
1031 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1032 if (Sym
->getSection() || Ctx
.GetSymbolValue(Sym
))
1033 return Error(IDLoc
, "invalid symbol redefinition");
1035 // Create the Symbol as a common or local common with Size and Pow2Alignment
1036 Out
.EmitCommonSymbol(Sym
, Size
, Pow2Alignment
, IsLocal
);
1041 /// ParseDirectiveDarwinZerofill
1042 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1043 /// , align_expression ]]
1044 bool AsmParser::ParseDirectiveDarwinZerofill() {
1045 // FIXME: Handle quoted names here.
1047 if (Lexer
.isNot(AsmToken::Identifier
))
1048 return TokError("expected segment name after '.zerofill' directive");
1049 std::string Section
= Lexer
.getTok().getString();
1052 if (Lexer
.isNot(AsmToken::Comma
))
1053 return TokError("unexpected token in directive");
1057 if (Lexer
.isNot(AsmToken::Identifier
))
1058 return TokError("expected section name after comma in '.zerofill' "
1060 Section
+= Lexer
.getTok().getString().str();
1063 // FIXME: we will need to tell GetSection() that this is to be created with or
1064 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1065 // below could be done but for now it is not as EmitZerofill() does not know
1066 // how to deal with a section type in the section name like
1067 // ParseDirectiveDarwinSection() allows.
1069 // Section += "zerofill";
1071 // If this is the end of the line all that was wanted was to create the
1072 // the section but with no symbol.
1073 if (Lexer
.is(AsmToken::EndOfStatement
)) {
1074 // FIXME: Arch specific.
1075 MCSection
*S
= Ctx
.GetSection(Section
);
1077 S
= MCSection::Create(Section
, false, SectionKind(), Ctx
);
1079 // Create the zerofill section but no symbol
1080 Out
.EmitZerofill(S
);
1084 if (Lexer
.isNot(AsmToken::Comma
))
1085 return TokError("unexpected token in directive");
1088 if (Lexer
.isNot(AsmToken::Identifier
))
1089 return TokError("expected identifier in directive");
1091 // handle the identifier as the key symbol.
1092 SMLoc IDLoc
= Lexer
.getLoc();
1093 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getTok().getString());
1096 if (Lexer
.isNot(AsmToken::Comma
))
1097 return TokError("unexpected token in directive");
1101 SMLoc SizeLoc
= Lexer
.getLoc();
1102 if (ParseAbsoluteExpression(Size
))
1105 int64_t Pow2Alignment
= 0;
1106 SMLoc Pow2AlignmentLoc
;
1107 if (Lexer
.is(AsmToken::Comma
)) {
1109 Pow2AlignmentLoc
= Lexer
.getLoc();
1110 if (ParseAbsoluteExpression(Pow2Alignment
))
1114 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1115 return TokError("unexpected token in '.zerofill' directive");
1120 return Error(SizeLoc
, "invalid '.zerofill' directive size, can't be less "
1123 // NOTE: The alignment in the directive is a power of 2 value, the assember
1124 // may internally end up wanting an alignment in bytes.
1125 // FIXME: Diagnose overflow.
1126 if (Pow2Alignment
< 0)
1127 return Error(Pow2AlignmentLoc
, "invalid '.zerofill' directive alignment, "
1128 "can't be less than zero");
1130 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1131 if (Sym
->getSection() || Ctx
.GetSymbolValue(Sym
))
1132 return Error(IDLoc
, "invalid symbol redefinition");
1134 // FIXME: Arch specific.
1135 MCSection
*S
= Ctx
.GetSection(Section
);
1137 S
= MCSection::Create(Section
, false, SectionKind(), Ctx
);
1139 // Create the zerofill Symbol with Size and Pow2Alignment
1140 Out
.EmitZerofill(S
, Sym
, Size
, Pow2Alignment
);
1145 /// ParseDirectiveDarwinSubsectionsViaSymbols
1146 /// ::= .subsections_via_symbols
1147 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1148 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1149 return TokError("unexpected token in '.subsections_via_symbols' directive");
1153 Out
.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols
);
1158 /// ParseDirectiveAbort
1159 /// ::= .abort [ "abort_string" ]
1160 bool AsmParser::ParseDirectiveAbort() {
1161 // FIXME: Use loc from directive.
1162 SMLoc Loc
= Lexer
.getLoc();
1165 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1166 if (Lexer
.isNot(AsmToken::String
))
1167 return TokError("expected string in '.abort' directive");
1169 Str
= Lexer
.getTok().getString();
1174 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1175 return TokError("unexpected token in '.abort' directive");
1179 // FIXME: Handle here.
1181 Error(Loc
, ".abort detected. Assembly stopping.");
1183 Error(Loc
, ".abort '" + Str
+ "' detected. Assembly stopping.");
1188 /// ParseDirectiveLsym
1189 /// ::= .lsym identifier , expression
1190 bool AsmParser::ParseDirectiveDarwinLsym() {
1192 if (ParseIdentifier(Name
))
1193 return TokError("expected identifier in directive");
1195 // Handle the identifier as the key symbol.
1196 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
);
1198 if (Lexer
.isNot(AsmToken::Comma
))
1199 return TokError("unexpected token in '.lsym' directive");
1203 if (ParseRelocatableExpression(Expr
))
1206 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1207 return TokError("unexpected token in '.lsym' directive");
1211 // Create the Sym with the value of the Expr
1212 Out
.EmitLocalSymbol(Sym
, Expr
);
1217 /// ParseDirectiveInclude
1218 /// ::= .include "filename"
1219 bool AsmParser::ParseDirectiveInclude() {
1220 if (Lexer
.isNot(AsmToken::String
))
1221 return TokError("expected string in '.include' directive");
1223 std::string Filename
= Lexer
.getTok().getString();
1224 SMLoc IncludeLoc
= Lexer
.getLoc();
1227 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1228 return TokError("unexpected token in '.include' directive");
1230 // Strip the quotes.
1231 Filename
= Filename
.substr(1, Filename
.size()-2);
1233 // Attempt to switch the lexer to the included file before consuming the end
1234 // of statement to avoid losing it when we switch.
1235 if (Lexer
.EnterIncludeFile(Filename
)) {
1236 Lexer
.PrintMessage(IncludeLoc
,
1237 "Could not find include file '" + Filename
+ "'",
1245 /// ParseDirectiveDarwinDumpOrLoad
1246 /// ::= ( .dump | .load ) "filename"
1247 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc
, bool IsDump
) {
1248 if (Lexer
.isNot(AsmToken::String
))
1249 return TokError("expected string in '.dump' or '.load' directive");
1253 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1254 return TokError("unexpected token in '.dump' or '.load' directive");
1258 // FIXME: If/when .dump and .load are implemented they will be done in the
1259 // the assembly parser and not have any need for an MCStreamer API.
1261 Warning(IDLoc
, "ignoring directive .dump for now");
1263 Warning(IDLoc
, "ignoring directive .load for now");