blob: 171dfcd4aa9bf212ef6cee169ca75ec58f46745a [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"
Kevin Enderbyf92f9902009-09-04 21:45:34 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattner351a7ef2009-09-27 21:16:52 +000024#include "llvm/ADT/StringMap.h"
Chris Lattnerb0133452009-06-21 20:16:42 +000025
26namespace llvm {
Kevin Enderbyd9f95292009-08-07 22:46:00 +000027class AsmCond;
Chris Lattner3f5738d2009-06-24 04:31:49 +000028class MCContext;
Daniel Dunbar115e4d62009-08-31 08:06:59 +000029class MCExpr;
Chris Lattner3417d712009-06-23 18:41:30 +000030class MCInst;
Chris Lattner92ffdd12009-06-24 00:52:40 +000031class MCStreamer;
Kevin Enderbyf92f9902009-09-04 21:45:34 +000032class MCAsmInfo;
Daniel Dunbarbd4bf3d2009-06-30 01:49:52 +000033class MCValue;
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +000034class TargetAsmParser;
Daniel Dunbareb6bb322009-07-27 23:20:52 +000035class Twine;
Daniel Dunbarbd4bf3d2009-06-30 01:49:52 +000036
Daniel Dunbarf59ee962009-07-28 20:47:52 +000037class AsmParser : public MCAsmParser {
Daniel Dunbard0a08e02009-06-30 23:38:38 +000038private:
Chris Lattnerb0133452009-06-21 20:16:42 +000039 AsmLexer Lexer;
Chris Lattner3f5738d2009-06-24 04:31:49 +000040 MCContext &Ctx;
Chris Lattner92ffdd12009-06-24 00:52:40 +000041 MCStreamer &Out;
Daniel Dunbarf59ee962009-07-28 20:47:52 +000042 TargetAsmParser *TargetParser;
Kevin Enderbyd9f95292009-08-07 22:46:00 +000043
44 AsmCond TheCondState;
45 std::vector<AsmCond> TheCondStack;
46
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000047 // FIXME: Figure out where this should leave, the code is a copy of that which
48 // is also used by TargetLoweringObjectFile.
49 mutable void *SectionUniquingMap;
50
Chris Lattner351a7ef2009-09-27 21:16:52 +000051 /// DirectiveMap - This is a table handlers for directives. Each handler is
52 /// invoked after the directive identifier is read and is responsible for
53 /// parsing and validating the rest of the directive. The handler is passed
54 /// in the directive name and the location of the directive keyword.
55 StringMap<bool(AsmParser::*)(StringRef, SMLoc)> DirectiveMap;
Chris Lattnerb0133452009-06-21 20:16:42 +000056public:
Kevin Enderbyf92f9902009-09-04 21:45:34 +000057 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
Chris Lattner351a7ef2009-09-27 21:16:52 +000058 const MCAsmInfo &_MAI);
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000059 ~AsmParser();
Daniel Dunbarf59ee962009-07-28 20:47:52 +000060
Chris Lattnerb0133452009-06-21 20:16:42 +000061 bool Run();
Chris Lattner351a7ef2009-09-27 21:16:52 +000062
Chris Lattnerb0133452009-06-21 20:16:42 +000063
Chris Lattner351a7ef2009-09-27 21:16:52 +000064 void AddDirectiveHandler(StringRef Directive,
65 bool (AsmParser::*Handler)(StringRef, SMLoc)) {
66 DirectiveMap[Directive] = Handler;
67 }
Daniel Dunbar3c2a8932009-07-20 18:55:04 +000068public:
Daniel Dunbarf59ee962009-07-28 20:47:52 +000069 TargetAsmParser &getTargetParser() const { return *TargetParser; }
70 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
Daniel Dunbar3c2a8932009-07-20 18:55:04 +000071
Daniel Dunbarc43267a2009-07-28 22:22:31 +000072 /// @name MCAsmParser Interface
73 /// {
74
Daniel Dunbar2b11c7d2009-07-20 20:01:54 +000075 virtual MCAsmLexer &getLexer() { return Lexer; }
Daniel Dunbar940cda22009-08-31 08:07:44 +000076 virtual MCContext &getContext() { return Ctx; }
Daniel Dunbara944234a2009-09-10 00:59:15 +000077 virtual MCStreamer &getStreamer() { return Out; }
78
Daniel Dunbarc43267a2009-07-28 22:22:31 +000079 virtual void Warning(SMLoc L, const Twine &Meg);
Daniel Dunbarc43267a2009-07-28 22:22:31 +000080 virtual bool Error(SMLoc L, const Twine &Msg);
81
Daniel Dunbarf3636452009-08-31 08:07:22 +000082 virtual bool ParseExpression(const MCExpr *&Res);
Daniel Dunbar7c82d562009-08-31 08:08:17 +000083 virtual bool ParseParenExpression(const MCExpr *&Res);
Daniel Dunbarc43267a2009-07-28 22:22:31 +000084 virtual bool ParseAbsoluteExpression(int64_t &Res);
85
Daniel Dunbarc43267a2009-07-28 22:22:31 +000086 /// }
87
Chris Lattner36e02122009-06-21 20:54:55 +000088private:
Daniel Dunbare73b2672009-08-26 22:13:22 +000089 MCSymbol *CreateSymbol(StringRef Name);
90
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000091 // FIXME: See comment on SectionUniquingMap.
92 const MCSection *getMachOSection(const StringRef &Segment,
93 const StringRef &Section,
94 unsigned TypeAndAttributes,
95 unsigned Reserved2,
96 SectionKind Kind) const;
97
Chris Lattner36e02122009-06-21 20:54:55 +000098 bool ParseStatement();
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +000099
Chris Lattner2adc9e72009-06-21 21:22:11 +0000100 bool TokError(const char *Msg);
Chris Lattnere5074c42009-06-22 01:29:09 +0000101
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000102 bool ParseConditionalAssemblyDirectives(StringRef Directive,
103 SMLoc DirectiveLoc);
Chris Lattnere5074c42009-06-22 01:29:09 +0000104 void EatToEndOfStatement();
105
Daniel Dunbarb7b20972009-08-31 08:09:09 +0000106 bool ParseAssignment(const StringRef &Name);
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000107
Daniel Dunbarf3636452009-08-31 08:07:22 +0000108 bool ParsePrimaryExpr(const MCExpr *&Res);
109 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res);
110 bool ParseParenExpr(const MCExpr *&Res);
Daniel Dunbarc54ecb32009-08-01 00:48:30 +0000111
112 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
113 /// and set \arg Res to the identifier contents.
114 bool ParseIdentifier(StringRef &Res);
Chris Lattner3417d712009-06-23 18:41:30 +0000115
Chris Lattnerbedf6c22009-06-24 04:43:34 +0000116 // Directive Parsing.
Chris Lattnerf7ebca42009-06-24 05:13:15 +0000117 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
Chris Lattnercb307a272009-08-10 01:39:42 +0000118 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000119 unsigned TAA = 0, unsigned ImplicitAlign = 0,
120 unsigned StubSize = 0);
Daniel Dunbara10e5192009-06-24 23:30:00 +0000121 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
122 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
123 bool ParseDirectiveFill(); // ".fill"
124 bool ParseDirectiveSpace(); // ".space"
Daniel Dunbar2d2ee152009-06-25 21:56:11 +0000125 bool ParseDirectiveSet(); // ".set"
Daniel Dunbar4a5a5612009-06-25 22:44:51 +0000126 bool ParseDirectiveOrg(); // ".org"
Daniel Dunbarcc566a712009-06-29 23:46:59 +0000127 // ".align{,32}", ".p2align{,w,l}"
128 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbara5508c82009-06-30 00:33:19 +0000129
130 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
131 /// accepts a single symbol (which should be a label or an external).
132 bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
Kevin Enderby4c21caa2009-07-14 18:17:10 +0000133 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
Kevin Enderbycbe475d2009-07-14 21:35:03 +0000134 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
Chris Lattnera1e11f52009-07-07 20:30:46 +0000135
Chris Lattner28ad7542009-07-09 17:25:12 +0000136 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Chris Lattner07cadaf2009-07-10 22:20:30 +0000137 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
Kevin Enderbyc9d93ef2009-07-13 21:03:15 +0000138
139 // Darwin specific ".subsections_via_symbols"
140 bool ParseDirectiveDarwinSubsectionsViaSymbols();
Kevin Enderby09ea5702009-07-15 15:30:11 +0000141 // Darwin specific .dump and .load
Kevin Enderbyee551352009-07-20 20:25:37 +0000142 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
Kevin Enderby56523ce2009-07-13 23:15:14 +0000143
144 bool ParseDirectiveAbort(); // ".abort"
Kevin Enderbyd1ea5392009-07-14 23:21:55 +0000145 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000146
147 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
148 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
149 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
150 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
151
Chris Lattner351a7ef2009-09-27 21:16:52 +0000152 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
153 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
154 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
Daniel Dunbaref668c12009-08-14 18:19:52 +0000155
156 /// ParseEscapedString - Parse the current token as a string which may include
157 /// escaped characters and return the string contents.
158 bool ParseEscapedString(std::string &Data);
Chris Lattnerb0133452009-06-21 20:16:42 +0000159};
160
161} // end namespace llvm
162
163#endif