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/Twine.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCParser/MCAsmLexer.h"
15 #include "llvm/MC/MCSectionCOFF.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/Support/COFF.h"
23 class COFFAsmParser : public MCAsmParserExtension {
24 template<bool (COFFAsmParser::*Handler)(StringRef, SMLoc)>
25 void AddDirectiveHandler(StringRef Directive) {
26 getParser().AddDirectiveHandler(this, Directive,
27 HandleDirective<COFFAsmParser, Handler>);
30 bool ParseSectionSwitch(StringRef Section,
31 unsigned Characteristics,
34 virtual void Initialize(MCAsmParser &Parser) {
35 // Call the base implementation.
36 MCAsmParserExtension::Initialize(Parser);
38 AddDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveText>(".text");
39 AddDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveData>(".data");
40 AddDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveBSS>(".bss");
41 AddDirectiveHandler<&COFFAsmParser::ParseDirectiveDef>(".def");
42 AddDirectiveHandler<&COFFAsmParser::ParseDirectiveScl>(".scl");
43 AddDirectiveHandler<&COFFAsmParser::ParseDirectiveType>(".type");
44 AddDirectiveHandler<&COFFAsmParser::ParseDirectiveEndef>(".endef");
46 // Win64 EH directives.
47 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartProc>(
49 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProc>(
51 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartChained>(
53 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndChained>(
55 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandler>(
57 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandlerData>(
59 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushReg>(
61 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSetFrame>(
63 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveAllocStack>(
65 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveReg>(
67 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveXMM>(
69 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushFrame>(
71 AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProlog>(
75 bool ParseSectionDirectiveText(StringRef, SMLoc) {
76 return ParseSectionSwitch(".text",
77 COFF::IMAGE_SCN_CNT_CODE
78 | COFF::IMAGE_SCN_MEM_EXECUTE
79 | COFF::IMAGE_SCN_MEM_READ,
80 SectionKind::getText());
82 bool ParseSectionDirectiveData(StringRef, SMLoc) {
83 return ParseSectionSwitch(".data",
84 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
85 | COFF::IMAGE_SCN_MEM_READ
86 | COFF::IMAGE_SCN_MEM_WRITE,
87 SectionKind::getDataRel());
89 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
90 return ParseSectionSwitch(".bss",
91 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
92 | COFF::IMAGE_SCN_MEM_READ
93 | COFF::IMAGE_SCN_MEM_WRITE,
94 SectionKind::getBSS());
97 bool ParseDirectiveDef(StringRef, SMLoc);
98 bool ParseDirectiveScl(StringRef, SMLoc);
99 bool ParseDirectiveType(StringRef, SMLoc);
100 bool ParseDirectiveEndef(StringRef, SMLoc);
102 // Win64 EH directives.
103 bool ParseSEHDirectiveStartProc(StringRef, SMLoc);
104 bool ParseSEHDirectiveEndProc(StringRef, SMLoc);
105 bool ParseSEHDirectiveStartChained(StringRef, SMLoc);
106 bool ParseSEHDirectiveEndChained(StringRef, SMLoc);
107 bool ParseSEHDirectiveHandler(StringRef, SMLoc);
108 bool ParseSEHDirectiveHandlerData(StringRef, SMLoc);
109 bool ParseSEHDirectivePushReg(StringRef, SMLoc L);
110 bool ParseSEHDirectiveSetFrame(StringRef, SMLoc L);
111 bool ParseSEHDirectiveAllocStack(StringRef, SMLoc L);
112 bool ParseSEHDirectiveSaveReg(StringRef, SMLoc L);
113 bool ParseSEHDirectiveSaveXMM(StringRef, SMLoc L);
114 bool ParseSEHDirectivePushFrame(StringRef, SMLoc L);
115 bool ParseSEHDirectiveEndProlog(StringRef, SMLoc);
117 bool ParseAtUnwindOrAtExcept(bool &unwind, bool &except);
122 } // end annonomous namespace.
124 bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
125 unsigned Characteristics,
127 if (getLexer().isNot(AsmToken::EndOfStatement))
128 return TokError("unexpected token in section switching directive");
131 getStreamer().SwitchSection(getContext().getCOFFSection(
132 Section, Characteristics, Kind));
137 bool COFFAsmParser::ParseDirectiveDef(StringRef, SMLoc) {
138 StringRef SymbolName;
140 if (getParser().ParseIdentifier(SymbolName))
141 return TokError("expected identifier in directive");
143 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
145 getStreamer().BeginCOFFSymbolDef(Sym);
151 bool COFFAsmParser::ParseDirectiveScl(StringRef, SMLoc) {
152 int64_t SymbolStorageClass;
153 if (getParser().ParseAbsoluteExpression(SymbolStorageClass))
156 if (getLexer().isNot(AsmToken::EndOfStatement))
157 return TokError("unexpected token in directive");
160 getStreamer().EmitCOFFSymbolStorageClass(SymbolStorageClass);
164 bool COFFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
166 if (getParser().ParseAbsoluteExpression(Type))
169 if (getLexer().isNot(AsmToken::EndOfStatement))
170 return TokError("unexpected token in directive");
173 getStreamer().EmitCOFFSymbolType(Type);
177 bool COFFAsmParser::ParseDirectiveEndef(StringRef, SMLoc) {
179 getStreamer().EndCOFFSymbolDef();
183 bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc) {
185 const MCSymbolRefExpr *funcExpr;
186 SMLoc startLoc = getLexer().getLoc();
187 if (getParser().ParseExpression(e))
190 if (!(funcExpr = dyn_cast<MCSymbolRefExpr>(e)))
191 return Error(startLoc, "expected symbol");
193 if (getLexer().isNot(AsmToken::EndOfStatement))
194 return TokError("unexpected token in directive");
197 getStreamer().EmitWin64EHStartProc(&funcExpr->getSymbol());
201 bool COFFAsmParser::ParseSEHDirectiveEndProc(StringRef, SMLoc) {
203 getStreamer().EmitWin64EHEndProc();
207 bool COFFAsmParser::ParseSEHDirectiveStartChained(StringRef, SMLoc) {
209 getStreamer().EmitWin64EHStartChained();
213 bool COFFAsmParser::ParseSEHDirectiveEndChained(StringRef, SMLoc) {
215 getStreamer().EmitWin64EHEndChained();
219 bool COFFAsmParser::ParseSEHDirectiveHandler(StringRef, SMLoc) {
221 const MCSymbolRefExpr *funcExpr;
222 SMLoc startLoc = getLexer().getLoc();
223 if (getParser().ParseExpression(e))
226 if (!(funcExpr = dyn_cast<MCSymbolRefExpr>(e)))
227 return Error(startLoc, "expected symbol");
229 bool unwind = false, except = false;
230 if (!ParseAtUnwindOrAtExcept(unwind, except))
232 if (getLexer().is(AsmToken::Comma)) {
234 if (!ParseAtUnwindOrAtExcept(unwind, except))
237 if (getLexer().isNot(AsmToken::EndOfStatement))
238 return TokError("unexpected token in directive");
241 getStreamer().EmitWin64EHHandler(&funcExpr->getSymbol(), unwind, except);
245 bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc) {
247 getStreamer().EmitWin64EHHandlerData();
251 bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) {
252 return Error(L, "not implemented yet");
255 bool COFFAsmParser::ParseSEHDirectiveSetFrame(StringRef, SMLoc L) {
256 return Error(L, "not implemented yet");
259 bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc) {
261 if (getParser().ParseAbsoluteExpression(Size))
264 if (getLexer().isNot(AsmToken::EndOfStatement))
265 return TokError("unexpected token in directive");
268 getStreamer().EmitWin64EHAllocStack(Size);
272 bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) {
273 return Error(L, "not implemented yet");
276 bool COFFAsmParser::ParseSEHDirectiveSaveXMM(StringRef, SMLoc L) {
277 return Error(L, "not implemented yet");
280 bool COFFAsmParser::ParseSEHDirectivePushFrame(StringRef, SMLoc) {
283 SMLoc startLoc = getLexer().getLoc();
284 if (!getParser().ParseIdentifier(CodeID)) {
285 if (CodeID != "@code")
286 return Error(startLoc, "expected @code");
290 if (getLexer().isNot(AsmToken::EndOfStatement))
291 return TokError("unexpected token in directive");
294 getStreamer().EmitWin64EHPushFrame(Code);
298 bool COFFAsmParser::ParseSEHDirectiveEndProlog(StringRef, SMLoc) {
300 getStreamer().EmitWin64EHEndProlog();
304 bool COFFAsmParser::ParseAtUnwindOrAtExcept(bool &unwind, bool &except) {
305 StringRef identifier;
306 SMLoc startLoc = getLexer().getLoc();
307 if (!getParser().ParseIdentifier(identifier))
308 return Error(startLoc, "expected @unwind or @except");
309 if (identifier == "@unwind")
311 else if (identifier == "@except")
314 return Error(startLoc, "expected @unwind or @except");
320 MCAsmParserExtension *createCOFFAsmParser() {
321 return new COFFAsmParser;