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 bool AsmParser::Error(SMLoc L, const char *Msg) {
26 Lexer.PrintMessage(L, Msg);
30 bool AsmParser::TokError(const char *Msg) {
31 Lexer.PrintMessage(Lexer.getLoc(), Msg);
35 bool AsmParser::Run() {
39 while (Lexer.isNot(asmtok::Eof))
46 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
47 void AsmParser::EatToEndOfStatement() {
48 while (Lexer.isNot(asmtok::EndOfStatement) &&
49 Lexer.isNot(asmtok::Eof))
53 if (Lexer.is(asmtok::EndOfStatement))
58 /// ParseParenExpr - Parse a paren expression and return it.
59 /// NOTE: This assumes the leading '(' has already been consumed.
61 /// parenexpr ::= expr)
63 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
64 if (ParseExpression(Res)) return true;
65 if (Lexer.isNot(asmtok::RParen))
66 return TokError("expected ')' in parentheses expression");
71 /// ParsePrimaryExpr - Parse a primary expression and return it.
72 /// primaryexpr ::= (parenexpr
73 /// primaryexpr ::= symbol
74 /// primaryexpr ::= number
75 /// primaryexpr ::= ~,+,- primaryexpr
76 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
77 switch (Lexer.getKind()) {
79 return TokError("unknown token in expression");
81 Lexer.Lex(); // Eat the operator.
82 if (ParsePrimaryExpr(Res))
84 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
86 case asmtok::Identifier: {
87 // This is a label, this should be parsed as part of an expression, to
88 // handle things like LFOO+4.
89 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
91 // If this is use of an undefined symbol then mark it external.
92 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
93 Sym->setExternal(true);
95 Res = new AsmSymbolRefExpr(Sym);
96 Lexer.Lex(); // Eat identifier.
100 Res = new AsmConstantExpr(Lexer.getCurIntVal());
101 Lexer.Lex(); // Eat identifier.
104 Lexer.Lex(); // Eat the '('.
105 return ParseParenExpr(Res);
107 Lexer.Lex(); // Eat the operator.
108 if (ParsePrimaryExpr(Res))
110 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
113 Lexer.Lex(); // Eat the operator.
114 if (ParsePrimaryExpr(Res))
116 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
119 Lexer.Lex(); // Eat the operator.
120 if (ParsePrimaryExpr(Res))
122 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
127 /// ParseExpression - Parse an expression and return it.
129 /// expr ::= expr +,- expr -> lowest.
130 /// expr ::= expr |,^,&,! expr -> middle.
131 /// expr ::= expr *,/,%,<<,>> expr -> highest.
132 /// expr ::= primaryexpr
134 bool AsmParser::ParseExpression(AsmExpr *&Res) {
136 return ParsePrimaryExpr(Res) ||
137 ParseBinOpRHS(1, Res);
140 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
143 if (ParseExpression(Expr))
146 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
147 return TokError("expected absolute expression");
152 static unsigned getBinOpPrecedence(asmtok::TokKind K,
153 AsmBinaryExpr::Opcode &Kind) {
155 default: return 0; // not a binop.
157 // Lowest Precedence: &&, ||
159 Kind = AsmBinaryExpr::LAnd;
161 case asmtok::PipePipe:
162 Kind = AsmBinaryExpr::LOr;
165 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
167 Kind = AsmBinaryExpr::Add;
170 Kind = AsmBinaryExpr::Sub;
172 case asmtok::EqualEqual:
173 Kind = AsmBinaryExpr::EQ;
175 case asmtok::ExclaimEqual:
176 case asmtok::LessGreater:
177 Kind = AsmBinaryExpr::NE;
180 Kind = AsmBinaryExpr::LT;
182 case asmtok::LessEqual:
183 Kind = AsmBinaryExpr::LTE;
185 case asmtok::Greater:
186 Kind = AsmBinaryExpr::GT;
188 case asmtok::GreaterEqual:
189 Kind = AsmBinaryExpr::GTE;
192 // Intermediate Precedence: |, &, ^
194 // FIXME: gas seems to support '!' as an infix operator?
196 Kind = AsmBinaryExpr::Or;
199 Kind = AsmBinaryExpr::Xor;
202 Kind = AsmBinaryExpr::And;
205 // Highest Precedence: *, /, %, <<, >>
207 Kind = AsmBinaryExpr::Mul;
210 Kind = AsmBinaryExpr::Div;
212 case asmtok::Percent:
213 Kind = AsmBinaryExpr::Mod;
215 case asmtok::LessLess:
216 Kind = AsmBinaryExpr::Shl;
218 case asmtok::GreaterGreater:
219 Kind = AsmBinaryExpr::Shr;
225 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
226 /// Res contains the LHS of the expression on input.
227 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
229 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
230 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
232 // If the next token is lower precedence than we are allowed to eat, return
233 // successfully with what we ate already.
234 if (TokPrec < Precedence)
239 // Eat the next primary expression.
241 if (ParsePrimaryExpr(RHS)) return true;
243 // If BinOp binds less tightly with RHS than the operator after RHS, let
244 // the pending operator take RHS as its LHS.
245 AsmBinaryExpr::Opcode Dummy;
246 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
247 if (TokPrec < NextTokPrec) {
248 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
251 // Merge LHS and RHS according to operator.
252 Res = new AsmBinaryExpr(Kind, Res, RHS);
260 /// ::= EndOfStatement
261 /// ::= Label* Directive ...Operands... EndOfStatement
262 /// ::= Label* Identifier OperandList* EndOfStatement
263 bool AsmParser::ParseStatement() {
264 switch (Lexer.getKind()) {
266 return TokError("unexpected token at start of statement");
267 case asmtok::EndOfStatement:
270 case asmtok::Identifier:
272 // TODO: Recurse on local labels etc.
275 // If we have an identifier, handle it as the key symbol.
276 SMLoc IDLoc = Lexer.getLoc();
277 const char *IDVal = Lexer.getCurStrVal();
279 // Consume the identifier, see what is after it.
280 switch (Lexer.Lex()) {
281 case asmtok::Colon: {
282 // identifier ':' -> Label.
285 // Diagnose attempt to use a variable as a label.
287 // FIXME: Diagnostics. Note the location of the definition as a label.
288 // FIXME: This doesn't diagnose assignment to a symbol which has been
289 // implicitly marked as external.
290 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
291 if (Sym->getSection())
292 return Error(IDLoc, "invalid symbol redefinition");
293 if (Ctx.GetSymbolValue(Sym))
294 return Error(IDLoc, "symbol already used as assembler variable");
296 // Since we saw a label, create a symbol and emit it.
297 // FIXME: If the label starts with L it is an assembler temporary label.
298 // Why does the client of this api need to know this?
301 return ParseStatement();
305 // identifier '=' ... -> assignment statement
308 return ParseAssignment(IDVal, false);
310 default: // Normal instruction or directive.
314 // Otherwise, we have a normal instruction or directive.
315 if (IDVal[0] == '.') {
316 // FIXME: This should be driven based on a hash lookup and callback.
317 if (!strcmp(IDVal, ".section"))
318 return ParseDirectiveDarwinSection();
319 if (!strcmp(IDVal, ".text"))
320 // FIXME: This changes behavior based on the -static flag to the
322 return ParseDirectiveSectionSwitch("__TEXT,__text",
323 "regular,pure_instructions");
324 if (!strcmp(IDVal, ".const"))
325 return ParseDirectiveSectionSwitch("__TEXT,__const");
326 if (!strcmp(IDVal, ".static_const"))
327 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
328 if (!strcmp(IDVal, ".cstring"))
329 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
331 if (!strcmp(IDVal, ".literal4"))
332 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
333 if (!strcmp(IDVal, ".literal8"))
334 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
335 if (!strcmp(IDVal, ".literal16"))
336 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
338 if (!strcmp(IDVal, ".constructor"))
339 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
340 if (!strcmp(IDVal, ".destructor"))
341 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
342 if (!strcmp(IDVal, ".fvmlib_init0"))
343 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
344 if (!strcmp(IDVal, ".fvmlib_init1"))
345 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
346 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
347 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
348 "self_modifying_code+pure_instructions,5");
349 // FIXME: .picsymbol_stub on PPC.
350 if (!strcmp(IDVal, ".data"))
351 return ParseDirectiveSectionSwitch("__DATA,__data");
352 if (!strcmp(IDVal, ".static_data"))
353 return ParseDirectiveSectionSwitch("__DATA,__static_data");
354 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
355 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
356 "non_lazy_symbol_pointers");
357 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
358 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
359 "lazy_symbol_pointers");
360 if (!strcmp(IDVal, ".dyld"))
361 return ParseDirectiveSectionSwitch("__DATA,__dyld");
362 if (!strcmp(IDVal, ".mod_init_func"))
363 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
365 if (!strcmp(IDVal, ".mod_term_func"))
366 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
368 if (!strcmp(IDVal, ".const_data"))
369 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
372 // FIXME: Verify attributes on sections.
373 if (!strcmp(IDVal, ".objc_class"))
374 return ParseDirectiveSectionSwitch("__OBJC,__class");
375 if (!strcmp(IDVal, ".objc_meta_class"))
376 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
377 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
378 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
379 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
380 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
381 if (!strcmp(IDVal, ".objc_protocol"))
382 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
383 if (!strcmp(IDVal, ".objc_string_object"))
384 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
385 if (!strcmp(IDVal, ".objc_cls_meth"))
386 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
387 if (!strcmp(IDVal, ".objc_inst_meth"))
388 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
389 if (!strcmp(IDVal, ".objc_cls_refs"))
390 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
391 if (!strcmp(IDVal, ".objc_message_refs"))
392 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
393 if (!strcmp(IDVal, ".objc_symbols"))
394 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
395 if (!strcmp(IDVal, ".objc_category"))
396 return ParseDirectiveSectionSwitch("__OBJC,__category");
397 if (!strcmp(IDVal, ".objc_class_vars"))
398 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
399 if (!strcmp(IDVal, ".objc_instance_vars"))
400 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
401 if (!strcmp(IDVal, ".objc_module_info"))
402 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
403 if (!strcmp(IDVal, ".objc_class_names"))
404 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
405 if (!strcmp(IDVal, ".objc_meth_var_types"))
406 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
407 if (!strcmp(IDVal, ".objc_meth_var_names"))
408 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
409 if (!strcmp(IDVal, ".objc_selector_strs"))
410 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
412 // Assembler features
413 if (!strcmp(IDVal, ".set"))
414 return ParseDirectiveSet();
418 if (!strcmp(IDVal, ".ascii"))
419 return ParseDirectiveAscii(false);
420 if (!strcmp(IDVal, ".asciz"))
421 return ParseDirectiveAscii(true);
423 // FIXME: Target hooks for size? Also for "word", "hword".
424 if (!strcmp(IDVal, ".byte"))
425 return ParseDirectiveValue(1);
426 if (!strcmp(IDVal, ".short"))
427 return ParseDirectiveValue(2);
428 if (!strcmp(IDVal, ".long"))
429 return ParseDirectiveValue(4);
430 if (!strcmp(IDVal, ".quad"))
431 return ParseDirectiveValue(8);
433 // FIXME: Target hooks for IsPow2.
434 if (!strcmp(IDVal, ".align"))
435 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
436 if (!strcmp(IDVal, ".align32"))
437 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
438 if (!strcmp(IDVal, ".balign"))
439 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
440 if (!strcmp(IDVal, ".balignw"))
441 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
442 if (!strcmp(IDVal, ".balignl"))
443 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
444 if (!strcmp(IDVal, ".p2align"))
445 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
446 if (!strcmp(IDVal, ".p2alignw"))
447 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
448 if (!strcmp(IDVal, ".p2alignl"))
449 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
451 if (!strcmp(IDVal, ".org"))
452 return ParseDirectiveOrg();
454 if (!strcmp(IDVal, ".fill"))
455 return ParseDirectiveFill();
456 if (!strcmp(IDVal, ".space"))
457 return ParseDirectiveSpace();
459 Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now");
460 EatToEndOfStatement();
465 if (ParseX86InstOperands(Inst))
468 if (Lexer.isNot(asmtok::EndOfStatement))
469 return TokError("unexpected token in argument list");
471 // Eat the end of statement marker.
474 // Instruction is good, process it.
475 outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
478 // Skip to end of line for now.
482 bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
483 // FIXME: Use better location, we should use proper tokens.
484 SMLoc EqualLoc = Lexer.getLoc();
487 if (ParseAbsoluteExpression(Value))
490 if (Lexer.isNot(asmtok::EndOfStatement))
491 return TokError("unexpected token in assignment");
493 // Eat the end of statement marker.
496 // Diagnose assignment to a label.
498 // FIXME: Diagnostics. Note the location of the definition as a label.
499 // FIXME: This doesn't diagnose assignment to a symbol which has been
500 // implicitly marked as external.
501 // FIXME: Handle '.'.
502 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
503 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
504 if (Sym->getSection())
505 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
506 if (Sym->isExternal())
507 return Error(EqualLoc, "invalid assignment to external symbol");
509 // Do the assignment.
510 Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
515 /// ParseDirectiveSet:
516 /// ::= .set identifier ',' expression
517 bool AsmParser::ParseDirectiveSet() {
518 if (Lexer.isNot(asmtok::Identifier))
519 return TokError("expected identifier after '.set' directive");
521 const char *Name = Lexer.getCurStrVal();
523 if (Lexer.Lex() != asmtok::Comma)
524 return TokError("unexpected token in '.set'");
527 return ParseAssignment(Name, true);
530 /// ParseDirectiveSection:
531 /// ::= .section identifier (',' identifier)*
532 /// FIXME: This should actually parse out the segment, section, attributes and
533 /// sizeof_stub fields.
534 bool AsmParser::ParseDirectiveDarwinSection() {
535 if (Lexer.isNot(asmtok::Identifier))
536 return TokError("expected identifier after '.section' directive");
538 std::string Section = Lexer.getCurStrVal();
541 // Accept a comma separated list of modifiers.
542 while (Lexer.is(asmtok::Comma)) {
545 if (Lexer.isNot(asmtok::Identifier))
546 return TokError("expected identifier in '.section' directive");
548 Section += Lexer.getCurStrVal();
552 if (Lexer.isNot(asmtok::EndOfStatement))
553 return TokError("unexpected token in '.section' directive");
556 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
560 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
561 const char *Directives) {
562 if (Lexer.isNot(asmtok::EndOfStatement))
563 return TokError("unexpected token in section switching directive");
566 std::string SectionStr = Section;
567 if (Directives && Directives[0]) {
569 SectionStr += Directives;
572 Out.SwitchSection(Ctx.GetSection(Section));
576 /// ParseDirectiveAscii:
577 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
578 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
579 if (Lexer.isNot(asmtok::EndOfStatement)) {
581 if (Lexer.isNot(asmtok::String))
582 return TokError("expected string in '.ascii' or '.asciz' directive");
584 // FIXME: This shouldn't use a const char* + strlen, the string could have
586 // FIXME: Should have accessor for getting string contents.
587 const char *Str = Lexer.getCurStrVal();
588 Out.EmitBytes(Str + 1, strlen(Str) - 2);
590 Out.EmitBytes("\0", 1);
594 if (Lexer.is(asmtok::EndOfStatement))
597 if (Lexer.isNot(asmtok::Comma))
598 return TokError("unexpected token in '.ascii' or '.asciz' directive");
607 /// ParseDirectiveValue
608 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
609 bool AsmParser::ParseDirectiveValue(unsigned Size) {
610 if (Lexer.isNot(asmtok::EndOfStatement)) {
613 if (ParseAbsoluteExpression(Expr))
616 Out.EmitValue(MCValue::get(Expr), Size);
618 if (Lexer.is(asmtok::EndOfStatement))
621 // FIXME: Improve diagnostic.
622 if (Lexer.isNot(asmtok::Comma))
623 return TokError("unexpected token in directive");
632 /// ParseDirectiveSpace
633 /// ::= .space expression [ , expression ]
634 bool AsmParser::ParseDirectiveSpace() {
636 if (ParseAbsoluteExpression(NumBytes))
639 int64_t FillExpr = 0;
640 bool HasFillExpr = false;
641 if (Lexer.isNot(asmtok::EndOfStatement)) {
642 if (Lexer.isNot(asmtok::Comma))
643 return TokError("unexpected token in '.space' directive");
646 if (ParseAbsoluteExpression(FillExpr))
651 if (Lexer.isNot(asmtok::EndOfStatement))
652 return TokError("unexpected token in '.space' directive");
658 return TokError("invalid number of bytes in '.space' directive");
660 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
661 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
662 Out.EmitValue(MCValue::get(FillExpr), 1);
667 /// ParseDirectiveFill
668 /// ::= .fill expression , expression , expression
669 bool AsmParser::ParseDirectiveFill() {
671 if (ParseAbsoluteExpression(NumValues))
674 if (Lexer.isNot(asmtok::Comma))
675 return TokError("unexpected token in '.fill' directive");
679 if (ParseAbsoluteExpression(FillSize))
682 if (Lexer.isNot(asmtok::Comma))
683 return TokError("unexpected token in '.fill' directive");
687 if (ParseAbsoluteExpression(FillExpr))
690 if (Lexer.isNot(asmtok::EndOfStatement))
691 return TokError("unexpected token in '.fill' directive");
695 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
696 return TokError("invalid '.fill' size, expected 1, 2, or 4");
698 for (uint64_t i = 0, e = NumValues; i != e; ++i)
699 Out.EmitValue(MCValue::get(FillExpr), FillSize);
704 /// ParseDirectiveOrg
705 /// ::= .org expression [ , expression ]
706 bool AsmParser::ParseDirectiveOrg() {
708 if (ParseAbsoluteExpression(Offset))
711 // Parse optional fill expression.
712 int64_t FillExpr = 0;
713 if (Lexer.isNot(asmtok::EndOfStatement)) {
714 if (Lexer.isNot(asmtok::Comma))
715 return TokError("unexpected token in '.org' directive");
718 if (ParseAbsoluteExpression(FillExpr))
721 if (Lexer.isNot(asmtok::EndOfStatement))
722 return TokError("unexpected token in '.org' directive");
727 Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
732 /// ParseDirectiveAlign
733 /// ::= {.align, ...} expression [ , expression [ , expression ]]
734 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
736 if (ParseAbsoluteExpression(Alignment))
740 bool HasFillExpr = false;
741 int64_t FillExpr = 0;
742 int64_t MaxBytesToFill = 0;
743 if (Lexer.isNot(asmtok::EndOfStatement)) {
744 if (Lexer.isNot(asmtok::Comma))
745 return TokError("unexpected token in directive");
748 // The fill expression can be omitted while specifying a maximum number of
749 // alignment bytes, e.g:
751 if (Lexer.isNot(asmtok::Comma)) {
753 if (ParseAbsoluteExpression(FillExpr))
757 if (Lexer.isNot(asmtok::EndOfStatement)) {
758 if (Lexer.isNot(asmtok::Comma))
759 return TokError("unexpected token in directive");
762 MaxBytesLoc = Lexer.getLoc();
763 if (ParseAbsoluteExpression(MaxBytesToFill))
766 if (Lexer.isNot(asmtok::EndOfStatement))
767 return TokError("unexpected token in directive");
774 // FIXME: Sometimes fill with nop.
778 // Compute alignment in bytes.
780 // FIXME: Diagnose overflow.
781 Alignment = 1 << Alignment;
784 // Diagnose non-sensical max bytes to fill.
785 if (MaxBytesLoc.isValid()) {
786 if (MaxBytesToFill < 1) {
787 Lexer.PrintMessage(MaxBytesLoc, "warning: alignment directive can never "
788 "be satisfied in this many bytes, ignoring");
792 if (MaxBytesToFill >= Alignment) {
793 Lexer.PrintMessage(MaxBytesLoc, "warning: maximum bytes expression "
794 "exceeds alignment and has no effect");
799 // FIXME: Target specific behavior about how the "extra" bytes are filled.
800 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);