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 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
159 if (ParseExpression(Expr))
162 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
163 return TokError("expected relocatable expression");
168 static unsigned getBinOpPrecedence(asmtok::TokKind K,
169 AsmBinaryExpr::Opcode &Kind) {
171 default: return 0; // not a binop.
173 // Lowest Precedence: &&, ||
175 Kind = AsmBinaryExpr::LAnd;
177 case asmtok::PipePipe:
178 Kind = AsmBinaryExpr::LOr;
181 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
183 Kind = AsmBinaryExpr::Add;
186 Kind = AsmBinaryExpr::Sub;
188 case asmtok::EqualEqual:
189 Kind = AsmBinaryExpr::EQ;
191 case asmtok::ExclaimEqual:
192 case asmtok::LessGreater:
193 Kind = AsmBinaryExpr::NE;
196 Kind = AsmBinaryExpr::LT;
198 case asmtok::LessEqual:
199 Kind = AsmBinaryExpr::LTE;
201 case asmtok::Greater:
202 Kind = AsmBinaryExpr::GT;
204 case asmtok::GreaterEqual:
205 Kind = AsmBinaryExpr::GTE;
208 // Intermediate Precedence: |, &, ^
210 // FIXME: gas seems to support '!' as an infix operator?
212 Kind = AsmBinaryExpr::Or;
215 Kind = AsmBinaryExpr::Xor;
218 Kind = AsmBinaryExpr::And;
221 // Highest Precedence: *, /, %, <<, >>
223 Kind = AsmBinaryExpr::Mul;
226 Kind = AsmBinaryExpr::Div;
228 case asmtok::Percent:
229 Kind = AsmBinaryExpr::Mod;
231 case asmtok::LessLess:
232 Kind = AsmBinaryExpr::Shl;
234 case asmtok::GreaterGreater:
235 Kind = AsmBinaryExpr::Shr;
241 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
242 /// Res contains the LHS of the expression on input.
243 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
245 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
246 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
248 // If the next token is lower precedence than we are allowed to eat, return
249 // successfully with what we ate already.
250 if (TokPrec < Precedence)
255 // Eat the next primary expression.
257 if (ParsePrimaryExpr(RHS)) return true;
259 // If BinOp binds less tightly with RHS than the operator after RHS, let
260 // the pending operator take RHS as its LHS.
261 AsmBinaryExpr::Opcode Dummy;
262 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
263 if (TokPrec < NextTokPrec) {
264 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
267 // Merge LHS and RHS according to operator.
268 Res = new AsmBinaryExpr(Kind, Res, RHS);
276 /// ::= EndOfStatement
277 /// ::= Label* Directive ...Operands... EndOfStatement
278 /// ::= Label* Identifier OperandList* EndOfStatement
279 bool AsmParser::ParseStatement() {
280 switch (Lexer.getKind()) {
282 return TokError("unexpected token at start of statement");
283 case asmtok::EndOfStatement:
286 case asmtok::Identifier:
288 // TODO: Recurse on local labels etc.
291 // If we have an identifier, handle it as the key symbol.
292 SMLoc IDLoc = Lexer.getLoc();
293 const char *IDVal = Lexer.getCurStrVal();
295 // Consume the identifier, see what is after it.
296 switch (Lexer.Lex()) {
297 case asmtok::Colon: {
298 // identifier ':' -> Label.
301 // Diagnose attempt to use a variable as a label.
303 // FIXME: Diagnostics. Note the location of the definition as a label.
304 // FIXME: This doesn't diagnose assignment to a symbol which has been
305 // implicitly marked as external.
306 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
307 if (Sym->getSection())
308 return Error(IDLoc, "invalid symbol redefinition");
309 if (Ctx.GetSymbolValue(Sym))
310 return Error(IDLoc, "symbol already used as assembler variable");
312 // Since we saw a label, create a symbol and emit it.
313 // FIXME: If the label starts with L it is an assembler temporary label.
314 // Why does the client of this api need to know this?
317 return ParseStatement();
321 // identifier '=' ... -> assignment statement
324 return ParseAssignment(IDVal, false);
326 default: // Normal instruction or directive.
330 // Otherwise, we have a normal instruction or directive.
331 if (IDVal[0] == '.') {
332 // FIXME: This should be driven based on a hash lookup and callback.
333 if (!strcmp(IDVal, ".section"))
334 return ParseDirectiveDarwinSection();
335 if (!strcmp(IDVal, ".text"))
336 // FIXME: This changes behavior based on the -static flag to the
338 return ParseDirectiveSectionSwitch("__TEXT,__text",
339 "regular,pure_instructions");
340 if (!strcmp(IDVal, ".const"))
341 return ParseDirectiveSectionSwitch("__TEXT,__const");
342 if (!strcmp(IDVal, ".static_const"))
343 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
344 if (!strcmp(IDVal, ".cstring"))
345 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
347 if (!strcmp(IDVal, ".literal4"))
348 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
349 if (!strcmp(IDVal, ".literal8"))
350 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
351 if (!strcmp(IDVal, ".literal16"))
352 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
354 if (!strcmp(IDVal, ".constructor"))
355 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
356 if (!strcmp(IDVal, ".destructor"))
357 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
358 if (!strcmp(IDVal, ".fvmlib_init0"))
359 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
360 if (!strcmp(IDVal, ".fvmlib_init1"))
361 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
362 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
363 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
364 "self_modifying_code+pure_instructions,5");
365 // FIXME: .picsymbol_stub on PPC.
366 if (!strcmp(IDVal, ".data"))
367 return ParseDirectiveSectionSwitch("__DATA,__data");
368 if (!strcmp(IDVal, ".static_data"))
369 return ParseDirectiveSectionSwitch("__DATA,__static_data");
370 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
371 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
372 "non_lazy_symbol_pointers");
373 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
374 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
375 "lazy_symbol_pointers");
376 if (!strcmp(IDVal, ".dyld"))
377 return ParseDirectiveSectionSwitch("__DATA,__dyld");
378 if (!strcmp(IDVal, ".mod_init_func"))
379 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
381 if (!strcmp(IDVal, ".mod_term_func"))
382 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
384 if (!strcmp(IDVal, ".const_data"))
385 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
388 // FIXME: Verify attributes on sections.
389 if (!strcmp(IDVal, ".objc_class"))
390 return ParseDirectiveSectionSwitch("__OBJC,__class");
391 if (!strcmp(IDVal, ".objc_meta_class"))
392 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
393 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
394 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
395 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
396 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
397 if (!strcmp(IDVal, ".objc_protocol"))
398 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
399 if (!strcmp(IDVal, ".objc_string_object"))
400 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
401 if (!strcmp(IDVal, ".objc_cls_meth"))
402 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
403 if (!strcmp(IDVal, ".objc_inst_meth"))
404 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
405 if (!strcmp(IDVal, ".objc_cls_refs"))
406 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
407 if (!strcmp(IDVal, ".objc_message_refs"))
408 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
409 if (!strcmp(IDVal, ".objc_symbols"))
410 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
411 if (!strcmp(IDVal, ".objc_category"))
412 return ParseDirectiveSectionSwitch("__OBJC,__category");
413 if (!strcmp(IDVal, ".objc_class_vars"))
414 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
415 if (!strcmp(IDVal, ".objc_instance_vars"))
416 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
417 if (!strcmp(IDVal, ".objc_module_info"))
418 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
419 if (!strcmp(IDVal, ".objc_class_names"))
420 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
421 if (!strcmp(IDVal, ".objc_meth_var_types"))
422 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
423 if (!strcmp(IDVal, ".objc_meth_var_names"))
424 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
425 if (!strcmp(IDVal, ".objc_selector_strs"))
426 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
428 // Assembler features
429 if (!strcmp(IDVal, ".set"))
430 return ParseDirectiveSet();
434 if (!strcmp(IDVal, ".ascii"))
435 return ParseDirectiveAscii(false);
436 if (!strcmp(IDVal, ".asciz"))
437 return ParseDirectiveAscii(true);
439 // FIXME: Target hooks for size? Also for "word", "hword".
440 if (!strcmp(IDVal, ".byte"))
441 return ParseDirectiveValue(1);
442 if (!strcmp(IDVal, ".short"))
443 return ParseDirectiveValue(2);
444 if (!strcmp(IDVal, ".long"))
445 return ParseDirectiveValue(4);
446 if (!strcmp(IDVal, ".quad"))
447 return ParseDirectiveValue(8);
449 // FIXME: Target hooks for IsPow2.
450 if (!strcmp(IDVal, ".align"))
451 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
452 if (!strcmp(IDVal, ".align32"))
453 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
454 if (!strcmp(IDVal, ".balign"))
455 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
456 if (!strcmp(IDVal, ".balignw"))
457 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
458 if (!strcmp(IDVal, ".balignl"))
459 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
460 if (!strcmp(IDVal, ".p2align"))
461 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
462 if (!strcmp(IDVal, ".p2alignw"))
463 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
464 if (!strcmp(IDVal, ".p2alignl"))
465 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
467 if (!strcmp(IDVal, ".org"))
468 return ParseDirectiveOrg();
470 if (!strcmp(IDVal, ".fill"))
471 return ParseDirectiveFill();
472 if (!strcmp(IDVal, ".space"))
473 return ParseDirectiveSpace();
475 // Symbol attribute directives
476 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
477 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
478 if (!strcmp(IDVal, ".hidden"))
479 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
480 if (!strcmp(IDVal, ".indirect_symbol"))
481 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
482 if (!strcmp(IDVal, ".internal"))
483 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
484 if (!strcmp(IDVal, ".lazy_reference"))
485 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
486 if (!strcmp(IDVal, ".no_dead_strip"))
487 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
488 if (!strcmp(IDVal, ".private_extern"))
489 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
490 if (!strcmp(IDVal, ".protected"))
491 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
492 if (!strcmp(IDVal, ".reference"))
493 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
494 if (!strcmp(IDVal, ".weak"))
495 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
496 if (!strcmp(IDVal, ".weak_definition"))
497 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
498 if (!strcmp(IDVal, ".weak_reference"))
499 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
501 Warning(IDLoc, "ignoring directive for now");
502 EatToEndOfStatement();
507 if (ParseX86InstOperands(Inst))
510 if (Lexer.isNot(asmtok::EndOfStatement))
511 return TokError("unexpected token in argument list");
513 // Eat the end of statement marker.
516 // Instruction is good, process it.
517 outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
520 // Skip to end of line for now.
524 bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
525 // FIXME: Use better location, we should use proper tokens.
526 SMLoc EqualLoc = Lexer.getLoc();
529 if (ParseAbsoluteExpression(Value))
532 if (Lexer.isNot(asmtok::EndOfStatement))
533 return TokError("unexpected token in assignment");
535 // Eat the end of statement marker.
538 // Diagnose assignment to a label.
540 // FIXME: Diagnostics. Note the location of the definition as a label.
541 // FIXME: This doesn't diagnose assignment to a symbol which has been
542 // implicitly marked as external.
543 // FIXME: Handle '.'.
544 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
545 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
546 if (Sym->getSection())
547 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
548 if (Sym->isExternal())
549 return Error(EqualLoc, "invalid assignment to external symbol");
551 // Do the assignment.
552 Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
557 /// ParseDirectiveSet:
558 /// ::= .set identifier ',' expression
559 bool AsmParser::ParseDirectiveSet() {
560 if (Lexer.isNot(asmtok::Identifier))
561 return TokError("expected identifier after '.set' directive");
563 const char *Name = Lexer.getCurStrVal();
565 if (Lexer.Lex() != asmtok::Comma)
566 return TokError("unexpected token in '.set'");
569 return ParseAssignment(Name, true);
572 /// ParseDirectiveSection:
573 /// ::= .section identifier (',' identifier)*
574 /// FIXME: This should actually parse out the segment, section, attributes and
575 /// sizeof_stub fields.
576 bool AsmParser::ParseDirectiveDarwinSection() {
577 if (Lexer.isNot(asmtok::Identifier))
578 return TokError("expected identifier after '.section' directive");
580 std::string Section = Lexer.getCurStrVal();
583 // Accept a comma separated list of modifiers.
584 while (Lexer.is(asmtok::Comma)) {
587 if (Lexer.isNot(asmtok::Identifier))
588 return TokError("expected identifier in '.section' directive");
590 Section += Lexer.getCurStrVal();
594 if (Lexer.isNot(asmtok::EndOfStatement))
595 return TokError("unexpected token in '.section' directive");
598 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
602 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
603 const char *Directives) {
604 if (Lexer.isNot(asmtok::EndOfStatement))
605 return TokError("unexpected token in section switching directive");
608 std::string SectionStr = Section;
609 if (Directives && Directives[0]) {
611 SectionStr += Directives;
614 Out.SwitchSection(Ctx.GetSection(Section));
618 /// ParseDirectiveAscii:
619 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
620 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
621 if (Lexer.isNot(asmtok::EndOfStatement)) {
623 if (Lexer.isNot(asmtok::String))
624 return TokError("expected string in '.ascii' or '.asciz' directive");
626 // FIXME: This shouldn't use a const char* + strlen, the string could have
628 // FIXME: Should have accessor for getting string contents.
629 const char *Str = Lexer.getCurStrVal();
630 Out.EmitBytes(Str + 1, strlen(Str) - 2);
632 Out.EmitBytes("\0", 1);
636 if (Lexer.is(asmtok::EndOfStatement))
639 if (Lexer.isNot(asmtok::Comma))
640 return TokError("unexpected token in '.ascii' or '.asciz' directive");
649 /// ParseDirectiveValue
650 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
651 bool AsmParser::ParseDirectiveValue(unsigned Size) {
652 if (Lexer.isNot(asmtok::EndOfStatement)) {
655 if (ParseAbsoluteExpression(Expr))
658 Out.EmitValue(MCValue::get(Expr), Size);
660 if (Lexer.is(asmtok::EndOfStatement))
663 // FIXME: Improve diagnostic.
664 if (Lexer.isNot(asmtok::Comma))
665 return TokError("unexpected token in directive");
674 /// ParseDirectiveSpace
675 /// ::= .space expression [ , expression ]
676 bool AsmParser::ParseDirectiveSpace() {
678 if (ParseAbsoluteExpression(NumBytes))
681 int64_t FillExpr = 0;
682 bool HasFillExpr = false;
683 if (Lexer.isNot(asmtok::EndOfStatement)) {
684 if (Lexer.isNot(asmtok::Comma))
685 return TokError("unexpected token in '.space' directive");
688 if (ParseAbsoluteExpression(FillExpr))
693 if (Lexer.isNot(asmtok::EndOfStatement))
694 return TokError("unexpected token in '.space' directive");
700 return TokError("invalid number of bytes in '.space' directive");
702 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
703 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
704 Out.EmitValue(MCValue::get(FillExpr), 1);
709 /// ParseDirectiveFill
710 /// ::= .fill expression , expression , expression
711 bool AsmParser::ParseDirectiveFill() {
713 if (ParseAbsoluteExpression(NumValues))
716 if (Lexer.isNot(asmtok::Comma))
717 return TokError("unexpected token in '.fill' directive");
721 if (ParseAbsoluteExpression(FillSize))
724 if (Lexer.isNot(asmtok::Comma))
725 return TokError("unexpected token in '.fill' directive");
729 if (ParseAbsoluteExpression(FillExpr))
732 if (Lexer.isNot(asmtok::EndOfStatement))
733 return TokError("unexpected token in '.fill' directive");
737 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
738 return TokError("invalid '.fill' size, expected 1, 2, or 4");
740 for (uint64_t i = 0, e = NumValues; i != e; ++i)
741 Out.EmitValue(MCValue::get(FillExpr), FillSize);
746 /// ParseDirectiveOrg
747 /// ::= .org expression [ , expression ]
748 bool AsmParser::ParseDirectiveOrg() {
750 if (ParseAbsoluteExpression(Offset))
753 // Parse optional fill expression.
754 int64_t FillExpr = 0;
755 if (Lexer.isNot(asmtok::EndOfStatement)) {
756 if (Lexer.isNot(asmtok::Comma))
757 return TokError("unexpected token in '.org' directive");
760 if (ParseAbsoluteExpression(FillExpr))
763 if (Lexer.isNot(asmtok::EndOfStatement))
764 return TokError("unexpected token in '.org' directive");
769 Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
774 /// ParseDirectiveAlign
775 /// ::= {.align, ...} expression [ , expression [ , expression ]]
776 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
778 if (ParseAbsoluteExpression(Alignment))
782 bool HasFillExpr = false;
783 int64_t FillExpr = 0;
784 int64_t MaxBytesToFill = 0;
785 if (Lexer.isNot(asmtok::EndOfStatement)) {
786 if (Lexer.isNot(asmtok::Comma))
787 return TokError("unexpected token in directive");
790 // The fill expression can be omitted while specifying a maximum number of
791 // alignment bytes, e.g:
793 if (Lexer.isNot(asmtok::Comma)) {
795 if (ParseAbsoluteExpression(FillExpr))
799 if (Lexer.isNot(asmtok::EndOfStatement)) {
800 if (Lexer.isNot(asmtok::Comma))
801 return TokError("unexpected token in directive");
804 MaxBytesLoc = Lexer.getLoc();
805 if (ParseAbsoluteExpression(MaxBytesToFill))
808 if (Lexer.isNot(asmtok::EndOfStatement))
809 return TokError("unexpected token in directive");
816 // FIXME: Sometimes fill with nop.
820 // Compute alignment in bytes.
822 // FIXME: Diagnose overflow.
823 Alignment = 1 << Alignment;
826 // Diagnose non-sensical max bytes to fill.
827 if (MaxBytesLoc.isValid()) {
828 if (MaxBytesToFill < 1) {
829 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
830 "many bytes, ignoring");
834 if (MaxBytesToFill >= Alignment) {
835 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
841 // FIXME: Target specific behavior about how the "extra" bytes are filled.
842 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
847 /// ParseDirectiveSymbolAttribute
848 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
849 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
850 if (Lexer.isNot(asmtok::EndOfStatement)) {
852 if (Lexer.isNot(asmtok::Identifier))
853 return TokError("expected identifier in directive");
855 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
858 // If this is use of an undefined symbol then mark it external.
859 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
860 Sym->setExternal(true);
862 Out.EmitSymbolAttribute(Sym, Attr);
864 if (Lexer.is(asmtok::EndOfStatement))
867 if (Lexer.isNot(asmtok::Comma))
868 return TokError("unexpected token in directive");