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 if (ParseExpression(Expr))
150 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
151 return TokError("expected absolute expression");
156 static unsigned getBinOpPrecedence(asmtok::TokKind K,
157 AsmBinaryExpr::Opcode &Kind) {
159 default: return 0; // not a binop.
161 // Lowest Precedence: &&, ||
163 Kind = AsmBinaryExpr::LAnd;
165 case asmtok::PipePipe:
166 Kind = AsmBinaryExpr::LOr;
169 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
171 Kind = AsmBinaryExpr::Add;
174 Kind = AsmBinaryExpr::Sub;
176 case asmtok::EqualEqual:
177 Kind = AsmBinaryExpr::EQ;
179 case asmtok::ExclaimEqual:
180 case asmtok::LessGreater:
181 Kind = AsmBinaryExpr::NE;
184 Kind = AsmBinaryExpr::LT;
186 case asmtok::LessEqual:
187 Kind = AsmBinaryExpr::LTE;
189 case asmtok::Greater:
190 Kind = AsmBinaryExpr::GT;
192 case asmtok::GreaterEqual:
193 Kind = AsmBinaryExpr::GTE;
196 // Intermediate Precedence: |, &, ^
198 // FIXME: gas seems to support '!' as an infix operator?
200 Kind = AsmBinaryExpr::Or;
203 Kind = AsmBinaryExpr::Xor;
206 Kind = AsmBinaryExpr::And;
209 // Highest Precedence: *, /, %, <<, >>
211 Kind = AsmBinaryExpr::Mul;
214 Kind = AsmBinaryExpr::Div;
216 case asmtok::Percent:
217 Kind = AsmBinaryExpr::Mod;
219 case asmtok::LessLess:
220 Kind = AsmBinaryExpr::Shl;
222 case asmtok::GreaterGreater:
223 Kind = AsmBinaryExpr::Shr;
229 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
230 /// Res contains the LHS of the expression on input.
231 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
233 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
234 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
236 // If the next token is lower precedence than we are allowed to eat, return
237 // successfully with what we ate already.
238 if (TokPrec < Precedence)
243 // Eat the next primary expression.
245 if (ParsePrimaryExpr(RHS)) return true;
247 // If BinOp binds less tightly with RHS than the operator after RHS, let
248 // the pending operator take RHS as its LHS.
249 AsmBinaryExpr::Opcode Dummy;
250 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
251 if (TokPrec < NextTokPrec) {
252 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
255 // Merge LHS and RHS according to operator.
256 Res = new AsmBinaryExpr(Kind, Res, RHS);
264 /// ::= EndOfStatement
265 /// ::= Label* Directive ...Operands... EndOfStatement
266 /// ::= Label* Identifier OperandList* EndOfStatement
267 bool AsmParser::ParseStatement() {
268 switch (Lexer.getKind()) {
270 return TokError("unexpected token at start of statement");
271 case asmtok::EndOfStatement:
274 case asmtok::Identifier:
276 // TODO: Recurse on local labels etc.
279 // If we have an identifier, handle it as the key symbol.
280 SMLoc IDLoc = Lexer.getLoc();
281 const char *IDVal = Lexer.getCurStrVal();
283 // Consume the identifier, see what is after it.
284 switch (Lexer.Lex()) {
285 case asmtok::Colon: {
286 // identifier ':' -> Label.
289 // Diagnose attempt to use a variable as a label.
291 // FIXME: Diagnostics. Note the location of the definition as a label.
292 // FIXME: This doesn't diagnose assignment to a symbol which has been
293 // implicitly marked as external.
294 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
295 if (Sym->getSection())
296 return Error(IDLoc, "invalid symbol redefinition");
297 if (Ctx.GetSymbolValue(Sym))
298 return Error(IDLoc, "symbol already used as assembler variable");
300 // Since we saw a label, create a symbol and emit it.
301 // FIXME: If the label starts with L it is an assembler temporary label.
302 // Why does the client of this api need to know this?
305 return ParseStatement();
309 // identifier '=' ... -> assignment statement
312 return ParseAssignment(IDVal, false);
314 default: // Normal instruction or directive.
318 // Otherwise, we have a normal instruction or directive.
319 if (IDVal[0] == '.') {
320 // FIXME: This should be driven based on a hash lookup and callback.
321 if (!strcmp(IDVal, ".section"))
322 return ParseDirectiveDarwinSection();
323 if (!strcmp(IDVal, ".text"))
324 // FIXME: This changes behavior based on the -static flag to the
326 return ParseDirectiveSectionSwitch("__TEXT,__text",
327 "regular,pure_instructions");
328 if (!strcmp(IDVal, ".const"))
329 return ParseDirectiveSectionSwitch("__TEXT,__const");
330 if (!strcmp(IDVal, ".static_const"))
331 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
332 if (!strcmp(IDVal, ".cstring"))
333 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
335 if (!strcmp(IDVal, ".literal4"))
336 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
337 if (!strcmp(IDVal, ".literal8"))
338 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
339 if (!strcmp(IDVal, ".literal16"))
340 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
342 if (!strcmp(IDVal, ".constructor"))
343 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
344 if (!strcmp(IDVal, ".destructor"))
345 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
346 if (!strcmp(IDVal, ".fvmlib_init0"))
347 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
348 if (!strcmp(IDVal, ".fvmlib_init1"))
349 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
350 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
351 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
352 "self_modifying_code+pure_instructions,5");
353 // FIXME: .picsymbol_stub on PPC.
354 if (!strcmp(IDVal, ".data"))
355 return ParseDirectiveSectionSwitch("__DATA,__data");
356 if (!strcmp(IDVal, ".static_data"))
357 return ParseDirectiveSectionSwitch("__DATA,__static_data");
358 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
359 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
360 "non_lazy_symbol_pointers");
361 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
362 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
363 "lazy_symbol_pointers");
364 if (!strcmp(IDVal, ".dyld"))
365 return ParseDirectiveSectionSwitch("__DATA,__dyld");
366 if (!strcmp(IDVal, ".mod_init_func"))
367 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
369 if (!strcmp(IDVal, ".mod_term_func"))
370 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
372 if (!strcmp(IDVal, ".const_data"))
373 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
376 // FIXME: Verify attributes on sections.
377 if (!strcmp(IDVal, ".objc_class"))
378 return ParseDirectiveSectionSwitch("__OBJC,__class");
379 if (!strcmp(IDVal, ".objc_meta_class"))
380 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
381 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
382 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
383 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
384 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
385 if (!strcmp(IDVal, ".objc_protocol"))
386 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
387 if (!strcmp(IDVal, ".objc_string_object"))
388 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
389 if (!strcmp(IDVal, ".objc_cls_meth"))
390 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
391 if (!strcmp(IDVal, ".objc_inst_meth"))
392 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
393 if (!strcmp(IDVal, ".objc_cls_refs"))
394 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
395 if (!strcmp(IDVal, ".objc_message_refs"))
396 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
397 if (!strcmp(IDVal, ".objc_symbols"))
398 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
399 if (!strcmp(IDVal, ".objc_category"))
400 return ParseDirectiveSectionSwitch("__OBJC,__category");
401 if (!strcmp(IDVal, ".objc_class_vars"))
402 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
403 if (!strcmp(IDVal, ".objc_instance_vars"))
404 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
405 if (!strcmp(IDVal, ".objc_module_info"))
406 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
407 if (!strcmp(IDVal, ".objc_class_names"))
408 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
409 if (!strcmp(IDVal, ".objc_meth_var_types"))
410 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
411 if (!strcmp(IDVal, ".objc_meth_var_names"))
412 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
413 if (!strcmp(IDVal, ".objc_selector_strs"))
414 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
416 // Assembler features
417 if (!strcmp(IDVal, ".set"))
418 return ParseDirectiveSet();
422 if (!strcmp(IDVal, ".ascii"))
423 return ParseDirectiveAscii(false);
424 if (!strcmp(IDVal, ".asciz"))
425 return ParseDirectiveAscii(true);
427 // FIXME: Target hooks for size? Also for "word", "hword".
428 if (!strcmp(IDVal, ".byte"))
429 return ParseDirectiveValue(1);
430 if (!strcmp(IDVal, ".short"))
431 return ParseDirectiveValue(2);
432 if (!strcmp(IDVal, ".long"))
433 return ParseDirectiveValue(4);
434 if (!strcmp(IDVal, ".quad"))
435 return ParseDirectiveValue(8);
437 // FIXME: Target hooks for IsPow2.
438 if (!strcmp(IDVal, ".align"))
439 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
440 if (!strcmp(IDVal, ".align32"))
441 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
442 if (!strcmp(IDVal, ".balign"))
443 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
444 if (!strcmp(IDVal, ".balignw"))
445 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
446 if (!strcmp(IDVal, ".balignl"))
447 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
448 if (!strcmp(IDVal, ".p2align"))
449 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
450 if (!strcmp(IDVal, ".p2alignw"))
451 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
452 if (!strcmp(IDVal, ".p2alignl"))
453 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
455 if (!strcmp(IDVal, ".org"))
456 return ParseDirectiveOrg();
458 if (!strcmp(IDVal, ".fill"))
459 return ParseDirectiveFill();
460 if (!strcmp(IDVal, ".space"))
461 return ParseDirectiveSpace();
463 // Symbol attribute directives
464 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
465 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
466 if (!strcmp(IDVal, ".hidden"))
467 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
468 if (!strcmp(IDVal, ".indirect_symbol"))
469 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
470 if (!strcmp(IDVal, ".internal"))
471 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
472 if (!strcmp(IDVal, ".lazy_reference"))
473 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
474 if (!strcmp(IDVal, ".no_dead_strip"))
475 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
476 if (!strcmp(IDVal, ".private_extern"))
477 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
478 if (!strcmp(IDVal, ".protected"))
479 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
480 if (!strcmp(IDVal, ".reference"))
481 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
482 if (!strcmp(IDVal, ".weak"))
483 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
484 if (!strcmp(IDVal, ".weak_definition"))
485 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
486 if (!strcmp(IDVal, ".weak_reference"))
487 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
489 Warning(IDLoc, "ignoring directive for now");
490 EatToEndOfStatement();
495 if (ParseX86InstOperands(Inst))
498 if (Lexer.isNot(asmtok::EndOfStatement))
499 return TokError("unexpected token in argument list");
501 // Eat the end of statement marker.
504 // Instruction is good, process it.
505 outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
508 // Skip to end of line for now.
512 bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
513 // FIXME: Use better location, we should use proper tokens.
514 SMLoc EqualLoc = Lexer.getLoc();
517 if (ParseAbsoluteExpression(Value))
520 if (Lexer.isNot(asmtok::EndOfStatement))
521 return TokError("unexpected token in assignment");
523 // Eat the end of statement marker.
526 // Diagnose assignment to a label.
528 // FIXME: Diagnostics. Note the location of the definition as a label.
529 // FIXME: This doesn't diagnose assignment to a symbol which has been
530 // implicitly marked as external.
531 // FIXME: Handle '.'.
532 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
533 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
534 if (Sym->getSection())
535 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
536 if (Sym->isExternal())
537 return Error(EqualLoc, "invalid assignment to external symbol");
539 // Do the assignment.
540 Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
545 /// ParseDirectiveSet:
546 /// ::= .set identifier ',' expression
547 bool AsmParser::ParseDirectiveSet() {
548 if (Lexer.isNot(asmtok::Identifier))
549 return TokError("expected identifier after '.set' directive");
551 const char *Name = Lexer.getCurStrVal();
553 if (Lexer.Lex() != asmtok::Comma)
554 return TokError("unexpected token in '.set'");
557 return ParseAssignment(Name, true);
560 /// ParseDirectiveSection:
561 /// ::= .section identifier (',' identifier)*
562 /// FIXME: This should actually parse out the segment, section, attributes and
563 /// sizeof_stub fields.
564 bool AsmParser::ParseDirectiveDarwinSection() {
565 if (Lexer.isNot(asmtok::Identifier))
566 return TokError("expected identifier after '.section' directive");
568 std::string Section = Lexer.getCurStrVal();
571 // Accept a comma separated list of modifiers.
572 while (Lexer.is(asmtok::Comma)) {
575 if (Lexer.isNot(asmtok::Identifier))
576 return TokError("expected identifier in '.section' directive");
578 Section += Lexer.getCurStrVal();
582 if (Lexer.isNot(asmtok::EndOfStatement))
583 return TokError("unexpected token in '.section' directive");
586 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
590 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
591 const char *Directives) {
592 if (Lexer.isNot(asmtok::EndOfStatement))
593 return TokError("unexpected token in section switching directive");
596 std::string SectionStr = Section;
597 if (Directives && Directives[0]) {
599 SectionStr += Directives;
602 Out.SwitchSection(Ctx.GetSection(Section));
606 /// ParseDirectiveAscii:
607 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
608 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
609 if (Lexer.isNot(asmtok::EndOfStatement)) {
611 if (Lexer.isNot(asmtok::String))
612 return TokError("expected string in '.ascii' or '.asciz' directive");
614 // FIXME: This shouldn't use a const char* + strlen, the string could have
616 // FIXME: Should have accessor for getting string contents.
617 const char *Str = Lexer.getCurStrVal();
618 Out.EmitBytes(Str + 1, strlen(Str) - 2);
620 Out.EmitBytes("\0", 1);
624 if (Lexer.is(asmtok::EndOfStatement))
627 if (Lexer.isNot(asmtok::Comma))
628 return TokError("unexpected token in '.ascii' or '.asciz' directive");
637 /// ParseDirectiveValue
638 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
639 bool AsmParser::ParseDirectiveValue(unsigned Size) {
640 if (Lexer.isNot(asmtok::EndOfStatement)) {
643 if (ParseAbsoluteExpression(Expr))
646 Out.EmitValue(MCValue::get(Expr), Size);
648 if (Lexer.is(asmtok::EndOfStatement))
651 // FIXME: Improve diagnostic.
652 if (Lexer.isNot(asmtok::Comma))
653 return TokError("unexpected token in directive");
662 /// ParseDirectiveSpace
663 /// ::= .space expression [ , expression ]
664 bool AsmParser::ParseDirectiveSpace() {
666 if (ParseAbsoluteExpression(NumBytes))
669 int64_t FillExpr = 0;
670 bool HasFillExpr = false;
671 if (Lexer.isNot(asmtok::EndOfStatement)) {
672 if (Lexer.isNot(asmtok::Comma))
673 return TokError("unexpected token in '.space' directive");
676 if (ParseAbsoluteExpression(FillExpr))
681 if (Lexer.isNot(asmtok::EndOfStatement))
682 return TokError("unexpected token in '.space' directive");
688 return TokError("invalid number of bytes in '.space' directive");
690 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
691 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
692 Out.EmitValue(MCValue::get(FillExpr), 1);
697 /// ParseDirectiveFill
698 /// ::= .fill expression , expression , expression
699 bool AsmParser::ParseDirectiveFill() {
701 if (ParseAbsoluteExpression(NumValues))
704 if (Lexer.isNot(asmtok::Comma))
705 return TokError("unexpected token in '.fill' directive");
709 if (ParseAbsoluteExpression(FillSize))
712 if (Lexer.isNot(asmtok::Comma))
713 return TokError("unexpected token in '.fill' directive");
717 if (ParseAbsoluteExpression(FillExpr))
720 if (Lexer.isNot(asmtok::EndOfStatement))
721 return TokError("unexpected token in '.fill' directive");
725 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
726 return TokError("invalid '.fill' size, expected 1, 2, or 4");
728 for (uint64_t i = 0, e = NumValues; i != e; ++i)
729 Out.EmitValue(MCValue::get(FillExpr), FillSize);
734 /// ParseDirectiveOrg
735 /// ::= .org expression [ , expression ]
736 bool AsmParser::ParseDirectiveOrg() {
738 if (ParseAbsoluteExpression(Offset))
741 // Parse optional fill expression.
742 int64_t FillExpr = 0;
743 if (Lexer.isNot(asmtok::EndOfStatement)) {
744 if (Lexer.isNot(asmtok::Comma))
745 return TokError("unexpected token in '.org' directive");
748 if (ParseAbsoluteExpression(FillExpr))
751 if (Lexer.isNot(asmtok::EndOfStatement))
752 return TokError("unexpected token in '.org' directive");
757 Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
762 /// ParseDirectiveAlign
763 /// ::= {.align, ...} expression [ , expression [ , expression ]]
764 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
766 if (ParseAbsoluteExpression(Alignment))
770 bool HasFillExpr = false;
771 int64_t FillExpr = 0;
772 int64_t MaxBytesToFill = 0;
773 if (Lexer.isNot(asmtok::EndOfStatement)) {
774 if (Lexer.isNot(asmtok::Comma))
775 return TokError("unexpected token in directive");
778 // The fill expression can be omitted while specifying a maximum number of
779 // alignment bytes, e.g:
781 if (Lexer.isNot(asmtok::Comma)) {
783 if (ParseAbsoluteExpression(FillExpr))
787 if (Lexer.isNot(asmtok::EndOfStatement)) {
788 if (Lexer.isNot(asmtok::Comma))
789 return TokError("unexpected token in directive");
792 MaxBytesLoc = Lexer.getLoc();
793 if (ParseAbsoluteExpression(MaxBytesToFill))
796 if (Lexer.isNot(asmtok::EndOfStatement))
797 return TokError("unexpected token in directive");
804 // FIXME: Sometimes fill with nop.
808 // Compute alignment in bytes.
810 // FIXME: Diagnose overflow.
811 Alignment = 1 << Alignment;
814 // Diagnose non-sensical max bytes to fill.
815 if (MaxBytesLoc.isValid()) {
816 if (MaxBytesToFill < 1) {
817 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
818 "many bytes, ignoring");
822 if (MaxBytesToFill >= Alignment) {
823 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
829 // FIXME: Target specific behavior about how the "extra" bytes are filled.
830 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
835 /// ParseDirectiveSymbolAttribute
836 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
837 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
838 if (Lexer.isNot(asmtok::EndOfStatement)) {
840 if (Lexer.isNot(asmtok::Identifier))
841 return TokError("expected identifier in directive");
843 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
846 // If this is use of an undefined symbol then mark it external.
847 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
848 Sym->setExternal(true);
850 Out.EmitSymbolAttribute(Sym, Attr);
852 if (Lexer.is(asmtok::EndOfStatement))
855 if (Lexer.isNot(asmtok::Comma))
856 return TokError("unexpected token in directive");