blob: a19f35f64aa9e3e3d7c01e2192a4f52d6cca2f95 [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 Dunbard7b267b2009-06-30 00:33:19 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000022
23namespace llvm {
Daniel Dunbar475839e2009-06-29 20:37:27 +000024class AsmExpr;
Kevin Enderbyc114ed72009-08-07 22:46:00 +000025class AsmCond;
Chris Lattnerc69485e2009-06-24 04:31:49 +000026class MCContext;
Chris Lattner29dfe7c2009-06-23 18:41:30 +000027class MCInst;
Chris Lattnercbc23f72009-06-24 00:52:40 +000028class MCStreamer;
Daniel Dunbar15d17072009-06-30 01:49:52 +000029class MCValue;
Daniel Dunbardbd692a2009-07-20 20:01:54 +000030class TargetAsmParser;
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000031class Twine;
Daniel Dunbar15d17072009-06-30 01:49:52 +000032
Daniel Dunbara2edbab2009-07-28 20:47:52 +000033class AsmParser : public MCAsmParser {
Daniel Dunbard9627e12009-06-30 23:38:38 +000034private:
Chris Lattner27aa7d22009-06-21 20:16:42 +000035 AsmLexer Lexer;
Chris Lattnerc69485e2009-06-24 04:31:49 +000036 MCContext &Ctx;
Chris Lattnercbc23f72009-06-24 00:52:40 +000037 MCStreamer &Out;
Daniel Dunbara2edbab2009-07-28 20:47:52 +000038 TargetAsmParser *TargetParser;
Kevin Enderbyc114ed72009-08-07 22:46:00 +000039
40 AsmCond TheCondState;
41 std::vector<AsmCond> TheCondStack;
42
Chris Lattner27aa7d22009-06-21 20:16:42 +000043public:
Daniel Dunbara2edbab2009-07-28 20:47:52 +000044 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out)
45 : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0) {}
Chris Lattner27aa7d22009-06-21 20:16:42 +000046 ~AsmParser() {}
Daniel Dunbara2edbab2009-07-28 20:47:52 +000047
Chris Lattner27aa7d22009-06-21 20:16:42 +000048 bool Run();
49
Daniel Dunbara3af3702009-07-20 18:55:04 +000050public:
Daniel Dunbara2edbab2009-07-28 20:47:52 +000051 TargetAsmParser &getTargetParser() const { return *TargetParser; }
52 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
Daniel Dunbara3af3702009-07-20 18:55:04 +000053
Daniel Dunbare240beb2009-07-28 22:22:31 +000054 /// @name MCAsmParser Interface
55 /// {
56
Daniel Dunbardbd692a2009-07-20 20:01:54 +000057 virtual MCAsmLexer &getLexer() { return Lexer; }
58
Daniel Dunbare240beb2009-07-28 22:22:31 +000059 virtual void Warning(SMLoc L, const Twine &Meg);
60
61 virtual bool Error(SMLoc L, const Twine &Msg);
62
63 virtual bool ParseExpression(AsmExpr *&Res);
64
65 virtual bool ParseAbsoluteExpression(int64_t &Res);
66
67 virtual bool ParseRelocatableExpression(MCValue &Res);
68
69 /// }
70
Chris Lattnerb0789ed2009-06-21 20:54:55 +000071private:
72 bool ParseStatement();
Daniel Dunbar3fb76832009-06-30 00:49:23 +000073
Chris Lattner14ee48a2009-06-21 21:22:11 +000074 bool TokError(const char *Msg);
Chris Lattner2cf5f142009-06-22 01:29:09 +000075
Kevin Enderbyc114ed72009-08-07 22:46:00 +000076 bool ParseConditionalAssemblyDirectives(StringRef Directive,
77 SMLoc DirectiveLoc);
Chris Lattner2cf5f142009-06-22 01:29:09 +000078 void EatToEndOfStatement();
79
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +000080 bool ParseAssignment(const StringRef &Name, bool IsDotSet);
Daniel Dunbar475839e2009-06-29 20:37:27 +000081
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +000082 /// ParseParenRelocatableExpression - Parse an expression which must be
83 /// relocatable, assuming that an initial '(' has already been consumed.
84 ///
85 /// @param Res - The relocatable expression value. The result is undefined on
86 /// error.
87 /// @result - False on success.
88 ///
89 /// @see ParseRelocatableExpression, ParseParenExpr.
90 bool ParseParenRelocatableExpression(MCValue &Res);
91
Daniel Dunbar475839e2009-06-29 20:37:27 +000092 bool ParsePrimaryExpr(AsmExpr *&Res);
93 bool ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res);
94 bool ParseParenExpr(AsmExpr *&Res);
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +000095
96 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
97 /// and set \arg Res to the identifier contents.
98 bool ParseIdentifier(StringRef &Res);
Chris Lattner29dfe7c2009-06-23 18:41:30 +000099
Chris Lattner9a023f72009-06-24 04:43:34 +0000100 // Directive Parsing.
Chris Lattner529fb542009-06-24 05:13:15 +0000101 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
Chris Lattnerff4bc462009-08-10 01:39:42 +0000102 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
103 unsigned TAA, unsigned StubSize = 0);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000104 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
105 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
106 bool ParseDirectiveFill(); // ".fill"
107 bool ParseDirectiveSpace(); // ".space"
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000108 bool ParseDirectiveSet(); // ".set"
Daniel Dunbarc238b582009-06-25 22:44:51 +0000109 bool ParseDirectiveOrg(); // ".org"
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000110 // ".align{,32}", ".p2align{,w,l}"
111 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000112
113 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
114 /// accepts a single symbol (which should be a label or an external).
115 bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000116 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
Kevin Enderby71148242009-07-14 21:35:03 +0000117 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000118
Chris Lattner1fc3d752009-07-09 17:25:12 +0000119 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Chris Lattner9be3fee2009-07-10 22:20:30 +0000120 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
Kevin Enderbya5c78322009-07-13 21:03:15 +0000121
122 // Darwin specific ".subsections_via_symbols"
123 bool ParseDirectiveDarwinSubsectionsViaSymbols();
Kevin Enderby6e68cd92009-07-15 15:30:11 +0000124 // Darwin specific .dump and .load
Kevin Enderby5026ae42009-07-20 20:25:37 +0000125 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000126
127 bool ParseDirectiveAbort(); // ".abort"
Kevin Enderby1f049b22009-07-14 23:21:55 +0000128 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000129
130 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
131 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
132 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
133 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
134
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000135 bool ParseDirectiveFile(SMLoc DirectiveLoc); // ".file"
136 bool ParseDirectiveLine(SMLoc DirectiveLoc); // ".line"
137 bool ParseDirectiveLoc(SMLoc DirectiveLoc); // ".loc"
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000138
139 /// ParseEscapedString - Parse the current token as a string which may include
140 /// escaped characters and return the string contents.
141 bool ParseEscapedString(std::string &Data);
Chris Lattner27aa7d22009-06-21 20:16:42 +0000142};
143
144} // end namespace llvm
145
146#endif