blob: 2e328d9f6fd31cf99bdb733a01ac9180e57b03f5 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.h - Parser for Assembly Files ------------------*- C++ -*-===//
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// This class declares the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef ASMPARSER_H
15#define ASMPARSER_H
16
Kevin Enderbyd9f95292009-08-07 22:46:00 +000017#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000018#include "AsmLexer.h"
Kevin Enderbyd9f95292009-08-07 22:46:00 +000019#include "AsmCond.h"
Daniel Dunbar3c2a8932009-07-20 18:55:04 +000020#include "llvm/MC/MCAsmParser.h"
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara5508c82009-06-30 00:33:19 +000022#include "llvm/MC/MCStreamer.h"
Chris Lattnerb0133452009-06-21 20:16:42 +000023
24namespace llvm {
Kevin Enderbyd9f95292009-08-07 22:46:00 +000025class AsmCond;
Chris Lattner3f5738d2009-06-24 04:31:49 +000026class MCContext;
Daniel Dunbar115e4d62009-08-31 08:06:59 +000027class MCExpr;
Chris Lattner3417d712009-06-23 18:41:30 +000028class MCInst;
Chris Lattner92ffdd12009-06-24 00:52:40 +000029class MCStreamer;
Daniel Dunbarbd4bf3d2009-06-30 01:49:52 +000030class MCValue;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +000031class TargetAsmParser;
Daniel Dunbareb6bb322009-07-27 23:20:52 +000032class Twine;
Daniel Dunbarbd4bf3d2009-06-30 01:49:52 +000033
Daniel Dunbarf59ee962009-07-28 20:47:52 +000034class AsmParser : public MCAsmParser {
Daniel Dunbard0a08e02009-06-30 23:38:38 +000035private:
Chris Lattnerb0133452009-06-21 20:16:42 +000036 AsmLexer Lexer;
Chris Lattner3f5738d2009-06-24 04:31:49 +000037 MCContext &Ctx;
Chris Lattner92ffdd12009-06-24 00:52:40 +000038 MCStreamer &Out;
Daniel Dunbarf59ee962009-07-28 20:47:52 +000039 TargetAsmParser *TargetParser;
Kevin Enderbyd9f95292009-08-07 22:46:00 +000040
41 AsmCond TheCondState;
42 std::vector<AsmCond> TheCondStack;
43
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000044 // FIXME: Figure out where this should leave, the code is a copy of that which
45 // is also used by TargetLoweringObjectFile.
46 mutable void *SectionUniquingMap;
47
Chris Lattnerb0133452009-06-21 20:16:42 +000048public:
Daniel Dunbarf59ee962009-07-28 20:47:52 +000049 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out)
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000050 : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0),
51 SectionUniquingMap(0) {}
52 ~AsmParser();
Daniel Dunbarf59ee962009-07-28 20:47:52 +000053
Chris Lattnerb0133452009-06-21 20:16:42 +000054 bool Run();
55
Daniel Dunbar3c2a8932009-07-20 18:55:04 +000056public:
Daniel Dunbarf59ee962009-07-28 20:47:52 +000057 TargetAsmParser &getTargetParser() const { return *TargetParser; }
58 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
Daniel Dunbar3c2a8932009-07-20 18:55:04 +000059
Daniel Dunbarc43267a2009-07-28 22:22:31 +000060 /// @name MCAsmParser Interface
61 /// {
62
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +000063 virtual MCAsmLexer &getLexer() { return Lexer; }
64
Daniel Dunbar940cda22009-08-31 08:07:44 +000065 virtual MCContext &getContext() { return Ctx; }
66
Daniel Dunbarc43267a2009-07-28 22:22:31 +000067 virtual void Warning(SMLoc L, const Twine &Meg);
68
69 virtual bool Error(SMLoc L, const Twine &Msg);
70
Daniel Dunbarf3636452009-08-31 08:07:22 +000071 virtual bool ParseExpression(const MCExpr *&Res);
Daniel Dunbarc43267a2009-07-28 22:22:31 +000072
Daniel Dunbar7c82d562009-08-31 08:08:17 +000073 virtual bool ParseParenExpression(const MCExpr *&Res);
74
Daniel Dunbarc43267a2009-07-28 22:22:31 +000075 virtual bool ParseAbsoluteExpression(int64_t &Res);
76
77 virtual bool ParseRelocatableExpression(MCValue &Res);
78
Daniel Dunbar940cda22009-08-31 08:07:44 +000079 virtual bool ParseParenRelocatableExpression(MCValue &Res);
80
Daniel Dunbarc43267a2009-07-28 22:22:31 +000081 /// }
82
Chris Lattner36e02122009-06-21 20:54:55 +000083private:
Daniel Dunbare73b2672009-08-26 22:13:22 +000084 MCSymbol *CreateSymbol(StringRef Name);
85
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000086 // FIXME: See comment on SectionUniquingMap.
87 const MCSection *getMachOSection(const StringRef &Segment,
88 const StringRef &Section,
89 unsigned TypeAndAttributes,
90 unsigned Reserved2,
91 SectionKind Kind) const;
92
Chris Lattner36e02122009-06-21 20:54:55 +000093 bool ParseStatement();
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +000094
Chris Lattner2adc9e72009-06-21 21:22:11 +000095 bool TokError(const char *Msg);
Chris Lattnere5074c42009-06-22 01:29:09 +000096
Kevin Enderbyd9f95292009-08-07 22:46:00 +000097 bool ParseConditionalAssemblyDirectives(StringRef Directive,
98 SMLoc DirectiveLoc);
Chris Lattnere5074c42009-06-22 01:29:09 +000099 void EatToEndOfStatement();
100
Daniel Dunbar52d03b22009-07-27 21:49:56 +0000101 bool ParseAssignment(const StringRef &Name, bool IsDotSet);
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000102
Daniel Dunbarf3636452009-08-31 08:07:22 +0000103 bool ParsePrimaryExpr(const MCExpr *&Res);
104 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res);
105 bool ParseParenExpr(const MCExpr *&Res);
Daniel Dunbarc54ecb32009-08-01 00:48:30 +0000106
107 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
108 /// and set \arg Res to the identifier contents.
109 bool ParseIdentifier(StringRef &Res);
Chris Lattner3417d712009-06-23 18:41:30 +0000110
Chris Lattnerbedf6c22009-06-24 04:43:34 +0000111 // Directive Parsing.
Chris Lattnerf7ebca42009-06-24 05:13:15 +0000112 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
Chris Lattnercb307a272009-08-10 01:39:42 +0000113 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000114 unsigned TAA = 0, unsigned ImplicitAlign = 0,
115 unsigned StubSize = 0);
Daniel Dunbara10e5192009-06-24 23:30:00 +0000116 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
117 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
118 bool ParseDirectiveFill(); // ".fill"
119 bool ParseDirectiveSpace(); // ".space"
Daniel Dunbar2d2ee152009-06-25 21:56:11 +0000120 bool ParseDirectiveSet(); // ".set"
Daniel Dunbar4a5a5612009-06-25 22:44:51 +0000121 bool ParseDirectiveOrg(); // ".org"
Daniel Dunbarcc566a712009-06-29 23:46:59 +0000122 // ".align{,32}", ".p2align{,w,l}"
123 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbara5508c82009-06-30 00:33:19 +0000124
125 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
126 /// accepts a single symbol (which should be a label or an external).
127 bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
Kevin Enderby4c21caa2009-07-14 18:17:10 +0000128 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
Kevin Enderbycbe475d2009-07-14 21:35:03 +0000129 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
Chris Lattnera1e11f52009-07-07 20:30:46 +0000130
Chris Lattner28ad7542009-07-09 17:25:12 +0000131 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Chris Lattner07cadaf2009-07-10 22:20:30 +0000132 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
Kevin Enderbyc9d93ef2009-07-13 21:03:15 +0000133
134 // Darwin specific ".subsections_via_symbols"
135 bool ParseDirectiveDarwinSubsectionsViaSymbols();
Kevin Enderby09ea5702009-07-15 15:30:11 +0000136 // Darwin specific .dump and .load
Kevin Enderbyee551352009-07-20 20:25:37 +0000137 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
Kevin Enderby56523ce2009-07-13 23:15:14 +0000138
139 bool ParseDirectiveAbort(); // ".abort"
Kevin Enderbyd1ea5392009-07-14 23:21:55 +0000140 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000141
142 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
143 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
144 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
145 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
146
Daniel Dunbara4b069c2009-08-11 04:24:50 +0000147 bool ParseDirectiveFile(SMLoc DirectiveLoc); // ".file"
148 bool ParseDirectiveLine(SMLoc DirectiveLoc); // ".line"
149 bool ParseDirectiveLoc(SMLoc DirectiveLoc); // ".loc"
Daniel Dunbaref668c12009-08-14 18:19:52 +0000150
151 /// ParseEscapedString - Parse the current token as a string which may include
152 /// escaped characters and return the string contents.
153 bool ParseEscapedString(std::string &Data);
Chris Lattnerb0133452009-06-21 20:16:42 +0000154};
155
156} // end namespace llvm
157
158#endif