OSDN Git Service

Implement the rest of the SEH directive-parsing methods in the COFFAsmParser.
[android-x86/external-llvm.git] / lib / MC / MCParser / COFFAsmParser.cpp
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
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/Target/TargetAsmInfo.h"
19 #include "llvm/Target/TargetAsmParser.h"
20 #include "llvm/Support/COFF.h"
21 using namespace llvm;
22
23 namespace {
24
25 class COFFAsmParser : public MCAsmParserExtension {
26   template<bool (COFFAsmParser::*Handler)(StringRef, SMLoc)>
27   void AddDirectiveHandler(StringRef Directive) {
28     getParser().AddDirectiveHandler(this, Directive,
29                                     HandleDirective<COFFAsmParser, Handler>);
30   }
31
32   bool ParseSectionSwitch(StringRef Section,
33                           unsigned Characteristics,
34                           SectionKind Kind);
35
36   virtual void Initialize(MCAsmParser &Parser) {
37     // Call the base implementation.
38     MCAsmParserExtension::Initialize(Parser);
39
40     AddDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveText>(".text");
41     AddDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveData>(".data");
42     AddDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveBSS>(".bss");
43     AddDirectiveHandler<&COFFAsmParser::ParseDirectiveDef>(".def");
44     AddDirectiveHandler<&COFFAsmParser::ParseDirectiveScl>(".scl");
45     AddDirectiveHandler<&COFFAsmParser::ParseDirectiveType>(".type");
46     AddDirectiveHandler<&COFFAsmParser::ParseDirectiveEndef>(".endef");
47
48     // Win64 EH directives.
49     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartProc>(
50                                                                    ".seh_proc");
51     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProc>(
52                                                                 ".seh_endproc");
53     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartChained>(
54                                                            ".seh_startchained");
55     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndChained>(
56                                                              ".seh_endchained");
57     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandler>(
58                                                                 ".seh_handler");
59     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandlerData>(
60                                                             ".seh_handlerdata");
61     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushReg>(
62                                                                 ".seh_pushreg");
63     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSetFrame>(
64                                                                ".seh_setframe");
65     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveAllocStack>(
66                                                              ".seh_stackalloc");
67     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveReg>(
68                                                                 ".seh_savereg");
69     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveSaveXMM>(
70                                                                 ".seh_savexmm");
71     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectivePushFrame>(
72                                                               ".seh_pushframe");
73     AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProlog>(
74                                                             ".seh_endprologue");
75   }
76
77   bool ParseSectionDirectiveText(StringRef, SMLoc) {
78     return ParseSectionSwitch(".text",
79                               COFF::IMAGE_SCN_CNT_CODE
80                             | COFF::IMAGE_SCN_MEM_EXECUTE
81                             | COFF::IMAGE_SCN_MEM_READ,
82                               SectionKind::getText());
83   }
84   bool ParseSectionDirectiveData(StringRef, SMLoc) {
85     return ParseSectionSwitch(".data",
86                               COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
87                             | COFF::IMAGE_SCN_MEM_READ
88                             | COFF::IMAGE_SCN_MEM_WRITE,
89                               SectionKind::getDataRel());
90   }
91   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
92     return ParseSectionSwitch(".bss",
93                               COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
94                             | COFF::IMAGE_SCN_MEM_READ
95                             | COFF::IMAGE_SCN_MEM_WRITE,
96                               SectionKind::getBSS());
97   }
98
99   bool ParseDirectiveDef(StringRef, SMLoc);
100   bool ParseDirectiveScl(StringRef, SMLoc);
101   bool ParseDirectiveType(StringRef, SMLoc);
102   bool ParseDirectiveEndef(StringRef, SMLoc);
103
104   // Win64 EH directives.
105   bool ParseSEHDirectiveStartProc(StringRef, SMLoc);
106   bool ParseSEHDirectiveEndProc(StringRef, SMLoc);
107   bool ParseSEHDirectiveStartChained(StringRef, SMLoc);
108   bool ParseSEHDirectiveEndChained(StringRef, SMLoc);
109   bool ParseSEHDirectiveHandler(StringRef, SMLoc);
110   bool ParseSEHDirectiveHandlerData(StringRef, SMLoc);
111   bool ParseSEHDirectivePushReg(StringRef, SMLoc);
112   bool ParseSEHDirectiveSetFrame(StringRef, SMLoc);
113   bool ParseSEHDirectiveAllocStack(StringRef, SMLoc);
114   bool ParseSEHDirectiveSaveReg(StringRef, SMLoc);
115   bool ParseSEHDirectiveSaveXMM(StringRef, SMLoc);
116   bool ParseSEHDirectivePushFrame(StringRef, SMLoc);
117   bool ParseSEHDirectiveEndProlog(StringRef, SMLoc);
118
119   bool ParseAtUnwindOrAtExcept(bool &unwind, bool &except);
120   bool ParseSEHRegisterNumber(unsigned &RegNo);
121 public:
122   COFFAsmParser() {}
123 };
124
125 } // end annonomous namespace.
126
127 bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
128                                        unsigned Characteristics,
129                                        SectionKind Kind) {
130   if (getLexer().isNot(AsmToken::EndOfStatement))
131     return TokError("unexpected token in section switching directive");
132   Lex();
133
134   getStreamer().SwitchSection(getContext().getCOFFSection(
135                                 Section, Characteristics, Kind));
136
137   return false;
138 }
139
140 bool COFFAsmParser::ParseDirectiveDef(StringRef, SMLoc) {
141   StringRef SymbolName;
142
143   if (getParser().ParseIdentifier(SymbolName))
144     return TokError("expected identifier in directive");
145
146   MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
147
148   getStreamer().BeginCOFFSymbolDef(Sym);
149
150   Lex();
151   return false;
152 }
153
154 bool COFFAsmParser::ParseDirectiveScl(StringRef, SMLoc) {
155   int64_t SymbolStorageClass;
156   if (getParser().ParseAbsoluteExpression(SymbolStorageClass))
157     return true;
158
159   if (getLexer().isNot(AsmToken::EndOfStatement))
160     return TokError("unexpected token in directive");
161
162   Lex();
163   getStreamer().EmitCOFFSymbolStorageClass(SymbolStorageClass);
164   return false;
165 }
166
167 bool COFFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
168   int64_t Type;
169   if (getParser().ParseAbsoluteExpression(Type))
170     return true;
171
172   if (getLexer().isNot(AsmToken::EndOfStatement))
173     return TokError("unexpected token in directive");
174
175   Lex();
176   getStreamer().EmitCOFFSymbolType(Type);
177   return false;
178 }
179
180 bool COFFAsmParser::ParseDirectiveEndef(StringRef, SMLoc) {
181   Lex();
182   getStreamer().EndCOFFSymbolDef();
183   return false;
184 }
185
186 bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc) {
187   const MCExpr *e;
188   const MCSymbolRefExpr *funcExpr;
189   SMLoc startLoc = getLexer().getLoc();
190   if (getParser().ParseExpression(e))
191     return true;
192
193   if (!(funcExpr = dyn_cast<MCSymbolRefExpr>(e)))
194     return Error(startLoc, "expected symbol");
195
196   if (getLexer().isNot(AsmToken::EndOfStatement))
197     return TokError("unexpected token in directive");
198
199   Lex();
200   getStreamer().EmitWin64EHStartProc(&funcExpr->getSymbol());
201   return false;
202 }
203
204 bool COFFAsmParser::ParseSEHDirectiveEndProc(StringRef, SMLoc) {
205   Lex();
206   getStreamer().EmitWin64EHEndProc();
207   return false;
208 }
209
210 bool COFFAsmParser::ParseSEHDirectiveStartChained(StringRef, SMLoc) {
211   Lex();
212   getStreamer().EmitWin64EHStartChained();
213   return false;
214 }
215
216 bool COFFAsmParser::ParseSEHDirectiveEndChained(StringRef, SMLoc) {
217   Lex();
218   getStreamer().EmitWin64EHEndChained();
219   return false;
220 }
221
222 bool COFFAsmParser::ParseSEHDirectiveHandler(StringRef, SMLoc) {
223   const MCExpr *e;
224   const MCSymbolRefExpr *funcExpr;
225   SMLoc startLoc = getLexer().getLoc();
226   if (getParser().ParseExpression(e))
227     return true;
228
229   if (!(funcExpr = dyn_cast<MCSymbolRefExpr>(e)))
230     return Error(startLoc, "expected symbol");
231
232   bool unwind = false, except = false;
233   startLoc = getLexer().getLoc();
234   if (!ParseAtUnwindOrAtExcept(unwind, except))
235     return Error(startLoc,"you must specify one or both of @unwind or @except");
236   if (getLexer().is(AsmToken::Comma)) {
237     Lex();
238     if (!ParseAtUnwindOrAtExcept(unwind, except))
239       return true;
240   }
241   if (getLexer().isNot(AsmToken::EndOfStatement))
242     return TokError("unexpected token in directive");
243
244   Lex();
245   getStreamer().EmitWin64EHHandler(&funcExpr->getSymbol(), unwind, except);
246   return false;
247 }
248
249 bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc) {
250   Lex();
251   getStreamer().EmitWin64EHHandlerData();
252   return false;
253 }
254
255 bool COFFAsmParser::ParseSEHDirectivePushReg(StringRef, SMLoc L) {
256   unsigned Reg;
257   if (ParseSEHRegisterNumber(Reg))
258     return true;
259
260   if (getLexer().isNot(AsmToken::EndOfStatement))
261     return TokError("unexpected token in directive");
262
263   Lex();
264   getStreamer().EmitWin64EHPushReg(Reg);
265   return false;
266 }
267
268 bool COFFAsmParser::ParseSEHDirectiveSetFrame(StringRef, SMLoc L) {
269   unsigned Reg;
270   int64_t Off;
271   if (ParseSEHRegisterNumber(Reg))
272     return true;
273   SMLoc startLoc = getLexer().getLoc();
274   if (getParser().ParseAbsoluteExpression(Off))
275     return true;
276
277   if (Off & 0x0F)
278     return Error(startLoc, "offset is not a multiple of 16");
279
280   if (getLexer().isNot(AsmToken::EndOfStatement))
281     return TokError("unexpected token in directive");
282
283   Lex();
284   getStreamer().EmitWin64EHSetFrame(Reg, Off);
285   return false;
286 }
287
288 bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc) {
289   int64_t Size;
290   SMLoc startLoc = getLexer().getLoc();
291   if (getParser().ParseAbsoluteExpression(Size))
292     return true;
293
294   if (Size & 7)
295     return Error(startLoc, "size is not a multiple of 8");
296
297   if (getLexer().isNot(AsmToken::EndOfStatement))
298     return TokError("unexpected token in directive");
299
300   Lex();
301   getStreamer().EmitWin64EHAllocStack(Size);
302   return false;
303 }
304
305 bool COFFAsmParser::ParseSEHDirectiveSaveReg(StringRef, SMLoc L) {
306   unsigned Reg;
307   int64_t Off;
308   if (ParseSEHRegisterNumber(Reg))
309     return true;
310   SMLoc startLoc = getLexer().getLoc();
311   if (getParser().ParseAbsoluteExpression(Off))
312     return true;
313
314   if (Off & 7)
315     return Error(startLoc, "size is not a multiple of 8");
316
317   if (getLexer().isNot(AsmToken::EndOfStatement))
318     return TokError("unexpected token in directive");
319
320   Lex();
321   // FIXME: Err on %xmm* registers
322   getStreamer().EmitWin64EHSaveReg(Reg, Off);
323   return false;
324 }
325
326 // FIXME: This method is inherently x86-specific. It should really be in the
327 // x86 backend.
328 bool COFFAsmParser::ParseSEHDirectiveSaveXMM(StringRef, SMLoc L) {
329   unsigned Reg;
330   int64_t Off;
331   if (ParseSEHRegisterNumber(Reg))
332     return true;
333   SMLoc startLoc = getLexer().getLoc();
334   if (getParser().ParseAbsoluteExpression(Off))
335     return true;
336
337   if (getLexer().isNot(AsmToken::EndOfStatement))
338     return TokError("unexpected token in directive");
339
340   if (Off & 0x0F)
341     return Error(startLoc, "offset is not a multiple of 16");
342
343   Lex();
344   // FIXME: Err on non-%xmm* registers
345   getStreamer().EmitWin64EHSaveXMM(Reg, Off);
346   return false;
347 }
348
349 bool COFFAsmParser::ParseSEHDirectivePushFrame(StringRef, SMLoc) {
350   bool Code;
351   StringRef CodeID;
352   SMLoc startLoc = getLexer().getLoc();
353   if (!getParser().ParseIdentifier(CodeID)) {
354     if (CodeID != "@code")
355       return Error(startLoc, "expected @code");
356     Code = true;
357   }
358
359   if (getLexer().isNot(AsmToken::EndOfStatement))
360     return TokError("unexpected token in directive");
361
362   Lex();
363   getStreamer().EmitWin64EHPushFrame(Code);
364   return false;
365 }
366
367 bool COFFAsmParser::ParseSEHDirectiveEndProlog(StringRef, SMLoc) {
368   Lex();
369   getStreamer().EmitWin64EHEndProlog();
370   return false;
371 }
372
373 bool COFFAsmParser::ParseAtUnwindOrAtExcept(bool &unwind, bool &except) {
374   StringRef identifier;
375   SMLoc startLoc = getLexer().getLoc();
376   if (!getParser().ParseIdentifier(identifier))
377     return Error(startLoc, "expected @unwind or @except");
378   if (identifier == "@unwind")
379     unwind = true;
380   else if (identifier == "@except")
381     except = true;
382   else
383     return Error(startLoc, "expected @unwind or @except");
384   return false;
385 }
386
387 bool COFFAsmParser::ParseSEHRegisterNumber(unsigned &RegNo) {
388   int64_t n;
389   SMLoc startLoc = getLexer().getLoc();
390   if (getParser().ParseAbsoluteExpression(n)) {
391     const TargetAsmInfo &asmInfo = getContext().getTargetAsmInfo();
392     SMLoc endLoc;
393     unsigned LLVMRegNo;
394     if (getParser().getTargetParser().ParseRegister(LLVMRegNo,startLoc,endLoc))
395       return Error(startLoc, "expected register or number");
396
397     // Check that this is a non-volatile register.
398     const unsigned *NVRegs = asmInfo.getCalleeSavedRegs();
399     unsigned i;
400     for (i = 0; NVRegs[i] != 0; ++i)
401       if (NVRegs[i] == LLVMRegNo)
402         break;
403     if (NVRegs[i] == 0)
404       return Error(startLoc, "expected non-volatile register");
405
406     int SEHRegNo = asmInfo.getSEHRegNum(LLVMRegNo);
407     if (SEHRegNo < 0)
408       return Error(startLoc,"register can't be represented in SEH unwind info");
409     RegNo = SEHRegNo;
410   }
411   else
412     RegNo = n;
413
414   if (RegNo > 15)
415     return Error(startLoc, "register number is too high");
416   return false;
417 }
418
419 namespace llvm {
420
421 MCAsmParserExtension *createCOFFAsmParser() {
422   return new COFFAsmParser;
423 }
424
425 }