blob: 0d6eaec35fb355e6ffe14a46a856d47392714c39 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
27#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000028#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000029using namespace llvm;
30
Chris Lattneraaec2052010-01-19 19:46:13 +000031
32enum { DEFAULT_ADDRSPACE = 0 };
33
Chris Lattnerebb89b42009-09-27 21:16:52 +000034AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
35 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000036 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
Chris Lattnerf0559e42010-04-08 20:30:37 +000037 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000038 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
39
Chris Lattnerebb89b42009-09-27 21:16:52 +000040 // Debugging directives.
41 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
42 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
43 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
44}
45
46
47
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000048AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000049}
50
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000051void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000052 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000053}
54
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000055bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000056 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000057 return true;
58}
59
60bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000061 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000062 return true;
63}
64
Sean Callananbf2013e2010-01-20 23:19:55 +000065void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
66 const char *Type) const {
67 SrcMgr.PrintMessage(Loc, Msg, Type);
68}
Sean Callananfd0b0282010-01-21 00:19:58 +000069
70bool AsmParser::EnterIncludeFile(const std::string &Filename) {
71 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
72 if (NewBuf == -1)
73 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000074
Sean Callananfd0b0282010-01-21 00:19:58 +000075 CurBuffer = NewBuf;
76
77 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
78
79 return false;
80}
81
82const AsmToken &AsmParser::Lex() {
83 const AsmToken *tok = &Lexer.Lex();
84
85 if (tok->is(AsmToken::Eof)) {
86 // If this is the end of an included file, pop the parent file off the
87 // include stack.
88 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
89 if (ParentIncludeLoc != SMLoc()) {
90 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
91 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
92 ParentIncludeLoc.getPointer());
93 tok = &Lexer.Lex();
94 }
95 }
96
97 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000098 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000099
Sean Callananfd0b0282010-01-21 00:19:58 +0000100 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000101}
102
Chris Lattner79180e22010-04-05 23:15:42 +0000103bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000104 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000105 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000106 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000107 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000108 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000109 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
110 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000111
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000112 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000113 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000114
Chris Lattnerb717fb02009-07-02 21:53:43 +0000115 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000116
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000117 AsmCond StartingCondState = TheCondState;
118
Chris Lattnerb717fb02009-07-02 21:53:43 +0000119 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000120 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000121 if (!ParseStatement()) continue;
122
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000123 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000124 HadError = true;
125 EatToEndOfStatement();
126 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000127
128 if (TheCondState.TheCond != StartingCondState.TheCond ||
129 TheCondState.Ignore != StartingCondState.Ignore)
130 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000131
Chris Lattner79180e22010-04-05 23:15:42 +0000132 // Finalize the output stream if there are no errors and if the client wants
133 // us to.
134 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000135 Out.Finish();
136
Chris Lattnerb717fb02009-07-02 21:53:43 +0000137 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000138}
139
Chris Lattner2cf5f142009-06-22 01:29:09 +0000140/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
141void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000142 while (Lexer.isNot(AsmToken::EndOfStatement) &&
143 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000144 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000145
146 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000147 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000148 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000149}
150
Chris Lattnerc4193832009-06-22 05:51:26 +0000151
Chris Lattner74ec1a32009-06-22 06:32:03 +0000152/// ParseParenExpr - Parse a paren expression and return it.
153/// NOTE: This assumes the leading '(' has already been consumed.
154///
155/// parenexpr ::= expr)
156///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000157bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000158 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000159 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000160 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000161 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000162 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000163 return false;
164}
Chris Lattnerc4193832009-06-22 05:51:26 +0000165
Daniel Dunbar959fd882009-08-26 22:13:22 +0000166MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000167 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000168 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000169}
170
Chris Lattner74ec1a32009-06-22 06:32:03 +0000171/// ParsePrimaryExpr - Parse a primary expression and return it.
172/// primaryexpr ::= (parenexpr
173/// primaryexpr ::= symbol
174/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000175/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000176/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000177bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000178 switch (Lexer.getKind()) {
179 default:
180 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000181 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000182 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000183 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000184 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000185 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000186 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000187 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000188 case AsmToken::Identifier: {
189 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000190 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
191 MCSymbol *Sym = CreateSymbol(Split.first);
192
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000193 // Mark the symbol as used in an expression.
194 Sym->setUsedInExpr(true);
195
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000196 // Lookup the symbol variant if used.
197 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
198 if (Split.first.size() != getTok().getIdentifier().size())
199 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
200
Chris Lattnerb4307b32010-01-15 19:28:38 +0000201 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000202 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000203
204 // If this is an absolute variable reference, substitute it now to preserve
205 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000206 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000207 if (Variant)
208 return Error(EndLoc, "unexpected modified on variable reference");
209
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000210 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000211 return false;
212 }
213
214 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000215 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000216 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000217 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000218 case AsmToken::Integer: {
219 SMLoc Loc = getTok().getLoc();
220 int64_t IntVal = getTok().getIntVal();
221 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000222 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000223 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000224 // Look for 'b' or 'f' following an Integer as a directional label
225 if (Lexer.getKind() == AsmToken::Identifier) {
226 StringRef IDVal = getTok().getString();
227 if (IDVal == "f" || IDVal == "b"){
228 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
229 IDVal == "f" ? 1 : 0);
230 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
231 getContext());
232 if(IDVal == "b" && Sym->isUndefined())
233 return Error(Loc, "invalid reference to undefined symbol");
234 EndLoc = Lexer.getLoc();
235 Lex(); // Eat identifier.
236 }
237 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000238 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000239 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000240 case AsmToken::Dot: {
241 // This is a '.' reference, which references the current PC. Emit a
242 // temporary label to the streamer and refer to it.
243 MCSymbol *Sym = Ctx.CreateTempSymbol();
244 Out.EmitLabel(Sym);
245 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
246 EndLoc = Lexer.getLoc();
247 Lex(); // Eat identifier.
248 return false;
249 }
250
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000252 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000253 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000254 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000255 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000256 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000258 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000260 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000261 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000262 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000264 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000266 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000267 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000268 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000270 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000272 }
273}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000274
Chris Lattnerb4307b32010-01-15 19:28:38 +0000275bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000276 SMLoc EndLoc;
277 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000278}
279
Chris Lattner74ec1a32009-06-22 06:32:03 +0000280/// ParseExpression - Parse an expression and return it.
281///
282/// expr ::= expr +,- expr -> lowest.
283/// expr ::= expr |,^,&,! expr -> middle.
284/// expr ::= expr *,/,%,<<,>> expr -> highest.
285/// expr ::= primaryexpr
286///
Chris Lattner54482b42010-01-15 19:39:23 +0000287bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000288 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000289 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000290 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
291 return true;
292
293 // Try to constant fold it up front, if possible.
294 int64_t Value;
295 if (Res->EvaluateAsAbsolute(Value))
296 Res = MCConstantExpr::Create(Value, getContext());
297
298 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000299}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000300
Chris Lattnerb4307b32010-01-15 19:28:38 +0000301bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000302 Res = 0;
303 return ParseParenExpr(Res, EndLoc) ||
304 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000305}
306
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000308 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000310 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000311 if (ParseExpression(Expr))
312 return true;
313
Daniel Dunbare00b0112009-10-16 01:57:52 +0000314 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000315 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000316
317 return false;
318}
319
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000321 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000322 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000323 default:
324 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325
326 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000327 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000328 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000329 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000331 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000332 return 1;
333
334 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000336 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000337 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000338 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000339 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000341 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000342 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000343 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000344 case AsmToken::ExclaimEqual:
345 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000349 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000351 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000352 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000353 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000354 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000355 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000356 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 2;
360
361 // Intermediate Precedence: |, &, ^
362 //
363 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000364 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000365 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000366 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000367 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000368 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000369 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000371 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000372 return 3;
373
374 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000381 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000382 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000383 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000384 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000385 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000386 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000387 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000388 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000389 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000390 }
391}
392
393
394/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
395/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000396bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
397 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000399 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401
402 // If the next token is lower precedence than we are allowed to eat, return
403 // successfully with what we ate already.
404 if (TokPrec < Precedence)
405 return false;
406
Sean Callanan79ed1a82010-01-19 20:22:31 +0000407 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000408
409 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000410 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000411 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000412
413 // If BinOp binds less tightly with RHS than the operator after RHS, let
414 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000416 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000417 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000418 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000419 }
420
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000422 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000423 }
424}
425
Chris Lattnerc4193832009-06-22 05:51:26 +0000426
427
428
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000429/// ParseStatement:
430/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000431/// ::= Label* Directive ...Operands... EndOfStatement
432/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000433bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000434 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000435 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000436 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000437 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000438 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000439
440 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000441 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000442 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000443 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000444 int64_t LocalLabelVal = -1;
445 // GUESS allow an integer followed by a ':' as a directional local label
446 if (Lexer.is(AsmToken::Integer)) {
447 LocalLabelVal = getTok().getIntVal();
448 if (LocalLabelVal < 0) {
449 if (!TheCondState.Ignore)
450 return TokError("unexpected token at start of statement");
451 IDVal = "";
452 }
453 else {
454 IDVal = getTok().getString();
455 Lex(); // Consume the integer token to be used as an identifier token.
456 if (Lexer.getKind() != AsmToken::Colon) {
457 if (!TheCondState.Ignore)
458 return TokError("unexpected token at start of statement");
459 }
460 }
461 }
462 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000463 if (!TheCondState.Ignore)
464 return TokError("unexpected token at start of statement");
465 IDVal = "";
466 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000467
Chris Lattner7834fac2010-04-17 18:14:27 +0000468 // Handle conditional assembly here before checking for skipping. We
469 // have to do this so that .endif isn't skipped in a ".if 0" block for
470 // example.
471 if (IDVal == ".if")
472 return ParseDirectiveIf(IDLoc);
473 if (IDVal == ".elseif")
474 return ParseDirectiveElseIf(IDLoc);
475 if (IDVal == ".else")
476 return ParseDirectiveElse(IDLoc);
477 if (IDVal == ".endif")
478 return ParseDirectiveEndIf(IDLoc);
479
480 // If we are in a ".if 0" block, ignore this statement.
481 if (TheCondState.Ignore) {
482 EatToEndOfStatement();
483 return false;
484 }
485
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000486 // FIXME: Recurse on local labels?
487
488 // See what kind of statement we have.
489 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000490 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000491 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000492 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000493
494 // Diagnose attempt to use a variable as a label.
495 //
496 // FIXME: Diagnostics. Note the location of the definition as a label.
497 // FIXME: This doesn't diagnose assignment to a symbol which has been
498 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000499 MCSymbol *Sym;
500 if (LocalLabelVal == -1)
501 Sym = CreateSymbol(IDVal);
502 else
503 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000504 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000505 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000506
Daniel Dunbar959fd882009-08-26 22:13:22 +0000507 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000508 Out.EmitLabel(Sym);
509
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000510 // Consume any end of statement token, if present, to avoid spurious
511 // AddBlankLine calls().
512 if (Lexer.is(AsmToken::EndOfStatement)) {
513 Lex();
514 if (Lexer.is(AsmToken::Eof))
515 return false;
516 }
517
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000518 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000519 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000520
Daniel Dunbar3f872332009-07-28 16:08:33 +0000521 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000522 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000523 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000524
Daniel Dunbare2ace502009-08-31 08:09:09 +0000525 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000526
527 default: // Normal instruction or directive.
528 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000529 }
530
531 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000532 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000533 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000535 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000537 // FIXME: This changes behavior based on the -static flag to the
538 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000539 return ParseDirectiveSectionSwitch("__TEXT", "__text",
540 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000546 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
547 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000549 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000550 MCSectionMachO::S_4BYTE_LITERALS,
551 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000553 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000554 MCSectionMachO::S_8BYTE_LITERALS,
555 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000557 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 MCSectionMachO::S_16BYTE_LITERALS,
559 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000563 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
568
569 // FIXME: The assembler manual claims that this has the self modify code
570 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000571 if (IDVal == ".symbol_stub")
572 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
573 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000574 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
575 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 0, 16);
577 // FIXME: PowerPC only?
578 if (IDVal == ".picsymbol_stub")
579 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
580 MCSectionMachO::S_SYMBOL_STUBS |
581 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
582 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000584 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
587
588 // FIXME: The section names of these two are misspelled in the assembler
589 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
592 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
593 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
596 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
597 4);
598
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000600 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000602 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000603 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
604 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000606 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
608 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000610 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000611
612
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000614 return ParseDirectiveSectionSwitch("__OBJC", "__class",
615 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000617 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
618 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000620 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
621 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000623 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
624 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000626 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
627 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000629 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
630 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000632 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
633 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000635 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
636 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000638 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
639 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
640 MCSectionMachO::S_LITERAL_POINTERS,
641 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000643 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
644 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
645 MCSectionMachO::S_LITERAL_POINTERS,
646 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000648 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
649 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000651 return ParseDirectiveSectionSwitch("__OBJC", "__category",
652 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000654 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
655 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000657 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
658 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000660 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
661 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000663 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
664 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000666 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
667 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000669 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
670 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000672 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
673 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000674
Eric Christopherc6177a42010-05-17 22:53:55 +0000675 if (IDVal == ".tdata")
676 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
677 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
678 if (IDVal == ".tlv")
679 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
680 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
681 if (IDVal == ".thread_init_func")
682 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
683 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
684
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000685 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000687 return ParseDirectiveSet();
688
Daniel Dunbara0d14262009-06-24 23:30:00 +0000689 // Data directives
690
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000692 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000694 return ParseDirectiveAscii(true);
695
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000697 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000698 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000699 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000701 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000703 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000704
705 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000707 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000709 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000711 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000713 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000715 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000717 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000719 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000721 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
722
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000724 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000725
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000727 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000729 return ParseDirectiveSpace();
730
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000731 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000732
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000734 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000736 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000738 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000740 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000742 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000744 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000746 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000748 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000750 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000751 if (IDVal == ".type")
752 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000754 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000756 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000757 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000758 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000759
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000761 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000763 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000765 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000767 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000768 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000769 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000770 if (IDVal == ".tbss")
771 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000772
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000773 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000774 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000775 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000776 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000777 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000778 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000779 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000780 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000781 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000782 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000783
Chris Lattnerebb89b42009-09-27 21:16:52 +0000784 // Look up the handler in the handler table,
785 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
786 if (Handler)
787 return (this->*Handler)(IDVal, IDLoc);
788
Kevin Enderby9c656452009-09-10 20:51:44 +0000789 // Target hook for parsing target specific directives.
790 if (!getTargetParser().ParseDirective(ID))
791 return false;
792
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000793 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000794 EatToEndOfStatement();
795 return false;
796 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000797
Chris Lattnera7f13542010-05-19 23:34:33 +0000798 // Canonicalize the opcode to lower case.
799 SmallString<128> Opcode;
800 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
801 Opcode.push_back(tolower(IDVal[i]));
802
Chris Lattner98986712010-01-14 22:21:20 +0000803 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000804 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000805 ParsedOperands);
806 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
807 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000808
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000809 // If parsing succeeded, match the instruction.
810 if (!HadError) {
811 MCInst Inst;
812 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
813 // Emit the instruction on success.
814 Out.EmitInstruction(Inst);
815 } else {
816 // Otherwise emit a diagnostic about the match failure and set the error
817 // flag.
818 //
819 // FIXME: We should give nicer diagnostics about the exact failure.
820 Error(IDLoc, "unrecognized instruction");
821 HadError = true;
822 }
823 }
Chris Lattner98986712010-01-14 22:21:20 +0000824
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000825 // If there was no error, consume the end-of-statement token. Otherwise this
826 // will be done by our caller.
827 if (!HadError)
828 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000829
830 // Free any parsed operands.
831 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
832 delete ParsedOperands[i];
833
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000834 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000835}
Chris Lattner9a023f72009-06-24 04:43:34 +0000836
Daniel Dunbare2ace502009-08-31 08:09:09 +0000837bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000838 // FIXME: Use better location, we should use proper tokens.
839 SMLoc EqualLoc = Lexer.getLoc();
840
Daniel Dunbar821e3332009-08-31 08:09:28 +0000841 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000842 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000843 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000844 return true;
845
Daniel Dunbar3f872332009-07-28 16:08:33 +0000846 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000847 return TokError("unexpected token in assignment");
848
849 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000850 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000851
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000852 // Validate that the LHS is allowed to be a variable (either it has not been
853 // used as a symbol, or it is an absolute symbol).
854 MCSymbol *Sym = getContext().LookupSymbol(Name);
855 if (Sym) {
856 // Diagnose assignment to a label.
857 //
858 // FIXME: Diagnostics. Note the location of the definition as a label.
859 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000860 if (Sym->isUndefined() && !Sym->isUsedInExpr())
861 ; // Allow redefinitions of undefined symbols only used in directives.
862 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000863 return Error(EqualLoc, "redefinition of '" + Name + "'");
864 else if (!Sym->isVariable())
865 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000866 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000867 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
868 Name + "'");
869 } else
870 Sym = CreateSymbol(Name);
871
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000872 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000873
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000874 Sym->setUsedInExpr(true);
875
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000876 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000877 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000878
879 return false;
880}
881
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000882/// ParseIdentifier:
883/// ::= identifier
884/// ::= string
885bool AsmParser::ParseIdentifier(StringRef &Res) {
886 if (Lexer.isNot(AsmToken::Identifier) &&
887 Lexer.isNot(AsmToken::String))
888 return true;
889
Sean Callanan18b83232010-01-19 21:44:56 +0000890 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000891
Sean Callanan79ed1a82010-01-19 20:22:31 +0000892 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000893
894 return false;
895}
896
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000897/// ParseDirectiveSet:
898/// ::= .set identifier ',' expression
899bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000900 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000902 if (ParseIdentifier(Name))
903 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000904
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000905 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000906 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000907 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000908
Daniel Dunbare2ace502009-08-31 08:09:09 +0000909 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000910}
911
Chris Lattner9a023f72009-06-24 04:43:34 +0000912/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000913/// ::= .section identifier (',' identifier)*
914/// FIXME: This should actually parse out the segment, section, attributes and
915/// sizeof_stub fields.
916bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000917 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000918
Daniel Dunbarace63122009-08-11 03:42:33 +0000919 StringRef SectionName;
920 if (ParseIdentifier(SectionName))
921 return Error(Loc, "expected identifier after '.section' directive");
922
923 // Verify there is a following comma.
924 if (!Lexer.is(AsmToken::Comma))
925 return TokError("unexpected token in '.section' directive");
926
Chris Lattnerff4bc462009-08-10 01:39:42 +0000927 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000928 SectionSpec += ",";
929
930 // Add all the tokens until the end of the line, ParseSectionSpecifier will
931 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000932 StringRef EOL = Lexer.LexUntilEndOfStatement();
933 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000934
Sean Callanan79ed1a82010-01-19 20:22:31 +0000935 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000936 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000937 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000938 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000939
Chris Lattnerff4bc462009-08-10 01:39:42 +0000940
941 StringRef Segment, Section;
942 unsigned TAA, StubSize;
943 std::string ErrorStr =
944 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
945 TAA, StubSize);
946
947 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000948 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000949
Chris Lattner56594f92009-07-31 17:47:16 +0000950 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000951 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000952 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
953 isText ? SectionKind::getText()
954 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000955 return false;
956}
957
Chris Lattnere15c2d72009-08-10 18:05:55 +0000958/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000959bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
960 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000961 unsigned TAA, unsigned Align,
962 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000964 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000965 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000966
Chris Lattner56594f92009-07-31 17:47:16 +0000967 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000968 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000969 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
970 isText ? SectionKind::getText()
971 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000972
973 // Set the implicit alignment, if any.
974 //
975 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
976 // alignment on the section (e.g., if one manually inserts bytes into the
977 // section, then just issueing the section switch directive will not realign
978 // the section. However, this is arguably more reasonable behavior, and there
979 // is no good reason for someone to intentionally emit incorrectly sized
980 // values into the implicitly aligned sections.
981 if (Align)
982 Out.EmitValueToAlignment(Align, 0, 1, 0);
983
Chris Lattner529fb542009-06-24 05:13:15 +0000984 return false;
985}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000986
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000987bool AsmParser::ParseEscapedString(std::string &Data) {
988 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
989
990 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000991 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000992 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
993 if (Str[i] != '\\') {
994 Data += Str[i];
995 continue;
996 }
997
998 // Recognize escaped characters. Note that this escape semantics currently
999 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1000 ++i;
1001 if (i == e)
1002 return TokError("unexpected backslash at end of string");
1003
1004 // Recognize octal sequences.
1005 if ((unsigned) (Str[i] - '0') <= 7) {
1006 // Consume up to three octal characters.
1007 unsigned Value = Str[i] - '0';
1008
1009 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1010 ++i;
1011 Value = Value * 8 + (Str[i] - '0');
1012
1013 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1014 ++i;
1015 Value = Value * 8 + (Str[i] - '0');
1016 }
1017 }
1018
1019 if (Value > 255)
1020 return TokError("invalid octal escape sequence (out of range)");
1021
1022 Data += (unsigned char) Value;
1023 continue;
1024 }
1025
1026 // Otherwise recognize individual escapes.
1027 switch (Str[i]) {
1028 default:
1029 // Just reject invalid escape sequences for now.
1030 return TokError("invalid escape sequence (unrecognized character)");
1031
1032 case 'b': Data += '\b'; break;
1033 case 'f': Data += '\f'; break;
1034 case 'n': Data += '\n'; break;
1035 case 'r': Data += '\r'; break;
1036 case 't': Data += '\t'; break;
1037 case '"': Data += '"'; break;
1038 case '\\': Data += '\\'; break;
1039 }
1040 }
1041
1042 return false;
1043}
1044
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001046/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001047bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001050 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 return TokError("expected string in '.ascii' or '.asciz' directive");
1052
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001053 std::string Data;
1054 if (ParseEscapedString(Data))
1055 return true;
1056
Chris Lattneraaec2052010-01-19 19:46:13 +00001057 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001058 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001059 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060
Sean Callanan79ed1a82010-01-19 20:22:31 +00001061 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062
Daniel Dunbar3f872332009-07-28 16:08:33 +00001063 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064 break;
1065
Daniel Dunbar3f872332009-07-28 16:08:33 +00001066 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001067 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001068 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001069 }
1070 }
1071
Sean Callanan79ed1a82010-01-19 20:22:31 +00001072 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 return false;
1074}
1075
1076/// ParseDirectiveValue
1077/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1078bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001081 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001082 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001083 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return true;
1085
Chris Lattneraaec2052010-01-19 19:46:13 +00001086 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087
Daniel Dunbar3f872332009-07-28 16:08:33 +00001088 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001089 break;
1090
1091 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001092 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001094 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001095 }
1096 }
1097
Sean Callanan79ed1a82010-01-19 20:22:31 +00001098 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001099 return false;
1100}
1101
1102/// ParseDirectiveSpace
1103/// ::= .space expression [ , expression ]
1104bool AsmParser::ParseDirectiveSpace() {
1105 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001106 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001107 return true;
1108
1109 int64_t FillExpr = 0;
1110 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001111 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1112 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001114 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115
Daniel Dunbar475839e2009-06-29 20:37:27 +00001116 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 return true;
1118
1119 HasFillExpr = true;
1120
Daniel Dunbar3f872332009-07-28 16:08:33 +00001121 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122 return TokError("unexpected token in '.space' directive");
1123 }
1124
Sean Callanan79ed1a82010-01-19 20:22:31 +00001125 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126
1127 if (NumBytes <= 0)
1128 return TokError("invalid number of bytes in '.space' directive");
1129
1130 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001131 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132
1133 return false;
1134}
1135
1136/// ParseDirectiveFill
1137/// ::= .fill expression , expression , expression
1138bool AsmParser::ParseDirectiveFill() {
1139 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001140 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001141 return true;
1142
Daniel Dunbar3f872332009-07-28 16:08:33 +00001143 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001144 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001145 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146
1147 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001148 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001149 return true;
1150
Daniel Dunbar3f872332009-07-28 16:08:33 +00001151 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001152 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001153 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154
1155 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001156 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001157 return true;
1158
Daniel Dunbar3f872332009-07-28 16:08:33 +00001159 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001160 return TokError("unexpected token in '.fill' directive");
1161
Sean Callanan79ed1a82010-01-19 20:22:31 +00001162 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001163
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001164 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1165 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001166
1167 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001168 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1169 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170
1171 return false;
1172}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001173
1174/// ParseDirectiveOrg
1175/// ::= .org expression [ , expression ]
1176bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001177 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001178 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001179 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001180 return true;
1181
1182 // Parse optional fill expression.
1183 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001184 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1185 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001186 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001187 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001188
Daniel Dunbar475839e2009-06-29 20:37:27 +00001189 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001190 return true;
1191
Daniel Dunbar3f872332009-07-28 16:08:33 +00001192 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001193 return TokError("unexpected token in '.org' directive");
1194 }
1195
Sean Callanan79ed1a82010-01-19 20:22:31 +00001196 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001197
1198 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1199 // has to be relative to the current section.
1200 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001201
1202 return false;
1203}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001204
1205/// ParseDirectiveAlign
1206/// ::= {.align, ...} expression [ , expression [ , expression ]]
1207bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001208 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001209 int64_t Alignment;
1210 if (ParseAbsoluteExpression(Alignment))
1211 return true;
1212
1213 SMLoc MaxBytesLoc;
1214 bool HasFillExpr = false;
1215 int64_t FillExpr = 0;
1216 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001217 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1218 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001219 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001220 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001221
1222 // The fill expression can be omitted while specifying a maximum number of
1223 // alignment bytes, e.g:
1224 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001225 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001226 HasFillExpr = true;
1227 if (ParseAbsoluteExpression(FillExpr))
1228 return true;
1229 }
1230
Daniel Dunbar3f872332009-07-28 16:08:33 +00001231 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1232 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001233 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001234 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001235
1236 MaxBytesLoc = Lexer.getLoc();
1237 if (ParseAbsoluteExpression(MaxBytesToFill))
1238 return true;
1239
Daniel Dunbar3f872332009-07-28 16:08:33 +00001240 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001241 return TokError("unexpected token in directive");
1242 }
1243 }
1244
Sean Callanan79ed1a82010-01-19 20:22:31 +00001245 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001246
Daniel Dunbar648ac512010-05-17 21:54:30 +00001247 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001248 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001249
1250 // Compute alignment in bytes.
1251 if (IsPow2) {
1252 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001253 if (Alignment >= 32) {
1254 Error(AlignmentLoc, "invalid alignment value");
1255 Alignment = 31;
1256 }
1257
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001258 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001259 }
1260
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001261 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001262 if (MaxBytesLoc.isValid()) {
1263 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001264 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1265 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001266 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001267 }
1268
1269 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001270 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1271 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001272 MaxBytesToFill = 0;
1273 }
1274 }
1275
Daniel Dunbar648ac512010-05-17 21:54:30 +00001276 // Check whether we should use optimal code alignment for this .align
1277 // directive.
1278 //
1279 // FIXME: This should be using a target hook.
1280 bool UseCodeAlign = false;
1281 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1282 Out.getCurrentSection()))
1283 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1284 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1285 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001286 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001287 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001288 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1289 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001290 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001291
1292 return false;
1293}
1294
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001295/// ParseDirectiveSymbolAttribute
1296/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001297bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001298 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001299 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001300 StringRef Name;
1301
1302 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001303 return TokError("expected identifier in directive");
1304
Daniel Dunbar959fd882009-08-26 22:13:22 +00001305 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001306
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001307 Out.EmitSymbolAttribute(Sym, Attr);
1308
Daniel Dunbar3f872332009-07-28 16:08:33 +00001309 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001310 break;
1311
Daniel Dunbar3f872332009-07-28 16:08:33 +00001312 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001313 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001314 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001315 }
1316 }
1317
Sean Callanan79ed1a82010-01-19 20:22:31 +00001318 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001319 return false;
1320}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001321
Matt Fleming924c5e52010-05-21 11:36:59 +00001322/// ParseDirectiveELFType
1323/// ::= .type identifier , @attribute
1324bool AsmParser::ParseDirectiveELFType() {
1325 StringRef Name;
1326 if (ParseIdentifier(Name))
1327 return TokError("expected identifier in directive");
1328
1329 // Handle the identifier as the key symbol.
1330 MCSymbol *Sym = CreateSymbol(Name);
1331
1332 if (Lexer.isNot(AsmToken::Comma))
1333 return TokError("unexpected token in '.type' directive");
1334 Lex();
1335
1336 if (Lexer.isNot(AsmToken::At))
1337 return TokError("expected '@' before type");
1338 Lex();
1339
1340 StringRef Type;
1341 SMLoc TypeLoc;
1342
1343 TypeLoc = Lexer.getLoc();
1344 if (ParseIdentifier(Type))
1345 return TokError("expected symbol type in directive");
1346
1347 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1348 .Case("function", MCSA_ELF_TypeFunction)
1349 .Case("object", MCSA_ELF_TypeObject)
1350 .Case("tls_object", MCSA_ELF_TypeTLS)
1351 .Case("common", MCSA_ELF_TypeCommon)
1352 .Case("notype", MCSA_ELF_TypeNoType)
1353 .Default(MCSA_Invalid);
1354
1355 if (Attr == MCSA_Invalid)
1356 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1357
1358 if (Lexer.isNot(AsmToken::EndOfStatement))
1359 return TokError("unexpected token in '.type' directive");
1360
1361 Lex();
1362
1363 Out.EmitSymbolAttribute(Sym, Attr);
1364
1365 return false;
1366}
1367
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001368/// ParseDirectiveDarwinSymbolDesc
1369/// ::= .desc identifier , expression
1370bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001371 StringRef Name;
1372 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001373 return TokError("expected identifier in directive");
1374
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001375 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001376 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001377
Daniel Dunbar3f872332009-07-28 16:08:33 +00001378 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001379 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001380 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001381
1382 SMLoc DescLoc = Lexer.getLoc();
1383 int64_t DescValue;
1384 if (ParseAbsoluteExpression(DescValue))
1385 return true;
1386
Daniel Dunbar3f872332009-07-28 16:08:33 +00001387 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001388 return TokError("unexpected token in '.desc' directive");
1389
Sean Callanan79ed1a82010-01-19 20:22:31 +00001390 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001391
1392 // Set the n_desc field of this Symbol to this DescValue
1393 Out.EmitSymbolDesc(Sym, DescValue);
1394
1395 return false;
1396}
1397
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001398/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001399/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1400bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001401 SMLoc IDLoc = Lexer.getLoc();
1402 StringRef Name;
1403 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001404 return TokError("expected identifier in directive");
1405
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001406 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001407 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001408
Daniel Dunbar3f872332009-07-28 16:08:33 +00001409 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001410 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001411 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001412
1413 int64_t Size;
1414 SMLoc SizeLoc = Lexer.getLoc();
1415 if (ParseAbsoluteExpression(Size))
1416 return true;
1417
1418 int64_t Pow2Alignment = 0;
1419 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001420 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001421 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001422 Pow2AlignmentLoc = Lexer.getLoc();
1423 if (ParseAbsoluteExpression(Pow2Alignment))
1424 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001425
1426 // If this target takes alignments in bytes (not log) validate and convert.
1427 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1428 if (!isPowerOf2_64(Pow2Alignment))
1429 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1430 Pow2Alignment = Log2_64(Pow2Alignment);
1431 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001432 }
1433
Daniel Dunbar3f872332009-07-28 16:08:33 +00001434 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001435 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001436
Sean Callanan79ed1a82010-01-19 20:22:31 +00001437 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001438
Chris Lattner1fc3d752009-07-09 17:25:12 +00001439 // NOTE: a size of zero for a .comm should create a undefined symbol
1440 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001441 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001442 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1443 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001444
Eric Christopherc260a3e2010-05-14 01:38:54 +00001445 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001446 // may internally end up wanting an alignment in bytes.
1447 // FIXME: Diagnose overflow.
1448 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001449 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1450 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001451
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001452 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001453 return Error(IDLoc, "invalid symbol redefinition");
1454
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001455 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001456 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001457 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001458 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1459 MCSectionMachO::S_ZEROFILL, 0,
1460 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001461 Sym, Size, 1 << Pow2Alignment);
1462 return false;
1463 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001464
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001465 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001466 return false;
1467}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001468
1469/// ParseDirectiveDarwinZerofill
1470/// ::= .zerofill segname , sectname [, identifier , size_expression [
1471/// , align_expression ]]
1472bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001473 StringRef Segment;
1474 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001475 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001476
Daniel Dunbar3f872332009-07-28 16:08:33 +00001477 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001478 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001479 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001480
1481 StringRef Section;
1482 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001483 return TokError("expected section name after comma in '.zerofill' "
1484 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001485
Chris Lattner9be3fee2009-07-10 22:20:30 +00001486 // If this is the end of the line all that was wanted was to create the
1487 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001488 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001489 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001490 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1491 MCSectionMachO::S_ZEROFILL, 0,
1492 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001493 return false;
1494 }
1495
Daniel Dunbar3f872332009-07-28 16:08:33 +00001496 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001497 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001498 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001499
Chris Lattner7bb7c552010-05-13 00:10:34 +00001500 SMLoc IDLoc = Lexer.getLoc();
1501 StringRef IDStr;
1502 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001503 return TokError("expected identifier in directive");
1504
1505 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001506 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001507
Daniel Dunbar3f872332009-07-28 16:08:33 +00001508 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001509 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001510 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001511
1512 int64_t Size;
1513 SMLoc SizeLoc = Lexer.getLoc();
1514 if (ParseAbsoluteExpression(Size))
1515 return true;
1516
1517 int64_t Pow2Alignment = 0;
1518 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001519 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001520 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001521 Pow2AlignmentLoc = Lexer.getLoc();
1522 if (ParseAbsoluteExpression(Pow2Alignment))
1523 return true;
1524 }
1525
Daniel Dunbar3f872332009-07-28 16:08:33 +00001526 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001527 return TokError("unexpected token in '.zerofill' directive");
1528
Sean Callanan79ed1a82010-01-19 20:22:31 +00001529 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001530
1531 if (Size < 0)
1532 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1533 "than zero");
1534
Eric Christopherc260a3e2010-05-14 01:38:54 +00001535 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001536 // may internally end up wanting an alignment in bytes.
1537 // FIXME: Diagnose overflow.
1538 if (Pow2Alignment < 0)
1539 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1540 "can't be less than zero");
1541
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001542 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001543 return Error(IDLoc, "invalid symbol redefinition");
1544
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001545 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001546 //
1547 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001548 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1549 MCSectionMachO::S_ZEROFILL, 0,
1550 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001551 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001552
1553 return false;
1554}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001555
Eric Christopher482eba02010-05-14 01:50:28 +00001556/// ParseDirectiveDarwinTBSS
1557/// ::= .tbss identifier, size, align
1558bool AsmParser::ParseDirectiveDarwinTBSS() {
1559 SMLoc IDLoc = Lexer.getLoc();
1560 StringRef Name;
1561 if (ParseIdentifier(Name))
1562 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001563
Eric Christopher482eba02010-05-14 01:50:28 +00001564 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001565 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001566
1567 if (Lexer.isNot(AsmToken::Comma))
1568 return TokError("unexpected token in directive");
1569 Lex();
1570
1571 int64_t Size;
1572 SMLoc SizeLoc = Lexer.getLoc();
1573 if (ParseAbsoluteExpression(Size))
1574 return true;
1575
1576 int64_t Pow2Alignment = 0;
1577 SMLoc Pow2AlignmentLoc;
1578 if (Lexer.is(AsmToken::Comma)) {
1579 Lex();
1580 Pow2AlignmentLoc = Lexer.getLoc();
1581 if (ParseAbsoluteExpression(Pow2Alignment))
1582 return true;
1583 }
1584
1585 if (Lexer.isNot(AsmToken::EndOfStatement))
1586 return TokError("unexpected token in '.tbss' directive");
1587
1588 Lex();
1589
1590 if (Size < 0)
1591 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1592 "zero");
1593
1594 // FIXME: Diagnose overflow.
1595 if (Pow2Alignment < 0)
1596 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1597 "than zero");
1598
1599 if (!Sym->isUndefined())
1600 return Error(IDLoc, "invalid symbol redefinition");
1601
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001602 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1603 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1604 0, SectionKind::getThreadBSS()),
1605 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001606
1607 return false;
1608}
1609
Kevin Enderbya5c78322009-07-13 21:03:15 +00001610/// ParseDirectiveDarwinSubsectionsViaSymbols
1611/// ::= .subsections_via_symbols
1612bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001613 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001614 return TokError("unexpected token in '.subsections_via_symbols' directive");
1615
Sean Callanan79ed1a82010-01-19 20:22:31 +00001616 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001617
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001618 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001619
1620 return false;
1621}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001622
1623/// ParseDirectiveAbort
1624/// ::= .abort [ "abort_string" ]
1625bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001626 // FIXME: Use loc from directive.
1627 SMLoc Loc = Lexer.getLoc();
1628
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001629 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001630 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1631 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001632 return TokError("expected string in '.abort' directive");
1633
Sean Callanan18b83232010-01-19 21:44:56 +00001634 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001635
Sean Callanan79ed1a82010-01-19 20:22:31 +00001636 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001637 }
1638
Daniel Dunbar3f872332009-07-28 16:08:33 +00001639 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001640 return TokError("unexpected token in '.abort' directive");
1641
Sean Callanan79ed1a82010-01-19 20:22:31 +00001642 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001643
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001644 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001645 if (Str.empty())
1646 Error(Loc, ".abort detected. Assembly stopping.");
1647 else
1648 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001649
1650 return false;
1651}
Kevin Enderby71148242009-07-14 21:35:03 +00001652
1653/// ParseDirectiveLsym
1654/// ::= .lsym identifier , expression
1655bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001656 StringRef Name;
1657 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001658 return TokError("expected identifier in directive");
1659
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001660 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001661 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001662
Daniel Dunbar3f872332009-07-28 16:08:33 +00001663 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001664 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001665 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001666
Daniel Dunbar821e3332009-08-31 08:09:28 +00001667 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001668 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001669 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001670 return true;
1671
Daniel Dunbar3f872332009-07-28 16:08:33 +00001672 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001673 return TokError("unexpected token in '.lsym' directive");
1674
Sean Callanan79ed1a82010-01-19 20:22:31 +00001675 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001676
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001677 // We don't currently support this directive.
1678 //
1679 // FIXME: Diagnostic location!
1680 (void) Sym;
1681 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001682}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001683
1684/// ParseDirectiveInclude
1685/// ::= .include "filename"
1686bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001687 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001688 return TokError("expected string in '.include' directive");
1689
Sean Callanan18b83232010-01-19 21:44:56 +00001690 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001691 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001692 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001693
Daniel Dunbar3f872332009-07-28 16:08:33 +00001694 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001695 return TokError("unexpected token in '.include' directive");
1696
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001697 // Strip the quotes.
1698 Filename = Filename.substr(1, Filename.size()-2);
1699
1700 // Attempt to switch the lexer to the included file before consuming the end
1701 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001702 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001703 PrintMessage(IncludeLoc,
1704 "Could not find include file '" + Filename + "'",
1705 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001706 return true;
1707 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001708
1709 return false;
1710}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001711
1712/// ParseDirectiveDarwinDumpOrLoad
1713/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001714bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001715 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001716 return TokError("expected string in '.dump' or '.load' directive");
1717
Sean Callanan79ed1a82010-01-19 20:22:31 +00001718 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001719
Daniel Dunbar3f872332009-07-28 16:08:33 +00001720 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001721 return TokError("unexpected token in '.dump' or '.load' directive");
1722
Sean Callanan79ed1a82010-01-19 20:22:31 +00001723 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001724
Kevin Enderby5026ae42009-07-20 20:25:37 +00001725 // FIXME: If/when .dump and .load are implemented they will be done in the
1726 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001727 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001728 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001729 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001730 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001731
1732 return false;
1733}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001734
1735/// ParseDirectiveIf
1736/// ::= .if expression
1737bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001738 TheCondStack.push_back(TheCondState);
1739 TheCondState.TheCond = AsmCond::IfCond;
1740 if(TheCondState.Ignore) {
1741 EatToEndOfStatement();
1742 }
1743 else {
1744 int64_t ExprValue;
1745 if (ParseAbsoluteExpression(ExprValue))
1746 return true;
1747
1748 if (Lexer.isNot(AsmToken::EndOfStatement))
1749 return TokError("unexpected token in '.if' directive");
1750
Sean Callanan79ed1a82010-01-19 20:22:31 +00001751 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001752
1753 TheCondState.CondMet = ExprValue;
1754 TheCondState.Ignore = !TheCondState.CondMet;
1755 }
1756
1757 return false;
1758}
1759
1760/// ParseDirectiveElseIf
1761/// ::= .elseif expression
1762bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1763 if (TheCondState.TheCond != AsmCond::IfCond &&
1764 TheCondState.TheCond != AsmCond::ElseIfCond)
1765 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1766 " an .elseif");
1767 TheCondState.TheCond = AsmCond::ElseIfCond;
1768
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001769 bool LastIgnoreState = false;
1770 if (!TheCondStack.empty())
1771 LastIgnoreState = TheCondStack.back().Ignore;
1772 if (LastIgnoreState || TheCondState.CondMet) {
1773 TheCondState.Ignore = true;
1774 EatToEndOfStatement();
1775 }
1776 else {
1777 int64_t ExprValue;
1778 if (ParseAbsoluteExpression(ExprValue))
1779 return true;
1780
1781 if (Lexer.isNot(AsmToken::EndOfStatement))
1782 return TokError("unexpected token in '.elseif' directive");
1783
Sean Callanan79ed1a82010-01-19 20:22:31 +00001784 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001785 TheCondState.CondMet = ExprValue;
1786 TheCondState.Ignore = !TheCondState.CondMet;
1787 }
1788
1789 return false;
1790}
1791
1792/// ParseDirectiveElse
1793/// ::= .else
1794bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001795 if (Lexer.isNot(AsmToken::EndOfStatement))
1796 return TokError("unexpected token in '.else' directive");
1797
Sean Callanan79ed1a82010-01-19 20:22:31 +00001798 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001799
1800 if (TheCondState.TheCond != AsmCond::IfCond &&
1801 TheCondState.TheCond != AsmCond::ElseIfCond)
1802 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1803 ".elseif");
1804 TheCondState.TheCond = AsmCond::ElseCond;
1805 bool LastIgnoreState = false;
1806 if (!TheCondStack.empty())
1807 LastIgnoreState = TheCondStack.back().Ignore;
1808 if (LastIgnoreState || TheCondState.CondMet)
1809 TheCondState.Ignore = true;
1810 else
1811 TheCondState.Ignore = false;
1812
1813 return false;
1814}
1815
1816/// ParseDirectiveEndIf
1817/// ::= .endif
1818bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001819 if (Lexer.isNot(AsmToken::EndOfStatement))
1820 return TokError("unexpected token in '.endif' directive");
1821
Sean Callanan79ed1a82010-01-19 20:22:31 +00001822 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001823
1824 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1825 TheCondStack.empty())
1826 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1827 ".else");
1828 if (!TheCondStack.empty()) {
1829 TheCondState = TheCondStack.back();
1830 TheCondStack.pop_back();
1831 }
1832
1833 return false;
1834}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001835
1836/// ParseDirectiveFile
1837/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001838bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001839 // FIXME: I'm not sure what this is.
1840 int64_t FileNumber = -1;
1841 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001842 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001843 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001844
1845 if (FileNumber < 1)
1846 return TokError("file number less than one");
1847 }
1848
1849 if (Lexer.isNot(AsmToken::String))
1850 return TokError("unexpected token in '.file' directive");
1851
Chris Lattnerd32e8032010-01-25 19:02:58 +00001852 StringRef Filename = getTok().getString();
1853 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001854 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001855
1856 if (Lexer.isNot(AsmToken::EndOfStatement))
1857 return TokError("unexpected token in '.file' directive");
1858
Chris Lattnerd32e8032010-01-25 19:02:58 +00001859 if (FileNumber == -1)
1860 Out.EmitFileDirective(Filename);
1861 else
1862 Out.EmitDwarfFileDirective(FileNumber, Filename);
1863
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001864 return false;
1865}
1866
1867/// ParseDirectiveLine
1868/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001869bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001870 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1871 if (Lexer.isNot(AsmToken::Integer))
1872 return TokError("unexpected token in '.line' directive");
1873
Sean Callanan18b83232010-01-19 21:44:56 +00001874 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001875 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001876 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001877
1878 // FIXME: Do something with the .line.
1879 }
1880
1881 if (Lexer.isNot(AsmToken::EndOfStatement))
1882 return TokError("unexpected token in '.file' directive");
1883
1884 return false;
1885}
1886
1887
1888/// ParseDirectiveLoc
1889/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001890bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001891 if (Lexer.isNot(AsmToken::Integer))
1892 return TokError("unexpected token in '.loc' directive");
1893
1894 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001895 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001896 (void) FileNumber;
1897 // FIXME: Validate file.
1898
Sean Callanan79ed1a82010-01-19 20:22:31 +00001899 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001900 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1901 if (Lexer.isNot(AsmToken::Integer))
1902 return TokError("unexpected token in '.loc' directive");
1903
Sean Callanan18b83232010-01-19 21:44:56 +00001904 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001905 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001906 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001907
1908 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1909 if (Lexer.isNot(AsmToken::Integer))
1910 return TokError("unexpected token in '.loc' directive");
1911
Sean Callanan18b83232010-01-19 21:44:56 +00001912 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001913 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001914 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001915
1916 // FIXME: Do something with the .loc.
1917 }
1918 }
1919
1920 if (Lexer.isNot(AsmToken::EndOfStatement))
1921 return TokError("unexpected token in '.file' directive");
1922
1923 return false;
1924}
1925