blob: 21792eb0e9527167fa0e8e26761202ff68921a41 [file] [log] [blame]
Chris Lattner27aa7d22009-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 Enderbyc114ed72009-08-07 22:46:00 +000017#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000018#include "AsmLexer.h"
Kevin Enderbyc114ed72009-08-07 22:46:00 +000019#include "AsmCond.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000020#include "llvm/MC/MCAsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbard7b267b2009-06-30 00:33:19 +000022#include "llvm/MC/MCStreamer.h"
Kevin Enderby9823ca92009-09-04 21:45:34 +000023#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerebb89b42009-09-27 21:16:52 +000024#include "llvm/ADT/StringMap.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000025
26namespace llvm {
Kevin Enderbyc114ed72009-08-07 22:46:00 +000027class AsmCond;
Sean Callanan79ed1a82010-01-19 20:22:31 +000028class AsmToken;
Chris Lattnerc69485e2009-06-24 04:31:49 +000029class MCContext;
Daniel Dunbar28c251b2009-08-31 08:06:59 +000030class MCExpr;
Chris Lattner29dfe7c2009-06-23 18:41:30 +000031class MCInst;
Chris Lattnercbc23f72009-06-24 00:52:40 +000032class MCStreamer;
Kevin Enderby9823ca92009-09-04 21:45:34 +000033class MCAsmInfo;
Daniel Dunbar15d17072009-06-30 01:49:52 +000034class MCValue;
Sean Callanan10d33a42010-01-20 22:45:23 +000035class SourceMgr;
Daniel Dunbardbd692a2009-07-20 20:01:54 +000036class TargetAsmParser;
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000037class Twine;
Daniel Dunbar15d17072009-06-30 01:49:52 +000038
Daniel Dunbara2edbab2009-07-28 20:47:52 +000039class AsmParser : public MCAsmParser {
Sean Callanan10d33a42010-01-20 22:45:23 +000040private:
Chris Lattner27aa7d22009-06-21 20:16:42 +000041 AsmLexer Lexer;
Chris Lattnerc69485e2009-06-24 04:31:49 +000042 MCContext &Ctx;
Chris Lattnercbc23f72009-06-24 00:52:40 +000043 MCStreamer &Out;
Sean Callanan10d33a42010-01-20 22:45:23 +000044 SourceMgr &SrcMgr;
Daniel Dunbara2edbab2009-07-28 20:47:52 +000045 TargetAsmParser *TargetParser;
Kevin Enderbyc114ed72009-08-07 22:46:00 +000046
47 AsmCond TheCondState;
48 std::vector<AsmCond> TheCondStack;
49
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000050 // FIXME: Figure out where this should leave, the code is a copy of that which
51 // is also used by TargetLoweringObjectFile.
52 mutable void *SectionUniquingMap;
53
Chris Lattnerebb89b42009-09-27 21:16:52 +000054 /// DirectiveMap - This is a table handlers for directives. Each handler is
55 /// invoked after the directive identifier is read and is responsible for
56 /// parsing and validating the rest of the directive. The handler is passed
57 /// in the directive name and the location of the directive keyword.
58 StringMap<bool(AsmParser::*)(StringRef, SMLoc)> DirectiveMap;
Chris Lattner27aa7d22009-06-21 20:16:42 +000059public:
Kevin Enderby9823ca92009-09-04 21:45:34 +000060 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
Chris Lattnerebb89b42009-09-27 21:16:52 +000061 const MCAsmInfo &_MAI);
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000062 ~AsmParser();
Daniel Dunbara2edbab2009-07-28 20:47:52 +000063
Chris Lattner27aa7d22009-06-21 20:16:42 +000064 bool Run();
Chris Lattnerebb89b42009-09-27 21:16:52 +000065
Chris Lattner27aa7d22009-06-21 20:16:42 +000066
Chris Lattnerebb89b42009-09-27 21:16:52 +000067 void AddDirectiveHandler(StringRef Directive,
68 bool (AsmParser::*Handler)(StringRef, SMLoc)) {
69 DirectiveMap[Directive] = Handler;
70 }
Daniel Dunbara3af3702009-07-20 18:55:04 +000071public:
Daniel Dunbara2edbab2009-07-28 20:47:52 +000072 TargetAsmParser &getTargetParser() const { return *TargetParser; }
73 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
Daniel Dunbara3af3702009-07-20 18:55:04 +000074
Daniel Dunbare240beb2009-07-28 22:22:31 +000075 /// @name MCAsmParser Interface
76 /// {
77
Daniel Dunbardbd692a2009-07-20 20:01:54 +000078 virtual MCAsmLexer &getLexer() { return Lexer; }
Daniel Dunbar6ce004d2009-08-31 08:07:44 +000079 virtual MCContext &getContext() { return Ctx; }
Daniel Dunbarb8b70522009-09-10 00:59:15 +000080 virtual MCStreamer &getStreamer() { return Out; }
81
Daniel Dunbare240beb2009-07-28 22:22:31 +000082 virtual void Warning(SMLoc L, const Twine &Meg);
Daniel Dunbare240beb2009-07-28 22:22:31 +000083 virtual bool Error(SMLoc L, const Twine &Msg);
84
Sean Callanan79ed1a82010-01-19 20:22:31 +000085 const AsmToken &Lex();
86
Chris Lattnerb4307b32010-01-15 19:28:38 +000087 bool ParseExpression(const MCExpr *&Res);
Chris Lattner54482b42010-01-15 19:39:23 +000088 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +000089 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbare240beb2009-07-28 22:22:31 +000090 virtual bool ParseAbsoluteExpression(int64_t &Res);
91
Daniel Dunbare240beb2009-07-28 22:22:31 +000092 /// }
93
Chris Lattnerb0789ed2009-06-21 20:54:55 +000094private:
Daniel Dunbar959fd882009-08-26 22:13:22 +000095 MCSymbol *CreateSymbol(StringRef Name);
96
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000097 // FIXME: See comment on SectionUniquingMap.
98 const MCSection *getMachOSection(const StringRef &Segment,
99 const StringRef &Section,
100 unsigned TypeAndAttributes,
101 unsigned Reserved2,
102 SectionKind Kind) const;
103
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000104 bool ParseStatement();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000105
Chris Lattner14ee48a2009-06-21 21:22:11 +0000106 bool TokError(const char *Msg);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000107
Sean Callananbf2013e2010-01-20 23:19:55 +0000108 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
109
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000110 bool ParseConditionalAssemblyDirectives(StringRef Directive,
111 SMLoc DirectiveLoc);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000112 void EatToEndOfStatement();
113
Daniel Dunbare2ace502009-08-31 08:09:09 +0000114 bool ParseAssignment(const StringRef &Name);
Daniel Dunbar475839e2009-06-29 20:37:27 +0000115
Chris Lattnerb4307b32010-01-15 19:28:38 +0000116 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
117 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
118 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000119
120 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
121 /// and set \arg Res to the identifier contents.
122 bool ParseIdentifier(StringRef &Res);
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000123
Chris Lattner9a023f72009-06-24 04:43:34 +0000124 // Directive Parsing.
Chris Lattner529fb542009-06-24 05:13:15 +0000125 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
Chris Lattnerff4bc462009-08-10 01:39:42 +0000126 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000127 unsigned TAA = 0, unsigned ImplicitAlign = 0,
128 unsigned StubSize = 0);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000129 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
130 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
131 bool ParseDirectiveFill(); // ".fill"
132 bool ParseDirectiveSpace(); // ".space"
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000133 bool ParseDirectiveSet(); // ".set"
Daniel Dunbarc238b582009-06-25 22:44:51 +0000134 bool ParseDirectiveOrg(); // ".org"
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000135 // ".align{,32}", ".p2align{,w,l}"
136 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000137
138 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
139 /// accepts a single symbol (which should be a label or an external).
140 bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000141 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
Kevin Enderby71148242009-07-14 21:35:03 +0000142 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000143
Chris Lattner1fc3d752009-07-09 17:25:12 +0000144 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Chris Lattner9be3fee2009-07-10 22:20:30 +0000145 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
Kevin Enderbya5c78322009-07-13 21:03:15 +0000146
147 // Darwin specific ".subsections_via_symbols"
148 bool ParseDirectiveDarwinSubsectionsViaSymbols();
Kevin Enderby6e68cd92009-07-15 15:30:11 +0000149 // Darwin specific .dump and .load
Kevin Enderby5026ae42009-07-20 20:25:37 +0000150 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000151
152 bool ParseDirectiveAbort(); // ".abort"
Kevin Enderby1f049b22009-07-14 23:21:55 +0000153 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000154
155 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
156 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
157 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
158 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
159
Chris Lattnerebb89b42009-09-27 21:16:52 +0000160 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
161 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
162 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000163
164 /// ParseEscapedString - Parse the current token as a string which may include
165 /// escaped characters and return the string contents.
166 bool ParseEscapedString(std::string &Data);
Chris Lattner27aa7d22009-06-21 20:16:42 +0000167};
168
169} // end namespace llvm
170
171#endif