1 //===- COFFAsmParser.cpp - COFF Assembly Parser ---------------------------===//
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 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/BinaryFormat/COFF.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCObjectFileInfo.h"
18 #include "llvm/MC/MCParser/MCAsmLexer.h"
19 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
20 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCSectionCOFF.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/SectionKind.h"
25 #include "llvm/Support/SMLoc.h"
35 class COFFAsmParser : public MCAsmParserExtension {
36 template<bool (COFFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
37 void addDirectiveHandler(StringRef Directive) {
38 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
39 this, HandleDirective<COFFAsmParser, HandlerMethod>);
40 getParser().addDirectiveHandler(Directive, Handler);
43 bool ParseSectionSwitch(StringRef Section,
44 unsigned Characteristics,
47 bool ParseSectionSwitch(StringRef Section, unsigned Characteristics,
48 SectionKind Kind, StringRef COMDATSymName,
49 COFF::COMDATType Type);
51 bool ParseSectionName(StringRef &SectionName);
52 bool ParseSectionFlags(StringRef SectionName, StringRef FlagsString,
55 void Initialize(MCAsmParser &Parser) override {
56 // Call the base implementation.
57 MCAsmParserExtension::Initialize(Parser);
59 addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveText>(".text");
60 addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveData>(".data");
61 addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveBSS>(".bss");
62 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSection>(".section");
63 addDirectiveHandler<&COFFAsmParser::ParseDirectiveDef>(".def");
64 addDirectiveHandler<&COFFAsmParser::ParseDirectiveScl>(".scl");
65 addDirectiveHandler<&COFFAsmParser::ParseDirectiveType>(".type");
66 addDirectiveHandler<&COFFAsmParser::ParseDirectiveEndef>(".endef");
67 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSecRel32>(".secrel32");
68 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSymIdx>(".symidx");
69 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSafeSEH>(".safeseh");
70 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSecIdx>(".secidx");
71 addDirectiveHandler<&COFFAsmParser::ParseDirectiveLinkOnce>(".linkonce");
72 addDirectiveHandler<&COFFAsmParser::ParseDirectiveRVA>(".rva");
74 // Win64 EH directives.
75 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartProc>(
77 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProc>(
79 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartChained>(
81 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndChained>(
83 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandler>(
85 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandlerData>(
87 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushReg>(
89 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSetFrame>(
91 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveAllocStack>(
93 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveReg>(
95 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveXMM>(
97 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushFrame>(
99 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProlog>(
101 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
104 bool ParseSectionDirectiveText(StringRef, SMLoc) {
105 return ParseSectionSwitch(".text",
106 COFF::IMAGE_SCN_CNT_CODE
107 | COFF::IMAGE_SCN_MEM_EXECUTE
108 | COFF::IMAGE_SCN_MEM_READ,
109 SectionKind::getText());
112 bool ParseSectionDirectiveData(StringRef, SMLoc) {
113 return ParseSectionSwitch(".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
114 COFF::IMAGE_SCN_MEM_READ |
115 COFF::IMAGE_SCN_MEM_WRITE,
116 SectionKind::getData());
119 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
120 return ParseSectionSwitch(".bss",
121 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
122 | COFF::IMAGE_SCN_MEM_READ
123 | COFF::IMAGE_SCN_MEM_WRITE,
124 SectionKind::getBSS());
127 bool ParseDirectiveSection(StringRef, SMLoc);
128 bool ParseDirectiveDef(StringRef, SMLoc);
129 bool ParseDirectiveScl(StringRef, SMLoc);
130 bool ParseDirectiveType(StringRef, SMLoc);
131 bool ParseDirectiveEndef(StringRef, SMLoc);
132 bool ParseDirectiveSecRel32(StringRef, SMLoc);
133 bool ParseDirectiveSecIdx(StringRef, SMLoc);
134 bool ParseDirectiveSafeSEH(StringRef, SMLoc);
135 bool ParseDirectiveSymIdx(StringRef, SMLoc);
136 bool parseCOMDATType(COFF::COMDATType &Type);
137 bool ParseDirectiveLinkOnce(StringRef, SMLoc);
138 bool ParseDirectiveRVA(StringRef, SMLoc);
140 // Win64 EH directives.
141 bool ParseSEHDirectiveStartProc(StringRef, SMLoc);
142 bool ParseSEHDirectiveEndProc(StringRef, SMLoc);
143 bool ParseSEHDirectiveStartChained(StringRef, SMLoc);
144 bool ParseSEHDirectiveEndChained(StringRef, SMLoc);
145 bool ParseSEHDirectiveHandler(StringRef, SMLoc);
146 bool ParseSEHDirectiveHandlerData(StringRef, SMLoc);
147 bool ParseSEHDirectivePushReg(StringRef, SMLoc);
148 bool ParseSEHDirectiveSetFrame(StringRef, SMLoc);
149 bool ParseSEHDirectiveAllocStack(StringRef, SMLoc);
150 bool ParseSEHDirectiveSaveReg(StringRef, SMLoc);
151 bool ParseSEHDirectiveSaveXMM(StringRef, SMLoc);
152 bool ParseSEHDirectivePushFrame(StringRef, SMLoc);
153 bool ParseSEHDirectiveEndProlog(StringRef, SMLoc);
155 bool ParseAtUnwindOrAtExcept(bool &unwind, bool &except);
156 bool ParseSEHRegisterNumber(unsigned &RegNo);
157 bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc);
160 COFFAsmParser() = default;
163 } // end annonomous namespace.
165 static SectionKind computeSectionKind(unsigned Flags) {
166 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
167 return SectionKind::getText();
168 if (Flags & COFF::IMAGE_SCN_MEM_READ &&
169 (Flags & COFF::IMAGE_SCN_MEM_WRITE) == 0)
170 return SectionKind::getReadOnly();
171 return SectionKind::getData();
174 bool COFFAsmParser::ParseSectionFlags(StringRef SectionName,
175 StringRef FlagsString, unsigned *Flags) {
186 Discardable = 1 << 8,
189 bool ReadOnlyRemoved = false;
190 unsigned SecFlags = None;
192 for (char FlagChar : FlagsString) {
198 case 'b': // bss section
200 if (SecFlags & InitData)
201 return TokError("conflicting section flags 'b' and 'd'.");
205 case 'd': // data section
206 SecFlags |= InitData;
207 if (SecFlags & Alloc)
208 return TokError("conflicting section flags 'b' and 'd'.");
209 SecFlags &= ~NoWrite;
210 if ((SecFlags & NoLoad) == 0)
214 case 'n': // section is not loaded
219 case 'D': // discardable
220 SecFlags |= Discardable;
223 case 'r': // read-only
224 ReadOnlyRemoved = false;
226 if ((SecFlags & Code) == 0)
227 SecFlags |= InitData;
228 if ((SecFlags & NoLoad) == 0)
232 case 's': // shared section
233 SecFlags |= Shared | InitData;
234 SecFlags &= ~NoWrite;
235 if ((SecFlags & NoLoad) == 0)
239 case 'w': // writable
240 SecFlags &= ~NoWrite;
241 ReadOnlyRemoved = true;
244 case 'x': // executable section
246 if ((SecFlags & NoLoad) == 0)
248 if (!ReadOnlyRemoved)
252 case 'y': // not readable
253 SecFlags |= NoRead | NoWrite;
257 return TokError("unknown flag");
263 if (SecFlags == None)
267 *Flags |= COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE;
268 if (SecFlags & InitData)
269 *Flags |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
270 if ((SecFlags & Alloc) && (SecFlags & Load) == 0)
271 *Flags |= COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
272 if (SecFlags & NoLoad)
273 *Flags |= COFF::IMAGE_SCN_LNK_REMOVE;
274 if ((SecFlags & Discardable) ||
275 MCSectionCOFF::isImplicitlyDiscardable(SectionName))
276 *Flags |= COFF::IMAGE_SCN_MEM_DISCARDABLE;
277 if ((SecFlags & NoRead) == 0)
278 *Flags |= COFF::IMAGE_SCN_MEM_READ;
279 if ((SecFlags & NoWrite) == 0)
280 *Flags |= COFF::IMAGE_SCN_MEM_WRITE;
281 if (SecFlags & Shared)
282 *Flags |= COFF::IMAGE_SCN_MEM_SHARED;
287 /// ParseDirectiveSymbolAttribute
288 /// ::= { ".weak", ... } [ identifier ( , identifier )* ]
289 bool COFFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
290 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
291 .Case(".weak", MCSA_Weak)
292 .Default(MCSA_Invalid);
293 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
294 if (getLexer().isNot(AsmToken::EndOfStatement)) {
298 if (getParser().parseIdentifier(Name))
299 return TokError("expected identifier in directive");
301 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
303 getStreamer().EmitSymbolAttribute(Sym, Attr);
305 if (getLexer().is(AsmToken::EndOfStatement))
308 if (getLexer().isNot(AsmToken::Comma))
309 return TokError("unexpected token in directive");
318 bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
319 unsigned Characteristics,
321 return ParseSectionSwitch(Section, Characteristics, Kind, "", (COFF::COMDATType)0);
324 bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
325 unsigned Characteristics,
327 StringRef COMDATSymName,
328 COFF::COMDATType Type) {
329 if (getLexer().isNot(AsmToken::EndOfStatement))
330 return TokError("unexpected token in section switching directive");
333 getStreamer().SwitchSection(getContext().getCOFFSection(
334 Section, Characteristics, Kind, COMDATSymName, Type));
339 bool COFFAsmParser::ParseSectionName(StringRef &SectionName) {
340 if (!getLexer().is(AsmToken::Identifier))
343 SectionName = getTok().getIdentifier();
348 // .section name [, "flags"] [, identifier [ identifier ], identifier]
352 // b: BSS section (uninitialized data)
353 // d: data section (initialized data)
354 // n: "noload" section (removed by linker)
355 // D: Discardable section
356 // r: Readable section
358 // w: Writable section
359 // x: Executable section
360 // y: Not-readable section (clears 'r')
362 // Subsections are not supported.
363 bool COFFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
364 StringRef SectionName;
366 if (ParseSectionName(SectionName))
367 return TokError("expected identifier in directive");
369 unsigned Flags = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
370 COFF::IMAGE_SCN_MEM_READ |
371 COFF::IMAGE_SCN_MEM_WRITE;
373 if (getLexer().is(AsmToken::Comma)) {
376 if (getLexer().isNot(AsmToken::String))
377 return TokError("expected string in directive");
379 StringRef FlagsStr = getTok().getStringContents();
382 if (ParseSectionFlags(SectionName, FlagsStr, &Flags))
386 COFF::COMDATType Type = (COFF::COMDATType)0;
387 StringRef COMDATSymName;
388 if (getLexer().is(AsmToken::Comma)) {
389 Type = COFF::IMAGE_COMDAT_SELECT_ANY;
392 Flags |= COFF::IMAGE_SCN_LNK_COMDAT;
394 if (!getLexer().is(AsmToken::Identifier))
395 return TokError("expected comdat type such as 'discard' or 'largest' "
396 "after protection bits");
398 if (parseCOMDATType(Type))
401 if (getLexer().isNot(AsmToken::Comma))
402 return TokError("expected comma in directive");
405 if (getParser().parseIdentifier(COMDATSymName))
406 return TokError("expected identifier in directive");
409 if (getLexer().isNot(AsmToken::EndOfStatement))
410 return TokError("unexpected token in directive");
412 SectionKind Kind = computeSectionKind(Flags);
414 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
415 if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
416 Flags |= COFF::IMAGE_SCN_MEM_16BIT;
418 ParseSectionSwitch(SectionName, Flags, Kind, COMDATSymName, Type);
422 bool COFFAsmParser::ParseDirectiveDef(StringRef, SMLoc) {
423 StringRef SymbolName;
425 if (getParser().parseIdentifier(SymbolName))
426 return TokError("expected identifier in directive");
428 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
430 getStreamer().BeginCOFFSymbolDef(Sym);
436 bool COFFAsmParser::ParseDirectiveScl(StringRef, SMLoc) {
437 int64_t SymbolStorageClass;
438 if (getParser().parseAbsoluteExpression(SymbolStorageClass))
441 if (getLexer().isNot(AsmToken::EndOfStatement))
442 return TokError("unexpected token in directive");
445 getStreamer().EmitCOFFSymbolStorageClass(SymbolStorageClass);
449 bool COFFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
451 if (getParser().parseAbsoluteExpression(Type))
454 if (getLexer().isNot(AsmToken::EndOfStatement))
455 return TokError("unexpected token in directive");
458 getStreamer().EmitCOFFSymbolType(Type);
462 bool COFFAsmParser::ParseDirectiveEndef(StringRef, SMLoc) {
464 getStreamer().EndCOFFSymbolDef();
468 bool COFFAsmParser::ParseDirectiveSecRel32(StringRef, SMLoc) {
470 if (getParser().parseIdentifier(SymbolID))
471 return TokError("expected identifier in directive");
475 if (getLexer().is(AsmToken::Plus)) {
476 OffsetLoc = getLexer().getLoc();
477 if (getParser().parseAbsoluteExpression(Offset))
481 if (getLexer().isNot(AsmToken::EndOfStatement))
482 return TokError("unexpected token in directive");
484 if (Offset < 0 || Offset > std::numeric_limits<uint32_t>::max())
487 "invalid '.secrel32' directive offset, can't be less "
488 "than zero or greater than std::numeric_limits<uint32_t>::max()");
490 MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
493 getStreamer().EmitCOFFSecRel32(Symbol, Offset);
497 bool COFFAsmParser::ParseDirectiveRVA(StringRef, SMLoc) {
498 auto parseOp = [&]() -> bool {
500 if (getParser().parseIdentifier(SymbolID))
501 return TokError("expected identifier in directive");
505 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
506 OffsetLoc = getLexer().getLoc();
507 if (getParser().parseAbsoluteExpression(Offset))
511 if (Offset < std::numeric_limits<int32_t>::min() ||
512 Offset > std::numeric_limits<int32_t>::max())
513 return Error(OffsetLoc, "invalid '.rva' directive offset, can't be less "
514 "than -2147483648 or greater than "
517 MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
519 getStreamer().EmitCOFFImgRel32(Symbol, Offset);
523 if (getParser().parseMany(parseOp))
524 return addErrorSuffix(" in directive");
528 bool COFFAsmParser::ParseDirectiveSafeSEH(StringRef, SMLoc) {
530 if (getParser().parseIdentifier(SymbolID))
531 return TokError("expected identifier in directive");
533 if (getLexer().isNot(AsmToken::EndOfStatement))
534 return TokError("unexpected token in directive");
536 MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
539 getStreamer().EmitCOFFSafeSEH(Symbol);
543 bool COFFAsmParser::ParseDirectiveSecIdx(StringRef, SMLoc) {
545 if (getParser().parseIdentifier(SymbolID))
546 return TokError("expected identifier in directive");
548 if (getLexer().isNot(AsmToken::EndOfStatement))
549 return TokError("unexpected token in directive");
551 MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
554 getStreamer().EmitCOFFSectionIndex(Symbol);
558 bool COFFAsmParser::ParseDirectiveSymIdx(StringRef, SMLoc) {
560 if (getParser().parseIdentifier(SymbolID))
561 return TokError("expected identifier in directive");
563 if (getLexer().isNot(AsmToken::EndOfStatement))
564 return TokError("unexpected token in directive");
566 MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
569 getStreamer().EmitCOFFSymbolIndex(Symbol);
573 /// ::= [ identifier ]
574 bool COFFAsmParser::parseCOMDATType(COFF::COMDATType &Type) {
575 StringRef TypeId = getTok().getIdentifier();
577 Type = StringSwitch<COFF::COMDATType>(TypeId)
578 .Case("one_only", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES)
579 .Case("discard", COFF::IMAGE_COMDAT_SELECT_ANY)
580 .Case("same_size", COFF::IMAGE_COMDAT_SELECT_SAME_SIZE)
581 .Case("same_contents", COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH)
582 .Case("associative", COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
583 .Case("largest", COFF::IMAGE_COMDAT_SELECT_LARGEST)
584 .Case("newest", COFF::IMAGE_COMDAT_SELECT_NEWEST)
585 .Default((COFF::COMDATType)0);
588 return TokError(Twine("unrecognized COMDAT type '" + TypeId + "'"));
595 /// ParseDirectiveLinkOnce
596 /// ::= .linkonce [ identifier ]
597 bool COFFAsmParser::ParseDirectiveLinkOnce(StringRef, SMLoc Loc) {
598 COFF::COMDATType Type = COFF::IMAGE_COMDAT_SELECT_ANY;
599 if (getLexer().is(AsmToken::Identifier))
600 if (parseCOMDATType(Type))
603 const MCSectionCOFF *Current =
604 static_cast<const MCSectionCOFF *>(getStreamer().getCurrentSectionOnly());
606 if (Type == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
607 return Error(Loc, "cannot make section associative with .linkonce");
609 if (Current->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT)
610 return Error(Loc, Twine("section '") + Current->getSectionName() +
611 "' is already linkonce");
613 Current->setSelection(Type);
615 if (getLexer().isNot(AsmToken::EndOfStatement))
616 return TokError("unexpected token in directive");
621 bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc Loc) {
623 if (getParser().parseIdentifier(SymbolID))
626 if (getLexer().isNot(AsmToken::EndOfStatement))
627 return TokError("unexpected token in directive");
629 MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
632 getStreamer().EmitWinCFIStartProc(Symbol, Loc);
636 bool COFFAsmParser::ParseSEHDirectiveEndProc(StringRef, SMLoc Loc) {
638 getStreamer().EmitWinCFIEndProc(Loc);
642 bool COFFAsmParser::ParseSEHDirectiveStartChained(StringRef, SMLoc Loc) {
644 getStreamer().EmitWinCFIStartChained(Loc);
648 bool COFFAsmParser::ParseSEHDirectiveEndChained(StringRef, SMLoc Loc) {
650 getStreamer().EmitWinCFIEndChained(Loc);
654 bool COFFAsmParser::ParseSEHDirectiveHandler(StringRef, SMLoc Loc) {
656 if (getParser().parseIdentifier(SymbolID))
659 if (getLexer().isNot(AsmToken::Comma))
660 return TokError("you must specify one or both of @unwind or @except");
662 bool unwind = false, except = false;
663 if (ParseAtUnwindOrAtExcept(unwind, except))
665 if (getLexer().is(AsmToken::Comma)) {
667 if (ParseAtUnwindOrAtExcept(unwind, except))
670 if (getLexer().isNot(AsmToken::EndOfStatement))
671 return TokError("unexpected token in directive");
673 MCSymbol *handler = getContext().getOrCreateSymbol(SymbolID);
676 getStreamer().EmitWinEHHandler(handler, unwind, except, Loc);
680 bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc Loc) {
682 getStreamer().EmitWinEHHandlerData();
686 bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc Loc) {
688 if (ParseSEHRegisterNumber(Reg))
691 if (getLexer().isNot(AsmToken::EndOfStatement))
692 return TokError("unexpected token in directive");
695 getStreamer().EmitWinCFIPushReg(Reg, Loc);
699 bool COFFAsmParser::ParseSEHDirectiveSetFrame(StringRef, SMLoc Loc) {
702 if (ParseSEHRegisterNumber(Reg))
704 if (getLexer().isNot(AsmToken::Comma))
705 return TokError("you must specify a stack pointer offset");
708 if (getParser().parseAbsoluteExpression(Off))
711 if (getLexer().isNot(AsmToken::EndOfStatement))
712 return TokError("unexpected token in directive");
715 getStreamer().EmitWinCFISetFrame(Reg, Off, Loc);
719 bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc Loc) {
721 if (getParser().parseAbsoluteExpression(Size))
724 if (getLexer().isNot(AsmToken::EndOfStatement))
725 return TokError("unexpected token in directive");
728 getStreamer().EmitWinCFIAllocStack(Size, Loc);
732 bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc Loc) {
735 if (ParseSEHRegisterNumber(Reg))
737 if (getLexer().isNot(AsmToken::Comma))
738 return TokError("you must specify an offset on the stack");
741 if (getParser().parseAbsoluteExpression(Off))
744 if (getLexer().isNot(AsmToken::EndOfStatement))
745 return TokError("unexpected token in directive");
748 // FIXME: Err on %xmm* registers
749 getStreamer().EmitWinCFISaveReg(Reg, Off, Loc);
753 // FIXME: This method is inherently x86-specific. It should really be in the
755 bool COFFAsmParser::ParseSEHDirectiveSaveXMM(StringRef, SMLoc Loc) {
758 if (ParseSEHRegisterNumber(Reg))
760 if (getLexer().isNot(AsmToken::Comma))
761 return TokError("you must specify an offset on the stack");
764 if (getParser().parseAbsoluteExpression(Off))
767 if (getLexer().isNot(AsmToken::EndOfStatement))
768 return TokError("unexpected token in directive");
771 // FIXME: Err on non-%xmm* registers
772 getStreamer().EmitWinCFISaveXMM(Reg, Off, Loc);
776 bool COFFAsmParser::ParseSEHDirectivePushFrame(StringRef, SMLoc Loc) {
779 if (getLexer().is(AsmToken::At)) {
780 SMLoc startLoc = getLexer().getLoc();
782 if (!getParser().parseIdentifier(CodeID)) {
783 if (CodeID != "code")
784 return Error(startLoc, "expected @code");
789 if (getLexer().isNot(AsmToken::EndOfStatement))
790 return TokError("unexpected token in directive");
793 getStreamer().EmitWinCFIPushFrame(Code, Loc);
797 bool COFFAsmParser::ParseSEHDirectiveEndProlog(StringRef, SMLoc Loc) {
799 getStreamer().EmitWinCFIEndProlog(Loc);
803 bool COFFAsmParser::ParseAtUnwindOrAtExcept(bool &unwind, bool &except) {
804 StringRef identifier;
805 if (getLexer().isNot(AsmToken::At))
806 return TokError("a handler attribute must begin with '@'");
807 SMLoc startLoc = getLexer().getLoc();
809 if (getParser().parseIdentifier(identifier))
810 return Error(startLoc, "expected @unwind or @except");
811 if (identifier == "unwind")
813 else if (identifier == "except")
816 return Error(startLoc, "expected @unwind or @except");
820 bool COFFAsmParser::ParseSEHRegisterNumber(unsigned &RegNo) {
821 SMLoc startLoc = getLexer().getLoc();
822 if (getLexer().is(AsmToken::Percent)) {
823 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
826 if (getParser().getTargetParser().ParseRegister(LLVMRegNo,startLoc,endLoc))
830 // FIXME: TargetAsmInfo::getCalleeSavedRegs() commits a serious layering
831 // violation so this validation code is disabled.
833 // Check that this is a non-volatile register.
834 const unsigned *NVRegs = TAI.getCalleeSavedRegs();
836 for (i = 0; NVRegs[i] != 0; ++i)
837 if (NVRegs[i] == LLVMRegNo)
840 return Error(startLoc, "expected non-volatile register");
843 int SEHRegNo = MRI->getSEHRegNum(LLVMRegNo);
845 return Error(startLoc,"register can't be represented in SEH unwind info");
850 if (getParser().parseAbsoluteExpression(n))
853 return Error(startLoc, "register number is too high");
862 MCAsmParserExtension *createCOFFAsmParser() {
863 return new COFFAsmParser;
866 } // end namespace llvm