blob: 860277409d92cec0de98e6966ac9f52a346199c5 [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"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Chris Lattneraaec2052010-01-19 19:46:13 +000032
33enum { DEFAULT_ADDRSPACE = 0 };
34
Daniel Dunbar9186fa62010-07-01 20:41:56 +000035AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
36 MCStreamer &_Out, const MCAsmInfo &_MAI)
37 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), 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
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000046AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000047}
48
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000049void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000050 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000051}
52
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000053bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000054 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000055 return true;
56}
57
58bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000059 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000060 return true;
61}
62
Sean Callananbf2013e2010-01-20 23:19:55 +000063void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
64 const char *Type) const {
65 SrcMgr.PrintMessage(Loc, Msg, Type);
66}
Sean Callananfd0b0282010-01-21 00:19:58 +000067
68bool AsmParser::EnterIncludeFile(const std::string &Filename) {
69 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
70 if (NewBuf == -1)
71 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000072
Sean Callananfd0b0282010-01-21 00:19:58 +000073 CurBuffer = NewBuf;
74
75 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
76
77 return false;
78}
79
80const AsmToken &AsmParser::Lex() {
81 const AsmToken *tok = &Lexer.Lex();
82
83 if (tok->is(AsmToken::Eof)) {
84 // If this is the end of an included file, pop the parent file off the
85 // include stack.
86 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
87 if (ParentIncludeLoc != SMLoc()) {
88 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
89 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
90 ParentIncludeLoc.getPointer());
91 tok = &Lexer.Lex();
92 }
93 }
94
95 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000096 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000097
Sean Callananfd0b0282010-01-21 00:19:58 +000098 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +000099}
100
Chris Lattner79180e22010-04-05 23:15:42 +0000101bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000102 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000103 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000104 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000105 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000106 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000107 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
108 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000109
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000110 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000111 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000112
Chris Lattnerb717fb02009-07-02 21:53:43 +0000113 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000114
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000115 AsmCond StartingCondState = TheCondState;
116
Chris Lattnerb717fb02009-07-02 21:53:43 +0000117 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000118 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000119 if (!ParseStatement()) continue;
120
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000121 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000122 HadError = true;
123 EatToEndOfStatement();
124 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000125
126 if (TheCondState.TheCond != StartingCondState.TheCond ||
127 TheCondState.Ignore != StartingCondState.Ignore)
128 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000129
Chris Lattner79180e22010-04-05 23:15:42 +0000130 // Finalize the output stream if there are no errors and if the client wants
131 // us to.
132 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000133 Out.Finish();
134
Chris Lattnerb717fb02009-07-02 21:53:43 +0000135 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000136}
137
Chris Lattner2cf5f142009-06-22 01:29:09 +0000138/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
139void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000140 while (Lexer.isNot(AsmToken::EndOfStatement) &&
141 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000142 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000143
144 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000145 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000146 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000147}
148
Chris Lattnerc4193832009-06-22 05:51:26 +0000149
Chris Lattner74ec1a32009-06-22 06:32:03 +0000150/// ParseParenExpr - Parse a paren expression and return it.
151/// NOTE: This assumes the leading '(' has already been consumed.
152///
153/// parenexpr ::= expr)
154///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000155bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000156 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000157 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000158 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000159 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000160 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000161 return false;
162}
Chris Lattnerc4193832009-06-22 05:51:26 +0000163
Daniel Dunbar959fd882009-08-26 22:13:22 +0000164MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000165 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000166 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000167}
168
Chris Lattner74ec1a32009-06-22 06:32:03 +0000169/// ParsePrimaryExpr - Parse a primary expression and return it.
170/// primaryexpr ::= (parenexpr
171/// primaryexpr ::= symbol
172/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000173/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000174/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000175bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000176 switch (Lexer.getKind()) {
177 default:
178 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000179 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000180 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000181 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000182 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000183 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000184 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000185 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000186 case AsmToken::Identifier: {
187 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000188 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
189 MCSymbol *Sym = CreateSymbol(Split.first);
190
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000191 // Mark the symbol as used in an expression.
192 Sym->setUsedInExpr(true);
193
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000194 // Lookup the symbol variant if used.
195 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
196 if (Split.first.size() != getTok().getIdentifier().size())
197 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
198
Chris Lattnerb4307b32010-01-15 19:28:38 +0000199 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000200 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000201
202 // If this is an absolute variable reference, substitute it now to preserve
203 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000204 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000205 if (Variant)
206 return Error(EndLoc, "unexpected modified on variable reference");
207
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000208 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000209 return false;
210 }
211
212 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000213 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000214 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000215 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000216 case AsmToken::Integer: {
217 SMLoc Loc = getTok().getLoc();
218 int64_t IntVal = getTok().getIntVal();
219 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000220 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000221 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000222 // Look for 'b' or 'f' following an Integer as a directional label
223 if (Lexer.getKind() == AsmToken::Identifier) {
224 StringRef IDVal = getTok().getString();
225 if (IDVal == "f" || IDVal == "b"){
226 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
227 IDVal == "f" ? 1 : 0);
228 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
229 getContext());
230 if(IDVal == "b" && Sym->isUndefined())
231 return Error(Loc, "invalid reference to undefined symbol");
232 EndLoc = Lexer.getLoc();
233 Lex(); // Eat identifier.
234 }
235 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000236 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000237 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000238 case AsmToken::Dot: {
239 // This is a '.' reference, which references the current PC. Emit a
240 // temporary label to the streamer and refer to it.
241 MCSymbol *Sym = Ctx.CreateTempSymbol();
242 Out.EmitLabel(Sym);
243 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
244 EndLoc = Lexer.getLoc();
245 Lex(); // Eat identifier.
246 return false;
247 }
248
Daniel Dunbar3f872332009-07-28 16:08:33 +0000249 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000250 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000251 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000252 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000253 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000256 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000259 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000260 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000262 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000264 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000265 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000266 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000267 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000268 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000270 }
271}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000272
Chris Lattnerb4307b32010-01-15 19:28:38 +0000273bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000274 SMLoc EndLoc;
275 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000276}
277
Chris Lattner74ec1a32009-06-22 06:32:03 +0000278/// ParseExpression - Parse an expression and return it.
279///
280/// expr ::= expr +,- expr -> lowest.
281/// expr ::= expr |,^,&,! expr -> middle.
282/// expr ::= expr *,/,%,<<,>> expr -> highest.
283/// expr ::= primaryexpr
284///
Chris Lattner54482b42010-01-15 19:39:23 +0000285bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000286 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000287 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000288 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
289 return true;
290
291 // Try to constant fold it up front, if possible.
292 int64_t Value;
293 if (Res->EvaluateAsAbsolute(Value))
294 Res = MCConstantExpr::Create(Value, getContext());
295
296 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000297}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000298
Chris Lattnerb4307b32010-01-15 19:28:38 +0000299bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000300 Res = 0;
301 return ParseParenExpr(Res, EndLoc) ||
302 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000303}
304
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000306 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000308 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309 if (ParseExpression(Expr))
310 return true;
311
Daniel Dunbare00b0112009-10-16 01:57:52 +0000312 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000313 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000314
315 return false;
316}
317
Daniel Dunbar3f872332009-07-28 16:08:33 +0000318static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000319 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000320 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000321 default:
322 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000323
324 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000325 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000326 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000327 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000328 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000329 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000330 return 1;
331
332 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000333 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000334 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000342 case AsmToken::ExclaimEqual:
343 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000344 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000345 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000353 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000354 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000357 return 2;
358
359 // Intermediate Precedence: |, &, ^
360 //
361 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000363 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000364 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000366 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000368 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000369 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000370 return 3;
371
372 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000373 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000374 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000375 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000376 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000377 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000378 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000379 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000380 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000381 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000382 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000383 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000384 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000386 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000387 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000388 }
389}
390
391
392/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
393/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000394bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
395 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000396 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000397 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000398 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000399
400 // If the next token is lower precedence than we are allowed to eat, return
401 // successfully with what we ate already.
402 if (TokPrec < Precedence)
403 return false;
404
Sean Callanan79ed1a82010-01-19 20:22:31 +0000405 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000406
407 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000408 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000409 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000410
411 // If BinOp binds less tightly with RHS than the operator after RHS, let
412 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000413 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000414 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000415 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000416 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000417 }
418
Daniel Dunbar475839e2009-06-29 20:37:27 +0000419 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000420 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000421 }
422}
423
Chris Lattnerc4193832009-06-22 05:51:26 +0000424
425
426
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000427/// ParseStatement:
428/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000429/// ::= Label* Directive ...Operands... EndOfStatement
430/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000431bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000432 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000433 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000434 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000435 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000436 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000437
438 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000439 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000440 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000441 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000442 int64_t LocalLabelVal = -1;
443 // GUESS allow an integer followed by a ':' as a directional local label
444 if (Lexer.is(AsmToken::Integer)) {
445 LocalLabelVal = getTok().getIntVal();
446 if (LocalLabelVal < 0) {
447 if (!TheCondState.Ignore)
448 return TokError("unexpected token at start of statement");
449 IDVal = "";
450 }
451 else {
452 IDVal = getTok().getString();
453 Lex(); // Consume the integer token to be used as an identifier token.
454 if (Lexer.getKind() != AsmToken::Colon) {
455 if (!TheCondState.Ignore)
456 return TokError("unexpected token at start of statement");
457 }
458 }
459 }
460 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000461 if (!TheCondState.Ignore)
462 return TokError("unexpected token at start of statement");
463 IDVal = "";
464 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000465
Chris Lattner7834fac2010-04-17 18:14:27 +0000466 // Handle conditional assembly here before checking for skipping. We
467 // have to do this so that .endif isn't skipped in a ".if 0" block for
468 // example.
469 if (IDVal == ".if")
470 return ParseDirectiveIf(IDLoc);
471 if (IDVal == ".elseif")
472 return ParseDirectiveElseIf(IDLoc);
473 if (IDVal == ".else")
474 return ParseDirectiveElse(IDLoc);
475 if (IDVal == ".endif")
476 return ParseDirectiveEndIf(IDLoc);
477
478 // If we are in a ".if 0" block, ignore this statement.
479 if (TheCondState.Ignore) {
480 EatToEndOfStatement();
481 return false;
482 }
483
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000484 // FIXME: Recurse on local labels?
485
486 // See what kind of statement we have.
487 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000488 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000489 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000490 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000491
492 // Diagnose attempt to use a variable as a label.
493 //
494 // FIXME: Diagnostics. Note the location of the definition as a label.
495 // FIXME: This doesn't diagnose assignment to a symbol which has been
496 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000497 MCSymbol *Sym;
498 if (LocalLabelVal == -1)
499 Sym = CreateSymbol(IDVal);
500 else
501 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000502 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000503 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000504
Daniel Dunbar959fd882009-08-26 22:13:22 +0000505 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000506 Out.EmitLabel(Sym);
507
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000508 // Consume any end of statement token, if present, to avoid spurious
509 // AddBlankLine calls().
510 if (Lexer.is(AsmToken::EndOfStatement)) {
511 Lex();
512 if (Lexer.is(AsmToken::Eof))
513 return false;
514 }
515
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000516 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000517 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000518
Daniel Dunbar3f872332009-07-28 16:08:33 +0000519 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000520 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000521 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000522
Daniel Dunbare2ace502009-08-31 08:09:09 +0000523 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000524
525 default: // Normal instruction or directive.
526 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000527 }
528
529 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000530 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000531 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000533 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000535 // FIXME: This changes behavior based on the -static flag to the
536 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000537 return ParseDirectiveSectionSwitch("__TEXT", "__text",
538 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000540 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000544 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
545 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000547 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 MCSectionMachO::S_4BYTE_LITERALS,
549 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000551 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 MCSectionMachO::S_8BYTE_LITERALS,
553 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000555 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 MCSectionMachO::S_16BYTE_LITERALS,
557 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000563 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
566
567 // FIXME: The assembler manual claims that this has the self modify code
568 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000569 if (IDVal == ".symbol_stub")
570 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
571 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000572 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
573 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 0, 16);
575 // FIXME: PowerPC only?
576 if (IDVal == ".picsymbol_stub")
577 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
578 MCSectionMachO::S_SYMBOL_STUBS |
579 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
580 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000582 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000584 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
585
586 // FIXME: The section names of these two are misspelled in the assembler
587 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
590 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
591 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000593 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
594 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
595 4);
596
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000598 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000600 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
602 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000604 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000605 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
606 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000609
610
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000612 return ParseDirectiveSectionSwitch("__OBJC", "__class",
613 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000615 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
616 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000618 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
619 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000621 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
622 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000624 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
625 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000627 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
628 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000630 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
631 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000633 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
634 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000636 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
637 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
638 MCSectionMachO::S_LITERAL_POINTERS,
639 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
642 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
643 MCSectionMachO::S_LITERAL_POINTERS,
644 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000646 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
647 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000649 return ParseDirectiveSectionSwitch("__OBJC", "__category",
650 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000652 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
653 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000655 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
656 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000658 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
659 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000661 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
662 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000664 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
665 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000667 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
668 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000670 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
671 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000672
Eric Christopherc6177a42010-05-17 22:53:55 +0000673 if (IDVal == ".tdata")
674 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
675 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
676 if (IDVal == ".tlv")
677 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
678 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
679 if (IDVal == ".thread_init_func")
680 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
681 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
682
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000683 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000685 return ParseDirectiveSet();
686
Daniel Dunbara0d14262009-06-24 23:30:00 +0000687 // Data directives
688
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000690 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000692 return ParseDirectiveAscii(true);
693
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000695 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000696 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000697 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000699 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000701 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000702
703 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000705 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000707 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000709 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000711 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000713 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000715 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000717 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000719 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
720
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000722 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000723
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000725 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000727 return ParseDirectiveSpace();
728
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000729 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000730
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000732 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000734 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000736 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000738 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000740 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000742 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000744 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000746 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000748 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000749 if (IDVal == ".type")
750 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000752 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000754 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000756 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000757
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000758 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000759 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000761 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000763 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000765 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000767 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000768 if (IDVal == ".tbss")
769 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000770
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000771 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000772 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000773 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000774 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000775 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000776 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000777 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000778 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000779 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000780 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbyf187ac52010-06-28 21:45:58 +0000781 if (IDVal == ".secure_log_unique")
782 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
783 if (IDVal == ".secure_log_reset")
784 return ParseDirectiveDarwinSecureLogReset(IDLoc);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000785
Chris Lattnerebb89b42009-09-27 21:16:52 +0000786 // Look up the handler in the handler table,
787 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
788 if (Handler)
789 return (this->*Handler)(IDVal, IDLoc);
790
Kevin Enderby9c656452009-09-10 20:51:44 +0000791 // Target hook for parsing target specific directives.
792 if (!getTargetParser().ParseDirective(ID))
793 return false;
794
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000795 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000796 EatToEndOfStatement();
797 return false;
798 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000799
Chris Lattnera7f13542010-05-19 23:34:33 +0000800 // Canonicalize the opcode to lower case.
801 SmallString<128> Opcode;
802 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
803 Opcode.push_back(tolower(IDVal[i]));
804
Chris Lattner98986712010-01-14 22:21:20 +0000805 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000806 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000807 ParsedOperands);
808 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
809 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000810
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000811 // If parsing succeeded, match the instruction.
812 if (!HadError) {
813 MCInst Inst;
814 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
815 // Emit the instruction on success.
816 Out.EmitInstruction(Inst);
817 } else {
818 // Otherwise emit a diagnostic about the match failure and set the error
819 // flag.
820 //
821 // FIXME: We should give nicer diagnostics about the exact failure.
822 Error(IDLoc, "unrecognized instruction");
823 HadError = true;
824 }
825 }
Chris Lattner98986712010-01-14 22:21:20 +0000826
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000827 // If there was no error, consume the end-of-statement token. Otherwise this
828 // will be done by our caller.
829 if (!HadError)
830 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000831
832 // Free any parsed operands.
833 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
834 delete ParsedOperands[i];
835
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000836 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000837}
Chris Lattner9a023f72009-06-24 04:43:34 +0000838
Daniel Dunbare2ace502009-08-31 08:09:09 +0000839bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000840 // FIXME: Use better location, we should use proper tokens.
841 SMLoc EqualLoc = Lexer.getLoc();
842
Daniel Dunbar821e3332009-08-31 08:09:28 +0000843 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000844 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000845 return true;
846
Daniel Dunbar3f872332009-07-28 16:08:33 +0000847 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000848 return TokError("unexpected token in assignment");
849
850 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000851 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000852
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000853 // Validate that the LHS is allowed to be a variable (either it has not been
854 // used as a symbol, or it is an absolute symbol).
855 MCSymbol *Sym = getContext().LookupSymbol(Name);
856 if (Sym) {
857 // Diagnose assignment to a label.
858 //
859 // FIXME: Diagnostics. Note the location of the definition as a label.
860 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000861 if (Sym->isUndefined() && !Sym->isUsedInExpr())
862 ; // Allow redefinitions of undefined symbols only used in directives.
863 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000864 return Error(EqualLoc, "redefinition of '" + Name + "'");
865 else if (!Sym->isVariable())
866 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000867 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000868 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
869 Name + "'");
870 } else
871 Sym = CreateSymbol(Name);
872
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000873 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000874
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000875 Sym->setUsedInExpr(true);
876
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000877 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000878 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000879
880 return false;
881}
882
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000883/// ParseIdentifier:
884/// ::= identifier
885/// ::= string
886bool AsmParser::ParseIdentifier(StringRef &Res) {
887 if (Lexer.isNot(AsmToken::Identifier) &&
888 Lexer.isNot(AsmToken::String))
889 return true;
890
Sean Callanan18b83232010-01-19 21:44:56 +0000891 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000892
Sean Callanan79ed1a82010-01-19 20:22:31 +0000893 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000894
895 return false;
896}
897
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000898/// ParseDirectiveSet:
899/// ::= .set identifier ',' expression
900bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000901 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000902
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000903 if (ParseIdentifier(Name))
904 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000905
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000906 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000907 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000908 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000909
Daniel Dunbare2ace502009-08-31 08:09:09 +0000910 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000911}
912
Chris Lattner9a023f72009-06-24 04:43:34 +0000913/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000914/// ::= .section identifier (',' identifier)*
915/// FIXME: This should actually parse out the segment, section, attributes and
916/// sizeof_stub fields.
917bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000918 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000919
Daniel Dunbarace63122009-08-11 03:42:33 +0000920 StringRef SectionName;
921 if (ParseIdentifier(SectionName))
922 return Error(Loc, "expected identifier after '.section' directive");
923
924 // Verify there is a following comma.
925 if (!Lexer.is(AsmToken::Comma))
926 return TokError("unexpected token in '.section' directive");
927
Chris Lattnerff4bc462009-08-10 01:39:42 +0000928 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000929 SectionSpec += ",";
930
931 // Add all the tokens until the end of the line, ParseSectionSpecifier will
932 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000933 StringRef EOL = Lexer.LexUntilEndOfStatement();
934 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000935
Sean Callanan79ed1a82010-01-19 20:22:31 +0000936 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000937 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000938 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000939 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000940
Chris Lattnerff4bc462009-08-10 01:39:42 +0000941
942 StringRef Segment, Section;
943 unsigned TAA, StubSize;
944 std::string ErrorStr =
945 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
946 TAA, StubSize);
947
948 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000949 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000950
Chris Lattner56594f92009-07-31 17:47:16 +0000951 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000952 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000953 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
954 isText ? SectionKind::getText()
955 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000956 return false;
957}
958
Chris Lattnere15c2d72009-08-10 18:05:55 +0000959/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000960bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
961 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000962 unsigned TAA, unsigned Align,
963 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000964 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000965 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000966 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000967
Chris Lattner56594f92009-07-31 17:47:16 +0000968 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000969 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000970 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
971 isText ? SectionKind::getText()
972 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000973
974 // Set the implicit alignment, if any.
975 //
976 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
977 // alignment on the section (e.g., if one manually inserts bytes into the
978 // section, then just issueing the section switch directive will not realign
979 // the section. However, this is arguably more reasonable behavior, and there
980 // is no good reason for someone to intentionally emit incorrectly sized
981 // values into the implicitly aligned sections.
982 if (Align)
983 Out.EmitValueToAlignment(Align, 0, 1, 0);
984
Chris Lattner529fb542009-06-24 05:13:15 +0000985 return false;
986}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000987
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000988bool AsmParser::ParseEscapedString(std::string &Data) {
989 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
990
991 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000992 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000993 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
994 if (Str[i] != '\\') {
995 Data += Str[i];
996 continue;
997 }
998
999 // Recognize escaped characters. Note that this escape semantics currently
1000 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1001 ++i;
1002 if (i == e)
1003 return TokError("unexpected backslash at end of string");
1004
1005 // Recognize octal sequences.
1006 if ((unsigned) (Str[i] - '0') <= 7) {
1007 // Consume up to three octal characters.
1008 unsigned Value = Str[i] - '0';
1009
1010 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1011 ++i;
1012 Value = Value * 8 + (Str[i] - '0');
1013
1014 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1015 ++i;
1016 Value = Value * 8 + (Str[i] - '0');
1017 }
1018 }
1019
1020 if (Value > 255)
1021 return TokError("invalid octal escape sequence (out of range)");
1022
1023 Data += (unsigned char) Value;
1024 continue;
1025 }
1026
1027 // Otherwise recognize individual escapes.
1028 switch (Str[i]) {
1029 default:
1030 // Just reject invalid escape sequences for now.
1031 return TokError("invalid escape sequence (unrecognized character)");
1032
1033 case 'b': Data += '\b'; break;
1034 case 'f': Data += '\f'; break;
1035 case 'n': Data += '\n'; break;
1036 case 'r': Data += '\r'; break;
1037 case 't': Data += '\t'; break;
1038 case '"': Data += '"'; break;
1039 case '\\': Data += '\\'; break;
1040 }
1041 }
1042
1043 return false;
1044}
1045
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001047/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001050 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001051 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052 return TokError("expected string in '.ascii' or '.asciz' directive");
1053
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001054 std::string Data;
1055 if (ParseEscapedString(Data))
1056 return true;
1057
Chris Lattneraaec2052010-01-19 19:46:13 +00001058 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001060 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001061
Sean Callanan79ed1a82010-01-19 20:22:31 +00001062 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063
Daniel Dunbar3f872332009-07-28 16:08:33 +00001064 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001065 break;
1066
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001069 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001070 }
1071 }
1072
Sean Callanan79ed1a82010-01-19 20:22:31 +00001073 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001074 return false;
1075}
1076
1077/// ParseDirectiveValue
1078/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1079bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001080 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001082 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001083 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001084 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001085 return true;
1086
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001087 // Special case constant expressions to match code generator.
1088 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1089 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1090 else
1091 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092
Daniel Dunbar3f872332009-07-28 16:08:33 +00001093 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001094 break;
1095
1096 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001097 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001098 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001099 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001100 }
1101 }
1102
Sean Callanan79ed1a82010-01-19 20:22:31 +00001103 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001104 return false;
1105}
1106
1107/// ParseDirectiveSpace
1108/// ::= .space expression [ , expression ]
1109bool AsmParser::ParseDirectiveSpace() {
1110 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001111 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001112 return true;
1113
1114 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001115 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1116 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119
Daniel Dunbar475839e2009-06-29 20:37:27 +00001120 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001121 return true;
1122
Daniel Dunbar3f872332009-07-28 16:08:33 +00001123 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124 return TokError("unexpected token in '.space' directive");
1125 }
1126
Sean Callanan79ed1a82010-01-19 20:22:31 +00001127 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001128
1129 if (NumBytes <= 0)
1130 return TokError("invalid number of bytes in '.space' directive");
1131
1132 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001133 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001134
1135 return false;
1136}
1137
1138/// ParseDirectiveFill
1139/// ::= .fill expression , expression , expression
1140bool AsmParser::ParseDirectiveFill() {
1141 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001142 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001143 return true;
1144
Daniel Dunbar3f872332009-07-28 16:08:33 +00001145 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001147 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148
1149 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001150 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001151 return true;
1152
Daniel Dunbar3f872332009-07-28 16:08:33 +00001153 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001155 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001156
1157 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001158 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001159 return true;
1160
Daniel Dunbar3f872332009-07-28 16:08:33 +00001161 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001162 return TokError("unexpected token in '.fill' directive");
1163
Sean Callanan79ed1a82010-01-19 20:22:31 +00001164 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001165
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001166 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1167 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001168
1169 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001170 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001171
1172 return false;
1173}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001174
1175/// ParseDirectiveOrg
1176/// ::= .org expression [ , expression ]
1177bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001178 const MCExpr *Offset;
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
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001382 int64_t DescValue;
1383 if (ParseAbsoluteExpression(DescValue))
1384 return true;
1385
Daniel Dunbar3f872332009-07-28 16:08:33 +00001386 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001387 return TokError("unexpected token in '.desc' directive");
1388
Sean Callanan79ed1a82010-01-19 20:22:31 +00001389 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001390
1391 // Set the n_desc field of this Symbol to this DescValue
1392 Out.EmitSymbolDesc(Sym, DescValue);
1393
1394 return false;
1395}
1396
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001397/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001398/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1399bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001400 SMLoc IDLoc = Lexer.getLoc();
1401 StringRef Name;
1402 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001403 return TokError("expected identifier in directive");
1404
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001405 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001406 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001407
Daniel Dunbar3f872332009-07-28 16:08:33 +00001408 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001409 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001410 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001411
1412 int64_t Size;
1413 SMLoc SizeLoc = Lexer.getLoc();
1414 if (ParseAbsoluteExpression(Size))
1415 return true;
1416
1417 int64_t Pow2Alignment = 0;
1418 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001419 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001420 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001421 Pow2AlignmentLoc = Lexer.getLoc();
1422 if (ParseAbsoluteExpression(Pow2Alignment))
1423 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001424
1425 // If this target takes alignments in bytes (not log) validate and convert.
1426 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1427 if (!isPowerOf2_64(Pow2Alignment))
1428 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1429 Pow2Alignment = Log2_64(Pow2Alignment);
1430 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001431 }
1432
Daniel Dunbar3f872332009-07-28 16:08:33 +00001433 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001434 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001435
Sean Callanan79ed1a82010-01-19 20:22:31 +00001436 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001437
Chris Lattner1fc3d752009-07-09 17:25:12 +00001438 // NOTE: a size of zero for a .comm should create a undefined symbol
1439 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001440 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001441 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1442 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001443
Eric Christopherc260a3e2010-05-14 01:38:54 +00001444 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001445 // may internally end up wanting an alignment in bytes.
1446 // FIXME: Diagnose overflow.
1447 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001448 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1449 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001450
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001451 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001452 return Error(IDLoc, "invalid symbol redefinition");
1453
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001454 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001455 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001456 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001457 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1458 MCSectionMachO::S_ZEROFILL, 0,
1459 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001460 Sym, Size, 1 << Pow2Alignment);
1461 return false;
1462 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001463
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001464 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001465 return false;
1466}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001467
1468/// ParseDirectiveDarwinZerofill
1469/// ::= .zerofill segname , sectname [, identifier , size_expression [
1470/// , align_expression ]]
1471bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001472 StringRef Segment;
1473 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001474 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001475
Daniel Dunbar3f872332009-07-28 16:08:33 +00001476 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001477 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001478 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001479
1480 StringRef Section;
1481 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001482 return TokError("expected section name after comma in '.zerofill' "
1483 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001484
Chris Lattner9be3fee2009-07-10 22:20:30 +00001485 // If this is the end of the line all that was wanted was to create the
1486 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001487 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001488 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001489 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1490 MCSectionMachO::S_ZEROFILL, 0,
1491 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001492 return false;
1493 }
1494
Daniel Dunbar3f872332009-07-28 16:08:33 +00001495 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001496 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001497 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001498
Chris Lattner7bb7c552010-05-13 00:10:34 +00001499 SMLoc IDLoc = Lexer.getLoc();
1500 StringRef IDStr;
1501 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001502 return TokError("expected identifier in directive");
1503
1504 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001505 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001506
Daniel Dunbar3f872332009-07-28 16:08:33 +00001507 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001508 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001509 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001510
1511 int64_t Size;
1512 SMLoc SizeLoc = Lexer.getLoc();
1513 if (ParseAbsoluteExpression(Size))
1514 return true;
1515
1516 int64_t Pow2Alignment = 0;
1517 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001518 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001519 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001520 Pow2AlignmentLoc = Lexer.getLoc();
1521 if (ParseAbsoluteExpression(Pow2Alignment))
1522 return true;
1523 }
1524
Daniel Dunbar3f872332009-07-28 16:08:33 +00001525 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001526 return TokError("unexpected token in '.zerofill' directive");
1527
Sean Callanan79ed1a82010-01-19 20:22:31 +00001528 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001529
1530 if (Size < 0)
1531 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1532 "than zero");
1533
Eric Christopherc260a3e2010-05-14 01:38:54 +00001534 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001535 // may internally end up wanting an alignment in bytes.
1536 // FIXME: Diagnose overflow.
1537 if (Pow2Alignment < 0)
1538 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1539 "can't be less than zero");
1540
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001541 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001542 return Error(IDLoc, "invalid symbol redefinition");
1543
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001544 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001545 //
1546 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001547 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1548 MCSectionMachO::S_ZEROFILL, 0,
1549 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001550 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001551
1552 return false;
1553}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001554
Eric Christopher482eba02010-05-14 01:50:28 +00001555/// ParseDirectiveDarwinTBSS
1556/// ::= .tbss identifier, size, align
1557bool AsmParser::ParseDirectiveDarwinTBSS() {
1558 SMLoc IDLoc = Lexer.getLoc();
1559 StringRef Name;
1560 if (ParseIdentifier(Name))
1561 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001562
Eric Christopher482eba02010-05-14 01:50:28 +00001563 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001564 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001565
1566 if (Lexer.isNot(AsmToken::Comma))
1567 return TokError("unexpected token in directive");
1568 Lex();
1569
1570 int64_t Size;
1571 SMLoc SizeLoc = Lexer.getLoc();
1572 if (ParseAbsoluteExpression(Size))
1573 return true;
1574
1575 int64_t Pow2Alignment = 0;
1576 SMLoc Pow2AlignmentLoc;
1577 if (Lexer.is(AsmToken::Comma)) {
1578 Lex();
1579 Pow2AlignmentLoc = Lexer.getLoc();
1580 if (ParseAbsoluteExpression(Pow2Alignment))
1581 return true;
1582 }
1583
1584 if (Lexer.isNot(AsmToken::EndOfStatement))
1585 return TokError("unexpected token in '.tbss' directive");
1586
1587 Lex();
1588
1589 if (Size < 0)
1590 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1591 "zero");
1592
1593 // FIXME: Diagnose overflow.
1594 if (Pow2Alignment < 0)
1595 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1596 "than zero");
1597
1598 if (!Sym->isUndefined())
1599 return Error(IDLoc, "invalid symbol redefinition");
1600
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001601 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1602 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1603 0, SectionKind::getThreadBSS()),
1604 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001605
1606 return false;
1607}
1608
Kevin Enderbya5c78322009-07-13 21:03:15 +00001609/// ParseDirectiveDarwinSubsectionsViaSymbols
1610/// ::= .subsections_via_symbols
1611bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001612 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001613 return TokError("unexpected token in '.subsections_via_symbols' directive");
1614
Sean Callanan79ed1a82010-01-19 20:22:31 +00001615 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001616
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001617 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001618
1619 return false;
1620}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001621
1622/// ParseDirectiveAbort
1623/// ::= .abort [ "abort_string" ]
1624bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001625 // FIXME: Use loc from directive.
1626 SMLoc Loc = Lexer.getLoc();
1627
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001628 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001629 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1630 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001631 return TokError("expected string in '.abort' directive");
1632
Sean Callanan18b83232010-01-19 21:44:56 +00001633 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001634
Sean Callanan79ed1a82010-01-19 20:22:31 +00001635 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001636 }
1637
Daniel Dunbar3f872332009-07-28 16:08:33 +00001638 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001639 return TokError("unexpected token in '.abort' directive");
1640
Sean Callanan79ed1a82010-01-19 20:22:31 +00001641 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001642
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001643 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001644 if (Str.empty())
1645 Error(Loc, ".abort detected. Assembly stopping.");
1646 else
1647 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001648
1649 return false;
1650}
Kevin Enderby71148242009-07-14 21:35:03 +00001651
1652/// ParseDirectiveLsym
1653/// ::= .lsym identifier , expression
1654bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001655 StringRef Name;
1656 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001657 return TokError("expected identifier in directive");
1658
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001659 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001660 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001661
Daniel Dunbar3f872332009-07-28 16:08:33 +00001662 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001663 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001664 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001665
Daniel Dunbar821e3332009-08-31 08:09:28 +00001666 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001667 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001668 return true;
1669
Daniel Dunbar3f872332009-07-28 16:08:33 +00001670 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001671 return TokError("unexpected token in '.lsym' directive");
1672
Sean Callanan79ed1a82010-01-19 20:22:31 +00001673 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001674
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001675 // We don't currently support this directive.
1676 //
1677 // FIXME: Diagnostic location!
1678 (void) Sym;
1679 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001680}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001681
1682/// ParseDirectiveInclude
1683/// ::= .include "filename"
1684bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001685 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001686 return TokError("expected string in '.include' directive");
1687
Sean Callanan18b83232010-01-19 21:44:56 +00001688 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001689 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001690 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001691
Daniel Dunbar3f872332009-07-28 16:08:33 +00001692 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001693 return TokError("unexpected token in '.include' directive");
1694
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001695 // Strip the quotes.
1696 Filename = Filename.substr(1, Filename.size()-2);
1697
1698 // Attempt to switch the lexer to the included file before consuming the end
1699 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001700 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001701 PrintMessage(IncludeLoc,
1702 "Could not find include file '" + Filename + "'",
1703 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001704 return true;
1705 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001706
1707 return false;
1708}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001709
1710/// ParseDirectiveDarwinDumpOrLoad
1711/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001712bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001713 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001714 return TokError("expected string in '.dump' or '.load' directive");
1715
Sean Callanan79ed1a82010-01-19 20:22:31 +00001716 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001717
Daniel Dunbar3f872332009-07-28 16:08:33 +00001718 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001719 return TokError("unexpected token in '.dump' or '.load' directive");
1720
Sean Callanan79ed1a82010-01-19 20:22:31 +00001721 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001722
Kevin Enderby5026ae42009-07-20 20:25:37 +00001723 // FIXME: If/when .dump and .load are implemented they will be done in the
1724 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001725 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001726 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001727 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001728 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001729
1730 return false;
1731}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001732
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001733/// ParseDirectiveDarwinSecureLogUnique
1734/// ::= .secure_log_unique "log message"
1735bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1736 std::string LogMessage;
1737
1738 if (Lexer.isNot(AsmToken::String))
1739 LogMessage = "";
1740 else{
1741 LogMessage = getTok().getString();
1742 Lex();
1743 }
1744
1745 if (Lexer.isNot(AsmToken::EndOfStatement))
1746 return TokError("unexpected token in '.secure_log_unique' directive");
1747
1748 if (getContext().getSecureLogUsed() != false)
1749 return Error(IDLoc, ".secure_log_unique specified multiple times");
1750
1751 char *SecureLogFile = getContext().getSecureLogFile();
1752 if (SecureLogFile == NULL)
1753 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1754 "environment variable unset.");
1755
1756 raw_ostream *OS = getContext().getSecureLog();
1757 if (OS == NULL) {
1758 std::string Err;
1759 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1760 if (!Err.empty()) {
1761 delete OS;
1762 return Error(IDLoc, Twine("can't open secure log file: ") +
1763 SecureLogFile + " (" + Err + ")");
1764 }
1765 getContext().setSecureLog(OS);
1766 }
1767
1768 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1769 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1770 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1771 << LogMessage + "\n";
1772
1773 getContext().setSecureLogUsed(true);
1774
1775 return false;
1776}
1777
1778/// ParseDirectiveDarwinSecureLogReset
1779/// ::= .secure_log_reset
1780bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1781 if (Lexer.isNot(AsmToken::EndOfStatement))
1782 return TokError("unexpected token in '.secure_log_reset' directive");
1783
1784 Lex();
1785
1786 getContext().setSecureLogUsed(false);
1787
1788 return false;
1789}
1790
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001791/// ParseDirectiveIf
1792/// ::= .if expression
1793bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001794 TheCondStack.push_back(TheCondState);
1795 TheCondState.TheCond = AsmCond::IfCond;
1796 if(TheCondState.Ignore) {
1797 EatToEndOfStatement();
1798 }
1799 else {
1800 int64_t ExprValue;
1801 if (ParseAbsoluteExpression(ExprValue))
1802 return true;
1803
1804 if (Lexer.isNot(AsmToken::EndOfStatement))
1805 return TokError("unexpected token in '.if' directive");
1806
Sean Callanan79ed1a82010-01-19 20:22:31 +00001807 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001808
1809 TheCondState.CondMet = ExprValue;
1810 TheCondState.Ignore = !TheCondState.CondMet;
1811 }
1812
1813 return false;
1814}
1815
1816/// ParseDirectiveElseIf
1817/// ::= .elseif expression
1818bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1819 if (TheCondState.TheCond != AsmCond::IfCond &&
1820 TheCondState.TheCond != AsmCond::ElseIfCond)
1821 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1822 " an .elseif");
1823 TheCondState.TheCond = AsmCond::ElseIfCond;
1824
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001825 bool LastIgnoreState = false;
1826 if (!TheCondStack.empty())
1827 LastIgnoreState = TheCondStack.back().Ignore;
1828 if (LastIgnoreState || TheCondState.CondMet) {
1829 TheCondState.Ignore = true;
1830 EatToEndOfStatement();
1831 }
1832 else {
1833 int64_t ExprValue;
1834 if (ParseAbsoluteExpression(ExprValue))
1835 return true;
1836
1837 if (Lexer.isNot(AsmToken::EndOfStatement))
1838 return TokError("unexpected token in '.elseif' directive");
1839
Sean Callanan79ed1a82010-01-19 20:22:31 +00001840 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001841 TheCondState.CondMet = ExprValue;
1842 TheCondState.Ignore = !TheCondState.CondMet;
1843 }
1844
1845 return false;
1846}
1847
1848/// ParseDirectiveElse
1849/// ::= .else
1850bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001851 if (Lexer.isNot(AsmToken::EndOfStatement))
1852 return TokError("unexpected token in '.else' directive");
1853
Sean Callanan79ed1a82010-01-19 20:22:31 +00001854 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001855
1856 if (TheCondState.TheCond != AsmCond::IfCond &&
1857 TheCondState.TheCond != AsmCond::ElseIfCond)
1858 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1859 ".elseif");
1860 TheCondState.TheCond = AsmCond::ElseCond;
1861 bool LastIgnoreState = false;
1862 if (!TheCondStack.empty())
1863 LastIgnoreState = TheCondStack.back().Ignore;
1864 if (LastIgnoreState || TheCondState.CondMet)
1865 TheCondState.Ignore = true;
1866 else
1867 TheCondState.Ignore = false;
1868
1869 return false;
1870}
1871
1872/// ParseDirectiveEndIf
1873/// ::= .endif
1874bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001875 if (Lexer.isNot(AsmToken::EndOfStatement))
1876 return TokError("unexpected token in '.endif' directive");
1877
Sean Callanan79ed1a82010-01-19 20:22:31 +00001878 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001879
1880 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1881 TheCondStack.empty())
1882 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1883 ".else");
1884 if (!TheCondStack.empty()) {
1885 TheCondState = TheCondStack.back();
1886 TheCondStack.pop_back();
1887 }
1888
1889 return false;
1890}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001891
1892/// ParseDirectiveFile
1893/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001894bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001895 // FIXME: I'm not sure what this is.
1896 int64_t FileNumber = -1;
1897 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001898 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001899 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001900
1901 if (FileNumber < 1)
1902 return TokError("file number less than one");
1903 }
1904
1905 if (Lexer.isNot(AsmToken::String))
1906 return TokError("unexpected token in '.file' directive");
1907
Chris Lattnerd32e8032010-01-25 19:02:58 +00001908 StringRef Filename = getTok().getString();
1909 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001910 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001911
1912 if (Lexer.isNot(AsmToken::EndOfStatement))
1913 return TokError("unexpected token in '.file' directive");
1914
Chris Lattnerd32e8032010-01-25 19:02:58 +00001915 if (FileNumber == -1)
1916 Out.EmitFileDirective(Filename);
1917 else
1918 Out.EmitDwarfFileDirective(FileNumber, Filename);
1919
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001920 return false;
1921}
1922
1923/// ParseDirectiveLine
1924/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001925bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001926 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1927 if (Lexer.isNot(AsmToken::Integer))
1928 return TokError("unexpected token in '.line' directive");
1929
Sean Callanan18b83232010-01-19 21:44:56 +00001930 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001931 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001932 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001933
1934 // FIXME: Do something with the .line.
1935 }
1936
1937 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001938 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001939
1940 return false;
1941}
1942
1943
1944/// ParseDirectiveLoc
1945/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001946bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001947 if (Lexer.isNot(AsmToken::Integer))
1948 return TokError("unexpected token in '.loc' directive");
1949
1950 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001951 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001952 (void) FileNumber;
1953 // FIXME: Validate file.
1954
Sean Callanan79ed1a82010-01-19 20:22:31 +00001955 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001956 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1957 if (Lexer.isNot(AsmToken::Integer))
1958 return TokError("unexpected token in '.loc' directive");
1959
Sean Callanan18b83232010-01-19 21:44:56 +00001960 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001961 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001962 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001963
1964 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1965 if (Lexer.isNot(AsmToken::Integer))
1966 return TokError("unexpected token in '.loc' directive");
1967
Sean Callanan18b83232010-01-19 21:44:56 +00001968 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001969 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001970 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001971
1972 // FIXME: Do something with the .loc.
1973 }
1974 }
1975
1976 if (Lexer.isNot(AsmToken::EndOfStatement))
1977 return TokError("unexpected token in '.file' directive");
1978
1979 return false;
1980}
1981