blob: 4523eabb6371f6432cbbd60ddc16bdd9b12ffeee [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
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 implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
27#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000028#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000029using namespace llvm;
30
Chris Lattneraaec2052010-01-19 19:46:13 +000031
32enum { DEFAULT_ADDRSPACE = 0 };
33
Chris Lattnerebb89b42009-09-27 21:16:52 +000034AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
35 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000036 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
Chris Lattnerf0559e42010-04-08 20:30:37 +000037 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000038 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
39
Chris Lattnerebb89b42009-09-27 21:16:52 +000040 // Debugging directives.
41 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
42 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
43 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
44}
45
46
47
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000048AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000049}
50
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000051void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000052 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000053}
54
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000055bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000056 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000057 return true;
58}
59
60bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000061 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000062 return true;
63}
64
Sean Callananbf2013e2010-01-20 23:19:55 +000065void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
66 const char *Type) const {
67 SrcMgr.PrintMessage(Loc, Msg, Type);
68}
Sean Callananfd0b0282010-01-21 00:19:58 +000069
70bool AsmParser::EnterIncludeFile(const std::string &Filename) {
71 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
72 if (NewBuf == -1)
73 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000074
Sean Callananfd0b0282010-01-21 00:19:58 +000075 CurBuffer = NewBuf;
76
77 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
78
79 return false;
80}
81
82const AsmToken &AsmParser::Lex() {
83 const AsmToken *tok = &Lexer.Lex();
84
85 if (tok->is(AsmToken::Eof)) {
86 // If this is the end of an included file, pop the parent file off the
87 // include stack.
88 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
89 if (ParentIncludeLoc != SMLoc()) {
90 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
91 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
92 ParentIncludeLoc.getPointer());
93 tok = &Lexer.Lex();
94 }
95 }
96
97 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000098 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000099
Sean Callananfd0b0282010-01-21 00:19:58 +0000100 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000101}
102
Chris Lattner79180e22010-04-05 23:15:42 +0000103bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000104 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000105 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000106 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000107 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000108 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000109 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
110 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000111
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000112 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000113 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000114
Chris Lattnerb717fb02009-07-02 21:53:43 +0000115 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000116
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000117 AsmCond StartingCondState = TheCondState;
118
Chris Lattnerb717fb02009-07-02 21:53:43 +0000119 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000120 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000121 if (!ParseStatement()) continue;
122
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000123 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000124 HadError = true;
125 EatToEndOfStatement();
126 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000127
128 if (TheCondState.TheCond != StartingCondState.TheCond ||
129 TheCondState.Ignore != StartingCondState.Ignore)
130 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000131
Chris Lattner79180e22010-04-05 23:15:42 +0000132 // Finalize the output stream if there are no errors and if the client wants
133 // us to.
134 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000135 Out.Finish();
136
Chris Lattnerb717fb02009-07-02 21:53:43 +0000137 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000138}
139
Chris Lattner2cf5f142009-06-22 01:29:09 +0000140/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
141void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000142 while (Lexer.isNot(AsmToken::EndOfStatement) &&
143 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000144 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000145
146 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000147 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000148 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000149}
150
Chris Lattnerc4193832009-06-22 05:51:26 +0000151
Chris Lattner74ec1a32009-06-22 06:32:03 +0000152/// ParseParenExpr - Parse a paren expression and return it.
153/// NOTE: This assumes the leading '(' has already been consumed.
154///
155/// parenexpr ::= expr)
156///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000157bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000158 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000159 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000160 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000161 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000162 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000163 return false;
164}
Chris Lattnerc4193832009-06-22 05:51:26 +0000165
Daniel Dunbar959fd882009-08-26 22:13:22 +0000166MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000167 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000168 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000169}
170
Chris Lattner74ec1a32009-06-22 06:32:03 +0000171/// ParsePrimaryExpr - Parse a primary expression and return it.
172/// primaryexpr ::= (parenexpr
173/// primaryexpr ::= symbol
174/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000175/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000176/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000177bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000178 switch (Lexer.getKind()) {
179 default:
180 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000181 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000182 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000183 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000184 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000185 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000186 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000187 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000188 case AsmToken::Identifier: {
189 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000190 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
191 MCSymbol *Sym = CreateSymbol(Split.first);
192
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000193 // Mark the symbol as used in an expression.
194 Sym->setUsedInExpr(true);
195
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000196 // Lookup the symbol variant if used.
197 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
198 if (Split.first.size() != getTok().getIdentifier().size())
199 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
200
Chris Lattnerb4307b32010-01-15 19:28:38 +0000201 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000202 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000203
204 // If this is an absolute variable reference, substitute it now to preserve
205 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000206 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000207 if (Variant)
208 return Error(EndLoc, "unexpected modified on variable reference");
209
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000210 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000211 return false;
212 }
213
214 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000215 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000216 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000217 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000218 case AsmToken::Integer: {
219 SMLoc Loc = getTok().getLoc();
220 int64_t IntVal = getTok().getIntVal();
221 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000222 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000223 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000224 // Look for 'b' or 'f' following an Integer as a directional label
225 if (Lexer.getKind() == AsmToken::Identifier) {
226 StringRef IDVal = getTok().getString();
227 if (IDVal == "f" || IDVal == "b"){
228 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
229 IDVal == "f" ? 1 : 0);
230 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
231 getContext());
232 if(IDVal == "b" && Sym->isUndefined())
233 return Error(Loc, "invalid reference to undefined symbol");
234 EndLoc = Lexer.getLoc();
235 Lex(); // Eat identifier.
236 }
237 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000238 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000239 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000240 case AsmToken::Dot: {
241 // This is a '.' reference, which references the current PC. Emit a
242 // temporary label to the streamer and refer to it.
243 MCSymbol *Sym = Ctx.CreateTempSymbol();
244 Out.EmitLabel(Sym);
245 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
246 EndLoc = Lexer.getLoc();
247 Lex(); // Eat identifier.
248 return false;
249 }
250
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000252 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000253 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000254 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000255 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000256 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000258 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000260 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000261 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000262 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000264 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000266 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000267 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000268 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000270 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000272 }
273}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000274
Chris Lattnerb4307b32010-01-15 19:28:38 +0000275bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000276 SMLoc EndLoc;
277 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000278}
279
Chris Lattner74ec1a32009-06-22 06:32:03 +0000280/// ParseExpression - Parse an expression and return it.
281///
282/// expr ::= expr +,- expr -> lowest.
283/// expr ::= expr |,^,&,! expr -> middle.
284/// expr ::= expr *,/,%,<<,>> expr -> highest.
285/// expr ::= primaryexpr
286///
Chris Lattner54482b42010-01-15 19:39:23 +0000287bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000288 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000289 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000290 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
291 return true;
292
293 // Try to constant fold it up front, if possible.
294 int64_t Value;
295 if (Res->EvaluateAsAbsolute(Value))
296 Res = MCConstantExpr::Create(Value, getContext());
297
298 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000299}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000300
Chris Lattnerb4307b32010-01-15 19:28:38 +0000301bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000302 Res = 0;
303 return ParseParenExpr(Res, EndLoc) ||
304 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000305}
306
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000308 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000310 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000311 if (ParseExpression(Expr))
312 return true;
313
Daniel Dunbare00b0112009-10-16 01:57:52 +0000314 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000315 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000316
317 return false;
318}
319
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000321 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000322 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000323 default:
324 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325
326 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000327 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000328 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000329 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000331 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000332 return 1;
333
334 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000336 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000337 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000338 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000339 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000341 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000342 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000343 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000344 case AsmToken::ExclaimEqual:
345 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000349 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000351 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000352 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000353 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000354 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000355 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000356 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 2;
360
361 // Intermediate Precedence: |, &, ^
362 //
363 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000364 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000365 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000366 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000367 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000368 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000369 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000371 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000372 return 3;
373
374 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000381 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000382 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000383 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000384 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000385 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000386 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000387 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000388 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000389 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000390 }
391}
392
393
394/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
395/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000396bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
397 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000399 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401
402 // If the next token is lower precedence than we are allowed to eat, return
403 // successfully with what we ate already.
404 if (TokPrec < Precedence)
405 return false;
406
Sean Callanan79ed1a82010-01-19 20:22:31 +0000407 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000408
409 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000410 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000411 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000412
413 // If BinOp binds less tightly with RHS than the operator after RHS, let
414 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000416 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000417 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000418 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000419 }
420
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000422 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000423 }
424}
425
Chris Lattnerc4193832009-06-22 05:51:26 +0000426
427
428
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000429/// ParseStatement:
430/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000431/// ::= Label* Directive ...Operands... EndOfStatement
432/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000433bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000434 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000435 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000436 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000437 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000438 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000439
440 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000441 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000442 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000443 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000444 int64_t LocalLabelVal = -1;
445 // GUESS allow an integer followed by a ':' as a directional local label
446 if (Lexer.is(AsmToken::Integer)) {
447 LocalLabelVal = getTok().getIntVal();
448 if (LocalLabelVal < 0) {
449 if (!TheCondState.Ignore)
450 return TokError("unexpected token at start of statement");
451 IDVal = "";
452 }
453 else {
454 IDVal = getTok().getString();
455 Lex(); // Consume the integer token to be used as an identifier token.
456 if (Lexer.getKind() != AsmToken::Colon) {
457 if (!TheCondState.Ignore)
458 return TokError("unexpected token at start of statement");
459 }
460 }
461 }
462 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000463 if (!TheCondState.Ignore)
464 return TokError("unexpected token at start of statement");
465 IDVal = "";
466 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000467
Chris Lattner7834fac2010-04-17 18:14:27 +0000468 // Handle conditional assembly here before checking for skipping. We
469 // have to do this so that .endif isn't skipped in a ".if 0" block for
470 // example.
471 if (IDVal == ".if")
472 return ParseDirectiveIf(IDLoc);
473 if (IDVal == ".elseif")
474 return ParseDirectiveElseIf(IDLoc);
475 if (IDVal == ".else")
476 return ParseDirectiveElse(IDLoc);
477 if (IDVal == ".endif")
478 return ParseDirectiveEndIf(IDLoc);
479
480 // If we are in a ".if 0" block, ignore this statement.
481 if (TheCondState.Ignore) {
482 EatToEndOfStatement();
483 return false;
484 }
485
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000486 // FIXME: Recurse on local labels?
487
488 // See what kind of statement we have.
489 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000490 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000491 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000492 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000493
494 // Diagnose attempt to use a variable as a label.
495 //
496 // FIXME: Diagnostics. Note the location of the definition as a label.
497 // FIXME: This doesn't diagnose assignment to a symbol which has been
498 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000499 MCSymbol *Sym;
500 if (LocalLabelVal == -1)
501 Sym = CreateSymbol(IDVal);
502 else
503 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000504 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000505 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000506
Daniel Dunbar959fd882009-08-26 22:13:22 +0000507 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000508 Out.EmitLabel(Sym);
509
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000510 // Consume any end of statement token, if present, to avoid spurious
511 // AddBlankLine calls().
512 if (Lexer.is(AsmToken::EndOfStatement)) {
513 Lex();
514 if (Lexer.is(AsmToken::Eof))
515 return false;
516 }
517
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000518 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000519 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000520
Daniel Dunbar3f872332009-07-28 16:08:33 +0000521 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000522 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000523 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000524
Daniel Dunbare2ace502009-08-31 08:09:09 +0000525 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000526
527 default: // Normal instruction or directive.
528 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000529 }
530
531 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000532 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000533 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000535 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000537 // FIXME: This changes behavior based on the -static flag to the
538 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000539 return ParseDirectiveSectionSwitch("__TEXT", "__text",
540 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000546 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
547 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000549 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000550 MCSectionMachO::S_4BYTE_LITERALS,
551 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000553 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000554 MCSectionMachO::S_8BYTE_LITERALS,
555 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000557 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 MCSectionMachO::S_16BYTE_LITERALS,
559 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000563 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
568
569 // FIXME: The assembler manual claims that this has the self modify code
570 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000571 if (IDVal == ".symbol_stub")
572 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
573 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000574 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
575 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 0, 16);
577 // FIXME: PowerPC only?
578 if (IDVal == ".picsymbol_stub")
579 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
580 MCSectionMachO::S_SYMBOL_STUBS |
581 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
582 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000584 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
587
588 // FIXME: The section names of these two are misspelled in the assembler
589 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
592 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
593 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
596 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
597 4);
598
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000600 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000602 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000603 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
604 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000606 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
608 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000610 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000611
612
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000614 return ParseDirectiveSectionSwitch("__OBJC", "__class",
615 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000617 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
618 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000620 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
621 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000623 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
624 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000626 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
627 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000629 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
630 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000632 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
633 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000635 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
636 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000638 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
639 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
640 MCSectionMachO::S_LITERAL_POINTERS,
641 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000643 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
644 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
645 MCSectionMachO::S_LITERAL_POINTERS,
646 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000648 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
649 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000651 return ParseDirectiveSectionSwitch("__OBJC", "__category",
652 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000654 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
655 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000657 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
658 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000660 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
661 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000663 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
664 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000666 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
667 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000669 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
670 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000672 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
673 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000674
Eric Christopherc6177a42010-05-17 22:53:55 +0000675 if (IDVal == ".tdata")
676 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
677 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
678 if (IDVal == ".tlv")
679 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
680 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
681 if (IDVal == ".thread_init_func")
682 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
683 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
684
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000685 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000687 return ParseDirectiveSet();
688
Daniel Dunbara0d14262009-06-24 23:30:00 +0000689 // Data directives
690
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000692 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000694 return ParseDirectiveAscii(true);
695
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000697 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000698 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000699 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000701 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000703 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000704
705 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000707 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000709 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000711 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000713 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000715 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000717 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000719 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000721 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
722
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000724 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000725
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000727 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000729 return ParseDirectiveSpace();
730
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000731 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000732
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000734 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000736 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000738 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000740 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000742 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000744 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000746 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000748 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000750 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000751 if (IDVal == ".type")
752 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000754 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000756 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000757 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000758 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000759
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000761 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000763 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000765 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000767 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000768 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000769 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000770 if (IDVal == ".tbss")
771 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000772
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000773 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000774 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000775 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000776 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000777 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000778 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000779 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000780 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000781 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000782 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000783
Chris Lattnerebb89b42009-09-27 21:16:52 +0000784 // Look up the handler in the handler table,
785 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
786 if (Handler)
787 return (this->*Handler)(IDVal, IDLoc);
788
Kevin Enderby9c656452009-09-10 20:51:44 +0000789 // Target hook for parsing target specific directives.
790 if (!getTargetParser().ParseDirective(ID))
791 return false;
792
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000793 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000794 EatToEndOfStatement();
795 return false;
796 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000797
Chris Lattnera7f13542010-05-19 23:34:33 +0000798 // Canonicalize the opcode to lower case.
799 SmallString<128> Opcode;
800 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
801 Opcode.push_back(tolower(IDVal[i]));
802
Chris Lattner98986712010-01-14 22:21:20 +0000803 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000804 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000805 ParsedOperands);
806 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
807 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000808
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000809 // If parsing succeeded, match the instruction.
810 if (!HadError) {
811 MCInst Inst;
812 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
813 // Emit the instruction on success.
814 Out.EmitInstruction(Inst);
815 } else {
816 // Otherwise emit a diagnostic about the match failure and set the error
817 // flag.
818 //
819 // FIXME: We should give nicer diagnostics about the exact failure.
820 Error(IDLoc, "unrecognized instruction");
821 HadError = true;
822 }
823 }
Chris Lattner98986712010-01-14 22:21:20 +0000824
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000825 // If there was no error, consume the end-of-statement token. Otherwise this
826 // will be done by our caller.
827 if (!HadError)
828 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000829
830 // Free any parsed operands.
831 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
832 delete ParsedOperands[i];
833
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000834 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000835}
Chris Lattner9a023f72009-06-24 04:43:34 +0000836
Daniel Dunbare2ace502009-08-31 08:09:09 +0000837bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000838 // FIXME: Use better location, we should use proper tokens.
839 SMLoc EqualLoc = Lexer.getLoc();
840
Daniel Dunbar821e3332009-08-31 08:09:28 +0000841 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000842 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000843 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000844 return true;
845
Daniel Dunbar3f872332009-07-28 16:08:33 +0000846 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000847 return TokError("unexpected token in assignment");
848
849 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000850 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000851
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000852 // Validate that the LHS is allowed to be a variable (either it has not been
853 // used as a symbol, or it is an absolute symbol).
854 MCSymbol *Sym = getContext().LookupSymbol(Name);
855 if (Sym) {
856 // Diagnose assignment to a label.
857 //
858 // FIXME: Diagnostics. Note the location of the definition as a label.
859 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000860 if (Sym->isUndefined() && !Sym->isUsedInExpr())
861 ; // Allow redefinitions of undefined symbols only used in directives.
862 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000863 return Error(EqualLoc, "redefinition of '" + Name + "'");
864 else if (!Sym->isVariable())
865 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000866 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000867 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
868 Name + "'");
869 } else
870 Sym = CreateSymbol(Name);
871
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000872 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000873
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000874 Sym->setUsedInExpr(true);
875
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000876 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000877 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000878
879 return false;
880}
881
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000882/// ParseIdentifier:
883/// ::= identifier
884/// ::= string
885bool AsmParser::ParseIdentifier(StringRef &Res) {
886 if (Lexer.isNot(AsmToken::Identifier) &&
887 Lexer.isNot(AsmToken::String))
888 return true;
889
Sean Callanan18b83232010-01-19 21:44:56 +0000890 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000891
Sean Callanan79ed1a82010-01-19 20:22:31 +0000892 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000893
894 return false;
895}
896
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000897/// ParseDirectiveSet:
898/// ::= .set identifier ',' expression
899bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000900 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000902 if (ParseIdentifier(Name))
903 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000904
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000905 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000906 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000907 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000908
Daniel Dunbare2ace502009-08-31 08:09:09 +0000909 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000910}
911
Chris Lattner9a023f72009-06-24 04:43:34 +0000912/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000913/// ::= .section identifier (',' identifier)*
914/// FIXME: This should actually parse out the segment, section, attributes and
915/// sizeof_stub fields.
916bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000917 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000918
Daniel Dunbarace63122009-08-11 03:42:33 +0000919 StringRef SectionName;
920 if (ParseIdentifier(SectionName))
921 return Error(Loc, "expected identifier after '.section' directive");
922
923 // Verify there is a following comma.
924 if (!Lexer.is(AsmToken::Comma))
925 return TokError("unexpected token in '.section' directive");
926
Chris Lattnerff4bc462009-08-10 01:39:42 +0000927 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000928 SectionSpec += ",";
929
930 // Add all the tokens until the end of the line, ParseSectionSpecifier will
931 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000932 StringRef EOL = Lexer.LexUntilEndOfStatement();
933 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000934
Sean Callanan79ed1a82010-01-19 20:22:31 +0000935 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000936 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000937 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000938 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000939
Chris Lattnerff4bc462009-08-10 01:39:42 +0000940
941 StringRef Segment, Section;
942 unsigned TAA, StubSize;
943 std::string ErrorStr =
944 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
945 TAA, StubSize);
946
947 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000948 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000949
Chris Lattner56594f92009-07-31 17:47:16 +0000950 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000951 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000952 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
953 isText ? SectionKind::getText()
954 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000955 return false;
956}
957
Chris Lattnere15c2d72009-08-10 18:05:55 +0000958/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000959bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
960 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000961 unsigned TAA, unsigned Align,
962 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000964 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000965 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000966
Chris Lattner56594f92009-07-31 17:47:16 +0000967 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000968 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000969 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
970 isText ? SectionKind::getText()
971 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000972
973 // Set the implicit alignment, if any.
974 //
975 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
976 // alignment on the section (e.g., if one manually inserts bytes into the
977 // section, then just issueing the section switch directive will not realign
978 // the section. However, this is arguably more reasonable behavior, and there
979 // is no good reason for someone to intentionally emit incorrectly sized
980 // values into the implicitly aligned sections.
981 if (Align)
982 Out.EmitValueToAlignment(Align, 0, 1, 0);
983
Chris Lattner529fb542009-06-24 05:13:15 +0000984 return false;
985}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000986
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000987bool AsmParser::ParseEscapedString(std::string &Data) {
988 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
989
990 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000991 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000992 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
993 if (Str[i] != '\\') {
994 Data += Str[i];
995 continue;
996 }
997
998 // Recognize escaped characters. Note that this escape semantics currently
999 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1000 ++i;
1001 if (i == e)
1002 return TokError("unexpected backslash at end of string");
1003
1004 // Recognize octal sequences.
1005 if ((unsigned) (Str[i] - '0') <= 7) {
1006 // Consume up to three octal characters.
1007 unsigned Value = Str[i] - '0';
1008
1009 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1010 ++i;
1011 Value = Value * 8 + (Str[i] - '0');
1012
1013 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1014 ++i;
1015 Value = Value * 8 + (Str[i] - '0');
1016 }
1017 }
1018
1019 if (Value > 255)
1020 return TokError("invalid octal escape sequence (out of range)");
1021
1022 Data += (unsigned char) Value;
1023 continue;
1024 }
1025
1026 // Otherwise recognize individual escapes.
1027 switch (Str[i]) {
1028 default:
1029 // Just reject invalid escape sequences for now.
1030 return TokError("invalid escape sequence (unrecognized character)");
1031
1032 case 'b': Data += '\b'; break;
1033 case 'f': Data += '\f'; break;
1034 case 'n': Data += '\n'; break;
1035 case 'r': Data += '\r'; break;
1036 case 't': Data += '\t'; break;
1037 case '"': Data += '"'; break;
1038 case '\\': Data += '\\'; break;
1039 }
1040 }
1041
1042 return false;
1043}
1044
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001046/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001047bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001050 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 return TokError("expected string in '.ascii' or '.asciz' directive");
1052
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001053 std::string Data;
1054 if (ParseEscapedString(Data))
1055 return true;
1056
Chris Lattneraaec2052010-01-19 19:46:13 +00001057 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001058 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001059 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060
Sean Callanan79ed1a82010-01-19 20:22:31 +00001061 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062
Daniel Dunbar3f872332009-07-28 16:08:33 +00001063 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064 break;
1065
Daniel Dunbar3f872332009-07-28 16:08:33 +00001066 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001067 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001068 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001069 }
1070 }
1071
Sean Callanan79ed1a82010-01-19 20:22:31 +00001072 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 return false;
1074}
1075
1076/// ParseDirectiveValue
1077/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1078bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001081 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001082 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001083 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return true;
1085
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001086 // Special case constant expressions to match code generator.
1087 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1088 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1089 else
1090 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001091
Daniel Dunbar3f872332009-07-28 16:08:33 +00001092 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093 break;
1094
1095 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001096 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001097 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001098 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001099 }
1100 }
1101
Sean Callanan79ed1a82010-01-19 20:22:31 +00001102 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001103 return false;
1104}
1105
1106/// ParseDirectiveSpace
1107/// ::= .space expression [ , expression ]
1108bool AsmParser::ParseDirectiveSpace() {
1109 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001110 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001111 return true;
1112
1113 int64_t FillExpr = 0;
1114 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001115 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1116 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119
Daniel Dunbar475839e2009-06-29 20:37:27 +00001120 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001121 return true;
1122
1123 HasFillExpr = true;
1124
Daniel Dunbar3f872332009-07-28 16:08:33 +00001125 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126 return TokError("unexpected token in '.space' directive");
1127 }
1128
Sean Callanan79ed1a82010-01-19 20:22:31 +00001129 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130
1131 if (NumBytes <= 0)
1132 return TokError("invalid number of bytes in '.space' directive");
1133
1134 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001135 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001136
1137 return false;
1138}
1139
1140/// ParseDirectiveFill
1141/// ::= .fill expression , expression , expression
1142bool AsmParser::ParseDirectiveFill() {
1143 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001144 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001145 return true;
1146
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001149 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001150
1151 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001152 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001153 return true;
1154
Daniel Dunbar3f872332009-07-28 16:08:33 +00001155 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001156 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001157 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001158
1159 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001160 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001161 return true;
1162
Daniel Dunbar3f872332009-07-28 16:08:33 +00001163 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001164 return TokError("unexpected token in '.fill' directive");
1165
Sean Callanan79ed1a82010-01-19 20:22:31 +00001166 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001167
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001168 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1169 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170
1171 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001172 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001173
1174 return false;
1175}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001176
1177/// ParseDirectiveOrg
1178/// ::= .org expression [ , expression ]
1179bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001180 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001181 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001182 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001183 return true;
1184
1185 // Parse optional fill expression.
1186 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1188 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001189 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001190 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001191
Daniel Dunbar475839e2009-06-29 20:37:27 +00001192 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001193 return true;
1194
Daniel Dunbar3f872332009-07-28 16:08:33 +00001195 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001196 return TokError("unexpected token in '.org' directive");
1197 }
1198
Sean Callanan79ed1a82010-01-19 20:22:31 +00001199 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001200
1201 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1202 // has to be relative to the current section.
1203 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001204
1205 return false;
1206}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207
1208/// ParseDirectiveAlign
1209/// ::= {.align, ...} expression [ , expression [ , expression ]]
1210bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001211 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001212 int64_t Alignment;
1213 if (ParseAbsoluteExpression(Alignment))
1214 return true;
1215
1216 SMLoc MaxBytesLoc;
1217 bool HasFillExpr = false;
1218 int64_t FillExpr = 0;
1219 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1221 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001222 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001223 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224
1225 // The fill expression can be omitted while specifying a maximum number of
1226 // alignment bytes, e.g:
1227 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001228 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 HasFillExpr = true;
1230 if (ParseAbsoluteExpression(FillExpr))
1231 return true;
1232 }
1233
Daniel Dunbar3f872332009-07-28 16:08:33 +00001234 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1235 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001237 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001238
1239 MaxBytesLoc = Lexer.getLoc();
1240 if (ParseAbsoluteExpression(MaxBytesToFill))
1241 return true;
1242
Daniel Dunbar3f872332009-07-28 16:08:33 +00001243 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001244 return TokError("unexpected token in directive");
1245 }
1246 }
1247
Sean Callanan79ed1a82010-01-19 20:22:31 +00001248 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001249
Daniel Dunbar648ac512010-05-17 21:54:30 +00001250 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001251 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001252
1253 // Compute alignment in bytes.
1254 if (IsPow2) {
1255 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001256 if (Alignment >= 32) {
1257 Error(AlignmentLoc, "invalid alignment value");
1258 Alignment = 31;
1259 }
1260
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001261 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001262 }
1263
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001264 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001265 if (MaxBytesLoc.isValid()) {
1266 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001267 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1268 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001269 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001270 }
1271
1272 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001273 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1274 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001275 MaxBytesToFill = 0;
1276 }
1277 }
1278
Daniel Dunbar648ac512010-05-17 21:54:30 +00001279 // Check whether we should use optimal code alignment for this .align
1280 // directive.
1281 //
1282 // FIXME: This should be using a target hook.
1283 bool UseCodeAlign = false;
1284 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1285 Out.getCurrentSection()))
1286 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1287 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1288 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001289 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001290 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001291 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1292 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001293 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001294
1295 return false;
1296}
1297
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001298/// ParseDirectiveSymbolAttribute
1299/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001300bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001301 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001302 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001303 StringRef Name;
1304
1305 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001306 return TokError("expected identifier in directive");
1307
Daniel Dunbar959fd882009-08-26 22:13:22 +00001308 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001309
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001310 Out.EmitSymbolAttribute(Sym, Attr);
1311
Daniel Dunbar3f872332009-07-28 16:08:33 +00001312 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001313 break;
1314
Daniel Dunbar3f872332009-07-28 16:08:33 +00001315 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001316 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001317 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001318 }
1319 }
1320
Sean Callanan79ed1a82010-01-19 20:22:31 +00001321 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001322 return false;
1323}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001324
Matt Fleming924c5e52010-05-21 11:36:59 +00001325/// ParseDirectiveELFType
1326/// ::= .type identifier , @attribute
1327bool AsmParser::ParseDirectiveELFType() {
1328 StringRef Name;
1329 if (ParseIdentifier(Name))
1330 return TokError("expected identifier in directive");
1331
1332 // Handle the identifier as the key symbol.
1333 MCSymbol *Sym = CreateSymbol(Name);
1334
1335 if (Lexer.isNot(AsmToken::Comma))
1336 return TokError("unexpected token in '.type' directive");
1337 Lex();
1338
1339 if (Lexer.isNot(AsmToken::At))
1340 return TokError("expected '@' before type");
1341 Lex();
1342
1343 StringRef Type;
1344 SMLoc TypeLoc;
1345
1346 TypeLoc = Lexer.getLoc();
1347 if (ParseIdentifier(Type))
1348 return TokError("expected symbol type in directive");
1349
1350 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1351 .Case("function", MCSA_ELF_TypeFunction)
1352 .Case("object", MCSA_ELF_TypeObject)
1353 .Case("tls_object", MCSA_ELF_TypeTLS)
1354 .Case("common", MCSA_ELF_TypeCommon)
1355 .Case("notype", MCSA_ELF_TypeNoType)
1356 .Default(MCSA_Invalid);
1357
1358 if (Attr == MCSA_Invalid)
1359 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1360
1361 if (Lexer.isNot(AsmToken::EndOfStatement))
1362 return TokError("unexpected token in '.type' directive");
1363
1364 Lex();
1365
1366 Out.EmitSymbolAttribute(Sym, Attr);
1367
1368 return false;
1369}
1370
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001371/// ParseDirectiveDarwinSymbolDesc
1372/// ::= .desc identifier , expression
1373bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001374 StringRef Name;
1375 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001376 return TokError("expected identifier in directive");
1377
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001378 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001379 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001380
Daniel Dunbar3f872332009-07-28 16:08:33 +00001381 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001382 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001383 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001384
1385 SMLoc DescLoc = Lexer.getLoc();
1386 int64_t DescValue;
1387 if (ParseAbsoluteExpression(DescValue))
1388 return true;
1389
Daniel Dunbar3f872332009-07-28 16:08:33 +00001390 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001391 return TokError("unexpected token in '.desc' directive");
1392
Sean Callanan79ed1a82010-01-19 20:22:31 +00001393 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001394
1395 // Set the n_desc field of this Symbol to this DescValue
1396 Out.EmitSymbolDesc(Sym, DescValue);
1397
1398 return false;
1399}
1400
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001401/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001402/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1403bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001404 SMLoc IDLoc = Lexer.getLoc();
1405 StringRef Name;
1406 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001407 return TokError("expected identifier in directive");
1408
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001409 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001410 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001411
Daniel Dunbar3f872332009-07-28 16:08:33 +00001412 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001413 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001414 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001415
1416 int64_t Size;
1417 SMLoc SizeLoc = Lexer.getLoc();
1418 if (ParseAbsoluteExpression(Size))
1419 return true;
1420
1421 int64_t Pow2Alignment = 0;
1422 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001423 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001424 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001425 Pow2AlignmentLoc = Lexer.getLoc();
1426 if (ParseAbsoluteExpression(Pow2Alignment))
1427 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001428
1429 // If this target takes alignments in bytes (not log) validate and convert.
1430 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1431 if (!isPowerOf2_64(Pow2Alignment))
1432 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1433 Pow2Alignment = Log2_64(Pow2Alignment);
1434 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001435 }
1436
Daniel Dunbar3f872332009-07-28 16:08:33 +00001437 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001438 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001439
Sean Callanan79ed1a82010-01-19 20:22:31 +00001440 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001441
Chris Lattner1fc3d752009-07-09 17:25:12 +00001442 // NOTE: a size of zero for a .comm should create a undefined symbol
1443 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001444 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001445 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1446 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001447
Eric Christopherc260a3e2010-05-14 01:38:54 +00001448 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001449 // may internally end up wanting an alignment in bytes.
1450 // FIXME: Diagnose overflow.
1451 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001452 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1453 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001454
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001455 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001456 return Error(IDLoc, "invalid symbol redefinition");
1457
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001458 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001459 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001460 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001461 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1462 MCSectionMachO::S_ZEROFILL, 0,
1463 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001464 Sym, Size, 1 << Pow2Alignment);
1465 return false;
1466 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001467
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001468 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001469 return false;
1470}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001471
1472/// ParseDirectiveDarwinZerofill
1473/// ::= .zerofill segname , sectname [, identifier , size_expression [
1474/// , align_expression ]]
1475bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001476 StringRef Segment;
1477 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001478 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001479
Daniel Dunbar3f872332009-07-28 16:08:33 +00001480 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001481 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001482 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001483
1484 StringRef Section;
1485 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001486 return TokError("expected section name after comma in '.zerofill' "
1487 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001488
Chris Lattner9be3fee2009-07-10 22:20:30 +00001489 // If this is the end of the line all that was wanted was to create the
1490 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001491 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001492 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001493 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1494 MCSectionMachO::S_ZEROFILL, 0,
1495 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001496 return false;
1497 }
1498
Daniel Dunbar3f872332009-07-28 16:08:33 +00001499 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001500 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001501 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001502
Chris Lattner7bb7c552010-05-13 00:10:34 +00001503 SMLoc IDLoc = Lexer.getLoc();
1504 StringRef IDStr;
1505 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001506 return TokError("expected identifier in directive");
1507
1508 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001509 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001510
Daniel Dunbar3f872332009-07-28 16:08:33 +00001511 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001512 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001513 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001514
1515 int64_t Size;
1516 SMLoc SizeLoc = Lexer.getLoc();
1517 if (ParseAbsoluteExpression(Size))
1518 return true;
1519
1520 int64_t Pow2Alignment = 0;
1521 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001522 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001523 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001524 Pow2AlignmentLoc = Lexer.getLoc();
1525 if (ParseAbsoluteExpression(Pow2Alignment))
1526 return true;
1527 }
1528
Daniel Dunbar3f872332009-07-28 16:08:33 +00001529 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001530 return TokError("unexpected token in '.zerofill' directive");
1531
Sean Callanan79ed1a82010-01-19 20:22:31 +00001532 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001533
1534 if (Size < 0)
1535 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1536 "than zero");
1537
Eric Christopherc260a3e2010-05-14 01:38:54 +00001538 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001539 // may internally end up wanting an alignment in bytes.
1540 // FIXME: Diagnose overflow.
1541 if (Pow2Alignment < 0)
1542 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1543 "can't be less than zero");
1544
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001545 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001546 return Error(IDLoc, "invalid symbol redefinition");
1547
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001548 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001549 //
1550 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001551 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1552 MCSectionMachO::S_ZEROFILL, 0,
1553 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001554 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001555
1556 return false;
1557}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001558
Eric Christopher482eba02010-05-14 01:50:28 +00001559/// ParseDirectiveDarwinTBSS
1560/// ::= .tbss identifier, size, align
1561bool AsmParser::ParseDirectiveDarwinTBSS() {
1562 SMLoc IDLoc = Lexer.getLoc();
1563 StringRef Name;
1564 if (ParseIdentifier(Name))
1565 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001566
Eric Christopher482eba02010-05-14 01:50:28 +00001567 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001568 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001569
1570 if (Lexer.isNot(AsmToken::Comma))
1571 return TokError("unexpected token in directive");
1572 Lex();
1573
1574 int64_t Size;
1575 SMLoc SizeLoc = Lexer.getLoc();
1576 if (ParseAbsoluteExpression(Size))
1577 return true;
1578
1579 int64_t Pow2Alignment = 0;
1580 SMLoc Pow2AlignmentLoc;
1581 if (Lexer.is(AsmToken::Comma)) {
1582 Lex();
1583 Pow2AlignmentLoc = Lexer.getLoc();
1584 if (ParseAbsoluteExpression(Pow2Alignment))
1585 return true;
1586 }
1587
1588 if (Lexer.isNot(AsmToken::EndOfStatement))
1589 return TokError("unexpected token in '.tbss' directive");
1590
1591 Lex();
1592
1593 if (Size < 0)
1594 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1595 "zero");
1596
1597 // FIXME: Diagnose overflow.
1598 if (Pow2Alignment < 0)
1599 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1600 "than zero");
1601
1602 if (!Sym->isUndefined())
1603 return Error(IDLoc, "invalid symbol redefinition");
1604
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001605 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1606 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1607 0, SectionKind::getThreadBSS()),
1608 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001609
1610 return false;
1611}
1612
Kevin Enderbya5c78322009-07-13 21:03:15 +00001613/// ParseDirectiveDarwinSubsectionsViaSymbols
1614/// ::= .subsections_via_symbols
1615bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001616 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001617 return TokError("unexpected token in '.subsections_via_symbols' directive");
1618
Sean Callanan79ed1a82010-01-19 20:22:31 +00001619 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001620
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001621 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001622
1623 return false;
1624}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001625
1626/// ParseDirectiveAbort
1627/// ::= .abort [ "abort_string" ]
1628bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001629 // FIXME: Use loc from directive.
1630 SMLoc Loc = Lexer.getLoc();
1631
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001632 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001633 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1634 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001635 return TokError("expected string in '.abort' directive");
1636
Sean Callanan18b83232010-01-19 21:44:56 +00001637 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001638
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001640 }
1641
Daniel Dunbar3f872332009-07-28 16:08:33 +00001642 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001643 return TokError("unexpected token in '.abort' directive");
1644
Sean Callanan79ed1a82010-01-19 20:22:31 +00001645 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001646
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001647 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001648 if (Str.empty())
1649 Error(Loc, ".abort detected. Assembly stopping.");
1650 else
1651 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001652
1653 return false;
1654}
Kevin Enderby71148242009-07-14 21:35:03 +00001655
1656/// ParseDirectiveLsym
1657/// ::= .lsym identifier , expression
1658bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001659 StringRef Name;
1660 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001661 return TokError("expected identifier in directive");
1662
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001663 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001664 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001665
Daniel Dunbar3f872332009-07-28 16:08:33 +00001666 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001667 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001668 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001669
Daniel Dunbar821e3332009-08-31 08:09:28 +00001670 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001671 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001672 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001673 return true;
1674
Daniel Dunbar3f872332009-07-28 16:08:33 +00001675 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001676 return TokError("unexpected token in '.lsym' directive");
1677
Sean Callanan79ed1a82010-01-19 20:22:31 +00001678 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001679
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001680 // We don't currently support this directive.
1681 //
1682 // FIXME: Diagnostic location!
1683 (void) Sym;
1684 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001685}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001686
1687/// ParseDirectiveInclude
1688/// ::= .include "filename"
1689bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001690 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001691 return TokError("expected string in '.include' directive");
1692
Sean Callanan18b83232010-01-19 21:44:56 +00001693 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001694 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001695 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001696
Daniel Dunbar3f872332009-07-28 16:08:33 +00001697 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001698 return TokError("unexpected token in '.include' directive");
1699
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001700 // Strip the quotes.
1701 Filename = Filename.substr(1, Filename.size()-2);
1702
1703 // Attempt to switch the lexer to the included file before consuming the end
1704 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001705 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001706 PrintMessage(IncludeLoc,
1707 "Could not find include file '" + Filename + "'",
1708 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001709 return true;
1710 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001711
1712 return false;
1713}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001714
1715/// ParseDirectiveDarwinDumpOrLoad
1716/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001717bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001718 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001719 return TokError("expected string in '.dump' or '.load' directive");
1720
Sean Callanan79ed1a82010-01-19 20:22:31 +00001721 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001722
Daniel Dunbar3f872332009-07-28 16:08:33 +00001723 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001724 return TokError("unexpected token in '.dump' or '.load' directive");
1725
Sean Callanan79ed1a82010-01-19 20:22:31 +00001726 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001727
Kevin Enderby5026ae42009-07-20 20:25:37 +00001728 // FIXME: If/when .dump and .load are implemented they will be done in the
1729 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001730 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001731 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001732 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001733 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001734
1735 return false;
1736}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001737
1738/// ParseDirectiveIf
1739/// ::= .if expression
1740bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001741 TheCondStack.push_back(TheCondState);
1742 TheCondState.TheCond = AsmCond::IfCond;
1743 if(TheCondState.Ignore) {
1744 EatToEndOfStatement();
1745 }
1746 else {
1747 int64_t ExprValue;
1748 if (ParseAbsoluteExpression(ExprValue))
1749 return true;
1750
1751 if (Lexer.isNot(AsmToken::EndOfStatement))
1752 return TokError("unexpected token in '.if' directive");
1753
Sean Callanan79ed1a82010-01-19 20:22:31 +00001754 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001755
1756 TheCondState.CondMet = ExprValue;
1757 TheCondState.Ignore = !TheCondState.CondMet;
1758 }
1759
1760 return false;
1761}
1762
1763/// ParseDirectiveElseIf
1764/// ::= .elseif expression
1765bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1766 if (TheCondState.TheCond != AsmCond::IfCond &&
1767 TheCondState.TheCond != AsmCond::ElseIfCond)
1768 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1769 " an .elseif");
1770 TheCondState.TheCond = AsmCond::ElseIfCond;
1771
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001772 bool LastIgnoreState = false;
1773 if (!TheCondStack.empty())
1774 LastIgnoreState = TheCondStack.back().Ignore;
1775 if (LastIgnoreState || TheCondState.CondMet) {
1776 TheCondState.Ignore = true;
1777 EatToEndOfStatement();
1778 }
1779 else {
1780 int64_t ExprValue;
1781 if (ParseAbsoluteExpression(ExprValue))
1782 return true;
1783
1784 if (Lexer.isNot(AsmToken::EndOfStatement))
1785 return TokError("unexpected token in '.elseif' directive");
1786
Sean Callanan79ed1a82010-01-19 20:22:31 +00001787 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001788 TheCondState.CondMet = ExprValue;
1789 TheCondState.Ignore = !TheCondState.CondMet;
1790 }
1791
1792 return false;
1793}
1794
1795/// ParseDirectiveElse
1796/// ::= .else
1797bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001798 if (Lexer.isNot(AsmToken::EndOfStatement))
1799 return TokError("unexpected token in '.else' directive");
1800
Sean Callanan79ed1a82010-01-19 20:22:31 +00001801 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001802
1803 if (TheCondState.TheCond != AsmCond::IfCond &&
1804 TheCondState.TheCond != AsmCond::ElseIfCond)
1805 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1806 ".elseif");
1807 TheCondState.TheCond = AsmCond::ElseCond;
1808 bool LastIgnoreState = false;
1809 if (!TheCondStack.empty())
1810 LastIgnoreState = TheCondStack.back().Ignore;
1811 if (LastIgnoreState || TheCondState.CondMet)
1812 TheCondState.Ignore = true;
1813 else
1814 TheCondState.Ignore = false;
1815
1816 return false;
1817}
1818
1819/// ParseDirectiveEndIf
1820/// ::= .endif
1821bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001822 if (Lexer.isNot(AsmToken::EndOfStatement))
1823 return TokError("unexpected token in '.endif' directive");
1824
Sean Callanan79ed1a82010-01-19 20:22:31 +00001825 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001826
1827 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1828 TheCondStack.empty())
1829 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1830 ".else");
1831 if (!TheCondStack.empty()) {
1832 TheCondState = TheCondStack.back();
1833 TheCondStack.pop_back();
1834 }
1835
1836 return false;
1837}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001838
1839/// ParseDirectiveFile
1840/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001841bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001842 // FIXME: I'm not sure what this is.
1843 int64_t FileNumber = -1;
1844 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001845 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001846 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001847
1848 if (FileNumber < 1)
1849 return TokError("file number less than one");
1850 }
1851
1852 if (Lexer.isNot(AsmToken::String))
1853 return TokError("unexpected token in '.file' directive");
1854
Chris Lattnerd32e8032010-01-25 19:02:58 +00001855 StringRef Filename = getTok().getString();
1856 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001857 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001858
1859 if (Lexer.isNot(AsmToken::EndOfStatement))
1860 return TokError("unexpected token in '.file' directive");
1861
Chris Lattnerd32e8032010-01-25 19:02:58 +00001862 if (FileNumber == -1)
1863 Out.EmitFileDirective(Filename);
1864 else
1865 Out.EmitDwarfFileDirective(FileNumber, Filename);
1866
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001867 return false;
1868}
1869
1870/// ParseDirectiveLine
1871/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001872bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001873 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1874 if (Lexer.isNot(AsmToken::Integer))
1875 return TokError("unexpected token in '.line' directive");
1876
Sean Callanan18b83232010-01-19 21:44:56 +00001877 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001878 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001879 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001880
1881 // FIXME: Do something with the .line.
1882 }
1883
1884 if (Lexer.isNot(AsmToken::EndOfStatement))
1885 return TokError("unexpected token in '.file' directive");
1886
1887 return false;
1888}
1889
1890
1891/// ParseDirectiveLoc
1892/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001893bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001894 if (Lexer.isNot(AsmToken::Integer))
1895 return TokError("unexpected token in '.loc' directive");
1896
1897 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001898 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001899 (void) FileNumber;
1900 // FIXME: Validate file.
1901
Sean Callanan79ed1a82010-01-19 20:22:31 +00001902 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001903 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1904 if (Lexer.isNot(AsmToken::Integer))
1905 return TokError("unexpected token in '.loc' directive");
1906
Sean Callanan18b83232010-01-19 21:44:56 +00001907 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001908 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001909 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001910
1911 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1912 if (Lexer.isNot(AsmToken::Integer))
1913 return TokError("unexpected token in '.loc' directive");
1914
Sean Callanan18b83232010-01-19 21:44:56 +00001915 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001916 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001917 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001918
1919 // FIXME: Do something with the .loc.
1920 }
1921 }
1922
1923 if (Lexer.isNot(AsmToken::EndOfStatement))
1924 return TokError("unexpected token in '.file' directive");
1925
1926 return false;
1927}
1928