/3rd_party/llvm/lib/MC/MCParser/COFFAsmParser.cpp

https://code.google.com/p/softart/ · C++ · 777 lines · 603 code · 136 blank · 38 comment · 125 complexity · ef2f1bc74d2911ca7c4933de3757cb78 MD5 · raw file

  1. //===- COFFAsmParser.cpp - COFF Assembly Parser ---------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  10. #include "llvm/ADT/StringSwitch.h"
  11. #include "llvm/ADT/Twine.h"
  12. #include "llvm/MC/MCAsmInfo.h"
  13. #include "llvm/MC/MCContext.h"
  14. #include "llvm/MC/MCExpr.h"
  15. #include "llvm/MC/MCParser/MCAsmLexer.h"
  16. #include "llvm/MC/MCRegisterInfo.h"
  17. #include "llvm/MC/MCSectionCOFF.h"
  18. #include "llvm/MC/MCStreamer.h"
  19. #include "llvm/MC/MCTargetAsmParser.h"
  20. #include "llvm/Support/COFF.h"
  21. using namespace llvm;
  22. namespace {
  23. class COFFAsmParser : public MCAsmParserExtension {
  24. template<bool (COFFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
  25. void addDirectiveHandler(StringRef Directive) {
  26. MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
  27. this, HandleDirective<COFFAsmParser, HandlerMethod>);
  28. getParser().addDirectiveHandler(Directive, Handler);
  29. }
  30. bool ParseSectionSwitch(StringRef Section,
  31. unsigned Characteristics,
  32. SectionKind Kind);
  33. bool ParseSectionSwitch(StringRef Section, unsigned Characteristics,
  34. SectionKind Kind, StringRef COMDATSymName,
  35. COFF::COMDATType Type, const MCSectionCOFF *Assoc);
  36. bool ParseSectionName(StringRef &SectionName);
  37. bool ParseSectionFlags(StringRef FlagsString, unsigned* Flags);
  38. virtual void Initialize(MCAsmParser &Parser) {
  39. // Call the base implementation.
  40. MCAsmParserExtension::Initialize(Parser);
  41. addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveText>(".text");
  42. addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveData>(".data");
  43. addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveBSS>(".bss");
  44. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSection>(".section");
  45. addDirectiveHandler<&COFFAsmParser::ParseDirectiveDef>(".def");
  46. addDirectiveHandler<&COFFAsmParser::ParseDirectiveScl>(".scl");
  47. addDirectiveHandler<&COFFAsmParser::ParseDirectiveType>(".type");
  48. addDirectiveHandler<&COFFAsmParser::ParseDirectiveEndef>(".endef");
  49. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSecRel32>(".secrel32");
  50. addDirectiveHandler<&COFFAsmParser::ParseDirectiveLinkOnce>(".linkonce");
  51. // Win64 EH directives.
  52. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartProc>(
  53. ".seh_proc");
  54. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProc>(
  55. ".seh_endproc");
  56. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartChained>(
  57. ".seh_startchained");
  58. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndChained>(
  59. ".seh_endchained");
  60. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandler>(
  61. ".seh_handler");
  62. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandlerData>(
  63. ".seh_handlerdata");
  64. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushReg>(
  65. ".seh_pushreg");
  66. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSetFrame>(
  67. ".seh_setframe");
  68. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveAllocStack>(
  69. ".seh_stackalloc");
  70. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveReg>(
  71. ".seh_savereg");
  72. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveXMM>(
  73. ".seh_savexmm");
  74. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushFrame>(
  75. ".seh_pushframe");
  76. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProlog>(
  77. ".seh_endprologue");
  78. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
  79. }
  80. bool ParseSectionDirectiveText(StringRef, SMLoc) {
  81. return ParseSectionSwitch(".text",
  82. COFF::IMAGE_SCN_CNT_CODE
  83. | COFF::IMAGE_SCN_MEM_EXECUTE
  84. | COFF::IMAGE_SCN_MEM_READ,
  85. SectionKind::getText());
  86. }
  87. bool ParseSectionDirectiveData(StringRef, SMLoc) {
  88. return ParseSectionSwitch(".data",
  89. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
  90. | COFF::IMAGE_SCN_MEM_READ
  91. | COFF::IMAGE_SCN_MEM_WRITE,
  92. SectionKind::getDataRel());
  93. }
  94. bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
  95. return ParseSectionSwitch(".bss",
  96. COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
  97. | COFF::IMAGE_SCN_MEM_READ
  98. | COFF::IMAGE_SCN_MEM_WRITE,
  99. SectionKind::getBSS());
  100. }
  101. bool ParseDirectiveSection(StringRef, SMLoc);
  102. bool ParseDirectiveDef(StringRef, SMLoc);
  103. bool ParseDirectiveScl(StringRef, SMLoc);
  104. bool ParseDirectiveType(StringRef, SMLoc);
  105. bool ParseDirectiveEndef(StringRef, SMLoc);
  106. bool ParseDirectiveSecRel32(StringRef, SMLoc);
  107. bool parseCOMDATTypeAndAssoc(COFF::COMDATType &Type,
  108. const MCSectionCOFF *&Assoc);
  109. bool ParseDirectiveLinkOnce(StringRef, SMLoc);
  110. // Win64 EH directives.
  111. bool ParseSEHDirectiveStartProc(StringRef, SMLoc);
  112. bool ParseSEHDirectiveEndProc(StringRef, SMLoc);
  113. bool ParseSEHDirectiveStartChained(StringRef, SMLoc);
  114. bool ParseSEHDirectiveEndChained(StringRef, SMLoc);
  115. bool ParseSEHDirectiveHandler(StringRef, SMLoc);
  116. bool ParseSEHDirectiveHandlerData(StringRef, SMLoc);
  117. bool ParseSEHDirectivePushReg(StringRef, SMLoc);
  118. bool ParseSEHDirectiveSetFrame(StringRef, SMLoc);
  119. bool ParseSEHDirectiveAllocStack(StringRef, SMLoc);
  120. bool ParseSEHDirectiveSaveReg(StringRef, SMLoc);
  121. bool ParseSEHDirectiveSaveXMM(StringRef, SMLoc);
  122. bool ParseSEHDirectivePushFrame(StringRef, SMLoc);
  123. bool ParseSEHDirectiveEndProlog(StringRef, SMLoc);
  124. bool ParseAtUnwindOrAtExcept(bool &unwind, bool &except);
  125. bool ParseSEHRegisterNumber(unsigned &RegNo);
  126. bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc);
  127. public:
  128. COFFAsmParser() {}
  129. };
  130. } // end annonomous namespace.
  131. static SectionKind computeSectionKind(unsigned Flags) {
  132. if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
  133. return SectionKind::getText();
  134. if (Flags & COFF::IMAGE_SCN_MEM_READ &&
  135. (Flags & COFF::IMAGE_SCN_MEM_WRITE) == 0)
  136. return SectionKind::getReadOnly();
  137. return SectionKind::getDataRel();
  138. }
  139. bool COFFAsmParser::ParseSectionFlags(StringRef FlagsString, unsigned* Flags) {
  140. enum {
  141. None = 0,
  142. Alloc = 1 << 0,
  143. Code = 1 << 1,
  144. Load = 1 << 2,
  145. InitData = 1 << 3,
  146. Shared = 1 << 4,
  147. NoLoad = 1 << 5,
  148. NoRead = 1 << 6,
  149. NoWrite = 1 << 7
  150. };
  151. bool ReadOnlyRemoved = false;
  152. unsigned SecFlags = None;
  153. for (unsigned i = 0; i < FlagsString.size(); ++i) {
  154. switch (FlagsString[i]) {
  155. case 'a':
  156. // Ignored.
  157. break;
  158. case 'b': // bss section
  159. SecFlags |= Alloc;
  160. if (SecFlags & InitData)
  161. return TokError("conflicting section flags 'b' and 'd'.");
  162. SecFlags &= ~Load;
  163. break;
  164. case 'd': // data section
  165. SecFlags |= InitData;
  166. if (SecFlags & Alloc)
  167. return TokError("conflicting section flags 'b' and 'd'.");
  168. SecFlags &= ~NoWrite;
  169. if ((SecFlags & NoLoad) == 0)
  170. SecFlags |= Load;
  171. break;
  172. case 'n': // section is not loaded
  173. SecFlags |= NoLoad;
  174. SecFlags &= ~Load;
  175. break;
  176. case 'r': // read-only
  177. ReadOnlyRemoved = false;
  178. SecFlags |= NoWrite;
  179. if ((SecFlags & Code) == 0)
  180. SecFlags |= InitData;
  181. if ((SecFlags & NoLoad) == 0)
  182. SecFlags |= Load;
  183. break;
  184. case 's': // shared section
  185. SecFlags |= Shared | InitData;
  186. SecFlags &= ~NoWrite;
  187. if ((SecFlags & NoLoad) == 0)
  188. SecFlags |= Load;
  189. break;
  190. case 'w': // writable
  191. SecFlags &= ~NoWrite;
  192. ReadOnlyRemoved = true;
  193. break;
  194. case 'x': // executable section
  195. SecFlags |= Code;
  196. if ((SecFlags & NoLoad) == 0)
  197. SecFlags |= Load;
  198. if (!ReadOnlyRemoved)
  199. SecFlags |= NoWrite;
  200. break;
  201. case 'y': // not readable
  202. SecFlags |= NoRead | NoWrite;
  203. break;
  204. default:
  205. return TokError("unknown flag");
  206. }
  207. }
  208. *Flags = 0;
  209. if (SecFlags == None)
  210. SecFlags = InitData;
  211. if (SecFlags & Code)
  212. *Flags |= COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE;
  213. if (SecFlags & InitData)
  214. *Flags |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
  215. if ((SecFlags & Alloc) && (SecFlags & Load) == 0)
  216. *Flags |= COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
  217. if (SecFlags & NoLoad)
  218. *Flags |= COFF::IMAGE_SCN_LNK_REMOVE;
  219. if ((SecFlags & NoRead) == 0)
  220. *Flags |= COFF::IMAGE_SCN_MEM_READ;
  221. if ((SecFlags & NoWrite) == 0)
  222. *Flags |= COFF::IMAGE_SCN_MEM_WRITE;
  223. if (SecFlags & Shared)
  224. *Flags |= COFF::IMAGE_SCN_MEM_SHARED;
  225. return false;
  226. }
  227. /// ParseDirectiveSymbolAttribute
  228. /// ::= { ".weak", ... } [ identifier ( , identifier )* ]
  229. bool COFFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
  230. MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
  231. .Case(".weak", MCSA_Weak)
  232. .Default(MCSA_Invalid);
  233. assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
  234. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  235. for (;;) {
  236. StringRef Name;
  237. if (getParser().parseIdentifier(Name))
  238. return TokError("expected identifier in directive");
  239. MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
  240. getStreamer().EmitSymbolAttribute(Sym, Attr);
  241. if (getLexer().is(AsmToken::EndOfStatement))
  242. break;
  243. if (getLexer().isNot(AsmToken::Comma))
  244. return TokError("unexpected token in directive");
  245. Lex();
  246. }
  247. }
  248. Lex();
  249. return false;
  250. }
  251. bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
  252. unsigned Characteristics,
  253. SectionKind Kind) {
  254. return ParseSectionSwitch(Section, Characteristics, Kind, "",
  255. COFF::IMAGE_COMDAT_SELECT_ANY, 0);
  256. }
  257. bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
  258. unsigned Characteristics,
  259. SectionKind Kind,
  260. StringRef COMDATSymName,
  261. COFF::COMDATType Type,
  262. const MCSectionCOFF *Assoc) {
  263. if (getLexer().isNot(AsmToken::EndOfStatement))
  264. return TokError("unexpected token in section switching directive");
  265. Lex();
  266. getStreamer().SwitchSection(getContext().getCOFFSection(
  267. Section, Characteristics, Kind, COMDATSymName, Type, Assoc));
  268. return false;
  269. }
  270. bool COFFAsmParser::ParseSectionName(StringRef &SectionName) {
  271. if (!getLexer().is(AsmToken::Identifier))
  272. return true;
  273. SectionName = getTok().getIdentifier();
  274. Lex();
  275. return false;
  276. }
  277. // .section name [, "flags"] [, identifier [ identifier ], identifier]
  278. //
  279. // Supported flags:
  280. // a: Ignored.
  281. // b: BSS section (uninitialized data)
  282. // d: data section (initialized data)
  283. // n: Discardable section
  284. // r: Readable section
  285. // s: Shared section
  286. // w: Writable section
  287. // x: Executable section
  288. // y: Not-readable section (clears 'r')
  289. //
  290. // Subsections are not supported.
  291. bool COFFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
  292. StringRef SectionName;
  293. if (ParseSectionName(SectionName))
  294. return TokError("expected identifier in directive");
  295. unsigned Flags = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  296. COFF::IMAGE_SCN_MEM_READ |
  297. COFF::IMAGE_SCN_MEM_WRITE;
  298. if (getLexer().is(AsmToken::Comma)) {
  299. Lex();
  300. if (getLexer().isNot(AsmToken::String))
  301. return TokError("expected string in directive");
  302. StringRef FlagsStr = getTok().getStringContents();
  303. Lex();
  304. if (ParseSectionFlags(FlagsStr, &Flags))
  305. return true;
  306. }
  307. COFF::COMDATType Type = COFF::IMAGE_COMDAT_SELECT_ANY;
  308. const MCSectionCOFF *Assoc = 0;
  309. StringRef COMDATSymName;
  310. if (getLexer().is(AsmToken::Comma)) {
  311. Lex();
  312. Flags |= COFF::IMAGE_SCN_LNK_COMDAT;
  313. if (parseCOMDATTypeAndAssoc(Type, Assoc))
  314. return true;
  315. if (getLexer().isNot(AsmToken::Comma))
  316. return TokError("expected comma in directive");
  317. Lex();
  318. if (getParser().parseIdentifier(COMDATSymName))
  319. return TokError("expected identifier in directive");
  320. }
  321. if (getLexer().isNot(AsmToken::EndOfStatement))
  322. return TokError("unexpected token in directive");
  323. SectionKind Kind = computeSectionKind(Flags);
  324. ParseSectionSwitch(SectionName, Flags, Kind, COMDATSymName, Type, Assoc);
  325. return false;
  326. }
  327. bool COFFAsmParser::ParseDirectiveDef(StringRef, SMLoc) {
  328. StringRef SymbolName;
  329. if (getParser().parseIdentifier(SymbolName))
  330. return TokError("expected identifier in directive");
  331. MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
  332. getStreamer().BeginCOFFSymbolDef(Sym);
  333. Lex();
  334. return false;
  335. }
  336. bool COFFAsmParser::ParseDirectiveScl(StringRef, SMLoc) {
  337. int64_t SymbolStorageClass;
  338. if (getParser().parseAbsoluteExpression(SymbolStorageClass))
  339. return true;
  340. if (getLexer().isNot(AsmToken::EndOfStatement))
  341. return TokError("unexpected token in directive");
  342. Lex();
  343. getStreamer().EmitCOFFSymbolStorageClass(SymbolStorageClass);
  344. return false;
  345. }
  346. bool COFFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
  347. int64_t Type;
  348. if (getParser().parseAbsoluteExpression(Type))
  349. return true;
  350. if (getLexer().isNot(AsmToken::EndOfStatement))
  351. return TokError("unexpected token in directive");
  352. Lex();
  353. getStreamer().EmitCOFFSymbolType(Type);
  354. return false;
  355. }
  356. bool COFFAsmParser::ParseDirectiveEndef(StringRef, SMLoc) {
  357. Lex();
  358. getStreamer().EndCOFFSymbolDef();
  359. return false;
  360. }
  361. bool COFFAsmParser::ParseDirectiveSecRel32(StringRef, SMLoc) {
  362. StringRef SymbolID;
  363. if (getParser().parseIdentifier(SymbolID))
  364. return true;
  365. if (getLexer().isNot(AsmToken::EndOfStatement))
  366. return TokError("unexpected token in directive");
  367. MCSymbol *Symbol = getContext().GetOrCreateSymbol(SymbolID);
  368. Lex();
  369. getStreamer().EmitCOFFSecRel32(Symbol);
  370. return false;
  371. }
  372. /// ::= [ identifier [ identifier ] ]
  373. bool COFFAsmParser::parseCOMDATTypeAndAssoc(COFF::COMDATType &Type,
  374. const MCSectionCOFF *&Assoc) {
  375. StringRef TypeId = getTok().getIdentifier();
  376. Type = StringSwitch<COFF::COMDATType>(TypeId)
  377. .Case("one_only", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES)
  378. .Case("discard", COFF::IMAGE_COMDAT_SELECT_ANY)
  379. .Case("same_size", COFF::IMAGE_COMDAT_SELECT_SAME_SIZE)
  380. .Case("same_contents", COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH)
  381. .Case("associative", COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  382. .Case("largest", COFF::IMAGE_COMDAT_SELECT_LARGEST)
  383. .Case("newest", COFF::IMAGE_COMDAT_SELECT_NEWEST)
  384. .Default((COFF::COMDATType)0);
  385. if (Type == 0)
  386. return TokError(Twine("unrecognized COMDAT type '" + TypeId + "'"));
  387. Lex();
  388. if (Type == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
  389. SMLoc Loc = getTok().getLoc();
  390. StringRef AssocName;
  391. if (ParseSectionName(AssocName))
  392. return TokError("expected associated section name");
  393. Assoc = static_cast<const MCSectionCOFF*>(
  394. getContext().getCOFFSection(AssocName));
  395. if (!Assoc)
  396. return Error(Loc, "cannot associate unknown section '" + AssocName + "'");
  397. if (!(Assoc->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT))
  398. return Error(Loc, "associated section must be a COMDAT section");
  399. if (Assoc->getSelection() == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  400. return Error(Loc, "associated section cannot be itself associative");
  401. }
  402. return false;
  403. }
  404. /// ParseDirectiveLinkOnce
  405. /// ::= .linkonce [ identifier [ identifier ] ]
  406. bool COFFAsmParser::ParseDirectiveLinkOnce(StringRef, SMLoc Loc) {
  407. COFF::COMDATType Type = COFF::IMAGE_COMDAT_SELECT_ANY;
  408. const MCSectionCOFF *Assoc = 0;
  409. if (getLexer().is(AsmToken::Identifier))
  410. if (parseCOMDATTypeAndAssoc(Type, Assoc))
  411. return true;
  412. const MCSectionCOFF *Current = static_cast<const MCSectionCOFF*>(
  413. getStreamer().getCurrentSection().first);
  414. if (Type == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
  415. if (Assoc == Current)
  416. return Error(Loc, "cannot associate a section with itself");
  417. }
  418. if (Current->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT)
  419. return Error(Loc, Twine("section '") + Current->getSectionName() +
  420. "' is already linkonce");
  421. Current->setSelection(Type, Assoc);
  422. if (getLexer().isNot(AsmToken::EndOfStatement))
  423. return TokError("unexpected token in directive");
  424. return false;
  425. }
  426. bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc) {
  427. StringRef SymbolID;
  428. if (getParser().parseIdentifier(SymbolID))
  429. return true;
  430. if (getLexer().isNot(AsmToken::EndOfStatement))
  431. return TokError("unexpected token in directive");
  432. MCSymbol *Symbol = getContext().GetOrCreateSymbol(SymbolID);
  433. Lex();
  434. getStreamer().EmitWin64EHStartProc(Symbol);
  435. return false;
  436. }
  437. bool COFFAsmParser::ParseSEHDirectiveEndProc(StringRef, SMLoc) {
  438. Lex();
  439. getStreamer().EmitWin64EHEndProc();
  440. return false;
  441. }
  442. bool COFFAsmParser::ParseSEHDirectiveStartChained(StringRef, SMLoc) {
  443. Lex();
  444. getStreamer().EmitWin64EHStartChained();
  445. return false;
  446. }
  447. bool COFFAsmParser::ParseSEHDirectiveEndChained(StringRef, SMLoc) {
  448. Lex();
  449. getStreamer().EmitWin64EHEndChained();
  450. return false;
  451. }
  452. bool COFFAsmParser::ParseSEHDirectiveHandler(StringRef, SMLoc) {
  453. StringRef SymbolID;
  454. if (getParser().parseIdentifier(SymbolID))
  455. return true;
  456. if (getLexer().isNot(AsmToken::Comma))
  457. return TokError("you must specify one or both of @unwind or @except");
  458. Lex();
  459. bool unwind = false, except = false;
  460. if (ParseAtUnwindOrAtExcept(unwind, except))
  461. return true;
  462. if (getLexer().is(AsmToken::Comma)) {
  463. Lex();
  464. if (ParseAtUnwindOrAtExcept(unwind, except))
  465. return true;
  466. }
  467. if (getLexer().isNot(AsmToken::EndOfStatement))
  468. return TokError("unexpected token in directive");
  469. MCSymbol *handler = getContext().GetOrCreateSymbol(SymbolID);
  470. Lex();
  471. getStreamer().EmitWin64EHHandler(handler, unwind, except);
  472. return false;
  473. }
  474. bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc) {
  475. Lex();
  476. getStreamer().EmitWin64EHHandlerData();
  477. return false;
  478. }
  479. bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) {
  480. unsigned Reg;
  481. if (ParseSEHRegisterNumber(Reg))
  482. return true;
  483. if (getLexer().isNot(AsmToken::EndOfStatement))
  484. return TokError("unexpected token in directive");
  485. Lex();
  486. getStreamer().EmitWin64EHPushReg(Reg);
  487. return false;
  488. }
  489. bool COFFAsmParser::ParseSEHDirectiveSetFrame(StringRef, SMLoc L) {
  490. unsigned Reg;
  491. int64_t Off;
  492. if (ParseSEHRegisterNumber(Reg))
  493. return true;
  494. if (getLexer().isNot(AsmToken::Comma))
  495. return TokError("you must specify a stack pointer offset");
  496. Lex();
  497. SMLoc startLoc = getLexer().getLoc();
  498. if (getParser().parseAbsoluteExpression(Off))
  499. return true;
  500. if (Off & 0x0F)
  501. return Error(startLoc, "offset is not a multiple of 16");
  502. if (getLexer().isNot(AsmToken::EndOfStatement))
  503. return TokError("unexpected token in directive");
  504. Lex();
  505. getStreamer().EmitWin64EHSetFrame(Reg, Off);
  506. return false;
  507. }
  508. bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc) {
  509. int64_t Size;
  510. SMLoc startLoc = getLexer().getLoc();
  511. if (getParser().parseAbsoluteExpression(Size))
  512. return true;
  513. if (Size & 7)
  514. return Error(startLoc, "size is not a multiple of 8");
  515. if (getLexer().isNot(AsmToken::EndOfStatement))
  516. return TokError("unexpected token in directive");
  517. Lex();
  518. getStreamer().EmitWin64EHAllocStack(Size);
  519. return false;
  520. }
  521. bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) {
  522. unsigned Reg;
  523. int64_t Off;
  524. if (ParseSEHRegisterNumber(Reg))
  525. return true;
  526. if (getLexer().isNot(AsmToken::Comma))
  527. return TokError("you must specify an offset on the stack");
  528. Lex();
  529. SMLoc startLoc = getLexer().getLoc();
  530. if (getParser().parseAbsoluteExpression(Off))
  531. return true;
  532. if (Off & 7)
  533. return Error(startLoc, "size is not a multiple of 8");
  534. if (getLexer().isNot(AsmToken::EndOfStatement))
  535. return TokError("unexpected token in directive");
  536. Lex();
  537. // FIXME: Err on %xmm* registers
  538. getStreamer().EmitWin64EHSaveReg(Reg, Off);
  539. return false;
  540. }
  541. // FIXME: This method is inherently x86-specific. It should really be in the
  542. // x86 backend.
  543. bool COFFAsmParser::ParseSEHDirectiveSaveXMM(StringRef, SMLoc L) {
  544. unsigned Reg;
  545. int64_t Off;
  546. if (ParseSEHRegisterNumber(Reg))
  547. return true;
  548. if (getLexer().isNot(AsmToken::Comma))
  549. return TokError("you must specify an offset on the stack");
  550. Lex();
  551. SMLoc startLoc = getLexer().getLoc();
  552. if (getParser().parseAbsoluteExpression(Off))
  553. return true;
  554. if (getLexer().isNot(AsmToken::EndOfStatement))
  555. return TokError("unexpected token in directive");
  556. if (Off & 0x0F)
  557. return Error(startLoc, "offset is not a multiple of 16");
  558. Lex();
  559. // FIXME: Err on non-%xmm* registers
  560. getStreamer().EmitWin64EHSaveXMM(Reg, Off);
  561. return false;
  562. }
  563. bool COFFAsmParser::ParseSEHDirectivePushFrame(StringRef, SMLoc) {
  564. bool Code = false;
  565. StringRef CodeID;
  566. if (getLexer().is(AsmToken::At)) {
  567. SMLoc startLoc = getLexer().getLoc();
  568. Lex();
  569. if (!getParser().parseIdentifier(CodeID)) {
  570. if (CodeID != "code")
  571. return Error(startLoc, "expected @code");
  572. Code = true;
  573. }
  574. }
  575. if (getLexer().isNot(AsmToken::EndOfStatement))
  576. return TokError("unexpected token in directive");
  577. Lex();
  578. getStreamer().EmitWin64EHPushFrame(Code);
  579. return false;
  580. }
  581. bool COFFAsmParser::ParseSEHDirectiveEndProlog(StringRef, SMLoc) {
  582. Lex();
  583. getStreamer().EmitWin64EHEndProlog();
  584. return false;
  585. }
  586. bool COFFAsmParser::ParseAtUnwindOrAtExcept(bool &unwind, bool &except) {
  587. StringRef identifier;
  588. if (getLexer().isNot(AsmToken::At))
  589. return TokError("a handler attribute must begin with '@'");
  590. SMLoc startLoc = getLexer().getLoc();
  591. Lex();
  592. if (getParser().parseIdentifier(identifier))
  593. return Error(startLoc, "expected @unwind or @except");
  594. if (identifier == "unwind")
  595. unwind = true;
  596. else if (identifier == "except")
  597. except = true;
  598. else
  599. return Error(startLoc, "expected @unwind or @except");
  600. return false;
  601. }
  602. bool COFFAsmParser::ParseSEHRegisterNumber(unsigned &RegNo) {
  603. SMLoc startLoc = getLexer().getLoc();
  604. if (getLexer().is(AsmToken::Percent)) {
  605. const MCRegisterInfo *MRI = getContext().getRegisterInfo();
  606. SMLoc endLoc;
  607. unsigned LLVMRegNo;
  608. if (getParser().getTargetParser().ParseRegister(LLVMRegNo,startLoc,endLoc))
  609. return true;
  610. #if 0
  611. // FIXME: TargetAsmInfo::getCalleeSavedRegs() commits a serious layering
  612. // violation so this validation code is disabled.
  613. // Check that this is a non-volatile register.
  614. const unsigned *NVRegs = TAI.getCalleeSavedRegs();
  615. unsigned i;
  616. for (i = 0; NVRegs[i] != 0; ++i)
  617. if (NVRegs[i] == LLVMRegNo)
  618. break;
  619. if (NVRegs[i] == 0)
  620. return Error(startLoc, "expected non-volatile register");
  621. #endif
  622. int SEHRegNo = MRI->getSEHRegNum(LLVMRegNo);
  623. if (SEHRegNo < 0)
  624. return Error(startLoc,"register can't be represented in SEH unwind info");
  625. RegNo = SEHRegNo;
  626. }
  627. else {
  628. int64_t n;
  629. if (getParser().parseAbsoluteExpression(n))
  630. return true;
  631. if (n > 15)
  632. return Error(startLoc, "register number is too high");
  633. RegNo = n;
  634. }
  635. return false;
  636. }
  637. namespace llvm {
  638. MCAsmParserExtension *createCOFFAsmParser() {
  639. return new COFFAsmParser;
  640. }
  641. }