blob: 0134519177190e364daf2f400fc273b9771279c0 [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)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000435 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000436 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000437 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000438
439 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000440 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000441 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000442 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000443 int64_t LocalLabelVal = -1;
444 // GUESS allow an integer followed by a ':' as a directional local label
445 if (Lexer.is(AsmToken::Integer)) {
446 LocalLabelVal = getTok().getIntVal();
447 if (LocalLabelVal < 0) {
448 if (!TheCondState.Ignore)
449 return TokError("unexpected token at start of statement");
450 IDVal = "";
451 }
452 else {
453 IDVal = getTok().getString();
454 Lex(); // Consume the integer token to be used as an identifier token.
455 if (Lexer.getKind() != AsmToken::Colon) {
456 if (!TheCondState.Ignore)
457 return TokError("unexpected token at start of statement");
458 }
459 }
460 }
461 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000462 if (!TheCondState.Ignore)
463 return TokError("unexpected token at start of statement");
464 IDVal = "";
465 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000466
Chris Lattner7834fac2010-04-17 18:14:27 +0000467 // Handle conditional assembly here before checking for skipping. We
468 // have to do this so that .endif isn't skipped in a ".if 0" block for
469 // example.
470 if (IDVal == ".if")
471 return ParseDirectiveIf(IDLoc);
472 if (IDVal == ".elseif")
473 return ParseDirectiveElseIf(IDLoc);
474 if (IDVal == ".else")
475 return ParseDirectiveElse(IDLoc);
476 if (IDVal == ".endif")
477 return ParseDirectiveEndIf(IDLoc);
478
479 // If we are in a ".if 0" block, ignore this statement.
480 if (TheCondState.Ignore) {
481 EatToEndOfStatement();
482 return false;
483 }
484
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000485 // FIXME: Recurse on local labels?
486
487 // See what kind of statement we have.
488 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000489 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000490 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000491 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000492
493 // Diagnose attempt to use a variable as a label.
494 //
495 // FIXME: Diagnostics. Note the location of the definition as a label.
496 // FIXME: This doesn't diagnose assignment to a symbol which has been
497 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000498 MCSymbol *Sym;
499 if (LocalLabelVal == -1)
500 Sym = CreateSymbol(IDVal);
501 else
502 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000503 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000504 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000505
Daniel Dunbar959fd882009-08-26 22:13:22 +0000506 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000507 Out.EmitLabel(Sym);
508
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000509 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000510 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000511
Daniel Dunbar3f872332009-07-28 16:08:33 +0000512 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000513 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000514 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000515
Daniel Dunbare2ace502009-08-31 08:09:09 +0000516 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000517
518 default: // Normal instruction or directive.
519 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000520 }
521
522 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000523 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000524 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000526 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000528 // FIXME: This changes behavior based on the -static flag to the
529 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000530 return ParseDirectiveSectionSwitch("__TEXT", "__text",
531 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000533 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000537 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
538 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000540 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000541 MCSectionMachO::S_4BYTE_LITERALS,
542 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000544 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 MCSectionMachO::S_8BYTE_LITERALS,
546 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000548 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 MCSectionMachO::S_16BYTE_LITERALS,
550 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000553 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000554 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
559
560 // FIXME: The assembler manual claims that this has the self modify code
561 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000562 if (IDVal == ".symbol_stub")
563 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
564 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000565 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
566 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 0, 16);
568 // FIXME: PowerPC only?
569 if (IDVal == ".picsymbol_stub")
570 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
571 MCSectionMachO::S_SYMBOL_STUBS |
572 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
573 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000575 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
578
579 // FIXME: The section names of these two are misspelled in the assembler
580 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000582 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
583 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
584 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
587 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
588 4);
589
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000593 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
595 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000596 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000597 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000598 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
599 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000602
603
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000605 return ParseDirectiveSectionSwitch("__OBJC", "__class",
606 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
612 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000614 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
615 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000617 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
618 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000620 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
621 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000623 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
624 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000626 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
627 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000629 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
630 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
631 MCSectionMachO::S_LITERAL_POINTERS,
632 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000634 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
635 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
636 MCSectionMachO::S_LITERAL_POINTERS,
637 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000639 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
640 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000642 return ParseDirectiveSectionSwitch("__OBJC", "__category",
643 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000645 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
646 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000648 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
649 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000651 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
652 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000654 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
655 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000657 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
658 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000660 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
661 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000663 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
664 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000665
Eric Christopherc6177a42010-05-17 22:53:55 +0000666 if (IDVal == ".tdata")
667 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
668 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
669 if (IDVal == ".tlv")
670 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
671 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
672 if (IDVal == ".thread_init_func")
673 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
674 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
675
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000676 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000678 return ParseDirectiveSet();
679
Daniel Dunbara0d14262009-06-24 23:30:00 +0000680 // Data directives
681
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000683 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000685 return ParseDirectiveAscii(true);
686
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000688 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000689 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000690 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000692 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000694 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000695
696 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000698 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000700 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000702 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000704 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000706 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000708 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000710 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000712 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
713
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000715 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000716
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000718 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000720 return ParseDirectiveSpace();
721
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000722 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000723
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000725 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000727 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000729 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000731 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000732 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000733 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000735 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000737 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000738 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000739 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000741 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000742 if (IDVal == ".type")
743 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000745 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000747 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000749 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000750
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000752 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000754 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000756 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000757 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000758 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000759 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000760 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000761 if (IDVal == ".tbss")
762 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000763
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000765 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000767 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000768 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000769 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000770 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000771 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000772 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000773 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000774
Chris Lattnerebb89b42009-09-27 21:16:52 +0000775 // Look up the handler in the handler table,
776 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
777 if (Handler)
778 return (this->*Handler)(IDVal, IDLoc);
779
Kevin Enderby9c656452009-09-10 20:51:44 +0000780 // Target hook for parsing target specific directives.
781 if (!getTargetParser().ParseDirective(ID))
782 return false;
783
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000784 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000785 EatToEndOfStatement();
786 return false;
787 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000788
Chris Lattnera7f13542010-05-19 23:34:33 +0000789 // Canonicalize the opcode to lower case.
790 SmallString<128> Opcode;
791 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
792 Opcode.push_back(tolower(IDVal[i]));
793
Chris Lattner98986712010-01-14 22:21:20 +0000794 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000795 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000796 ParsedOperands);
797 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
798 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000799
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000800 // If parsing succeeded, match the instruction.
801 if (!HadError) {
802 MCInst Inst;
803 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
804 // Emit the instruction on success.
805 Out.EmitInstruction(Inst);
806 } else {
807 // Otherwise emit a diagnostic about the match failure and set the error
808 // flag.
809 //
810 // FIXME: We should give nicer diagnostics about the exact failure.
811 Error(IDLoc, "unrecognized instruction");
812 HadError = true;
813 }
814 }
Chris Lattner98986712010-01-14 22:21:20 +0000815
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000816 // If there was no error, consume the end-of-statement token. Otherwise this
817 // will be done by our caller.
818 if (!HadError)
819 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000820
821 // Free any parsed operands.
822 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
823 delete ParsedOperands[i];
824
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000825 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000826}
Chris Lattner9a023f72009-06-24 04:43:34 +0000827
Daniel Dunbare2ace502009-08-31 08:09:09 +0000828bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000829 // FIXME: Use better location, we should use proper tokens.
830 SMLoc EqualLoc = Lexer.getLoc();
831
Daniel Dunbar821e3332009-08-31 08:09:28 +0000832 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000833 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000834 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000835 return true;
836
Daniel Dunbar3f872332009-07-28 16:08:33 +0000837 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000838 return TokError("unexpected token in assignment");
839
840 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000841 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000842
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000843 // Validate that the LHS is allowed to be a variable (either it has not been
844 // used as a symbol, or it is an absolute symbol).
845 MCSymbol *Sym = getContext().LookupSymbol(Name);
846 if (Sym) {
847 // Diagnose assignment to a label.
848 //
849 // FIXME: Diagnostics. Note the location of the definition as a label.
850 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000851 if (Sym->isUndefined() && !Sym->isUsedInExpr())
852 ; // Allow redefinitions of undefined symbols only used in directives.
853 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000854 return Error(EqualLoc, "redefinition of '" + Name + "'");
855 else if (!Sym->isVariable())
856 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000857 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000858 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
859 Name + "'");
860 } else
861 Sym = CreateSymbol(Name);
862
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000863 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000864
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000865 Sym->setUsedInExpr(true);
866
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000867 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000868 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000869
870 return false;
871}
872
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000873/// ParseIdentifier:
874/// ::= identifier
875/// ::= string
876bool AsmParser::ParseIdentifier(StringRef &Res) {
877 if (Lexer.isNot(AsmToken::Identifier) &&
878 Lexer.isNot(AsmToken::String))
879 return true;
880
Sean Callanan18b83232010-01-19 21:44:56 +0000881 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000882
Sean Callanan79ed1a82010-01-19 20:22:31 +0000883 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000884
885 return false;
886}
887
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000888/// ParseDirectiveSet:
889/// ::= .set identifier ',' expression
890bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000891 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000892
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000893 if (ParseIdentifier(Name))
894 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000895
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000896 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000897 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000898 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000899
Daniel Dunbare2ace502009-08-31 08:09:09 +0000900 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901}
902
Chris Lattner9a023f72009-06-24 04:43:34 +0000903/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000904/// ::= .section identifier (',' identifier)*
905/// FIXME: This should actually parse out the segment, section, attributes and
906/// sizeof_stub fields.
907bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000908 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000909
Daniel Dunbarace63122009-08-11 03:42:33 +0000910 StringRef SectionName;
911 if (ParseIdentifier(SectionName))
912 return Error(Loc, "expected identifier after '.section' directive");
913
914 // Verify there is a following comma.
915 if (!Lexer.is(AsmToken::Comma))
916 return TokError("unexpected token in '.section' directive");
917
Chris Lattnerff4bc462009-08-10 01:39:42 +0000918 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000919 SectionSpec += ",";
920
921 // Add all the tokens until the end of the line, ParseSectionSpecifier will
922 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000923 StringRef EOL = Lexer.LexUntilEndOfStatement();
924 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000925
Sean Callanan79ed1a82010-01-19 20:22:31 +0000926 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000927 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000928 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000929 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000930
Chris Lattnerff4bc462009-08-10 01:39:42 +0000931
932 StringRef Segment, Section;
933 unsigned TAA, StubSize;
934 std::string ErrorStr =
935 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
936 TAA, StubSize);
937
938 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000939 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000940
Chris Lattner56594f92009-07-31 17:47:16 +0000941 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000942 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000943 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
944 isText ? SectionKind::getText()
945 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000946 return false;
947}
948
Chris Lattnere15c2d72009-08-10 18:05:55 +0000949/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000950bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
951 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000952 unsigned TAA, unsigned Align,
953 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000954 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000955 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000956 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000957
Chris Lattner56594f92009-07-31 17:47:16 +0000958 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000959 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000960 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
961 isText ? SectionKind::getText()
962 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000963
964 // Set the implicit alignment, if any.
965 //
966 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
967 // alignment on the section (e.g., if one manually inserts bytes into the
968 // section, then just issueing the section switch directive will not realign
969 // the section. However, this is arguably more reasonable behavior, and there
970 // is no good reason for someone to intentionally emit incorrectly sized
971 // values into the implicitly aligned sections.
972 if (Align)
973 Out.EmitValueToAlignment(Align, 0, 1, 0);
974
Chris Lattner529fb542009-06-24 05:13:15 +0000975 return false;
976}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000978bool AsmParser::ParseEscapedString(std::string &Data) {
979 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
980
981 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000982 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000983 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
984 if (Str[i] != '\\') {
985 Data += Str[i];
986 continue;
987 }
988
989 // Recognize escaped characters. Note that this escape semantics currently
990 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
991 ++i;
992 if (i == e)
993 return TokError("unexpected backslash at end of string");
994
995 // Recognize octal sequences.
996 if ((unsigned) (Str[i] - '0') <= 7) {
997 // Consume up to three octal characters.
998 unsigned Value = Str[i] - '0';
999
1000 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1001 ++i;
1002 Value = Value * 8 + (Str[i] - '0');
1003
1004 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1005 ++i;
1006 Value = Value * 8 + (Str[i] - '0');
1007 }
1008 }
1009
1010 if (Value > 255)
1011 return TokError("invalid octal escape sequence (out of range)");
1012
1013 Data += (unsigned char) Value;
1014 continue;
1015 }
1016
1017 // Otherwise recognize individual escapes.
1018 switch (Str[i]) {
1019 default:
1020 // Just reject invalid escape sequences for now.
1021 return TokError("invalid escape sequence (unrecognized character)");
1022
1023 case 'b': Data += '\b'; break;
1024 case 'f': Data += '\f'; break;
1025 case 'n': Data += '\n'; break;
1026 case 'r': Data += '\r'; break;
1027 case 't': Data += '\t'; break;
1028 case '"': Data += '"'; break;
1029 case '\\': Data += '\\'; break;
1030 }
1031 }
1032
1033 return false;
1034}
1035
Daniel Dunbara0d14262009-06-24 23:30:00 +00001036/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001037/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001038bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001039 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001040 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001041 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001042 return TokError("expected string in '.ascii' or '.asciz' directive");
1043
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001044 std::string Data;
1045 if (ParseEscapedString(Data))
1046 return true;
1047
Chris Lattneraaec2052010-01-19 19:46:13 +00001048 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001050 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051
Sean Callanan79ed1a82010-01-19 20:22:31 +00001052 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001053
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 break;
1056
Daniel Dunbar3f872332009-07-28 16:08:33 +00001057 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001058 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001059 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060 }
1061 }
1062
Sean Callanan79ed1a82010-01-19 20:22:31 +00001063 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064 return false;
1065}
1066
1067/// ParseDirectiveValue
1068/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1069bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001070 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001072 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001073 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001074 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001075 return true;
1076
Chris Lattneraaec2052010-01-19 19:46:13 +00001077 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080 break;
1081
1082 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001083 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001085 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086 }
1087 }
1088
Sean Callanan79ed1a82010-01-19 20:22:31 +00001089 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001090 return false;
1091}
1092
1093/// ParseDirectiveSpace
1094/// ::= .space expression [ , expression ]
1095bool AsmParser::ParseDirectiveSpace() {
1096 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001097 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001098 return true;
1099
1100 int64_t FillExpr = 0;
1101 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001102 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1103 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001104 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001105 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001106
Daniel Dunbar475839e2009-06-29 20:37:27 +00001107 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001108 return true;
1109
1110 HasFillExpr = true;
1111
Daniel Dunbar3f872332009-07-28 16:08:33 +00001112 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113 return TokError("unexpected token in '.space' directive");
1114 }
1115
Sean Callanan79ed1a82010-01-19 20:22:31 +00001116 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117
1118 if (NumBytes <= 0)
1119 return TokError("invalid number of bytes in '.space' directive");
1120
1121 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001122 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001123
1124 return false;
1125}
1126
1127/// ParseDirectiveFill
1128/// ::= .fill expression , expression , expression
1129bool AsmParser::ParseDirectiveFill() {
1130 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001131 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132 return true;
1133
Daniel Dunbar3f872332009-07-28 16:08:33 +00001134 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001135 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001136 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001137
1138 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001139 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001140 return true;
1141
Daniel Dunbar3f872332009-07-28 16:08:33 +00001142 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001143 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001144 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001145
1146 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001147 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148 return true;
1149
Daniel Dunbar3f872332009-07-28 16:08:33 +00001150 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001151 return TokError("unexpected token in '.fill' directive");
1152
Sean Callanan79ed1a82010-01-19 20:22:31 +00001153 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001155 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1156 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001157
1158 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001159 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1160 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001161
1162 return false;
1163}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001164
1165/// ParseDirectiveOrg
1166/// ::= .org expression [ , expression ]
1167bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001168 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001169 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001170 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001171 return true;
1172
1173 // Parse optional fill expression.
1174 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001175 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1176 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001177 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001178 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001179
Daniel Dunbar475839e2009-06-29 20:37:27 +00001180 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001181 return true;
1182
Daniel Dunbar3f872332009-07-28 16:08:33 +00001183 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001184 return TokError("unexpected token in '.org' directive");
1185 }
1186
Sean Callanan79ed1a82010-01-19 20:22:31 +00001187 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001188
1189 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1190 // has to be relative to the current section.
1191 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001192
1193 return false;
1194}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001195
1196/// ParseDirectiveAlign
1197/// ::= {.align, ...} expression [ , expression [ , expression ]]
1198bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001199 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001200 int64_t Alignment;
1201 if (ParseAbsoluteExpression(Alignment))
1202 return true;
1203
1204 SMLoc MaxBytesLoc;
1205 bool HasFillExpr = false;
1206 int64_t FillExpr = 0;
1207 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001208 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1209 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001210 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001211 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001212
1213 // The fill expression can be omitted while specifying a maximum number of
1214 // alignment bytes, e.g:
1215 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001216 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001217 HasFillExpr = true;
1218 if (ParseAbsoluteExpression(FillExpr))
1219 return true;
1220 }
1221
Daniel Dunbar3f872332009-07-28 16:08:33 +00001222 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1223 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001225 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001226
1227 MaxBytesLoc = Lexer.getLoc();
1228 if (ParseAbsoluteExpression(MaxBytesToFill))
1229 return true;
1230
Daniel Dunbar3f872332009-07-28 16:08:33 +00001231 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001232 return TokError("unexpected token in directive");
1233 }
1234 }
1235
Sean Callanan79ed1a82010-01-19 20:22:31 +00001236 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001237
Daniel Dunbar648ac512010-05-17 21:54:30 +00001238 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001239 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001240
1241 // Compute alignment in bytes.
1242 if (IsPow2) {
1243 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001244 if (Alignment >= 32) {
1245 Error(AlignmentLoc, "invalid alignment value");
1246 Alignment = 31;
1247 }
1248
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001249 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001250 }
1251
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001252 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001253 if (MaxBytesLoc.isValid()) {
1254 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001255 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1256 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001257 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001258 }
1259
1260 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001261 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1262 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001263 MaxBytesToFill = 0;
1264 }
1265 }
1266
Daniel Dunbar648ac512010-05-17 21:54:30 +00001267 // Check whether we should use optimal code alignment for this .align
1268 // directive.
1269 //
1270 // FIXME: This should be using a target hook.
1271 bool UseCodeAlign = false;
1272 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1273 Out.getCurrentSection()))
1274 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1275 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1276 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001277 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001278 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001279 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1280 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001281 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001282
1283 return false;
1284}
1285
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001286/// ParseDirectiveSymbolAttribute
1287/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001288bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001289 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001290 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001291 StringRef Name;
1292
1293 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001294 return TokError("expected identifier in directive");
1295
Daniel Dunbar959fd882009-08-26 22:13:22 +00001296 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001297
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001298 Out.EmitSymbolAttribute(Sym, Attr);
1299
Daniel Dunbar3f872332009-07-28 16:08:33 +00001300 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001301 break;
1302
Daniel Dunbar3f872332009-07-28 16:08:33 +00001303 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001304 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001305 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001306 }
1307 }
1308
Sean Callanan79ed1a82010-01-19 20:22:31 +00001309 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001310 return false;
1311}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001312
Matt Fleming924c5e52010-05-21 11:36:59 +00001313/// ParseDirectiveELFType
1314/// ::= .type identifier , @attribute
1315bool AsmParser::ParseDirectiveELFType() {
1316 StringRef Name;
1317 if (ParseIdentifier(Name))
1318 return TokError("expected identifier in directive");
1319
1320 // Handle the identifier as the key symbol.
1321 MCSymbol *Sym = CreateSymbol(Name);
1322
1323 if (Lexer.isNot(AsmToken::Comma))
1324 return TokError("unexpected token in '.type' directive");
1325 Lex();
1326
1327 if (Lexer.isNot(AsmToken::At))
1328 return TokError("expected '@' before type");
1329 Lex();
1330
1331 StringRef Type;
1332 SMLoc TypeLoc;
1333
1334 TypeLoc = Lexer.getLoc();
1335 if (ParseIdentifier(Type))
1336 return TokError("expected symbol type in directive");
1337
1338 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1339 .Case("function", MCSA_ELF_TypeFunction)
1340 .Case("object", MCSA_ELF_TypeObject)
1341 .Case("tls_object", MCSA_ELF_TypeTLS)
1342 .Case("common", MCSA_ELF_TypeCommon)
1343 .Case("notype", MCSA_ELF_TypeNoType)
1344 .Default(MCSA_Invalid);
1345
1346 if (Attr == MCSA_Invalid)
1347 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1348
1349 if (Lexer.isNot(AsmToken::EndOfStatement))
1350 return TokError("unexpected token in '.type' directive");
1351
1352 Lex();
1353
1354 Out.EmitSymbolAttribute(Sym, Attr);
1355
1356 return false;
1357}
1358
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001359/// ParseDirectiveDarwinSymbolDesc
1360/// ::= .desc identifier , expression
1361bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001362 StringRef Name;
1363 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001364 return TokError("expected identifier in directive");
1365
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001366 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001367 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001368
Daniel Dunbar3f872332009-07-28 16:08:33 +00001369 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001370 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001371 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001372
1373 SMLoc DescLoc = Lexer.getLoc();
1374 int64_t DescValue;
1375 if (ParseAbsoluteExpression(DescValue))
1376 return true;
1377
Daniel Dunbar3f872332009-07-28 16:08:33 +00001378 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001379 return TokError("unexpected token in '.desc' directive");
1380
Sean Callanan79ed1a82010-01-19 20:22:31 +00001381 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001382
1383 // Set the n_desc field of this Symbol to this DescValue
1384 Out.EmitSymbolDesc(Sym, DescValue);
1385
1386 return false;
1387}
1388
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001389/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001390/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1391bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001392 SMLoc IDLoc = Lexer.getLoc();
1393 StringRef Name;
1394 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001395 return TokError("expected identifier in directive");
1396
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001397 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001398 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001399
Daniel Dunbar3f872332009-07-28 16:08:33 +00001400 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001401 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001402 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001403
1404 int64_t Size;
1405 SMLoc SizeLoc = Lexer.getLoc();
1406 if (ParseAbsoluteExpression(Size))
1407 return true;
1408
1409 int64_t Pow2Alignment = 0;
1410 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001411 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001412 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001413 Pow2AlignmentLoc = Lexer.getLoc();
1414 if (ParseAbsoluteExpression(Pow2Alignment))
1415 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001416
1417 // If this target takes alignments in bytes (not log) validate and convert.
1418 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1419 if (!isPowerOf2_64(Pow2Alignment))
1420 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1421 Pow2Alignment = Log2_64(Pow2Alignment);
1422 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001423 }
1424
Daniel Dunbar3f872332009-07-28 16:08:33 +00001425 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001426 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001427
Sean Callanan79ed1a82010-01-19 20:22:31 +00001428 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001429
Chris Lattner1fc3d752009-07-09 17:25:12 +00001430 // NOTE: a size of zero for a .comm should create a undefined symbol
1431 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001432 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001433 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1434 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001435
Eric Christopherc260a3e2010-05-14 01:38:54 +00001436 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001437 // may internally end up wanting an alignment in bytes.
1438 // FIXME: Diagnose overflow.
1439 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001440 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1441 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001442
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001443 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001444 return Error(IDLoc, "invalid symbol redefinition");
1445
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001446 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001447 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001448 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001449 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1450 MCSectionMachO::S_ZEROFILL, 0,
1451 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001452 Sym, Size, 1 << Pow2Alignment);
1453 return false;
1454 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001455
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001456 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001457 return false;
1458}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001459
1460/// ParseDirectiveDarwinZerofill
1461/// ::= .zerofill segname , sectname [, identifier , size_expression [
1462/// , align_expression ]]
1463bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001464 StringRef Segment;
1465 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001466 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001467
Daniel Dunbar3f872332009-07-28 16:08:33 +00001468 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001469 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001470 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001471
1472 StringRef Section;
1473 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001474 return TokError("expected section name after comma in '.zerofill' "
1475 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001476
Chris Lattner9be3fee2009-07-10 22:20:30 +00001477 // If this is the end of the line all that was wanted was to create the
1478 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001479 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001480 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001481 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1482 MCSectionMachO::S_ZEROFILL, 0,
1483 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001484 return false;
1485 }
1486
Daniel Dunbar3f872332009-07-28 16:08:33 +00001487 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001488 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001489 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001490
Chris Lattner7bb7c552010-05-13 00:10:34 +00001491 SMLoc IDLoc = Lexer.getLoc();
1492 StringRef IDStr;
1493 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001494 return TokError("expected identifier in directive");
1495
1496 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001497 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001498
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
1503 int64_t Size;
1504 SMLoc SizeLoc = Lexer.getLoc();
1505 if (ParseAbsoluteExpression(Size))
1506 return true;
1507
1508 int64_t Pow2Alignment = 0;
1509 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001510 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001511 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001512 Pow2AlignmentLoc = Lexer.getLoc();
1513 if (ParseAbsoluteExpression(Pow2Alignment))
1514 return true;
1515 }
1516
Daniel Dunbar3f872332009-07-28 16:08:33 +00001517 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001518 return TokError("unexpected token in '.zerofill' directive");
1519
Sean Callanan79ed1a82010-01-19 20:22:31 +00001520 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001521
1522 if (Size < 0)
1523 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1524 "than zero");
1525
Eric Christopherc260a3e2010-05-14 01:38:54 +00001526 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001527 // may internally end up wanting an alignment in bytes.
1528 // FIXME: Diagnose overflow.
1529 if (Pow2Alignment < 0)
1530 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1531 "can't be less than zero");
1532
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001533 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001534 return Error(IDLoc, "invalid symbol redefinition");
1535
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001536 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001537 //
1538 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001539 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1540 MCSectionMachO::S_ZEROFILL, 0,
1541 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001542 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001543
1544 return false;
1545}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001546
Eric Christopher482eba02010-05-14 01:50:28 +00001547/// ParseDirectiveDarwinTBSS
1548/// ::= .tbss identifier, size, align
1549bool AsmParser::ParseDirectiveDarwinTBSS() {
1550 SMLoc IDLoc = Lexer.getLoc();
1551 StringRef Name;
1552 if (ParseIdentifier(Name))
1553 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001554
Eric Christopher482eba02010-05-14 01:50:28 +00001555 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001556 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001557
1558 if (Lexer.isNot(AsmToken::Comma))
1559 return TokError("unexpected token in directive");
1560 Lex();
1561
1562 int64_t Size;
1563 SMLoc SizeLoc = Lexer.getLoc();
1564 if (ParseAbsoluteExpression(Size))
1565 return true;
1566
1567 int64_t Pow2Alignment = 0;
1568 SMLoc Pow2AlignmentLoc;
1569 if (Lexer.is(AsmToken::Comma)) {
1570 Lex();
1571 Pow2AlignmentLoc = Lexer.getLoc();
1572 if (ParseAbsoluteExpression(Pow2Alignment))
1573 return true;
1574 }
1575
1576 if (Lexer.isNot(AsmToken::EndOfStatement))
1577 return TokError("unexpected token in '.tbss' directive");
1578
1579 Lex();
1580
1581 if (Size < 0)
1582 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1583 "zero");
1584
1585 // FIXME: Diagnose overflow.
1586 if (Pow2Alignment < 0)
1587 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1588 "than zero");
1589
1590 if (!Sym->isUndefined())
1591 return Error(IDLoc, "invalid symbol redefinition");
1592
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001593 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1594 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1595 0, SectionKind::getThreadBSS()),
1596 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001597
1598 return false;
1599}
1600
Kevin Enderbya5c78322009-07-13 21:03:15 +00001601/// ParseDirectiveDarwinSubsectionsViaSymbols
1602/// ::= .subsections_via_symbols
1603bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001604 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001605 return TokError("unexpected token in '.subsections_via_symbols' directive");
1606
Sean Callanan79ed1a82010-01-19 20:22:31 +00001607 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001608
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001609 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001610
1611 return false;
1612}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001613
1614/// ParseDirectiveAbort
1615/// ::= .abort [ "abort_string" ]
1616bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001617 // FIXME: Use loc from directive.
1618 SMLoc Loc = Lexer.getLoc();
1619
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001620 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001621 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1622 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001623 return TokError("expected string in '.abort' directive");
1624
Sean Callanan18b83232010-01-19 21:44:56 +00001625 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001626
Sean Callanan79ed1a82010-01-19 20:22:31 +00001627 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001628 }
1629
Daniel Dunbar3f872332009-07-28 16:08:33 +00001630 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001631 return TokError("unexpected token in '.abort' directive");
1632
Sean Callanan79ed1a82010-01-19 20:22:31 +00001633 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001634
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001635 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001636 if (Str.empty())
1637 Error(Loc, ".abort detected. Assembly stopping.");
1638 else
1639 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001640
1641 return false;
1642}
Kevin Enderby71148242009-07-14 21:35:03 +00001643
1644/// ParseDirectiveLsym
1645/// ::= .lsym identifier , expression
1646bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001647 StringRef Name;
1648 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001649 return TokError("expected identifier in directive");
1650
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001651 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001652 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001653
Daniel Dunbar3f872332009-07-28 16:08:33 +00001654 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001655 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001656 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001657
Daniel Dunbar821e3332009-08-31 08:09:28 +00001658 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001659 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001660 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001661 return true;
1662
Daniel Dunbar3f872332009-07-28 16:08:33 +00001663 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001664 return TokError("unexpected token in '.lsym' directive");
1665
Sean Callanan79ed1a82010-01-19 20:22:31 +00001666 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001667
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001668 // We don't currently support this directive.
1669 //
1670 // FIXME: Diagnostic location!
1671 (void) Sym;
1672 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001673}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001674
1675/// ParseDirectiveInclude
1676/// ::= .include "filename"
1677bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001678 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001679 return TokError("expected string in '.include' directive");
1680
Sean Callanan18b83232010-01-19 21:44:56 +00001681 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001682 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001683 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001684
Daniel Dunbar3f872332009-07-28 16:08:33 +00001685 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001686 return TokError("unexpected token in '.include' directive");
1687
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001688 // Strip the quotes.
1689 Filename = Filename.substr(1, Filename.size()-2);
1690
1691 // Attempt to switch the lexer to the included file before consuming the end
1692 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001693 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001694 PrintMessage(IncludeLoc,
1695 "Could not find include file '" + Filename + "'",
1696 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001697 return true;
1698 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001699
1700 return false;
1701}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001702
1703/// ParseDirectiveDarwinDumpOrLoad
1704/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001705bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001706 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001707 return TokError("expected string in '.dump' or '.load' directive");
1708
Sean Callanan79ed1a82010-01-19 20:22:31 +00001709 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001710
Daniel Dunbar3f872332009-07-28 16:08:33 +00001711 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001712 return TokError("unexpected token in '.dump' or '.load' directive");
1713
Sean Callanan79ed1a82010-01-19 20:22:31 +00001714 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001715
Kevin Enderby5026ae42009-07-20 20:25:37 +00001716 // FIXME: If/when .dump and .load are implemented they will be done in the
1717 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001718 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001719 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001720 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001721 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001722
1723 return false;
1724}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001725
1726/// ParseDirectiveIf
1727/// ::= .if expression
1728bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001729 TheCondStack.push_back(TheCondState);
1730 TheCondState.TheCond = AsmCond::IfCond;
1731 if(TheCondState.Ignore) {
1732 EatToEndOfStatement();
1733 }
1734 else {
1735 int64_t ExprValue;
1736 if (ParseAbsoluteExpression(ExprValue))
1737 return true;
1738
1739 if (Lexer.isNot(AsmToken::EndOfStatement))
1740 return TokError("unexpected token in '.if' directive");
1741
Sean Callanan79ed1a82010-01-19 20:22:31 +00001742 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001743
1744 TheCondState.CondMet = ExprValue;
1745 TheCondState.Ignore = !TheCondState.CondMet;
1746 }
1747
1748 return false;
1749}
1750
1751/// ParseDirectiveElseIf
1752/// ::= .elseif expression
1753bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1754 if (TheCondState.TheCond != AsmCond::IfCond &&
1755 TheCondState.TheCond != AsmCond::ElseIfCond)
1756 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1757 " an .elseif");
1758 TheCondState.TheCond = AsmCond::ElseIfCond;
1759
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001760 bool LastIgnoreState = false;
1761 if (!TheCondStack.empty())
1762 LastIgnoreState = TheCondStack.back().Ignore;
1763 if (LastIgnoreState || TheCondState.CondMet) {
1764 TheCondState.Ignore = true;
1765 EatToEndOfStatement();
1766 }
1767 else {
1768 int64_t ExprValue;
1769 if (ParseAbsoluteExpression(ExprValue))
1770 return true;
1771
1772 if (Lexer.isNot(AsmToken::EndOfStatement))
1773 return TokError("unexpected token in '.elseif' directive");
1774
Sean Callanan79ed1a82010-01-19 20:22:31 +00001775 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001776 TheCondState.CondMet = ExprValue;
1777 TheCondState.Ignore = !TheCondState.CondMet;
1778 }
1779
1780 return false;
1781}
1782
1783/// ParseDirectiveElse
1784/// ::= .else
1785bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001786 if (Lexer.isNot(AsmToken::EndOfStatement))
1787 return TokError("unexpected token in '.else' directive");
1788
Sean Callanan79ed1a82010-01-19 20:22:31 +00001789 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001790
1791 if (TheCondState.TheCond != AsmCond::IfCond &&
1792 TheCondState.TheCond != AsmCond::ElseIfCond)
1793 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1794 ".elseif");
1795 TheCondState.TheCond = AsmCond::ElseCond;
1796 bool LastIgnoreState = false;
1797 if (!TheCondStack.empty())
1798 LastIgnoreState = TheCondStack.back().Ignore;
1799 if (LastIgnoreState || TheCondState.CondMet)
1800 TheCondState.Ignore = true;
1801 else
1802 TheCondState.Ignore = false;
1803
1804 return false;
1805}
1806
1807/// ParseDirectiveEndIf
1808/// ::= .endif
1809bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001810 if (Lexer.isNot(AsmToken::EndOfStatement))
1811 return TokError("unexpected token in '.endif' directive");
1812
Sean Callanan79ed1a82010-01-19 20:22:31 +00001813 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001814
1815 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1816 TheCondStack.empty())
1817 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1818 ".else");
1819 if (!TheCondStack.empty()) {
1820 TheCondState = TheCondStack.back();
1821 TheCondStack.pop_back();
1822 }
1823
1824 return false;
1825}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001826
1827/// ParseDirectiveFile
1828/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001829bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001830 // FIXME: I'm not sure what this is.
1831 int64_t FileNumber = -1;
1832 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001833 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001834 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001835
1836 if (FileNumber < 1)
1837 return TokError("file number less than one");
1838 }
1839
1840 if (Lexer.isNot(AsmToken::String))
1841 return TokError("unexpected token in '.file' directive");
1842
Chris Lattnerd32e8032010-01-25 19:02:58 +00001843 StringRef Filename = getTok().getString();
1844 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001845 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001846
1847 if (Lexer.isNot(AsmToken::EndOfStatement))
1848 return TokError("unexpected token in '.file' directive");
1849
Chris Lattnerd32e8032010-01-25 19:02:58 +00001850 if (FileNumber == -1)
1851 Out.EmitFileDirective(Filename);
1852 else
1853 Out.EmitDwarfFileDirective(FileNumber, Filename);
1854
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001855 return false;
1856}
1857
1858/// ParseDirectiveLine
1859/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001860bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001861 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1862 if (Lexer.isNot(AsmToken::Integer))
1863 return TokError("unexpected token in '.line' directive");
1864
Sean Callanan18b83232010-01-19 21:44:56 +00001865 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001866 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001867 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001868
1869 // FIXME: Do something with the .line.
1870 }
1871
1872 if (Lexer.isNot(AsmToken::EndOfStatement))
1873 return TokError("unexpected token in '.file' directive");
1874
1875 return false;
1876}
1877
1878
1879/// ParseDirectiveLoc
1880/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001881bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001882 if (Lexer.isNot(AsmToken::Integer))
1883 return TokError("unexpected token in '.loc' directive");
1884
1885 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001886 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001887 (void) FileNumber;
1888 // FIXME: Validate file.
1889
Sean Callanan79ed1a82010-01-19 20:22:31 +00001890 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001891 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1892 if (Lexer.isNot(AsmToken::Integer))
1893 return TokError("unexpected token in '.loc' directive");
1894
Sean Callanan18b83232010-01-19 21:44:56 +00001895 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001896 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001898
1899 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1900 if (Lexer.isNot(AsmToken::Integer))
1901 return TokError("unexpected token in '.loc' directive");
1902
Sean Callanan18b83232010-01-19 21:44:56 +00001903 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001904 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001905 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001906
1907 // FIXME: Do something with the .loc.
1908 }
1909 }
1910
1911 if (Lexer.isNot(AsmToken::EndOfStatement))
1912 return TokError("unexpected token in '.file' directive");
1913
1914 return false;
1915}
1916