blob: 7f038c90ff0794e2a6bbe6003f092d97be2736fd [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 Lattner27aa7d22009-06-21 20:16:42 +000024
25namespace llvm {
Kevin Enderbyc114ed72009-08-07 22:46:00 +000026class AsmCond;
Chris Lattnerc69485e2009-06-24 04:31:49 +000027class MCContext;
Daniel Dunbar28c251b2009-08-31 08:06:59 +000028class MCExpr;
Chris Lattner29dfe7c2009-06-23 18:41:30 +000029class MCInst;
Chris Lattnercbc23f72009-06-24 00:52:40 +000030class MCStreamer;
Kevin Enderby9823ca92009-09-04 21:45:34 +000031class MCAsmInfo;
Daniel Dunbar15d17072009-06-30 01:49:52 +000032class MCValue;
Daniel Dunbardbd692a2009-07-20 20:01:54 +000033class TargetAsmParser;
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000034class Twine;
Daniel Dunbar15d17072009-06-30 01:49:52 +000035
Daniel Dunbara2edbab2009-07-28 20:47:52 +000036class AsmParser : public MCAsmParser {
Daniel Dunbard9627e12009-06-30 23:38:38 +000037private:
Chris Lattner27aa7d22009-06-21 20:16:42 +000038 AsmLexer Lexer;
Chris Lattnerc69485e2009-06-24 04:31:49 +000039 MCContext &Ctx;
Chris Lattnercbc23f72009-06-24 00:52:40 +000040 MCStreamer &Out;
Daniel Dunbara2edbab2009-07-28 20:47:52 +000041 TargetAsmParser *TargetParser;
Kevin Enderbyc114ed72009-08-07 22:46:00 +000042
43 AsmCond TheCondState;
44 std::vector<AsmCond> TheCondStack;
45
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000046 // FIXME: Figure out where this should leave, the code is a copy of that which
47 // is also used by TargetLoweringObjectFile.
48 mutable void *SectionUniquingMap;
49
Chris Lattner27aa7d22009-06-21 20:16:42 +000050public:
Kevin Enderby9823ca92009-09-04 21:45:34 +000051 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
52 const MCAsmInfo &_MAI)
53 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000054 SectionUniquingMap(0) {}
55 ~AsmParser();
Daniel Dunbara2edbab2009-07-28 20:47:52 +000056
Chris Lattner27aa7d22009-06-21 20:16:42 +000057 bool Run();
58
Daniel Dunbara3af3702009-07-20 18:55:04 +000059public:
Daniel Dunbara2edbab2009-07-28 20:47:52 +000060 TargetAsmParser &getTargetParser() const { return *TargetParser; }
61 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
Daniel Dunbara3af3702009-07-20 18:55:04 +000062
Daniel Dunbare240beb2009-07-28 22:22:31 +000063 /// @name MCAsmParser Interface
64 /// {
65
Daniel Dunbardbd692a2009-07-20 20:01:54 +000066 virtual MCAsmLexer &getLexer() { return Lexer; }
67
Daniel Dunbar6ce004d2009-08-31 08:07:44 +000068 virtual MCContext &getContext() { return Ctx; }
69
Daniel Dunbare240beb2009-07-28 22:22:31 +000070 virtual void Warning(SMLoc L, const Twine &Meg);
71
72 virtual bool Error(SMLoc L, const Twine &Msg);
73
Daniel Dunbar9643ac52009-08-31 08:07:22 +000074 virtual bool ParseExpression(const MCExpr *&Res);
Daniel Dunbare240beb2009-07-28 22:22:31 +000075
Daniel Dunbarc18274b2009-08-31 08:08:17 +000076 virtual bool ParseParenExpression(const MCExpr *&Res);
77
Daniel Dunbare240beb2009-07-28 22:22:31 +000078 virtual bool ParseAbsoluteExpression(int64_t &Res);
79
Daniel Dunbare240beb2009-07-28 22:22:31 +000080 /// }
81
Chris Lattnerb0789ed2009-06-21 20:54:55 +000082private:
Daniel Dunbar959fd882009-08-26 22:13:22 +000083 MCSymbol *CreateSymbol(StringRef Name);
84
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000085 // FIXME: See comment on SectionUniquingMap.
86 const MCSection *getMachOSection(const StringRef &Segment,
87 const StringRef &Section,
88 unsigned TypeAndAttributes,
89 unsigned Reserved2,
90 SectionKind Kind) const;
91
Chris Lattnerb0789ed2009-06-21 20:54:55 +000092 bool ParseStatement();
Daniel Dunbar3fb76832009-06-30 00:49:23 +000093
Chris Lattner14ee48a2009-06-21 21:22:11 +000094 bool TokError(const char *Msg);
Chris Lattner2cf5f142009-06-22 01:29:09 +000095
Kevin Enderbyc114ed72009-08-07 22:46:00 +000096 bool ParseConditionalAssemblyDirectives(StringRef Directive,
97 SMLoc DirectiveLoc);
Chris Lattner2cf5f142009-06-22 01:29:09 +000098 void EatToEndOfStatement();
99
Daniel Dunbare2ace502009-08-31 08:09:09 +0000100 bool ParseAssignment(const StringRef &Name);
Daniel Dunbar475839e2009-06-29 20:37:27 +0000101
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000102 bool ParsePrimaryExpr(const MCExpr *&Res);
103 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res);
104 bool ParseParenExpr(const MCExpr *&Res);
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000105
106 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
107 /// and set \arg Res to the identifier contents.
108 bool ParseIdentifier(StringRef &Res);
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000109
Chris Lattner9a023f72009-06-24 04:43:34 +0000110 // Directive Parsing.
Chris Lattner529fb542009-06-24 05:13:15 +0000111 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
Chris Lattnerff4bc462009-08-10 01:39:42 +0000112 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000113 unsigned TAA = 0, unsigned ImplicitAlign = 0,
114 unsigned StubSize = 0);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000115 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
116 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
117 bool ParseDirectiveFill(); // ".fill"
118 bool ParseDirectiveSpace(); // ".space"
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000119 bool ParseDirectiveSet(); // ".set"
Daniel Dunbarc238b582009-06-25 22:44:51 +0000120 bool ParseDirectiveOrg(); // ".org"
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000121 // ".align{,32}", ".p2align{,w,l}"
122 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000123
124 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
125 /// accepts a single symbol (which should be a label or an external).
126 bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000127 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
Kevin Enderby71148242009-07-14 21:35:03 +0000128 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000129
Chris Lattner1fc3d752009-07-09 17:25:12 +0000130 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Chris Lattner9be3fee2009-07-10 22:20:30 +0000131 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
Kevin Enderbya5c78322009-07-13 21:03:15 +0000132
133 // Darwin specific ".subsections_via_symbols"
134 bool ParseDirectiveDarwinSubsectionsViaSymbols();
Kevin Enderby6e68cd92009-07-15 15:30:11 +0000135 // Darwin specific .dump and .load
Kevin Enderby5026ae42009-07-20 20:25:37 +0000136 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000137
138 bool ParseDirectiveAbort(); // ".abort"
Kevin Enderby1f049b22009-07-14 23:21:55 +0000139 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000140
141 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
142 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
143 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
144 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
145
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000146 bool ParseDirectiveFile(SMLoc DirectiveLoc); // ".file"
147 bool ParseDirectiveLine(SMLoc DirectiveLoc); // ".line"
148 bool ParseDirectiveLoc(SMLoc DirectiveLoc); // ".loc"
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000149
150 /// ParseEscapedString - Parse the current token as a string which may include
151 /// escaped characters and return the string contents.
152 bool ParseEscapedString(std::string &Data);
Chris Lattner27aa7d22009-06-21 20:16:42 +0000153};
154
155} // end namespace llvm
156
157#endif