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"
25 void AsmParser::Warning(SMLoc L
, const char *Msg
) {
26 Lexer
.PrintMessage(L
, Msg
, "warning");
29 bool AsmParser::Error(SMLoc L
, const char *Msg
) {
30 Lexer
.PrintMessage(L
, Msg
, "error");
34 bool AsmParser::TokError(const char *Msg
) {
35 Lexer
.PrintMessage(Lexer
.getLoc(), Msg
, "error");
39 bool AsmParser::Run() {
43 bool HadError
= false;
45 // While we have input, parse each statement.
46 while (Lexer
.isNot(asmtok::Eof
)) {
47 if (!ParseStatement()) continue;
49 // If we had an error, remember it and recover by skipping to the next line.
51 EatToEndOfStatement();
57 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
58 void AsmParser::EatToEndOfStatement() {
59 while (Lexer
.isNot(asmtok::EndOfStatement
) &&
60 Lexer
.isNot(asmtok::Eof
))
64 if (Lexer
.is(asmtok::EndOfStatement
))
69 /// ParseParenExpr - Parse a paren expression and return it.
70 /// NOTE: This assumes the leading '(' has already been consumed.
72 /// parenexpr ::= expr)
74 bool AsmParser::ParseParenExpr(AsmExpr
*&Res
) {
75 if (ParseExpression(Res
)) return true;
76 if (Lexer
.isNot(asmtok::RParen
))
77 return TokError("expected ')' in parentheses expression");
82 /// ParsePrimaryExpr - Parse a primary expression and return it.
83 /// primaryexpr ::= (parenexpr
84 /// primaryexpr ::= symbol
85 /// primaryexpr ::= number
86 /// primaryexpr ::= ~,+,- primaryexpr
87 bool AsmParser::ParsePrimaryExpr(AsmExpr
*&Res
) {
88 switch (Lexer
.getKind()) {
90 return TokError("unknown token in expression");
92 Lexer
.Lex(); // Eat the operator.
93 if (ParsePrimaryExpr(Res
))
95 Res
= new AsmUnaryExpr(AsmUnaryExpr::LNot
, Res
);
97 case asmtok::Identifier
: {
98 // This is a label, this should be parsed as part of an expression, to
99 // handle things like LFOO+4.
100 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getCurStrVal());
102 // If this is use of an undefined symbol then mark it external.
103 if (!Sym
->getSection() && !Ctx
.GetSymbolValue(Sym
))
104 Sym
->setExternal(true);
106 Res
= new AsmSymbolRefExpr(Sym
);
107 Lexer
.Lex(); // Eat identifier.
111 Res
= new AsmConstantExpr(Lexer
.getCurIntVal());
112 Lexer
.Lex(); // Eat identifier.
115 Lexer
.Lex(); // Eat the '('.
116 return ParseParenExpr(Res
);
118 Lexer
.Lex(); // Eat the operator.
119 if (ParsePrimaryExpr(Res
))
121 Res
= new AsmUnaryExpr(AsmUnaryExpr::Minus
, Res
);
124 Lexer
.Lex(); // Eat the operator.
125 if (ParsePrimaryExpr(Res
))
127 Res
= new AsmUnaryExpr(AsmUnaryExpr::Plus
, Res
);
130 Lexer
.Lex(); // Eat the operator.
131 if (ParsePrimaryExpr(Res
))
133 Res
= new AsmUnaryExpr(AsmUnaryExpr::Not
, Res
);
138 /// ParseExpression - Parse an expression and return it.
140 /// expr ::= expr +,- expr -> lowest.
141 /// expr ::= expr |,^,&,! expr -> middle.
142 /// expr ::= expr *,/,%,<<,>> expr -> highest.
143 /// expr ::= primaryexpr
145 bool AsmParser::ParseExpression(AsmExpr
*&Res
) {
147 return ParsePrimaryExpr(Res
) ||
148 ParseBinOpRHS(1, Res
);
151 bool AsmParser::ParseAbsoluteExpression(int64_t &Res
) {
154 SMLoc StartLoc
= Lexer
.getLoc();
155 if (ParseExpression(Expr
))
158 if (!Expr
->EvaluateAsAbsolute(Ctx
, Res
))
159 return Error(StartLoc
, "expected absolute expression");
164 bool AsmParser::ParseRelocatableExpression(MCValue
&Res
) {
167 SMLoc StartLoc
= Lexer
.getLoc();
168 if (ParseExpression(Expr
))
171 if (!Expr
->EvaluateAsRelocatable(Ctx
, Res
))
172 return Error(StartLoc
, "expected relocatable expression");
177 bool AsmParser::ParseParenRelocatableExpression(MCValue
&Res
) {
180 SMLoc StartLoc
= Lexer
.getLoc();
181 if (ParseParenExpr(Expr
))
184 if (!Expr
->EvaluateAsRelocatable(Ctx
, Res
))
185 return Error(StartLoc
, "expected relocatable expression");
190 static unsigned getBinOpPrecedence(asmtok::TokKind K
,
191 AsmBinaryExpr::Opcode
&Kind
) {
193 default: return 0; // not a binop.
195 // Lowest Precedence: &&, ||
197 Kind
= AsmBinaryExpr::LAnd
;
199 case asmtok::PipePipe
:
200 Kind
= AsmBinaryExpr::LOr
;
203 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
205 Kind
= AsmBinaryExpr::Add
;
208 Kind
= AsmBinaryExpr::Sub
;
210 case asmtok::EqualEqual
:
211 Kind
= AsmBinaryExpr::EQ
;
213 case asmtok::ExclaimEqual
:
214 case asmtok::LessGreater
:
215 Kind
= AsmBinaryExpr::NE
;
218 Kind
= AsmBinaryExpr::LT
;
220 case asmtok::LessEqual
:
221 Kind
= AsmBinaryExpr::LTE
;
223 case asmtok::Greater
:
224 Kind
= AsmBinaryExpr::GT
;
226 case asmtok::GreaterEqual
:
227 Kind
= AsmBinaryExpr::GTE
;
230 // Intermediate Precedence: |, &, ^
232 // FIXME: gas seems to support '!' as an infix operator?
234 Kind
= AsmBinaryExpr::Or
;
237 Kind
= AsmBinaryExpr::Xor
;
240 Kind
= AsmBinaryExpr::And
;
243 // Highest Precedence: *, /, %, <<, >>
245 Kind
= AsmBinaryExpr::Mul
;
248 Kind
= AsmBinaryExpr::Div
;
250 case asmtok::Percent
:
251 Kind
= AsmBinaryExpr::Mod
;
253 case asmtok::LessLess
:
254 Kind
= AsmBinaryExpr::Shl
;
256 case asmtok::GreaterGreater
:
257 Kind
= AsmBinaryExpr::Shr
;
263 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
264 /// Res contains the LHS of the expression on input.
265 bool AsmParser::ParseBinOpRHS(unsigned Precedence
, AsmExpr
*&Res
) {
267 AsmBinaryExpr::Opcode Kind
= AsmBinaryExpr::Add
;
268 unsigned TokPrec
= getBinOpPrecedence(Lexer
.getKind(), Kind
);
270 // If the next token is lower precedence than we are allowed to eat, return
271 // successfully with what we ate already.
272 if (TokPrec
< Precedence
)
277 // Eat the next primary expression.
279 if (ParsePrimaryExpr(RHS
)) return true;
281 // If BinOp binds less tightly with RHS than the operator after RHS, let
282 // the pending operator take RHS as its LHS.
283 AsmBinaryExpr::Opcode Dummy
;
284 unsigned NextTokPrec
= getBinOpPrecedence(Lexer
.getKind(), Dummy
);
285 if (TokPrec
< NextTokPrec
) {
286 if (ParseBinOpRHS(Precedence
+1, RHS
)) return true;
289 // Merge LHS and RHS according to operator.
290 Res
= new AsmBinaryExpr(Kind
, Res
, RHS
);
298 /// ::= EndOfStatement
299 /// ::= Label* Directive ...Operands... EndOfStatement
300 /// ::= Label* Identifier OperandList* EndOfStatement
301 bool AsmParser::ParseStatement() {
302 switch (Lexer
.getKind()) {
304 return TokError("unexpected token at start of statement");
305 case asmtok::EndOfStatement
:
308 case asmtok::Identifier
:
310 // TODO: Recurse on local labels etc.
313 // If we have an identifier, handle it as the key symbol.
314 SMLoc IDLoc
= Lexer
.getLoc();
315 const char *IDVal
= Lexer
.getCurStrVal();
317 // Consume the identifier, see what is after it.
318 switch (Lexer
.Lex()) {
319 case asmtok::Colon
: {
320 // identifier ':' -> Label.
323 // Diagnose attempt to use a variable as a label.
325 // FIXME: Diagnostics. Note the location of the definition as a label.
326 // FIXME: This doesn't diagnose assignment to a symbol which has been
327 // implicitly marked as external.
328 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(IDVal
);
329 if (Sym
->getSection())
330 return Error(IDLoc
, "invalid symbol redefinition");
331 if (Ctx
.GetSymbolValue(Sym
))
332 return Error(IDLoc
, "symbol already used as assembler variable");
334 // Since we saw a label, create a symbol and emit it.
335 // FIXME: If the label starts with L it is an assembler temporary label.
336 // Why does the client of this api need to know this?
339 return ParseStatement();
343 // identifier '=' ... -> assignment statement
346 return ParseAssignment(IDVal
, false);
348 default: // Normal instruction or directive.
352 // Otherwise, we have a normal instruction or directive.
353 if (IDVal
[0] == '.') {
354 // FIXME: This should be driven based on a hash lookup and callback.
355 if (!strcmp(IDVal
, ".section"))
356 return ParseDirectiveDarwinSection();
357 if (!strcmp(IDVal
, ".text"))
358 // FIXME: This changes behavior based on the -static flag to the
360 return ParseDirectiveSectionSwitch("__TEXT,__text",
361 "regular,pure_instructions");
362 if (!strcmp(IDVal
, ".const"))
363 return ParseDirectiveSectionSwitch("__TEXT,__const");
364 if (!strcmp(IDVal
, ".static_const"))
365 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
366 if (!strcmp(IDVal
, ".cstring"))
367 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
369 if (!strcmp(IDVal
, ".literal4"))
370 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
371 if (!strcmp(IDVal
, ".literal8"))
372 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
373 if (!strcmp(IDVal
, ".literal16"))
374 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
376 if (!strcmp(IDVal
, ".constructor"))
377 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
378 if (!strcmp(IDVal
, ".destructor"))
379 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
380 if (!strcmp(IDVal
, ".fvmlib_init0"))
381 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
382 if (!strcmp(IDVal
, ".fvmlib_init1"))
383 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
384 if (!strcmp(IDVal
, ".symbol_stub")) // FIXME: Different on PPC.
385 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
386 "self_modifying_code+pure_instructions,5");
387 // FIXME: .picsymbol_stub on PPC.
388 if (!strcmp(IDVal
, ".data"))
389 return ParseDirectiveSectionSwitch("__DATA,__data");
390 if (!strcmp(IDVal
, ".static_data"))
391 return ParseDirectiveSectionSwitch("__DATA,__static_data");
392 if (!strcmp(IDVal
, ".non_lazy_symbol_pointer"))
393 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
394 "non_lazy_symbol_pointers");
395 if (!strcmp(IDVal
, ".lazy_symbol_pointer"))
396 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
397 "lazy_symbol_pointers");
398 if (!strcmp(IDVal
, ".dyld"))
399 return ParseDirectiveSectionSwitch("__DATA,__dyld");
400 if (!strcmp(IDVal
, ".mod_init_func"))
401 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
403 if (!strcmp(IDVal
, ".mod_term_func"))
404 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
406 if (!strcmp(IDVal
, ".const_data"))
407 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
410 // FIXME: Verify attributes on sections.
411 if (!strcmp(IDVal
, ".objc_class"))
412 return ParseDirectiveSectionSwitch("__OBJC,__class");
413 if (!strcmp(IDVal
, ".objc_meta_class"))
414 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
415 if (!strcmp(IDVal
, ".objc_cat_cls_meth"))
416 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
417 if (!strcmp(IDVal
, ".objc_cat_inst_meth"))
418 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
419 if (!strcmp(IDVal
, ".objc_protocol"))
420 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
421 if (!strcmp(IDVal
, ".objc_string_object"))
422 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
423 if (!strcmp(IDVal
, ".objc_cls_meth"))
424 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
425 if (!strcmp(IDVal
, ".objc_inst_meth"))
426 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
427 if (!strcmp(IDVal
, ".objc_cls_refs"))
428 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
429 if (!strcmp(IDVal
, ".objc_message_refs"))
430 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
431 if (!strcmp(IDVal
, ".objc_symbols"))
432 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
433 if (!strcmp(IDVal
, ".objc_category"))
434 return ParseDirectiveSectionSwitch("__OBJC,__category");
435 if (!strcmp(IDVal
, ".objc_class_vars"))
436 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
437 if (!strcmp(IDVal
, ".objc_instance_vars"))
438 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
439 if (!strcmp(IDVal
, ".objc_module_info"))
440 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
441 if (!strcmp(IDVal
, ".objc_class_names"))
442 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
443 if (!strcmp(IDVal
, ".objc_meth_var_types"))
444 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
445 if (!strcmp(IDVal
, ".objc_meth_var_names"))
446 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
447 if (!strcmp(IDVal
, ".objc_selector_strs"))
448 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
450 // Assembler features
451 if (!strcmp(IDVal
, ".set"))
452 return ParseDirectiveSet();
456 if (!strcmp(IDVal
, ".ascii"))
457 return ParseDirectiveAscii(false);
458 if (!strcmp(IDVal
, ".asciz"))
459 return ParseDirectiveAscii(true);
461 // FIXME: Target hooks for size? Also for "word", "hword".
462 if (!strcmp(IDVal
, ".byte"))
463 return ParseDirectiveValue(1);
464 if (!strcmp(IDVal
, ".short"))
465 return ParseDirectiveValue(2);
466 if (!strcmp(IDVal
, ".long"))
467 return ParseDirectiveValue(4);
468 if (!strcmp(IDVal
, ".quad"))
469 return ParseDirectiveValue(8);
471 // FIXME: Target hooks for IsPow2.
472 if (!strcmp(IDVal
, ".align"))
473 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
474 if (!strcmp(IDVal
, ".align32"))
475 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
476 if (!strcmp(IDVal
, ".balign"))
477 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
478 if (!strcmp(IDVal
, ".balignw"))
479 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
480 if (!strcmp(IDVal
, ".balignl"))
481 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
482 if (!strcmp(IDVal
, ".p2align"))
483 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
484 if (!strcmp(IDVal
, ".p2alignw"))
485 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
486 if (!strcmp(IDVal
, ".p2alignl"))
487 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
489 if (!strcmp(IDVal
, ".org"))
490 return ParseDirectiveOrg();
492 if (!strcmp(IDVal
, ".fill"))
493 return ParseDirectiveFill();
494 if (!strcmp(IDVal
, ".space"))
495 return ParseDirectiveSpace();
497 // Symbol attribute directives
498 if (!strcmp(IDVal
, ".globl") || !strcmp(IDVal
, ".global"))
499 return ParseDirectiveSymbolAttribute(MCStreamer::Global
);
500 if (!strcmp(IDVal
, ".hidden"))
501 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden
);
502 if (!strcmp(IDVal
, ".indirect_symbol"))
503 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol
);
504 if (!strcmp(IDVal
, ".internal"))
505 return ParseDirectiveSymbolAttribute(MCStreamer::Internal
);
506 if (!strcmp(IDVal
, ".lazy_reference"))
507 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference
);
508 if (!strcmp(IDVal
, ".no_dead_strip"))
509 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip
);
510 if (!strcmp(IDVal
, ".private_extern"))
511 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern
);
512 if (!strcmp(IDVal
, ".protected"))
513 return ParseDirectiveSymbolAttribute(MCStreamer::Protected
);
514 if (!strcmp(IDVal
, ".reference"))
515 return ParseDirectiveSymbolAttribute(MCStreamer::Reference
);
516 if (!strcmp(IDVal
, ".weak"))
517 return ParseDirectiveSymbolAttribute(MCStreamer::Weak
);
518 if (!strcmp(IDVal
, ".weak_definition"))
519 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition
);
520 if (!strcmp(IDVal
, ".weak_reference"))
521 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference
);
523 if (!strcmp(IDVal
, ".comm"))
524 return ParseDirectiveComm(/*IsLocal=*/false);
525 if (!strcmp(IDVal
, ".lcomm"))
526 return ParseDirectiveComm(/*IsLocal=*/true);
527 if (!strcmp(IDVal
, ".zerofill"))
528 return ParseDirectiveDarwinZerofill();
529 if (!strcmp(IDVal
, ".desc"))
530 return ParseDirectiveDarwinSymbolDesc();
531 if (!strcmp(IDVal
, ".lsym"))
532 return ParseDirectiveDarwinLsym();
534 if (!strcmp(IDVal
, ".subsections_via_symbols"))
535 return ParseDirectiveDarwinSubsectionsViaSymbols();
536 if (!strcmp(IDVal
, ".abort"))
537 return ParseDirectiveAbort();
538 if (!strcmp(IDVal
, ".include"))
539 return ParseDirectiveInclude();
540 if (!strcmp(IDVal
, ".dump"))
541 return ParseDirectiveDarwinDumpOrLoad(/*IsDump=*/true);
542 if (!strcmp(IDVal
, ".load"))
543 return ParseDirectiveDarwinDumpOrLoad(/*IsLoad=*/false);
545 Warning(IDLoc
, "ignoring directive for now");
546 EatToEndOfStatement();
551 if (ParseX86InstOperands(IDVal
, Inst
))
554 if (Lexer
.isNot(asmtok::EndOfStatement
))
555 return TokError("unexpected token in argument list");
557 // Eat the end of statement marker.
560 // Instruction is good, process it.
561 Out
.EmitInstruction(Inst
);
563 // Skip to end of line for now.
567 bool AsmParser::ParseAssignment(const char *Name
, bool IsDotSet
) {
568 // FIXME: Use better location, we should use proper tokens.
569 SMLoc EqualLoc
= Lexer
.getLoc();
572 if (ParseRelocatableExpression(Value
))
575 if (Lexer
.isNot(asmtok::EndOfStatement
))
576 return TokError("unexpected token in assignment");
578 // Eat the end of statement marker.
581 // Diagnose assignment to a label.
583 // FIXME: Diagnostics. Note the location of the definition as a label.
584 // FIXME: This doesn't diagnose assignment to a symbol which has been
585 // implicitly marked as external.
586 // FIXME: Handle '.'.
587 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
588 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
);
589 if (Sym
->getSection())
590 return Error(EqualLoc
, "invalid assignment to symbol emitted as a label");
591 if (Sym
->isExternal())
592 return Error(EqualLoc
, "invalid assignment to external symbol");
594 // Do the assignment.
595 Out
.EmitAssignment(Sym
, Value
, IsDotSet
);
600 /// ParseDirectiveSet:
601 /// ::= .set identifier ',' expression
602 bool AsmParser::ParseDirectiveSet() {
603 if (Lexer
.isNot(asmtok::Identifier
))
604 return TokError("expected identifier after '.set' directive");
606 const char *Name
= Lexer
.getCurStrVal();
608 if (Lexer
.Lex() != asmtok::Comma
)
609 return TokError("unexpected token in '.set'");
612 return ParseAssignment(Name
, true);
615 /// ParseDirectiveSection:
616 /// ::= .section identifier (',' identifier)*
617 /// FIXME: This should actually parse out the segment, section, attributes and
618 /// sizeof_stub fields.
619 bool AsmParser::ParseDirectiveDarwinSection() {
620 if (Lexer
.isNot(asmtok::Identifier
))
621 return TokError("expected identifier after '.section' directive");
623 std::string Section
= Lexer
.getCurStrVal();
626 // Accept a comma separated list of modifiers.
627 while (Lexer
.is(asmtok::Comma
)) {
630 if (Lexer
.isNot(asmtok::Identifier
))
631 return TokError("expected identifier in '.section' directive");
633 Section
+= Lexer
.getCurStrVal();
637 if (Lexer
.isNot(asmtok::EndOfStatement
))
638 return TokError("unexpected token in '.section' directive");
641 Out
.SwitchSection(Ctx
.GetSection(Section
.c_str()));
645 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section
,
646 const char *Directives
) {
647 if (Lexer
.isNot(asmtok::EndOfStatement
))
648 return TokError("unexpected token in section switching directive");
651 std::string SectionStr
= Section
;
652 if (Directives
&& Directives
[0]) {
654 SectionStr
+= Directives
;
657 Out
.SwitchSection(Ctx
.GetSection(Section
));
661 /// ParseDirectiveAscii:
662 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
663 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated
) {
664 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
666 if (Lexer
.isNot(asmtok::String
))
667 return TokError("expected string in '.ascii' or '.asciz' directive");
669 // FIXME: This shouldn't use a const char* + strlen, the string could have
671 // FIXME: Should have accessor for getting string contents.
672 const char *Str
= Lexer
.getCurStrVal();
673 Out
.EmitBytes(Str
+ 1, strlen(Str
) - 2);
675 Out
.EmitBytes("\0", 1);
679 if (Lexer
.is(asmtok::EndOfStatement
))
682 if (Lexer
.isNot(asmtok::Comma
))
683 return TokError("unexpected token in '.ascii' or '.asciz' directive");
692 /// ParseDirectiveValue
693 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
694 bool AsmParser::ParseDirectiveValue(unsigned Size
) {
695 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
698 if (ParseRelocatableExpression(Expr
))
701 Out
.EmitValue(Expr
, Size
);
703 if (Lexer
.is(asmtok::EndOfStatement
))
706 // FIXME: Improve diagnostic.
707 if (Lexer
.isNot(asmtok::Comma
))
708 return TokError("unexpected token in directive");
717 /// ParseDirectiveSpace
718 /// ::= .space expression [ , expression ]
719 bool AsmParser::ParseDirectiveSpace() {
721 if (ParseAbsoluteExpression(NumBytes
))
724 int64_t FillExpr
= 0;
725 bool HasFillExpr
= false;
726 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
727 if (Lexer
.isNot(asmtok::Comma
))
728 return TokError("unexpected token in '.space' directive");
731 if (ParseAbsoluteExpression(FillExpr
))
736 if (Lexer
.isNot(asmtok::EndOfStatement
))
737 return TokError("unexpected token in '.space' directive");
743 return TokError("invalid number of bytes in '.space' directive");
745 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
746 for (uint64_t i
= 0, e
= NumBytes
; i
!= e
; ++i
)
747 Out
.EmitValue(MCValue::get(FillExpr
), 1);
752 /// ParseDirectiveFill
753 /// ::= .fill expression , expression , expression
754 bool AsmParser::ParseDirectiveFill() {
756 if (ParseAbsoluteExpression(NumValues
))
759 if (Lexer
.isNot(asmtok::Comma
))
760 return TokError("unexpected token in '.fill' directive");
764 if (ParseAbsoluteExpression(FillSize
))
767 if (Lexer
.isNot(asmtok::Comma
))
768 return TokError("unexpected token in '.fill' directive");
772 if (ParseAbsoluteExpression(FillExpr
))
775 if (Lexer
.isNot(asmtok::EndOfStatement
))
776 return TokError("unexpected token in '.fill' directive");
780 if (FillSize
!= 1 && FillSize
!= 2 && FillSize
!= 4)
781 return TokError("invalid '.fill' size, expected 1, 2, or 4");
783 for (uint64_t i
= 0, e
= NumValues
; i
!= e
; ++i
)
784 Out
.EmitValue(MCValue::get(FillExpr
), FillSize
);
789 /// ParseDirectiveOrg
790 /// ::= .org expression [ , expression ]
791 bool AsmParser::ParseDirectiveOrg() {
793 if (ParseRelocatableExpression(Offset
))
796 // Parse optional fill expression.
797 int64_t FillExpr
= 0;
798 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
799 if (Lexer
.isNot(asmtok::Comma
))
800 return TokError("unexpected token in '.org' directive");
803 if (ParseAbsoluteExpression(FillExpr
))
806 if (Lexer
.isNot(asmtok::EndOfStatement
))
807 return TokError("unexpected token in '.org' directive");
812 // FIXME: Only limited forms of relocatable expressions are accepted here, it
813 // has to be relative to the current section.
814 Out
.EmitValueToOffset(Offset
, FillExpr
);
819 /// ParseDirectiveAlign
820 /// ::= {.align, ...} expression [ , expression [ , expression ]]
821 bool AsmParser::ParseDirectiveAlign(bool IsPow2
, unsigned ValueSize
) {
823 if (ParseAbsoluteExpression(Alignment
))
827 bool HasFillExpr
= false;
828 int64_t FillExpr
= 0;
829 int64_t MaxBytesToFill
= 0;
830 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
831 if (Lexer
.isNot(asmtok::Comma
))
832 return TokError("unexpected token in directive");
835 // The fill expression can be omitted while specifying a maximum number of
836 // alignment bytes, e.g:
838 if (Lexer
.isNot(asmtok::Comma
)) {
840 if (ParseAbsoluteExpression(FillExpr
))
844 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
845 if (Lexer
.isNot(asmtok::Comma
))
846 return TokError("unexpected token in directive");
849 MaxBytesLoc
= Lexer
.getLoc();
850 if (ParseAbsoluteExpression(MaxBytesToFill
))
853 if (Lexer
.isNot(asmtok::EndOfStatement
))
854 return TokError("unexpected token in directive");
861 // FIXME: Sometimes fill with nop.
865 // Compute alignment in bytes.
867 // FIXME: Diagnose overflow.
868 Alignment
= 1LL << Alignment
;
871 // Diagnose non-sensical max bytes to fill.
872 if (MaxBytesLoc
.isValid()) {
873 if (MaxBytesToFill
< 1) {
874 Warning(MaxBytesLoc
, "alignment directive can never be satisfied in this "
875 "many bytes, ignoring");
879 if (MaxBytesToFill
>= Alignment
) {
880 Warning(MaxBytesLoc
, "maximum bytes expression exceeds alignment and "
886 // FIXME: Target specific behavior about how the "extra" bytes are filled.
887 Out
.EmitValueToAlignment(Alignment
, FillExpr
, ValueSize
, MaxBytesToFill
);
892 /// ParseDirectiveSymbolAttribute
893 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
894 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr
) {
895 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
897 if (Lexer
.isNot(asmtok::Identifier
))
898 return TokError("expected identifier in directive");
900 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getCurStrVal());
903 // If this is use of an undefined symbol then mark it external.
904 if (!Sym
->getSection() && !Ctx
.GetSymbolValue(Sym
))
905 Sym
->setExternal(true);
907 Out
.EmitSymbolAttribute(Sym
, Attr
);
909 if (Lexer
.is(asmtok::EndOfStatement
))
912 if (Lexer
.isNot(asmtok::Comma
))
913 return TokError("unexpected token in directive");
922 /// ParseDirectiveDarwinSymbolDesc
923 /// ::= .desc identifier , expression
924 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
925 if (Lexer
.isNot(asmtok::Identifier
))
926 return TokError("expected identifier in directive");
928 // handle the identifier as the key symbol.
929 SMLoc IDLoc
= Lexer
.getLoc();
930 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getCurStrVal());
933 if (Lexer
.isNot(asmtok::Comma
))
934 return TokError("unexpected token in '.desc' directive");
937 SMLoc DescLoc
= Lexer
.getLoc();
939 if (ParseAbsoluteExpression(DescValue
))
942 if (Lexer
.isNot(asmtok::EndOfStatement
))
943 return TokError("unexpected token in '.desc' directive");
947 // Set the n_desc field of this Symbol to this DescValue
948 Out
.EmitSymbolDesc(Sym
, DescValue
);
953 /// ParseDirectiveComm
954 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
955 bool AsmParser::ParseDirectiveComm(bool IsLocal
) {
956 if (Lexer
.isNot(asmtok::Identifier
))
957 return TokError("expected identifier in directive");
959 // handle the identifier as the key symbol.
960 SMLoc IDLoc
= Lexer
.getLoc();
961 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getCurStrVal());
964 if (Lexer
.isNot(asmtok::Comma
))
965 return TokError("unexpected token in directive");
969 SMLoc SizeLoc
= Lexer
.getLoc();
970 if (ParseAbsoluteExpression(Size
))
973 int64_t Pow2Alignment
= 0;
974 SMLoc Pow2AlignmentLoc
;
975 if (Lexer
.is(asmtok::Comma
)) {
977 Pow2AlignmentLoc
= Lexer
.getLoc();
978 if (ParseAbsoluteExpression(Pow2Alignment
))
982 if (Lexer
.isNot(asmtok::EndOfStatement
))
983 return TokError("unexpected token in '.comm' or '.lcomm' directive");
987 // NOTE: a size of zero for a .comm should create a undefined symbol
988 // but a size of .lcomm creates a bss symbol of size zero.
990 return Error(SizeLoc
, "invalid '.comm' or '.lcomm' directive size, can't "
991 "be less than zero");
993 // NOTE: The alignment in the directive is a power of 2 value, the assember
994 // may internally end up wanting an alignment in bytes.
995 // FIXME: Diagnose overflow.
996 if (Pow2Alignment
< 0)
997 return Error(Pow2AlignmentLoc
, "invalid '.comm' or '.lcomm' directive "
998 "alignment, can't be less than zero");
1000 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1001 if (Sym
->getSection() || Ctx
.GetSymbolValue(Sym
))
1002 return Error(IDLoc
, "invalid symbol redefinition");
1004 // Create the Symbol as a common or local common with Size and Pow2Alignment
1005 Out
.EmitCommonSymbol(Sym
, Size
, Pow2Alignment
, IsLocal
);
1010 /// ParseDirectiveDarwinZerofill
1011 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1012 /// , align_expression ]]
1013 bool AsmParser::ParseDirectiveDarwinZerofill() {
1014 if (Lexer
.isNot(asmtok::Identifier
))
1015 return TokError("expected segment name after '.zerofill' directive");
1016 std::string Section
= Lexer
.getCurStrVal();
1019 if (Lexer
.isNot(asmtok::Comma
))
1020 return TokError("unexpected token in directive");
1024 if (Lexer
.isNot(asmtok::Identifier
))
1025 return TokError("expected section name after comma in '.zerofill' "
1027 Section
+= Lexer
.getCurStrVal();
1030 // FIXME: we will need to tell GetSection() that this is to be created with or
1031 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1032 // below could be done but for now it is not as EmitZerofill() does not know
1033 // how to deal with a section type in the section name like
1034 // ParseDirectiveDarwinSection() allows.
1036 // Section += "zerofill";
1038 // If this is the end of the line all that was wanted was to create the
1039 // the section but with no symbol.
1040 if (Lexer
.is(asmtok::EndOfStatement
)) {
1041 // Create the zerofill section but no symbol
1042 Out
.EmitZerofill(Ctx
.GetSection(Section
.c_str()));
1046 if (Lexer
.isNot(asmtok::Comma
))
1047 return TokError("unexpected token in directive");
1050 if (Lexer
.isNot(asmtok::Identifier
))
1051 return TokError("expected identifier in directive");
1053 // handle the identifier as the key symbol.
1054 SMLoc IDLoc
= Lexer
.getLoc();
1055 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getCurStrVal());
1058 if (Lexer
.isNot(asmtok::Comma
))
1059 return TokError("unexpected token in directive");
1063 SMLoc SizeLoc
= Lexer
.getLoc();
1064 if (ParseAbsoluteExpression(Size
))
1067 int64_t Pow2Alignment
= 0;
1068 SMLoc Pow2AlignmentLoc
;
1069 if (Lexer
.is(asmtok::Comma
)) {
1071 Pow2AlignmentLoc
= Lexer
.getLoc();
1072 if (ParseAbsoluteExpression(Pow2Alignment
))
1076 if (Lexer
.isNot(asmtok::EndOfStatement
))
1077 return TokError("unexpected token in '.zerofill' directive");
1082 return Error(SizeLoc
, "invalid '.zerofill' directive size, can't be less "
1085 // NOTE: The alignment in the directive is a power of 2 value, the assember
1086 // may internally end up wanting an alignment in bytes.
1087 // FIXME: Diagnose overflow.
1088 if (Pow2Alignment
< 0)
1089 return Error(Pow2AlignmentLoc
, "invalid '.zerofill' directive alignment, "
1090 "can't be less than zero");
1092 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1093 if (Sym
->getSection() || Ctx
.GetSymbolValue(Sym
))
1094 return Error(IDLoc
, "invalid symbol redefinition");
1096 // Create the zerofill Symbol with Size and Pow2Alignment
1097 Out
.EmitZerofill(Ctx
.GetSection(Section
.c_str()), Sym
, Size
, Pow2Alignment
);
1102 /// ParseDirectiveDarwinSubsectionsViaSymbols
1103 /// ::= .subsections_via_symbols
1104 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1105 if (Lexer
.isNot(asmtok::EndOfStatement
))
1106 return TokError("unexpected token in '.subsections_via_symbols' directive");
1110 Out
.SubsectionsViaSymbols();
1115 /// ParseDirectiveAbort
1116 /// ::= .abort [ "abort_string" ]
1117 bool AsmParser::ParseDirectiveAbort() {
1118 const char *Str
= NULL
;
1119 if (Lexer
.isNot(asmtok::EndOfStatement
)) {
1120 if (Lexer
.isNot(asmtok::String
))
1121 return TokError("expected string in '.abort' directive");
1123 Str
= Lexer
.getCurStrVal();
1128 if (Lexer
.isNot(asmtok::EndOfStatement
))
1129 return TokError("unexpected token in '.abort' directive");
1133 Out
.AbortAssembly(Str
);
1138 /// ParseDirectiveLsym
1139 /// ::= .lsym identifier , expression
1140 bool AsmParser::ParseDirectiveDarwinLsym() {
1141 if (Lexer
.isNot(asmtok::Identifier
))
1142 return TokError("expected identifier in directive");
1144 // handle the identifier as the key symbol.
1145 SMLoc IDLoc
= Lexer
.getLoc();
1146 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Lexer
.getCurStrVal());
1149 if (Lexer
.isNot(asmtok::Comma
))
1150 return TokError("unexpected token in '.lsym' directive");
1154 if (ParseRelocatableExpression(Expr
))
1157 if (Lexer
.isNot(asmtok::EndOfStatement
))
1158 return TokError("unexpected token in '.lsym' directive");
1162 // Create the Sym with the value of the Expr
1163 Out
.EmitLocalSymbol(Sym
, Expr
);
1168 /// ParseDirectiveInclude
1169 /// ::= .include "filename"
1170 bool AsmParser::ParseDirectiveInclude() {
1171 if (Lexer
.isNot(asmtok::String
))
1172 return TokError("expected string in '.include' directive");
1174 std::string Filename
= Lexer
.getCurStrVal();
1175 SMLoc IncludeLoc
= Lexer
.getLoc();
1178 if (Lexer
.isNot(asmtok::EndOfStatement
))
1179 return TokError("unexpected token in '.include' directive");
1181 // Strip the quotes.
1182 Filename
= Filename
.substr(1, Filename
.size()-2);
1184 // Attempt to switch the lexer to the included file before consuming the end
1185 // of statement to avoid losing it when we switch.
1186 if (Lexer
.EnterIncludeFile(Filename
)) {
1187 Lexer
.PrintMessage(IncludeLoc
,
1188 "Could not find include file '" + Filename
+ "'",
1196 /// ParseDirectiveDarwinDumpOrLoad
1197 /// ::= ( .dump | .load ) "filename"
1198 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(bool IsDump
) {
1201 if (Lexer
.isNot(asmtok::String
))
1202 return TokError("expected string in '.dump' or '.load' directive");
1204 Str
= Lexer
.getCurStrVal();
1208 if (Lexer
.isNot(asmtok::EndOfStatement
))
1209 return TokError("unexpected token in '.dump' or '.load' directive");
1214 Out
.DumpSymbolsandMacros(Str
);
1216 Out
.LoadSymbolsandMacros(Str
);