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/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCRegisterInfo.h"
18 #include "llvm/MC/MCSectionCOFF.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCTargetAsmParser.h"
21 #include "llvm/Support/COFF.h"
26 class COFFAsmParser : public MCAsmParserExtension {
27 template<bool (COFFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
28 void addDirectiveHandler(StringRef Directive) {
29 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
30 this, HandleDirective<COFFAsmParser, HandlerMethod>);
31 getParser().addDirectiveHandler(Directive, Handler);
34 bool ParseSectionSwitch(StringRef Section,
35 unsigned Characteristics,
38 bool ParseSectionSwitch(StringRef Section, unsigned Characteristics,
39 SectionKind Kind, StringRef COMDATSymName,
40 COFF::COMDATType Type, const MCSectionCOFF *Assoc);
42 bool ParseSectionName(StringRef &SectionName);
43 bool ParseSectionFlags(StringRef FlagsString, unsigned* Flags);
45 void Initialize(MCAsmParser &Parser) override {
46 // Call the base implementation.
47 MCAsmParserExtension::Initialize(Parser);
49 addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveText>(".text");
50 addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveData>(".data");
51 addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveBSS>(".bss");
52 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSection>(".section");
53 addDirectiveHandler<&COFFAsmParser::ParseDirectiveDef>(".def");
54 addDirectiveHandler<&COFFAsmParser::ParseDirectiveScl>(".scl");
55 addDirectiveHandler<&COFFAsmParser::ParseDirectiveType>(".type");
56 addDirectiveHandler<&COFFAsmParser::ParseDirectiveEndef>(".endef");
57 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSecRel32>(".secrel32");
58 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSecIdx>(".secidx");
59 addDirectiveHandler<&COFFAsmParser::ParseDirectiveLinkOnce>(".linkonce");
61 // Win64 EH directives.
62 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartProc>(
64 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProc>(
66 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartChained>(
68 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndChained>(
70 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandler>(
72 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandlerData>(
74 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushReg>(
76 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSetFrame>(
78 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveAllocStack>(
80 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveReg>(
82 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveXMM>(
84 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushFrame>(
86 addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProlog>(
88 addDirectiveHandler<&COFFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
91 bool ParseSectionDirectiveText(StringRef, SMLoc) {
92 return ParseSectionSwitch(".text",
93 COFF::IMAGE_SCN_CNT_CODE
94 | COFF::IMAGE_SCN_MEM_EXECUTE
95 | COFF::IMAGE_SCN_MEM_READ,
96 SectionKind::getText());
98 bool ParseSectionDirectiveData(StringRef, SMLoc) {
99 return ParseSectionSwitch(".data",
100 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
101 | COFF::IMAGE_SCN_MEM_READ
102 | COFF::IMAGE_SCN_MEM_WRITE,
103 SectionKind::getDataRel());
105 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
106 return ParseSectionSwitch(".bss",
107 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
108 | COFF::IMAGE_SCN_MEM_READ
109 | COFF::IMAGE_SCN_MEM_WRITE,
110 SectionKind::getBSS());
113 bool ParseDirectiveSection(StringRef, SMLoc);
114 bool ParseDirectiveDef(StringRef, SMLoc);
115 bool ParseDirectiveScl(StringRef, SMLoc);
116 bool ParseDirectiveType(StringRef, SMLoc);
117 bool ParseDirectiveEndef(StringRef, SMLoc);
118 bool ParseDirectiveSecRel32(StringRef, SMLoc);
119 bool ParseDirectiveSecIdx(StringRef, SMLoc);
120 bool parseCOMDATTypeAndAssoc(COFF::COMDATType &Type,
121 const MCSectionCOFF *&Assoc);
122 bool ParseDirectiveLinkOnce(StringRef, SMLoc);
124 // Win64 EH directives.
125 bool ParseSEHDirectiveStartProc(StringRef, SMLoc);
126 bool ParseSEHDirectiveEndProc(StringRef, SMLoc);
127 bool ParseSEHDirectiveStartChained(StringRef, SMLoc);
128 bool ParseSEHDirectiveEndChained(StringRef, SMLoc);
129 bool ParseSEHDirectiveHandler(StringRef, SMLoc);
130 bool ParseSEHDirectiveHandlerData(StringRef, SMLoc);
131 bool ParseSEHDirectivePushReg(StringRef, SMLoc);
132 bool ParseSEHDirectiveSetFrame(StringRef, SMLoc);
133 bool ParseSEHDirectiveAllocStack(StringRef, SMLoc);
134 bool ParseSEHDirectiveSaveReg(StringRef, SMLoc);
135 bool ParseSEHDirectiveSaveXMM(StringRef, SMLoc);
136 bool ParseSEHDirectivePushFrame(StringRef, SMLoc);
137 bool ParseSEHDirectiveEndProlog(StringRef, SMLoc);
139 bool ParseAtUnwindOrAtExcept(bool &unwind, bool &except);
140 bool ParseSEHRegisterNumber(unsigned &RegNo);
141 bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc);
146 } // end annonomous namespace.
148 static SectionKind computeSectionKind(unsigned Flags) {
149 if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
150 return SectionKind::getText();
151 if (Flags & COFF::IMAGE_SCN_MEM_READ &&
152 (Flags & COFF::IMAGE_SCN_MEM_WRITE) == 0)
153 return SectionKind::getReadOnly();
154 return SectionKind::getDataRel();
157 bool COFFAsmParser::ParseSectionFlags(StringRef FlagsString, unsigned* Flags) {
170 bool ReadOnlyRemoved = false;
171 unsigned SecFlags = None;
173 for (unsigned i = 0; i < FlagsString.size(); ++i) {
174 switch (FlagsString[i]) {
179 case 'b': // bss section
181 if (SecFlags & InitData)
182 return TokError("conflicting section flags 'b' and 'd'.");
186 case 'd': // data section
187 SecFlags |= InitData;
188 if (SecFlags & Alloc)
189 return TokError("conflicting section flags 'b' and 'd'.");
190 SecFlags &= ~NoWrite;
191 if ((SecFlags & NoLoad) == 0)
195 case 'n': // section is not loaded
200 case 'r': // read-only
201 ReadOnlyRemoved = false;
203 if ((SecFlags & Code) == 0)
204 SecFlags |= InitData;
205 if ((SecFlags & NoLoad) == 0)
209 case 's': // shared section
210 SecFlags |= Shared | InitData;
211 SecFlags &= ~NoWrite;
212 if ((SecFlags & NoLoad) == 0)
216 case 'w': // writable
217 SecFlags &= ~NoWrite;
218 ReadOnlyRemoved = true;
221 case 'x': // executable section
223 if ((SecFlags & NoLoad) == 0)
225 if (!ReadOnlyRemoved)
229 case 'y': // not readable
230 SecFlags |= NoRead | NoWrite;
234 return TokError("unknown flag");
240 if (SecFlags == None)
244 *Flags |= COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE;
245 if (SecFlags & InitData)
246 *Flags |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
247 if ((SecFlags & Alloc) && (SecFlags & Load) == 0)
248 *Flags |= COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
249 if (SecFlags & NoLoad)
250 *Flags |= COFF::IMAGE_SCN_LNK_REMOVE;
251 if ((SecFlags & NoRead) == 0)
252 *Flags |= COFF::IMAGE_SCN_MEM_READ;
253 if ((SecFlags & NoWrite) == 0)
254 *Flags |= COFF::IMAGE_SCN_MEM_WRITE;
255 if (SecFlags & Shared)
256 *Flags |= COFF::IMAGE_SCN_MEM_SHARED;
261 /// ParseDirectiveSymbolAttribute
262 /// ::= { ".weak", ... } [ identifier ( , identifier )* ]
263 bool COFFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
264 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
265 .Case(".weak", MCSA_Weak)
266 .Default(MCSA_Invalid);
267 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
268 if (getLexer().isNot(AsmToken::EndOfStatement)) {
272 if (getParser().parseIdentifier(Name))
273 return TokError("expected identifier in directive");
275 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
277 getStreamer().EmitSymbolAttribute(Sym, Attr);
279 if (getLexer().is(AsmToken::EndOfStatement))
282 if (getLexer().isNot(AsmToken::Comma))
283 return TokError("unexpected token in directive");
292 bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
293 unsigned Characteristics,
295 return ParseSectionSwitch(Section, Characteristics, Kind, "",
296 COFF::IMAGE_COMDAT_SELECT_ANY, nullptr);
299 bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
300 unsigned Characteristics,
302 StringRef COMDATSymName,
303 COFF::COMDATType Type,
304 const MCSectionCOFF *Assoc) {
305 if (getLexer().isNot(AsmToken::EndOfStatement))
306 return TokError("unexpected token in section switching directive");
309 getStreamer().SwitchSection(getContext().getCOFFSection(
310 Section, Characteristics, Kind, COMDATSymName, Type, Assoc));
315 bool COFFAsmParser::ParseSectionName(StringRef &SectionName) {
316 if (!getLexer().is(AsmToken::Identifier))
319 SectionName = getTok().getIdentifier();
324 // .section name [, "flags"] [, identifier [ identifier ], identifier]
328 // b: BSS section (uninitialized data)
329 // d: data section (initialized data)
330 // n: Discardable section
331 // r: Readable section
333 // w: Writable section
334 // x: Executable section
335 // y: Not-readable section (clears 'r')
337 // Subsections are not supported.
338 bool COFFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
339 StringRef SectionName;
341 if (ParseSectionName(SectionName))
342 return TokError("expected identifier in directive");
344 unsigned Flags = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
345 COFF::IMAGE_SCN_MEM_READ |
346 COFF::IMAGE_SCN_MEM_WRITE;
348 if (getLexer().is(AsmToken::Comma)) {
351 if (getLexer().isNot(AsmToken::String))
352 return TokError("expected string in directive");
354 StringRef FlagsStr = getTok().getStringContents();
357 if (ParseSectionFlags(FlagsStr, &Flags))
361 COFF::COMDATType Type = COFF::IMAGE_COMDAT_SELECT_ANY;
362 const MCSectionCOFF *Assoc = nullptr;
363 StringRef COMDATSymName;
364 if (getLexer().is(AsmToken::Comma)) {
367 Flags |= COFF::IMAGE_SCN_LNK_COMDAT;
369 if (parseCOMDATTypeAndAssoc(Type, Assoc))
372 if (getLexer().isNot(AsmToken::Comma))
373 return TokError("expected comma in directive");
376 if (getParser().parseIdentifier(COMDATSymName))
377 return TokError("expected identifier in directive");
380 if (getLexer().isNot(AsmToken::EndOfStatement))
381 return TokError("unexpected token in directive");
383 SectionKind Kind = computeSectionKind(Flags);
384 ParseSectionSwitch(SectionName, Flags, Kind, COMDATSymName, Type, Assoc);
388 bool COFFAsmParser::ParseDirectiveDef(StringRef, SMLoc) {
389 StringRef SymbolName;
391 if (getParser().parseIdentifier(SymbolName))
392 return TokError("expected identifier in directive");
394 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
396 getStreamer().BeginCOFFSymbolDef(Sym);
402 bool COFFAsmParser::ParseDirectiveScl(StringRef, SMLoc) {
403 int64_t SymbolStorageClass;
404 if (getParser().parseAbsoluteExpression(SymbolStorageClass))
407 if (getLexer().isNot(AsmToken::EndOfStatement))
408 return TokError("unexpected token in directive");
411 getStreamer().EmitCOFFSymbolStorageClass(SymbolStorageClass);
415 bool COFFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
417 if (getParser().parseAbsoluteExpression(Type))
420 if (getLexer().isNot(AsmToken::EndOfStatement))
421 return TokError("unexpected token in directive");
424 getStreamer().EmitCOFFSymbolType(Type);
428 bool COFFAsmParser::ParseDirectiveEndef(StringRef, SMLoc) {
430 getStreamer().EndCOFFSymbolDef();
434 bool COFFAsmParser::ParseDirectiveSecRel32(StringRef, SMLoc) {
436 if (getParser().parseIdentifier(SymbolID))
437 return TokError("expected identifier in directive");
439 if (getLexer().isNot(AsmToken::EndOfStatement))
440 return TokError("unexpected token in directive");
442 MCSymbol *Symbol = getContext().GetOrCreateSymbol(SymbolID);
445 getStreamer().EmitCOFFSecRel32(Symbol);
449 bool COFFAsmParser::ParseDirectiveSecIdx(StringRef, SMLoc) {
451 if (getParser().parseIdentifier(SymbolID))
452 return TokError("expected identifier in directive");
454 if (getLexer().isNot(AsmToken::EndOfStatement))
455 return TokError("unexpected token in directive");
457 MCSymbol *Symbol = getContext().GetOrCreateSymbol(SymbolID);
460 getStreamer().EmitCOFFSectionIndex(Symbol);
464 /// ::= [ identifier [ identifier ] ]
465 bool COFFAsmParser::parseCOMDATTypeAndAssoc(COFF::COMDATType &Type,
466 const MCSectionCOFF *&Assoc) {
467 StringRef TypeId = getTok().getIdentifier();
469 Type = StringSwitch<COFF::COMDATType>(TypeId)
470 .Case("one_only", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES)
471 .Case("discard", COFF::IMAGE_COMDAT_SELECT_ANY)
472 .Case("same_size", COFF::IMAGE_COMDAT_SELECT_SAME_SIZE)
473 .Case("same_contents", COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH)
474 .Case("associative", COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
475 .Case("largest", COFF::IMAGE_COMDAT_SELECT_LARGEST)
476 .Case("newest", COFF::IMAGE_COMDAT_SELECT_NEWEST)
477 .Default((COFF::COMDATType)0);
480 return TokError(Twine("unrecognized COMDAT type '" + TypeId + "'"));
484 if (Type == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
485 SMLoc Loc = getTok().getLoc();
487 if (ParseSectionName(AssocName))
488 return TokError("expected associated section name");
490 Assoc = static_cast<const MCSectionCOFF*>(
491 getContext().getCOFFSection(AssocName));
493 return Error(Loc, "cannot associate unknown section '" + AssocName + "'");
494 if (!(Assoc->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT))
495 return Error(Loc, "associated section must be a COMDAT section");
496 if (Assoc->getSelection() == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
497 return Error(Loc, "associated section cannot be itself associative");
503 /// ParseDirectiveLinkOnce
504 /// ::= .linkonce [ identifier [ identifier ] ]
505 bool COFFAsmParser::ParseDirectiveLinkOnce(StringRef, SMLoc Loc) {
506 COFF::COMDATType Type = COFF::IMAGE_COMDAT_SELECT_ANY;
507 const MCSectionCOFF *Assoc = nullptr;
508 if (getLexer().is(AsmToken::Identifier))
509 if (parseCOMDATTypeAndAssoc(Type, Assoc))
512 const MCSectionCOFF *Current = static_cast<const MCSectionCOFF*>(
513 getStreamer().getCurrentSection().first);
516 if (Type == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
517 if (Assoc == Current)
518 return Error(Loc, "cannot associate a section with itself");
521 if (Current->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT)
522 return Error(Loc, Twine("section '") + Current->getSectionName() +
523 "' is already linkonce");
525 Current->setSelection(Type, Assoc);
527 if (getLexer().isNot(AsmToken::EndOfStatement))
528 return TokError("unexpected token in directive");
533 bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc) {
535 if (getParser().parseIdentifier(SymbolID))
538 if (getLexer().isNot(AsmToken::EndOfStatement))
539 return TokError("unexpected token in directive");
541 MCSymbol *Symbol = getContext().GetOrCreateSymbol(SymbolID);
544 getStreamer().EmitWin64EHStartProc(Symbol);
548 bool COFFAsmParser::ParseSEHDirectiveEndProc(StringRef, SMLoc) {
550 getStreamer().EmitWin64EHEndProc();
554 bool COFFAsmParser::ParseSEHDirectiveStartChained(StringRef, SMLoc) {
556 getStreamer().EmitWin64EHStartChained();
560 bool COFFAsmParser::ParseSEHDirectiveEndChained(StringRef, SMLoc) {
562 getStreamer().EmitWin64EHEndChained();
566 bool COFFAsmParser::ParseSEHDirectiveHandler(StringRef, SMLoc) {
568 if (getParser().parseIdentifier(SymbolID))
571 if (getLexer().isNot(AsmToken::Comma))
572 return TokError("you must specify one or both of @unwind or @except");
574 bool unwind = false, except = false;
575 if (ParseAtUnwindOrAtExcept(unwind, except))
577 if (getLexer().is(AsmToken::Comma)) {
579 if (ParseAtUnwindOrAtExcept(unwind, except))
582 if (getLexer().isNot(AsmToken::EndOfStatement))
583 return TokError("unexpected token in directive");
585 MCSymbol *handler = getContext().GetOrCreateSymbol(SymbolID);
588 getStreamer().EmitWin64EHHandler(handler, unwind, except);
592 bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc) {
594 getStreamer().EmitWin64EHHandlerData();
598 bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) {
600 if (ParseSEHRegisterNumber(Reg))
603 if (getLexer().isNot(AsmToken::EndOfStatement))
604 return TokError("unexpected token in directive");
607 getStreamer().EmitWin64EHPushReg(Reg);
611 bool COFFAsmParser::ParseSEHDirectiveSetFrame(StringRef, SMLoc L) {
614 if (ParseSEHRegisterNumber(Reg))
616 if (getLexer().isNot(AsmToken::Comma))
617 return TokError("you must specify a stack pointer offset");
620 SMLoc startLoc = getLexer().getLoc();
621 if (getParser().parseAbsoluteExpression(Off))
625 return Error(startLoc, "offset is not a multiple of 16");
627 if (getLexer().isNot(AsmToken::EndOfStatement))
628 return TokError("unexpected token in directive");
631 getStreamer().EmitWin64EHSetFrame(Reg, Off);
635 bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc) {
637 SMLoc startLoc = getLexer().getLoc();
638 if (getParser().parseAbsoluteExpression(Size))
642 return Error(startLoc, "size is not a multiple of 8");
644 if (getLexer().isNot(AsmToken::EndOfStatement))
645 return TokError("unexpected token in directive");
648 getStreamer().EmitWin64EHAllocStack(Size);
652 bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) {
655 if (ParseSEHRegisterNumber(Reg))
657 if (getLexer().isNot(AsmToken::Comma))
658 return TokError("you must specify an offset on the stack");
661 SMLoc startLoc = getLexer().getLoc();
662 if (getParser().parseAbsoluteExpression(Off))
666 return Error(startLoc, "size is not a multiple of 8");
668 if (getLexer().isNot(AsmToken::EndOfStatement))
669 return TokError("unexpected token in directive");
672 // FIXME: Err on %xmm* registers
673 getStreamer().EmitWin64EHSaveReg(Reg, Off);
677 // FIXME: This method is inherently x86-specific. It should really be in the
679 bool COFFAsmParser::ParseSEHDirectiveSaveXMM(StringRef, SMLoc L) {
682 if (ParseSEHRegisterNumber(Reg))
684 if (getLexer().isNot(AsmToken::Comma))
685 return TokError("you must specify an offset on the stack");
688 SMLoc startLoc = getLexer().getLoc();
689 if (getParser().parseAbsoluteExpression(Off))
692 if (getLexer().isNot(AsmToken::EndOfStatement))
693 return TokError("unexpected token in directive");
696 return Error(startLoc, "offset is not a multiple of 16");
699 // FIXME: Err on non-%xmm* registers
700 getStreamer().EmitWin64EHSaveXMM(Reg, Off);
704 bool COFFAsmParser::ParseSEHDirectivePushFrame(StringRef, SMLoc) {
707 if (getLexer().is(AsmToken::At)) {
708 SMLoc startLoc = getLexer().getLoc();
710 if (!getParser().parseIdentifier(CodeID)) {
711 if (CodeID != "code")
712 return Error(startLoc, "expected @code");
717 if (getLexer().isNot(AsmToken::EndOfStatement))
718 return TokError("unexpected token in directive");
721 getStreamer().EmitWin64EHPushFrame(Code);
725 bool COFFAsmParser::ParseSEHDirectiveEndProlog(StringRef, SMLoc) {
727 getStreamer().EmitWin64EHEndProlog();
731 bool COFFAsmParser::ParseAtUnwindOrAtExcept(bool &unwind, bool &except) {
732 StringRef identifier;
733 if (getLexer().isNot(AsmToken::At))
734 return TokError("a handler attribute must begin with '@'");
735 SMLoc startLoc = getLexer().getLoc();
737 if (getParser().parseIdentifier(identifier))
738 return Error(startLoc, "expected @unwind or @except");
739 if (identifier == "unwind")
741 else if (identifier == "except")
744 return Error(startLoc, "expected @unwind or @except");
748 bool COFFAsmParser::ParseSEHRegisterNumber(unsigned &RegNo) {
749 SMLoc startLoc = getLexer().getLoc();
750 if (getLexer().is(AsmToken::Percent)) {
751 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
754 if (getParser().getTargetParser().ParseRegister(LLVMRegNo,startLoc,endLoc))
758 // FIXME: TargetAsmInfo::getCalleeSavedRegs() commits a serious layering
759 // violation so this validation code is disabled.
761 // Check that this is a non-volatile register.
762 const unsigned *NVRegs = TAI.getCalleeSavedRegs();
764 for (i = 0; NVRegs[i] != 0; ++i)
765 if (NVRegs[i] == LLVMRegNo)
768 return Error(startLoc, "expected non-volatile register");
771 int SEHRegNo = MRI->getSEHRegNum(LLVMRegNo);
773 return Error(startLoc,"register can't be represented in SEH unwind info");
778 if (getParser().parseAbsoluteExpression(n))
781 return Error(startLoc, "register number is too high");
790 MCAsmParserExtension *createCOFFAsmParser() {
791 return new COFFAsmParser;