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 while (Lexer.isNot(asmtok::Eof))
50 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
51 void AsmParser::EatToEndOfStatement() {
52 while (Lexer.isNot(asmtok::EndOfStatement) &&
53 Lexer.isNot(asmtok::Eof))
57 if (Lexer.is(asmtok::EndOfStatement))
62 /// ParseParenExpr - Parse a paren expression and return it.
63 /// NOTE: This assumes the leading '(' has already been consumed.
65 /// parenexpr ::= expr)
67 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
68 if (ParseExpression(Res)) return true;
69 if (Lexer.isNot(asmtok::RParen))
70 return TokError("expected ')' in parentheses expression");
75 /// ParsePrimaryExpr - Parse a primary expression and return it.
76 /// primaryexpr ::= (parenexpr
77 /// primaryexpr ::= symbol
78 /// primaryexpr ::= number
79 /// primaryexpr ::= ~,+,- primaryexpr
80 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
81 switch (Lexer.getKind()) {
83 return TokError("unknown token in expression");
85 Lexer.Lex(); // Eat the operator.
86 if (ParsePrimaryExpr(Res))
88 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
90 case asmtok::Identifier: {
91 // This is a label, this should be parsed as part of an expression, to
92 // handle things like LFOO+4.
93 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
95 // If this is use of an undefined symbol then mark it external.
96 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
97 Sym->setExternal(true);
99 Res = new AsmSymbolRefExpr(Sym);
100 Lexer.Lex(); // Eat identifier.
104 Res = new AsmConstantExpr(Lexer.getCurIntVal());
105 Lexer.Lex(); // Eat identifier.
108 Lexer.Lex(); // Eat the '('.
109 return ParseParenExpr(Res);
111 Lexer.Lex(); // Eat the operator.
112 if (ParsePrimaryExpr(Res))
114 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
117 Lexer.Lex(); // Eat the operator.
118 if (ParsePrimaryExpr(Res))
120 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
123 Lexer.Lex(); // Eat the operator.
124 if (ParsePrimaryExpr(Res))
126 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
131 /// ParseExpression - Parse an expression and return it.
133 /// expr ::= expr +,- expr -> lowest.
134 /// expr ::= expr |,^,&,! expr -> middle.
135 /// expr ::= expr *,/,%,<<,>> expr -> highest.
136 /// expr ::= primaryexpr
138 bool AsmParser::ParseExpression(AsmExpr *&Res) {
140 return ParsePrimaryExpr(Res) ||
141 ParseBinOpRHS(1, Res);
144 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
147 SMLoc StartLoc = Lexer.getLoc();
148 if (ParseExpression(Expr))
151 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
152 return Error(StartLoc, "expected absolute expression");
157 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
160 SMLoc StartLoc = Lexer.getLoc();
161 if (ParseExpression(Expr))
164 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
165 return Error(StartLoc, "expected relocatable expression");
170 bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
173 SMLoc StartLoc = Lexer.getLoc();
174 if (ParseParenExpr(Expr))
177 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
178 return Error(StartLoc, "expected relocatable expression");
183 static unsigned getBinOpPrecedence(asmtok::TokKind K,
184 AsmBinaryExpr::Opcode &Kind) {
186 default: return 0; // not a binop.
188 // Lowest Precedence: &&, ||
190 Kind = AsmBinaryExpr::LAnd;
192 case asmtok::PipePipe:
193 Kind = AsmBinaryExpr::LOr;
196 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
198 Kind = AsmBinaryExpr::Add;
201 Kind = AsmBinaryExpr::Sub;
203 case asmtok::EqualEqual:
204 Kind = AsmBinaryExpr::EQ;
206 case asmtok::ExclaimEqual:
207 case asmtok::LessGreater:
208 Kind = AsmBinaryExpr::NE;
211 Kind = AsmBinaryExpr::LT;
213 case asmtok::LessEqual:
214 Kind = AsmBinaryExpr::LTE;
216 case asmtok::Greater:
217 Kind = AsmBinaryExpr::GT;
219 case asmtok::GreaterEqual:
220 Kind = AsmBinaryExpr::GTE;
223 // Intermediate Precedence: |, &, ^
225 // FIXME: gas seems to support '!' as an infix operator?
227 Kind = AsmBinaryExpr::Or;
230 Kind = AsmBinaryExpr::Xor;
233 Kind = AsmBinaryExpr::And;
236 // Highest Precedence: *, /, %, <<, >>
238 Kind = AsmBinaryExpr::Mul;
241 Kind = AsmBinaryExpr::Div;
243 case asmtok::Percent:
244 Kind = AsmBinaryExpr::Mod;
246 case asmtok::LessLess:
247 Kind = AsmBinaryExpr::Shl;
249 case asmtok::GreaterGreater:
250 Kind = AsmBinaryExpr::Shr;
256 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
257 /// Res contains the LHS of the expression on input.
258 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
260 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
261 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
263 // If the next token is lower precedence than we are allowed to eat, return
264 // successfully with what we ate already.
265 if (TokPrec < Precedence)
270 // Eat the next primary expression.
272 if (ParsePrimaryExpr(RHS)) return true;
274 // If BinOp binds less tightly with RHS than the operator after RHS, let
275 // the pending operator take RHS as its LHS.
276 AsmBinaryExpr::Opcode Dummy;
277 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
278 if (TokPrec < NextTokPrec) {
279 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
282 // Merge LHS and RHS according to operator.
283 Res = new AsmBinaryExpr(Kind, Res, RHS);
291 /// ::= EndOfStatement
292 /// ::= Label* Directive ...Operands... EndOfStatement
293 /// ::= Label* Identifier OperandList* EndOfStatement
294 bool AsmParser::ParseStatement() {
295 switch (Lexer.getKind()) {
297 return TokError("unexpected token at start of statement");
298 case asmtok::EndOfStatement:
301 case asmtok::Identifier:
303 // TODO: Recurse on local labels etc.
306 // If we have an identifier, handle it as the key symbol.
307 SMLoc IDLoc = Lexer.getLoc();
308 const char *IDVal = Lexer.getCurStrVal();
310 // Consume the identifier, see what is after it.
311 switch (Lexer.Lex()) {
312 case asmtok::Colon: {
313 // identifier ':' -> Label.
316 // Diagnose attempt to use a variable as a label.
318 // FIXME: Diagnostics. Note the location of the definition as a label.
319 // FIXME: This doesn't diagnose assignment to a symbol which has been
320 // implicitly marked as external.
321 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
322 if (Sym->getSection())
323 return Error(IDLoc, "invalid symbol redefinition");
324 if (Ctx.GetSymbolValue(Sym))
325 return Error(IDLoc, "symbol already used as assembler variable");
327 // Since we saw a label, create a symbol and emit it.
328 // FIXME: If the label starts with L it is an assembler temporary label.
329 // Why does the client of this api need to know this?
332 return ParseStatement();
336 // identifier '=' ... -> assignment statement
339 return ParseAssignment(IDVal, false);
341 default: // Normal instruction or directive.
345 // Otherwise, we have a normal instruction or directive.
346 if (IDVal[0] == '.') {
347 // FIXME: This should be driven based on a hash lookup and callback.
348 if (!strcmp(IDVal, ".section"))
349 return ParseDirectiveDarwinSection();
350 if (!strcmp(IDVal, ".text"))
351 // FIXME: This changes behavior based on the -static flag to the
353 return ParseDirectiveSectionSwitch("__TEXT,__text",
354 "regular,pure_instructions");
355 if (!strcmp(IDVal, ".const"))
356 return ParseDirectiveSectionSwitch("__TEXT,__const");
357 if (!strcmp(IDVal, ".static_const"))
358 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
359 if (!strcmp(IDVal, ".cstring"))
360 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
362 if (!strcmp(IDVal, ".literal4"))
363 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
364 if (!strcmp(IDVal, ".literal8"))
365 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
366 if (!strcmp(IDVal, ".literal16"))
367 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
369 if (!strcmp(IDVal, ".constructor"))
370 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
371 if (!strcmp(IDVal, ".destructor"))
372 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
373 if (!strcmp(IDVal, ".fvmlib_init0"))
374 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
375 if (!strcmp(IDVal, ".fvmlib_init1"))
376 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
377 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
378 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
379 "self_modifying_code+pure_instructions,5");
380 // FIXME: .picsymbol_stub on PPC.
381 if (!strcmp(IDVal, ".data"))
382 return ParseDirectiveSectionSwitch("__DATA,__data");
383 if (!strcmp(IDVal, ".static_data"))
384 return ParseDirectiveSectionSwitch("__DATA,__static_data");
385 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
386 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
387 "non_lazy_symbol_pointers");
388 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
389 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
390 "lazy_symbol_pointers");
391 if (!strcmp(IDVal, ".dyld"))
392 return ParseDirectiveSectionSwitch("__DATA,__dyld");
393 if (!strcmp(IDVal, ".mod_init_func"))
394 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
396 if (!strcmp(IDVal, ".mod_term_func"))
397 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
399 if (!strcmp(IDVal, ".const_data"))
400 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
403 // FIXME: Verify attributes on sections.
404 if (!strcmp(IDVal, ".objc_class"))
405 return ParseDirectiveSectionSwitch("__OBJC,__class");
406 if (!strcmp(IDVal, ".objc_meta_class"))
407 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
408 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
409 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
410 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
411 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
412 if (!strcmp(IDVal, ".objc_protocol"))
413 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
414 if (!strcmp(IDVal, ".objc_string_object"))
415 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
416 if (!strcmp(IDVal, ".objc_cls_meth"))
417 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
418 if (!strcmp(IDVal, ".objc_inst_meth"))
419 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
420 if (!strcmp(IDVal, ".objc_cls_refs"))
421 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
422 if (!strcmp(IDVal, ".objc_message_refs"))
423 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
424 if (!strcmp(IDVal, ".objc_symbols"))
425 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
426 if (!strcmp(IDVal, ".objc_category"))
427 return ParseDirectiveSectionSwitch("__OBJC,__category");
428 if (!strcmp(IDVal, ".objc_class_vars"))
429 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
430 if (!strcmp(IDVal, ".objc_instance_vars"))
431 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
432 if (!strcmp(IDVal, ".objc_module_info"))
433 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
434 if (!strcmp(IDVal, ".objc_class_names"))
435 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
436 if (!strcmp(IDVal, ".objc_meth_var_types"))
437 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
438 if (!strcmp(IDVal, ".objc_meth_var_names"))
439 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
440 if (!strcmp(IDVal, ".objc_selector_strs"))
441 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
443 // Assembler features
444 if (!strcmp(IDVal, ".set"))
445 return ParseDirectiveSet();
449 if (!strcmp(IDVal, ".ascii"))
450 return ParseDirectiveAscii(false);
451 if (!strcmp(IDVal, ".asciz"))
452 return ParseDirectiveAscii(true);
454 // FIXME: Target hooks for size? Also for "word", "hword".
455 if (!strcmp(IDVal, ".byte"))
456 return ParseDirectiveValue(1);
457 if (!strcmp(IDVal, ".short"))
458 return ParseDirectiveValue(2);
459 if (!strcmp(IDVal, ".long"))
460 return ParseDirectiveValue(4);
461 if (!strcmp(IDVal, ".quad"))
462 return ParseDirectiveValue(8);
464 // FIXME: Target hooks for IsPow2.
465 if (!strcmp(IDVal, ".align"))
466 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
467 if (!strcmp(IDVal, ".align32"))
468 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
469 if (!strcmp(IDVal, ".balign"))
470 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
471 if (!strcmp(IDVal, ".balignw"))
472 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
473 if (!strcmp(IDVal, ".balignl"))
474 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
475 if (!strcmp(IDVal, ".p2align"))
476 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
477 if (!strcmp(IDVal, ".p2alignw"))
478 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
479 if (!strcmp(IDVal, ".p2alignl"))
480 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
482 if (!strcmp(IDVal, ".org"))
483 return ParseDirectiveOrg();
485 if (!strcmp(IDVal, ".fill"))
486 return ParseDirectiveFill();
487 if (!strcmp(IDVal, ".space"))
488 return ParseDirectiveSpace();
490 // Symbol attribute directives
491 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
492 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
493 if (!strcmp(IDVal, ".hidden"))
494 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
495 if (!strcmp(IDVal, ".indirect_symbol"))
496 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
497 if (!strcmp(IDVal, ".internal"))
498 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
499 if (!strcmp(IDVal, ".lazy_reference"))
500 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
501 if (!strcmp(IDVal, ".no_dead_strip"))
502 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
503 if (!strcmp(IDVal, ".private_extern"))
504 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
505 if (!strcmp(IDVal, ".protected"))
506 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
507 if (!strcmp(IDVal, ".reference"))
508 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
509 if (!strcmp(IDVal, ".weak"))
510 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
511 if (!strcmp(IDVal, ".weak_definition"))
512 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
513 if (!strcmp(IDVal, ".weak_reference"))
514 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
516 Warning(IDLoc, "ignoring directive for now");
517 EatToEndOfStatement();
522 if (ParseX86InstOperands(IDVal, Inst))
525 if (Lexer.isNot(asmtok::EndOfStatement))
526 return TokError("unexpected token in argument list");
528 // Eat the end of statement marker.
531 // Instruction is good, process it.
532 Out.EmitInstruction(Inst);
534 // Skip to end of line for now.
538 bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
539 // FIXME: Use better location, we should use proper tokens.
540 SMLoc EqualLoc = Lexer.getLoc();
543 if (ParseRelocatableExpression(Value))
546 if (Lexer.isNot(asmtok::EndOfStatement))
547 return TokError("unexpected token in assignment");
549 // Eat the end of statement marker.
552 // Diagnose assignment to a label.
554 // FIXME: Diagnostics. Note the location of the definition as a label.
555 // FIXME: This doesn't diagnose assignment to a symbol which has been
556 // implicitly marked as external.
557 // FIXME: Handle '.'.
558 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
559 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
560 if (Sym->getSection())
561 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
562 if (Sym->isExternal())
563 return Error(EqualLoc, "invalid assignment to external symbol");
565 // Do the assignment.
566 Out.EmitAssignment(Sym, Value, IsDotSet);
571 /// ParseDirectiveSet:
572 /// ::= .set identifier ',' expression
573 bool AsmParser::ParseDirectiveSet() {
574 if (Lexer.isNot(asmtok::Identifier))
575 return TokError("expected identifier after '.set' directive");
577 const char *Name = Lexer.getCurStrVal();
579 if (Lexer.Lex() != asmtok::Comma)
580 return TokError("unexpected token in '.set'");
583 return ParseAssignment(Name, true);
586 /// ParseDirectiveSection:
587 /// ::= .section identifier (',' identifier)*
588 /// FIXME: This should actually parse out the segment, section, attributes and
589 /// sizeof_stub fields.
590 bool AsmParser::ParseDirectiveDarwinSection() {
591 if (Lexer.isNot(asmtok::Identifier))
592 return TokError("expected identifier after '.section' directive");
594 std::string Section = Lexer.getCurStrVal();
597 // Accept a comma separated list of modifiers.
598 while (Lexer.is(asmtok::Comma)) {
601 if (Lexer.isNot(asmtok::Identifier))
602 return TokError("expected identifier in '.section' directive");
604 Section += Lexer.getCurStrVal();
608 if (Lexer.isNot(asmtok::EndOfStatement))
609 return TokError("unexpected token in '.section' directive");
612 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
616 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
617 const char *Directives) {
618 if (Lexer.isNot(asmtok::EndOfStatement))
619 return TokError("unexpected token in section switching directive");
622 std::string SectionStr = Section;
623 if (Directives && Directives[0]) {
625 SectionStr += Directives;
628 Out.SwitchSection(Ctx.GetSection(Section));
632 /// ParseDirectiveAscii:
633 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
634 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
635 if (Lexer.isNot(asmtok::EndOfStatement)) {
637 if (Lexer.isNot(asmtok::String))
638 return TokError("expected string in '.ascii' or '.asciz' directive");
640 // FIXME: This shouldn't use a const char* + strlen, the string could have
642 // FIXME: Should have accessor for getting string contents.
643 const char *Str = Lexer.getCurStrVal();
644 Out.EmitBytes(Str + 1, strlen(Str) - 2);
646 Out.EmitBytes("\0", 1);
650 if (Lexer.is(asmtok::EndOfStatement))
653 if (Lexer.isNot(asmtok::Comma))
654 return TokError("unexpected token in '.ascii' or '.asciz' directive");
663 /// ParseDirectiveValue
664 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
665 bool AsmParser::ParseDirectiveValue(unsigned Size) {
666 if (Lexer.isNot(asmtok::EndOfStatement)) {
669 if (ParseRelocatableExpression(Expr))
672 Out.EmitValue(Expr, Size);
674 if (Lexer.is(asmtok::EndOfStatement))
677 // FIXME: Improve diagnostic.
678 if (Lexer.isNot(asmtok::Comma))
679 return TokError("unexpected token in directive");
688 /// ParseDirectiveSpace
689 /// ::= .space expression [ , expression ]
690 bool AsmParser::ParseDirectiveSpace() {
692 if (ParseAbsoluteExpression(NumBytes))
695 int64_t FillExpr = 0;
696 bool HasFillExpr = false;
697 if (Lexer.isNot(asmtok::EndOfStatement)) {
698 if (Lexer.isNot(asmtok::Comma))
699 return TokError("unexpected token in '.space' directive");
702 if (ParseAbsoluteExpression(FillExpr))
707 if (Lexer.isNot(asmtok::EndOfStatement))
708 return TokError("unexpected token in '.space' directive");
714 return TokError("invalid number of bytes in '.space' directive");
716 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
717 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
718 Out.EmitValue(MCValue::get(FillExpr), 1);
723 /// ParseDirectiveFill
724 /// ::= .fill expression , expression , expression
725 bool AsmParser::ParseDirectiveFill() {
727 if (ParseAbsoluteExpression(NumValues))
730 if (Lexer.isNot(asmtok::Comma))
731 return TokError("unexpected token in '.fill' directive");
735 if (ParseAbsoluteExpression(FillSize))
738 if (Lexer.isNot(asmtok::Comma))
739 return TokError("unexpected token in '.fill' directive");
743 if (ParseAbsoluteExpression(FillExpr))
746 if (Lexer.isNot(asmtok::EndOfStatement))
747 return TokError("unexpected token in '.fill' directive");
751 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
752 return TokError("invalid '.fill' size, expected 1, 2, or 4");
754 for (uint64_t i = 0, e = NumValues; i != e; ++i)
755 Out.EmitValue(MCValue::get(FillExpr), FillSize);
760 /// ParseDirectiveOrg
761 /// ::= .org expression [ , expression ]
762 bool AsmParser::ParseDirectiveOrg() {
764 if (ParseRelocatableExpression(Offset))
767 // Parse optional fill expression.
768 int64_t FillExpr = 0;
769 if (Lexer.isNot(asmtok::EndOfStatement)) {
770 if (Lexer.isNot(asmtok::Comma))
771 return TokError("unexpected token in '.org' directive");
774 if (ParseAbsoluteExpression(FillExpr))
777 if (Lexer.isNot(asmtok::EndOfStatement))
778 return TokError("unexpected token in '.org' directive");
783 // FIXME: Only limited forms of relocatable expressions are accepted here, it
784 // has to be relative to the current section.
785 Out.EmitValueToOffset(Offset, FillExpr);
790 /// ParseDirectiveAlign
791 /// ::= {.align, ...} expression [ , expression [ , expression ]]
792 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
794 if (ParseAbsoluteExpression(Alignment))
798 bool HasFillExpr = false;
799 int64_t FillExpr = 0;
800 int64_t MaxBytesToFill = 0;
801 if (Lexer.isNot(asmtok::EndOfStatement)) {
802 if (Lexer.isNot(asmtok::Comma))
803 return TokError("unexpected token in directive");
806 // The fill expression can be omitted while specifying a maximum number of
807 // alignment bytes, e.g:
809 if (Lexer.isNot(asmtok::Comma)) {
811 if (ParseAbsoluteExpression(FillExpr))
815 if (Lexer.isNot(asmtok::EndOfStatement)) {
816 if (Lexer.isNot(asmtok::Comma))
817 return TokError("unexpected token in directive");
820 MaxBytesLoc = Lexer.getLoc();
821 if (ParseAbsoluteExpression(MaxBytesToFill))
824 if (Lexer.isNot(asmtok::EndOfStatement))
825 return TokError("unexpected token in directive");
832 // FIXME: Sometimes fill with nop.
836 // Compute alignment in bytes.
838 // FIXME: Diagnose overflow.
839 Alignment = 1 << Alignment;
842 // Diagnose non-sensical max bytes to fill.
843 if (MaxBytesLoc.isValid()) {
844 if (MaxBytesToFill < 1) {
845 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
846 "many bytes, ignoring");
850 if (MaxBytesToFill >= Alignment) {
851 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
857 // FIXME: Target specific behavior about how the "extra" bytes are filled.
858 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
863 /// ParseDirectiveSymbolAttribute
864 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
865 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
866 if (Lexer.isNot(asmtok::EndOfStatement)) {
868 if (Lexer.isNot(asmtok::Identifier))
869 return TokError("expected identifier in directive");
871 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
874 // If this is use of an undefined symbol then mark it external.
875 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
876 Sym->setExternal(true);
878 Out.EmitSymbolAttribute(Sym, Attr);
880 if (Lexer.is(asmtok::EndOfStatement))
883 if (Lexer.isNot(asmtok::Comma))
884 return TokError("unexpected token in directive");