blob: 775075cf975a1ba727daa47b701d69cb92ee0571 [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)
Daniel Dunbarb5709682010-07-01 20:48:51 +000037 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
38 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000039 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
40
Chris Lattnerebb89b42009-09-27 21:16:52 +000041 // Debugging directives.
42 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
43 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
44 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
45}
46
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000047AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000048}
49
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000050void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000051 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000052}
53
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000054bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000055 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000056 return true;
57}
58
59bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000060 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000061 return true;
62}
63
Sean Callananbf2013e2010-01-20 23:19:55 +000064void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
65 const char *Type) const {
66 SrcMgr.PrintMessage(Loc, Msg, Type);
67}
Sean Callananfd0b0282010-01-21 00:19:58 +000068
69bool AsmParser::EnterIncludeFile(const std::string &Filename) {
70 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
71 if (NewBuf == -1)
72 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000073
Sean Callananfd0b0282010-01-21 00:19:58 +000074 CurBuffer = NewBuf;
75
76 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
77
78 return false;
79}
80
81const AsmToken &AsmParser::Lex() {
82 const AsmToken *tok = &Lexer.Lex();
83
84 if (tok->is(AsmToken::Eof)) {
85 // If this is the end of an included file, pop the parent file off the
86 // include stack.
87 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
88 if (ParentIncludeLoc != SMLoc()) {
89 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
90 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
91 ParentIncludeLoc.getPointer());
92 tok = &Lexer.Lex();
93 }
94 }
95
96 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000097 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000098
Sean Callananfd0b0282010-01-21 00:19:58 +000099 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000100}
101
Chris Lattner79180e22010-04-05 23:15:42 +0000102bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000103 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000104 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000105 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000106 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000107 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
109 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000110
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000111 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000112 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000113
Chris Lattnerb717fb02009-07-02 21:53:43 +0000114 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000115
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000116 AsmCond StartingCondState = TheCondState;
117
Chris Lattnerb717fb02009-07-02 21:53:43 +0000118 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000119 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000120 if (!ParseStatement()) continue;
121
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000122 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000123 HadError = true;
124 EatToEndOfStatement();
125 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000126
127 if (TheCondState.TheCond != StartingCondState.TheCond ||
128 TheCondState.Ignore != StartingCondState.Ignore)
129 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000130
Chris Lattner79180e22010-04-05 23:15:42 +0000131 // Finalize the output stream if there are no errors and if the client wants
132 // us to.
133 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000134 Out.Finish();
135
Chris Lattnerb717fb02009-07-02 21:53:43 +0000136 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000137}
138
Chris Lattner2cf5f142009-06-22 01:29:09 +0000139/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
140void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000141 while (Lexer.isNot(AsmToken::EndOfStatement) &&
142 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000143 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000144
145 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000146 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000147 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000148}
149
Chris Lattnerc4193832009-06-22 05:51:26 +0000150
Chris Lattner74ec1a32009-06-22 06:32:03 +0000151/// ParseParenExpr - Parse a paren expression and return it.
152/// NOTE: This assumes the leading '(' has already been consumed.
153///
154/// parenexpr ::= expr)
155///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000156bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000157 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000158 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000159 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000160 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000161 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000162 return false;
163}
Chris Lattnerc4193832009-06-22 05:51:26 +0000164
Daniel Dunbar959fd882009-08-26 22:13:22 +0000165MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000166 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000167 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000168}
169
Chris Lattner74ec1a32009-06-22 06:32:03 +0000170/// ParsePrimaryExpr - Parse a primary expression and return it.
171/// primaryexpr ::= (parenexpr
172/// primaryexpr ::= symbol
173/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000174/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000175/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000176bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000177 switch (Lexer.getKind()) {
178 default:
179 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000180 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000181 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000182 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000183 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000184 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000185 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000186 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000187 case AsmToken::Identifier: {
188 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000189 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
190 MCSymbol *Sym = CreateSymbol(Split.first);
191
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000192 // Mark the symbol as used in an expression.
193 Sym->setUsedInExpr(true);
194
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000195 // Lookup the symbol variant if used.
196 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
197 if (Split.first.size() != getTok().getIdentifier().size())
198 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
199
Chris Lattnerb4307b32010-01-15 19:28:38 +0000200 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000201 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000202
203 // If this is an absolute variable reference, substitute it now to preserve
204 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000205 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000206 if (Variant)
207 return Error(EndLoc, "unexpected modified on variable reference");
208
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000209 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000210 return false;
211 }
212
213 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000214 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000215 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000216 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000217 case AsmToken::Integer: {
218 SMLoc Loc = getTok().getLoc();
219 int64_t IntVal = getTok().getIntVal();
220 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000221 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000222 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000223 // Look for 'b' or 'f' following an Integer as a directional label
224 if (Lexer.getKind() == AsmToken::Identifier) {
225 StringRef IDVal = getTok().getString();
226 if (IDVal == "f" || IDVal == "b"){
227 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
228 IDVal == "f" ? 1 : 0);
229 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
230 getContext());
231 if(IDVal == "b" && Sym->isUndefined())
232 return Error(Loc, "invalid reference to undefined symbol");
233 EndLoc = Lexer.getLoc();
234 Lex(); // Eat identifier.
235 }
236 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000237 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000238 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000239 case AsmToken::Dot: {
240 // This is a '.' reference, which references the current PC. Emit a
241 // temporary label to the streamer and refer to it.
242 MCSymbol *Sym = Ctx.CreateTempSymbol();
243 Out.EmitLabel(Sym);
244 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
245 EndLoc = Lexer.getLoc();
246 Lex(); // Eat identifier.
247 return false;
248 }
249
Daniel Dunbar3f872332009-07-28 16:08:33 +0000250 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000251 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000252 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000253 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000254 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000255 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000257 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000259 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000260 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000261 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000263 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000264 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000265 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000266 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000267 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000269 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000271 }
272}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000273
Chris Lattnerb4307b32010-01-15 19:28:38 +0000274bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000275 SMLoc EndLoc;
276 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000277}
278
Chris Lattner74ec1a32009-06-22 06:32:03 +0000279/// ParseExpression - Parse an expression and return it.
280///
281/// expr ::= expr +,- expr -> lowest.
282/// expr ::= expr |,^,&,! expr -> middle.
283/// expr ::= expr *,/,%,<<,>> expr -> highest.
284/// expr ::= primaryexpr
285///
Chris Lattner54482b42010-01-15 19:39:23 +0000286bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000287 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000288 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000289 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
290 return true;
291
292 // Try to constant fold it up front, if possible.
293 int64_t Value;
294 if (Res->EvaluateAsAbsolute(Value))
295 Res = MCConstantExpr::Create(Value, getContext());
296
297 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000298}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000299
Chris Lattnerb4307b32010-01-15 19:28:38 +0000300bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000301 Res = 0;
302 return ParseParenExpr(Res, EndLoc) ||
303 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000304}
305
Daniel Dunbar475839e2009-06-29 20:37:27 +0000306bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000307 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000308
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000309 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310 if (ParseExpression(Expr))
311 return true;
312
Daniel Dunbare00b0112009-10-16 01:57:52 +0000313 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000314 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315
316 return false;
317}
318
Daniel Dunbar3f872332009-07-28 16:08:33 +0000319static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000320 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000321 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000322 default:
323 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000324
325 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000327 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000328 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 return 1;
332
333 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000334 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000335 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000336 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000337 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000338 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000339 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000340 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000341 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::ExclaimEqual:
344 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000345 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000346 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000347 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000348 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000349 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000350 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000351 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000353 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000354 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000355 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000356 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000357 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358 return 2;
359
360 // Intermediate Precedence: |, &, ^
361 //
362 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000365 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 3;
372
373 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000374 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000375 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000377 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000378 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000379 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000383 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000384 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000386 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000387 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000389 }
390}
391
392
393/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
394/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000395bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
396 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000397 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000398 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000399 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000400
401 // If the next token is lower precedence than we are allowed to eat, return
402 // successfully with what we ate already.
403 if (TokPrec < Precedence)
404 return false;
405
Sean Callanan79ed1a82010-01-19 20:22:31 +0000406 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000407
408 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000409 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000410 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000411
412 // If BinOp binds less tightly with RHS than the operator after RHS, let
413 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000414 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000415 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000416 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000417 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000418 }
419
Daniel Dunbar475839e2009-06-29 20:37:27 +0000420 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000421 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000422 }
423}
424
Chris Lattnerc4193832009-06-22 05:51:26 +0000425
426
427
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000428/// ParseStatement:
429/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000430/// ::= Label* Directive ...Operands... EndOfStatement
431/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000432bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000433 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000434 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000435 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000436 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000437 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000438
439 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000440 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000441 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000442 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000443 int64_t LocalLabelVal = -1;
444 // GUESS allow an integer followed by a ':' as a directional local label
445 if (Lexer.is(AsmToken::Integer)) {
446 LocalLabelVal = getTok().getIntVal();
447 if (LocalLabelVal < 0) {
448 if (!TheCondState.Ignore)
449 return TokError("unexpected token at start of statement");
450 IDVal = "";
451 }
452 else {
453 IDVal = getTok().getString();
454 Lex(); // Consume the integer token to be used as an identifier token.
455 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000456 if (!TheCondState.Ignore)
457 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000458 }
459 }
460 }
461 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000462 if (!TheCondState.Ignore)
463 return TokError("unexpected token at start of statement");
464 IDVal = "";
465 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000466
Chris Lattner7834fac2010-04-17 18:14:27 +0000467 // Handle conditional assembly here before checking for skipping. We
468 // have to do this so that .endif isn't skipped in a ".if 0" block for
469 // example.
470 if (IDVal == ".if")
471 return ParseDirectiveIf(IDLoc);
472 if (IDVal == ".elseif")
473 return ParseDirectiveElseIf(IDLoc);
474 if (IDVal == ".else")
475 return ParseDirectiveElse(IDLoc);
476 if (IDVal == ".endif")
477 return ParseDirectiveEndIf(IDLoc);
478
479 // If we are in a ".if 0" block, ignore this statement.
480 if (TheCondState.Ignore) {
481 EatToEndOfStatement();
482 return false;
483 }
484
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000485 // FIXME: Recurse on local labels?
486
487 // See what kind of statement we have.
488 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000489 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000490 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000491 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000492
493 // Diagnose attempt to use a variable as a label.
494 //
495 // FIXME: Diagnostics. Note the location of the definition as a label.
496 // FIXME: This doesn't diagnose assignment to a symbol which has been
497 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000498 MCSymbol *Sym;
499 if (LocalLabelVal == -1)
500 Sym = CreateSymbol(IDVal);
501 else
502 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000503 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000504 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000505
Daniel Dunbar959fd882009-08-26 22:13:22 +0000506 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000507 Out.EmitLabel(Sym);
508
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000509 // Consume any end of statement token, if present, to avoid spurious
510 // AddBlankLine calls().
511 if (Lexer.is(AsmToken::EndOfStatement)) {
512 Lex();
513 if (Lexer.is(AsmToken::Eof))
514 return false;
515 }
516
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000517 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000518 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000519
Daniel Dunbar3f872332009-07-28 16:08:33 +0000520 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000521 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000522 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000523
Daniel Dunbare2ace502009-08-31 08:09:09 +0000524 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000525
526 default: // Normal instruction or directive.
527 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000528 }
529
530 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000531 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000532 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000534 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000536 // FIXME: This changes behavior based on the -static flag to the
537 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000538 return ParseDirectiveSectionSwitch("__TEXT", "__text",
539 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000541 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000545 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
546 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000548 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 MCSectionMachO::S_4BYTE_LITERALS,
550 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000552 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000553 MCSectionMachO::S_8BYTE_LITERALS,
554 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000556 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 MCSectionMachO::S_16BYTE_LITERALS,
558 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000559 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000560 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000562 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000564 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000565 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000566 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
567
568 // FIXME: The assembler manual claims that this has the self modify code
569 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000570 if (IDVal == ".symbol_stub")
571 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
572 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000573 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
574 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000575 0, 16);
576 // FIXME: PowerPC only?
577 if (IDVal == ".picsymbol_stub")
578 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
579 MCSectionMachO::S_SYMBOL_STUBS |
580 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
581 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
586
587 // FIXME: The section names of these two are misspelled in the assembler
588 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000590 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
591 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
592 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
595 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
596 4);
597
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000601 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000602 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
603 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000605 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000606 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
607 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000608 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000609 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000610
611
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__OBJC", "__class",
614 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000616 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
617 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000619 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
620 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000622 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
623 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000625 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
626 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000628 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
629 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000631 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
632 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000634 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
635 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000637 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
638 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
639 MCSectionMachO::S_LITERAL_POINTERS,
640 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000642 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
643 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
644 MCSectionMachO::S_LITERAL_POINTERS,
645 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000647 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
648 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000650 return ParseDirectiveSectionSwitch("__OBJC", "__category",
651 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000653 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
654 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000656 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
657 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000659 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
660 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000662 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
663 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000665 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
666 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000668 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
669 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000671 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
672 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000673
Eric Christopherc6177a42010-05-17 22:53:55 +0000674 if (IDVal == ".tdata")
675 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
676 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
677 if (IDVal == ".tlv")
678 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
679 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
680 if (IDVal == ".thread_init_func")
681 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
682 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
683
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000684 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000686 return ParseDirectiveSet();
687
Daniel Dunbara0d14262009-06-24 23:30:00 +0000688 // Data directives
689
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000691 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000693 return ParseDirectiveAscii(true);
694
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000696 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000697 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000698 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000700 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000702 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000703
704 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000706 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000708 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000710 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000712 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000714 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000716 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000718 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000720 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
721
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000723 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000724
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000726 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000728 return ParseDirectiveSpace();
729
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000730 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000731
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000732 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000733 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000735 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000737 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000738 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000739 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000741 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000743 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000745 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000747 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000749 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000750 if (IDVal == ".type")
751 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000752 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000753 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000754 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000755 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000756 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000757 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000758 if (IDVal == ".weak_def_can_be_hidden")
759 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000760
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000761 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000762 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000763 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000764 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000765 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000766 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000767 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000768 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000770 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000771 if (IDVal == ".tbss")
772 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000773
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000774 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000775 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000776 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000777 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000778 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000779 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000780 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000781 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000782 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000783 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbyf187ac52010-06-28 21:45:58 +0000784 if (IDVal == ".secure_log_unique")
785 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
786 if (IDVal == ".secure_log_reset")
787 return ParseDirectiveDarwinSecureLogReset(IDLoc);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000788
Chris Lattnerebb89b42009-09-27 21:16:52 +0000789 // Look up the handler in the handler table,
790 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
791 if (Handler)
792 return (this->*Handler)(IDVal, IDLoc);
793
Kevin Enderby9c656452009-09-10 20:51:44 +0000794 // Target hook for parsing target specific directives.
795 if (!getTargetParser().ParseDirective(ID))
796 return false;
797
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000798 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000799 EatToEndOfStatement();
800 return false;
801 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000802
Chris Lattnera7f13542010-05-19 23:34:33 +0000803 // Canonicalize the opcode to lower case.
804 SmallString<128> Opcode;
805 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
806 Opcode.push_back(tolower(IDVal[i]));
807
Chris Lattner98986712010-01-14 22:21:20 +0000808 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000809 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000810 ParsedOperands);
811 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
812 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000813
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000814 // If parsing succeeded, match the instruction.
815 if (!HadError) {
816 MCInst Inst;
817 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
818 // Emit the instruction on success.
819 Out.EmitInstruction(Inst);
820 } else {
821 // Otherwise emit a diagnostic about the match failure and set the error
822 // flag.
823 //
824 // FIXME: We should give nicer diagnostics about the exact failure.
825 Error(IDLoc, "unrecognized instruction");
826 HadError = true;
827 }
828 }
Chris Lattner98986712010-01-14 22:21:20 +0000829
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000830 // If there was no error, consume the end-of-statement token. Otherwise this
831 // will be done by our caller.
832 if (!HadError)
833 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000834
835 // Free any parsed operands.
836 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
837 delete ParsedOperands[i];
838
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000839 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000840}
Chris Lattner9a023f72009-06-24 04:43:34 +0000841
Daniel Dunbare2ace502009-08-31 08:09:09 +0000842bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000843 // FIXME: Use better location, we should use proper tokens.
844 SMLoc EqualLoc = Lexer.getLoc();
845
Daniel Dunbar821e3332009-08-31 08:09:28 +0000846 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000847 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000848 return true;
849
Daniel Dunbar3f872332009-07-28 16:08:33 +0000850 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000851 return TokError("unexpected token in assignment");
852
853 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000854 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000855
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000856 // Validate that the LHS is allowed to be a variable (either it has not been
857 // used as a symbol, or it is an absolute symbol).
858 MCSymbol *Sym = getContext().LookupSymbol(Name);
859 if (Sym) {
860 // Diagnose assignment to a label.
861 //
862 // FIXME: Diagnostics. Note the location of the definition as a label.
863 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000864 if (Sym->isUndefined() && !Sym->isUsedInExpr())
865 ; // Allow redefinitions of undefined symbols only used in directives.
866 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000867 return Error(EqualLoc, "redefinition of '" + Name + "'");
868 else if (!Sym->isVariable())
869 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000870 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000871 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
872 Name + "'");
873 } else
874 Sym = CreateSymbol(Name);
875
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000876 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000877
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000878 Sym->setUsedInExpr(true);
879
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000880 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000881 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000882
883 return false;
884}
885
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000886/// ParseIdentifier:
887/// ::= identifier
888/// ::= string
889bool AsmParser::ParseIdentifier(StringRef &Res) {
890 if (Lexer.isNot(AsmToken::Identifier) &&
891 Lexer.isNot(AsmToken::String))
892 return true;
893
Sean Callanan18b83232010-01-19 21:44:56 +0000894 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000895
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000897
898 return false;
899}
900
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901/// ParseDirectiveSet:
902/// ::= .set identifier ',' expression
903bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000904 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000905
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000906 if (ParseIdentifier(Name))
907 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000908
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000909 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000910 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000911 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000912
Daniel Dunbare2ace502009-08-31 08:09:09 +0000913 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000914}
915
Chris Lattner9a023f72009-06-24 04:43:34 +0000916/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000917/// ::= .section identifier (',' identifier)*
918/// FIXME: This should actually parse out the segment, section, attributes and
919/// sizeof_stub fields.
920bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000921 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000922
Daniel Dunbarace63122009-08-11 03:42:33 +0000923 StringRef SectionName;
924 if (ParseIdentifier(SectionName))
925 return Error(Loc, "expected identifier after '.section' directive");
926
927 // Verify there is a following comma.
928 if (!Lexer.is(AsmToken::Comma))
929 return TokError("unexpected token in '.section' directive");
930
Chris Lattnerff4bc462009-08-10 01:39:42 +0000931 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000932 SectionSpec += ",";
933
934 // Add all the tokens until the end of the line, ParseSectionSpecifier will
935 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000936 StringRef EOL = Lexer.LexUntilEndOfStatement();
937 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000938
Sean Callanan79ed1a82010-01-19 20:22:31 +0000939 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000941 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000942 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000943
Chris Lattnerff4bc462009-08-10 01:39:42 +0000944
945 StringRef Segment, Section;
946 unsigned TAA, StubSize;
947 std::string ErrorStr =
948 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
949 TAA, StubSize);
950
951 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000952 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000953
Chris Lattner56594f92009-07-31 17:47:16 +0000954 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000955 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000956 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
957 isText ? SectionKind::getText()
958 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000959 return false;
960}
961
Chris Lattnere15c2d72009-08-10 18:05:55 +0000962/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000963bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
964 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000965 unsigned TAA, unsigned Align,
966 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000967 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000968 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000969 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000970
Chris Lattner56594f92009-07-31 17:47:16 +0000971 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000972 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000973 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
974 isText ? SectionKind::getText()
975 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000976
977 // Set the implicit alignment, if any.
978 //
979 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
980 // alignment on the section (e.g., if one manually inserts bytes into the
981 // section, then just issueing the section switch directive will not realign
982 // the section. However, this is arguably more reasonable behavior, and there
983 // is no good reason for someone to intentionally emit incorrectly sized
984 // values into the implicitly aligned sections.
985 if (Align)
986 Out.EmitValueToAlignment(Align, 0, 1, 0);
987
Chris Lattner529fb542009-06-24 05:13:15 +0000988 return false;
989}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000990
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000991bool AsmParser::ParseEscapedString(std::string &Data) {
992 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
993
994 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000995 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000996 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
997 if (Str[i] != '\\') {
998 Data += Str[i];
999 continue;
1000 }
1001
1002 // Recognize escaped characters. Note that this escape semantics currently
1003 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1004 ++i;
1005 if (i == e)
1006 return TokError("unexpected backslash at end of string");
1007
1008 // Recognize octal sequences.
1009 if ((unsigned) (Str[i] - '0') <= 7) {
1010 // Consume up to three octal characters.
1011 unsigned Value = Str[i] - '0';
1012
1013 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1014 ++i;
1015 Value = Value * 8 + (Str[i] - '0');
1016
1017 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1018 ++i;
1019 Value = Value * 8 + (Str[i] - '0');
1020 }
1021 }
1022
1023 if (Value > 255)
1024 return TokError("invalid octal escape sequence (out of range)");
1025
1026 Data += (unsigned char) Value;
1027 continue;
1028 }
1029
1030 // Otherwise recognize individual escapes.
1031 switch (Str[i]) {
1032 default:
1033 // Just reject invalid escape sequences for now.
1034 return TokError("invalid escape sequence (unrecognized character)");
1035
1036 case 'b': Data += '\b'; break;
1037 case 'f': Data += '\f'; break;
1038 case 'n': Data += '\n'; break;
1039 case 'r': Data += '\r'; break;
1040 case 't': Data += '\t'; break;
1041 case '"': Data += '"'; break;
1042 case '\\': Data += '\\'; break;
1043 }
1044 }
1045
1046 return false;
1047}
1048
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001050/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001052 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001053 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 return TokError("expected string in '.ascii' or '.asciz' directive");
1056
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001057 std::string Data;
1058 if (ParseEscapedString(Data))
1059 return true;
1060
Chris Lattneraaec2052010-01-19 19:46:13 +00001061 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001063 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064
Sean Callanan79ed1a82010-01-19 20:22:31 +00001065 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001066
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 break;
1069
Daniel Dunbar3f872332009-07-28 16:08:33 +00001070 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001072 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 }
1074 }
1075
Sean Callanan79ed1a82010-01-19 20:22:31 +00001076 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return false;
1078}
1079
1080/// ParseDirectiveValue
1081/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1082bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001083 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001085 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001086 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001087 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088 return true;
1089
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001090 // Special case constant expressions to match code generator.
1091 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1092 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1093 else
1094 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001095
Daniel Dunbar3f872332009-07-28 16:08:33 +00001096 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001097 break;
1098
1099 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001100 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001102 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001103 }
1104 }
1105
Sean Callanan79ed1a82010-01-19 20:22:31 +00001106 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001107 return false;
1108}
1109
1110/// ParseDirectiveSpace
1111/// ::= .space expression [ , expression ]
1112bool AsmParser::ParseDirectiveSpace() {
1113 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001114 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115 return true;
1116
1117 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001118 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1119 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001120 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001121 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122
Daniel Dunbar475839e2009-06-29 20:37:27 +00001123 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124 return true;
1125
Daniel Dunbar3f872332009-07-28 16:08:33 +00001126 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127 return TokError("unexpected token in '.space' directive");
1128 }
1129
Sean Callanan79ed1a82010-01-19 20:22:31 +00001130 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131
1132 if (NumBytes <= 0)
1133 return TokError("invalid number of bytes in '.space' directive");
1134
1135 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001136 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001137
1138 return false;
1139}
1140
1141/// ParseDirectiveFill
1142/// ::= .fill expression , expression , expression
1143bool AsmParser::ParseDirectiveFill() {
1144 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001145 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146 return true;
1147
Daniel Dunbar3f872332009-07-28 16:08:33 +00001148 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001149 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001150 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001151
1152 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001153 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154 return true;
1155
Daniel Dunbar3f872332009-07-28 16:08:33 +00001156 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001157 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001158 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001159
1160 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001161 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001162 return true;
1163
Daniel Dunbar3f872332009-07-28 16:08:33 +00001164 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001165 return TokError("unexpected token in '.fill' directive");
1166
Sean Callanan79ed1a82010-01-19 20:22:31 +00001167 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001168
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001169 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1170 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001171
1172 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001173 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001174
1175 return false;
1176}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001177
1178/// ParseDirectiveOrg
1179/// ::= .org expression [ , expression ]
1180bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001181 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001182 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001183 return true;
1184
1185 // Parse optional fill expression.
1186 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1188 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001189 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001190 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001191
Daniel Dunbar475839e2009-06-29 20:37:27 +00001192 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001193 return true;
1194
Daniel Dunbar3f872332009-07-28 16:08:33 +00001195 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001196 return TokError("unexpected token in '.org' directive");
1197 }
1198
Sean Callanan79ed1a82010-01-19 20:22:31 +00001199 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001200
1201 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1202 // has to be relative to the current section.
1203 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001204
1205 return false;
1206}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207
1208/// ParseDirectiveAlign
1209/// ::= {.align, ...} expression [ , expression [ , expression ]]
1210bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001211 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001212 int64_t Alignment;
1213 if (ParseAbsoluteExpression(Alignment))
1214 return true;
1215
1216 SMLoc MaxBytesLoc;
1217 bool HasFillExpr = false;
1218 int64_t FillExpr = 0;
1219 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1221 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001222 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001223 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224
1225 // The fill expression can be omitted while specifying a maximum number of
1226 // alignment bytes, e.g:
1227 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001228 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 HasFillExpr = true;
1230 if (ParseAbsoluteExpression(FillExpr))
1231 return true;
1232 }
1233
Daniel Dunbar3f872332009-07-28 16:08:33 +00001234 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1235 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001237 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001238
1239 MaxBytesLoc = Lexer.getLoc();
1240 if (ParseAbsoluteExpression(MaxBytesToFill))
1241 return true;
1242
Daniel Dunbar3f872332009-07-28 16:08:33 +00001243 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001244 return TokError("unexpected token in directive");
1245 }
1246 }
1247
Sean Callanan79ed1a82010-01-19 20:22:31 +00001248 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001249
Daniel Dunbar648ac512010-05-17 21:54:30 +00001250 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001251 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001252
1253 // Compute alignment in bytes.
1254 if (IsPow2) {
1255 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001256 if (Alignment >= 32) {
1257 Error(AlignmentLoc, "invalid alignment value");
1258 Alignment = 31;
1259 }
1260
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001261 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001262 }
1263
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001264 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001265 if (MaxBytesLoc.isValid()) {
1266 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001267 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1268 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001269 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001270 }
1271
1272 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001273 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1274 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001275 MaxBytesToFill = 0;
1276 }
1277 }
1278
Daniel Dunbar648ac512010-05-17 21:54:30 +00001279 // Check whether we should use optimal code alignment for this .align
1280 // directive.
1281 //
1282 // FIXME: This should be using a target hook.
1283 bool UseCodeAlign = false;
1284 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1285 Out.getCurrentSection()))
1286 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1287 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1288 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001289 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001290 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001291 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1292 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001293 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001294
1295 return false;
1296}
1297
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001298/// ParseDirectiveSymbolAttribute
1299/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001300bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001301 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001302 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001303 StringRef Name;
1304
1305 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001306 return TokError("expected identifier in directive");
1307
Daniel Dunbar959fd882009-08-26 22:13:22 +00001308 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001309
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001310 Out.EmitSymbolAttribute(Sym, Attr);
1311
Daniel Dunbar3f872332009-07-28 16:08:33 +00001312 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001313 break;
1314
Daniel Dunbar3f872332009-07-28 16:08:33 +00001315 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001316 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001317 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001318 }
1319 }
1320
Sean Callanan79ed1a82010-01-19 20:22:31 +00001321 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001322 return false;
1323}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001324
Matt Fleming924c5e52010-05-21 11:36:59 +00001325/// ParseDirectiveELFType
1326/// ::= .type identifier , @attribute
1327bool AsmParser::ParseDirectiveELFType() {
1328 StringRef Name;
1329 if (ParseIdentifier(Name))
1330 return TokError("expected identifier in directive");
1331
1332 // Handle the identifier as the key symbol.
1333 MCSymbol *Sym = CreateSymbol(Name);
1334
1335 if (Lexer.isNot(AsmToken::Comma))
1336 return TokError("unexpected token in '.type' directive");
1337 Lex();
1338
1339 if (Lexer.isNot(AsmToken::At))
1340 return TokError("expected '@' before type");
1341 Lex();
1342
1343 StringRef Type;
1344 SMLoc TypeLoc;
1345
1346 TypeLoc = Lexer.getLoc();
1347 if (ParseIdentifier(Type))
1348 return TokError("expected symbol type in directive");
1349
1350 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1351 .Case("function", MCSA_ELF_TypeFunction)
1352 .Case("object", MCSA_ELF_TypeObject)
1353 .Case("tls_object", MCSA_ELF_TypeTLS)
1354 .Case("common", MCSA_ELF_TypeCommon)
1355 .Case("notype", MCSA_ELF_TypeNoType)
1356 .Default(MCSA_Invalid);
1357
1358 if (Attr == MCSA_Invalid)
1359 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1360
1361 if (Lexer.isNot(AsmToken::EndOfStatement))
1362 return TokError("unexpected token in '.type' directive");
1363
1364 Lex();
1365
1366 Out.EmitSymbolAttribute(Sym, Attr);
1367
1368 return false;
1369}
1370
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001371/// ParseDirectiveDarwinSymbolDesc
1372/// ::= .desc identifier , expression
1373bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001374 StringRef Name;
1375 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001376 return TokError("expected identifier in directive");
1377
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001378 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001379 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001380
Daniel Dunbar3f872332009-07-28 16:08:33 +00001381 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001382 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001383 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001384
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001385 int64_t DescValue;
1386 if (ParseAbsoluteExpression(DescValue))
1387 return true;
1388
Daniel Dunbar3f872332009-07-28 16:08:33 +00001389 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001390 return TokError("unexpected token in '.desc' directive");
1391
Sean Callanan79ed1a82010-01-19 20:22:31 +00001392 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001393
1394 // Set the n_desc field of this Symbol to this DescValue
1395 Out.EmitSymbolDesc(Sym, DescValue);
1396
1397 return false;
1398}
1399
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001400/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001401/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1402bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001403 SMLoc IDLoc = Lexer.getLoc();
1404 StringRef Name;
1405 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001406 return TokError("expected identifier in directive");
1407
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001408 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001409 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001410
Daniel Dunbar3f872332009-07-28 16:08:33 +00001411 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001412 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001414
1415 int64_t Size;
1416 SMLoc SizeLoc = Lexer.getLoc();
1417 if (ParseAbsoluteExpression(Size))
1418 return true;
1419
1420 int64_t Pow2Alignment = 0;
1421 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001422 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001423 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001424 Pow2AlignmentLoc = Lexer.getLoc();
1425 if (ParseAbsoluteExpression(Pow2Alignment))
1426 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001427
1428 // If this target takes alignments in bytes (not log) validate and convert.
1429 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1430 if (!isPowerOf2_64(Pow2Alignment))
1431 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1432 Pow2Alignment = Log2_64(Pow2Alignment);
1433 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001434 }
1435
Daniel Dunbar3f872332009-07-28 16:08:33 +00001436 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001437 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001438
Sean Callanan79ed1a82010-01-19 20:22:31 +00001439 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001440
Chris Lattner1fc3d752009-07-09 17:25:12 +00001441 // NOTE: a size of zero for a .comm should create a undefined symbol
1442 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001443 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001444 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1445 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001446
Eric Christopherc260a3e2010-05-14 01:38:54 +00001447 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001448 // may internally end up wanting an alignment in bytes.
1449 // FIXME: Diagnose overflow.
1450 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001451 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1452 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001453
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001454 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001455 return Error(IDLoc, "invalid symbol redefinition");
1456
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001457 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001458 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001459 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001460 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1461 MCSectionMachO::S_ZEROFILL, 0,
1462 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001463 Sym, Size, 1 << Pow2Alignment);
1464 return false;
1465 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001466
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001467 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001468 return false;
1469}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001470
1471/// ParseDirectiveDarwinZerofill
1472/// ::= .zerofill segname , sectname [, identifier , size_expression [
1473/// , align_expression ]]
1474bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001475 StringRef Segment;
1476 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001477 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001478
Daniel Dunbar3f872332009-07-28 16:08:33 +00001479 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001480 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001481 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001482
1483 StringRef Section;
1484 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001485 return TokError("expected section name after comma in '.zerofill' "
1486 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001487
Chris Lattner9be3fee2009-07-10 22:20:30 +00001488 // If this is the end of the line all that was wanted was to create the
1489 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001490 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001491 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001492 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1493 MCSectionMachO::S_ZEROFILL, 0,
1494 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001495 return false;
1496 }
1497
Daniel Dunbar3f872332009-07-28 16:08:33 +00001498 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001499 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001500 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001501
Chris Lattner7bb7c552010-05-13 00:10:34 +00001502 SMLoc IDLoc = Lexer.getLoc();
1503 StringRef IDStr;
1504 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001505 return TokError("expected identifier in directive");
1506
1507 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001508 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001509
Daniel Dunbar3f872332009-07-28 16:08:33 +00001510 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001511 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001512 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001513
1514 int64_t Size;
1515 SMLoc SizeLoc = Lexer.getLoc();
1516 if (ParseAbsoluteExpression(Size))
1517 return true;
1518
1519 int64_t Pow2Alignment = 0;
1520 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001521 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001522 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001523 Pow2AlignmentLoc = Lexer.getLoc();
1524 if (ParseAbsoluteExpression(Pow2Alignment))
1525 return true;
1526 }
1527
Daniel Dunbar3f872332009-07-28 16:08:33 +00001528 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001529 return TokError("unexpected token in '.zerofill' directive");
1530
Sean Callanan79ed1a82010-01-19 20:22:31 +00001531 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001532
1533 if (Size < 0)
1534 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1535 "than zero");
1536
Eric Christopherc260a3e2010-05-14 01:38:54 +00001537 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001538 // may internally end up wanting an alignment in bytes.
1539 // FIXME: Diagnose overflow.
1540 if (Pow2Alignment < 0)
1541 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1542 "can't be less than zero");
1543
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001544 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001545 return Error(IDLoc, "invalid symbol redefinition");
1546
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001547 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001548 //
1549 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001550 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1551 MCSectionMachO::S_ZEROFILL, 0,
1552 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001553 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001554
1555 return false;
1556}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001557
Eric Christopher482eba02010-05-14 01:50:28 +00001558/// ParseDirectiveDarwinTBSS
1559/// ::= .tbss identifier, size, align
1560bool AsmParser::ParseDirectiveDarwinTBSS() {
1561 SMLoc IDLoc = Lexer.getLoc();
1562 StringRef Name;
1563 if (ParseIdentifier(Name))
1564 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001565
Eric Christopher482eba02010-05-14 01:50:28 +00001566 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001567 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001568
1569 if (Lexer.isNot(AsmToken::Comma))
1570 return TokError("unexpected token in directive");
1571 Lex();
1572
1573 int64_t Size;
1574 SMLoc SizeLoc = Lexer.getLoc();
1575 if (ParseAbsoluteExpression(Size))
1576 return true;
1577
1578 int64_t Pow2Alignment = 0;
1579 SMLoc Pow2AlignmentLoc;
1580 if (Lexer.is(AsmToken::Comma)) {
1581 Lex();
1582 Pow2AlignmentLoc = Lexer.getLoc();
1583 if (ParseAbsoluteExpression(Pow2Alignment))
1584 return true;
1585 }
1586
1587 if (Lexer.isNot(AsmToken::EndOfStatement))
1588 return TokError("unexpected token in '.tbss' directive");
1589
1590 Lex();
1591
1592 if (Size < 0)
1593 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1594 "zero");
1595
1596 // FIXME: Diagnose overflow.
1597 if (Pow2Alignment < 0)
1598 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1599 "than zero");
1600
1601 if (!Sym->isUndefined())
1602 return Error(IDLoc, "invalid symbol redefinition");
1603
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001604 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1605 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1606 0, SectionKind::getThreadBSS()),
1607 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001608
1609 return false;
1610}
1611
Kevin Enderbya5c78322009-07-13 21:03:15 +00001612/// ParseDirectiveDarwinSubsectionsViaSymbols
1613/// ::= .subsections_via_symbols
1614bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001615 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001616 return TokError("unexpected token in '.subsections_via_symbols' directive");
1617
Sean Callanan79ed1a82010-01-19 20:22:31 +00001618 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001619
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001620 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001621
1622 return false;
1623}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001624
1625/// ParseDirectiveAbort
1626/// ::= .abort [ "abort_string" ]
1627bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001628 // FIXME: Use loc from directive.
1629 SMLoc Loc = Lexer.getLoc();
1630
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001631 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001632 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1633 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001634 return TokError("expected string in '.abort' directive");
1635
Sean Callanan18b83232010-01-19 21:44:56 +00001636 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001637
Sean Callanan79ed1a82010-01-19 20:22:31 +00001638 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001639 }
1640
Daniel Dunbar3f872332009-07-28 16:08:33 +00001641 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001642 return TokError("unexpected token in '.abort' directive");
1643
Sean Callanan79ed1a82010-01-19 20:22:31 +00001644 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001645
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001646 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001647 if (Str.empty())
1648 Error(Loc, ".abort detected. Assembly stopping.");
1649 else
1650 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001651
1652 return false;
1653}
Kevin Enderby71148242009-07-14 21:35:03 +00001654
1655/// ParseDirectiveLsym
1656/// ::= .lsym identifier , expression
1657bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001658 StringRef Name;
1659 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001660 return TokError("expected identifier in directive");
1661
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001662 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001663 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001664
Daniel Dunbar3f872332009-07-28 16:08:33 +00001665 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001666 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001667 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001668
Daniel Dunbar821e3332009-08-31 08:09:28 +00001669 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001670 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001671 return true;
1672
Daniel Dunbar3f872332009-07-28 16:08:33 +00001673 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001674 return TokError("unexpected token in '.lsym' directive");
1675
Sean Callanan79ed1a82010-01-19 20:22:31 +00001676 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001677
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001678 // We don't currently support this directive.
1679 //
1680 // FIXME: Diagnostic location!
1681 (void) Sym;
1682 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001683}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001684
1685/// ParseDirectiveInclude
1686/// ::= .include "filename"
1687bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001688 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001689 return TokError("expected string in '.include' directive");
1690
Sean Callanan18b83232010-01-19 21:44:56 +00001691 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001692 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001694
Daniel Dunbar3f872332009-07-28 16:08:33 +00001695 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001696 return TokError("unexpected token in '.include' directive");
1697
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001698 // Strip the quotes.
1699 Filename = Filename.substr(1, Filename.size()-2);
1700
1701 // Attempt to switch the lexer to the included file before consuming the end
1702 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001703 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001704 PrintMessage(IncludeLoc,
1705 "Could not find include file '" + Filename + "'",
1706 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001707 return true;
1708 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001709
1710 return false;
1711}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001712
1713/// ParseDirectiveDarwinDumpOrLoad
1714/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001715bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001716 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001717 return TokError("expected string in '.dump' or '.load' directive");
1718
Sean Callanan79ed1a82010-01-19 20:22:31 +00001719 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001720
Daniel Dunbar3f872332009-07-28 16:08:33 +00001721 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001722 return TokError("unexpected token in '.dump' or '.load' directive");
1723
Sean Callanan79ed1a82010-01-19 20:22:31 +00001724 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001725
Kevin Enderby5026ae42009-07-20 20:25:37 +00001726 // FIXME: If/when .dump and .load are implemented they will be done in the
1727 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001728 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001729 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001730 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001731 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001732
1733 return false;
1734}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001735
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001736/// ParseDirectiveDarwinSecureLogUnique
1737/// ::= .secure_log_unique "log message"
1738bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1739 std::string LogMessage;
1740
1741 if (Lexer.isNot(AsmToken::String))
1742 LogMessage = "";
1743 else{
1744 LogMessage = getTok().getString();
1745 Lex();
1746 }
1747
1748 if (Lexer.isNot(AsmToken::EndOfStatement))
1749 return TokError("unexpected token in '.secure_log_unique' directive");
1750
1751 if (getContext().getSecureLogUsed() != false)
1752 return Error(IDLoc, ".secure_log_unique specified multiple times");
1753
1754 char *SecureLogFile = getContext().getSecureLogFile();
1755 if (SecureLogFile == NULL)
1756 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1757 "environment variable unset.");
1758
1759 raw_ostream *OS = getContext().getSecureLog();
1760 if (OS == NULL) {
1761 std::string Err;
1762 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1763 if (!Err.empty()) {
1764 delete OS;
1765 return Error(IDLoc, Twine("can't open secure log file: ") +
1766 SecureLogFile + " (" + Err + ")");
1767 }
1768 getContext().setSecureLog(OS);
1769 }
1770
1771 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1772 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1773 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1774 << LogMessage + "\n";
1775
1776 getContext().setSecureLogUsed(true);
1777
1778 return false;
1779}
1780
1781/// ParseDirectiveDarwinSecureLogReset
1782/// ::= .secure_log_reset
1783bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1784 if (Lexer.isNot(AsmToken::EndOfStatement))
1785 return TokError("unexpected token in '.secure_log_reset' directive");
1786
1787 Lex();
1788
1789 getContext().setSecureLogUsed(false);
1790
1791 return false;
1792}
1793
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001794/// ParseDirectiveIf
1795/// ::= .if expression
1796bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001797 TheCondStack.push_back(TheCondState);
1798 TheCondState.TheCond = AsmCond::IfCond;
1799 if(TheCondState.Ignore) {
1800 EatToEndOfStatement();
1801 }
1802 else {
1803 int64_t ExprValue;
1804 if (ParseAbsoluteExpression(ExprValue))
1805 return true;
1806
1807 if (Lexer.isNot(AsmToken::EndOfStatement))
1808 return TokError("unexpected token in '.if' directive");
1809
Sean Callanan79ed1a82010-01-19 20:22:31 +00001810 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001811
1812 TheCondState.CondMet = ExprValue;
1813 TheCondState.Ignore = !TheCondState.CondMet;
1814 }
1815
1816 return false;
1817}
1818
1819/// ParseDirectiveElseIf
1820/// ::= .elseif expression
1821bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1822 if (TheCondState.TheCond != AsmCond::IfCond &&
1823 TheCondState.TheCond != AsmCond::ElseIfCond)
1824 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1825 " an .elseif");
1826 TheCondState.TheCond = AsmCond::ElseIfCond;
1827
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001828 bool LastIgnoreState = false;
1829 if (!TheCondStack.empty())
1830 LastIgnoreState = TheCondStack.back().Ignore;
1831 if (LastIgnoreState || TheCondState.CondMet) {
1832 TheCondState.Ignore = true;
1833 EatToEndOfStatement();
1834 }
1835 else {
1836 int64_t ExprValue;
1837 if (ParseAbsoluteExpression(ExprValue))
1838 return true;
1839
1840 if (Lexer.isNot(AsmToken::EndOfStatement))
1841 return TokError("unexpected token in '.elseif' directive");
1842
Sean Callanan79ed1a82010-01-19 20:22:31 +00001843 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001844 TheCondState.CondMet = ExprValue;
1845 TheCondState.Ignore = !TheCondState.CondMet;
1846 }
1847
1848 return false;
1849}
1850
1851/// ParseDirectiveElse
1852/// ::= .else
1853bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001854 if (Lexer.isNot(AsmToken::EndOfStatement))
1855 return TokError("unexpected token in '.else' directive");
1856
Sean Callanan79ed1a82010-01-19 20:22:31 +00001857 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001858
1859 if (TheCondState.TheCond != AsmCond::IfCond &&
1860 TheCondState.TheCond != AsmCond::ElseIfCond)
1861 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1862 ".elseif");
1863 TheCondState.TheCond = AsmCond::ElseCond;
1864 bool LastIgnoreState = false;
1865 if (!TheCondStack.empty())
1866 LastIgnoreState = TheCondStack.back().Ignore;
1867 if (LastIgnoreState || TheCondState.CondMet)
1868 TheCondState.Ignore = true;
1869 else
1870 TheCondState.Ignore = false;
1871
1872 return false;
1873}
1874
1875/// ParseDirectiveEndIf
1876/// ::= .endif
1877bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001878 if (Lexer.isNot(AsmToken::EndOfStatement))
1879 return TokError("unexpected token in '.endif' directive");
1880
Sean Callanan79ed1a82010-01-19 20:22:31 +00001881 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001882
1883 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1884 TheCondStack.empty())
1885 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1886 ".else");
1887 if (!TheCondStack.empty()) {
1888 TheCondState = TheCondStack.back();
1889 TheCondStack.pop_back();
1890 }
1891
1892 return false;
1893}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001894
1895/// ParseDirectiveFile
1896/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001897bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001898 // FIXME: I'm not sure what this is.
1899 int64_t FileNumber = -1;
1900 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001901 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001902 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001903
1904 if (FileNumber < 1)
1905 return TokError("file number less than one");
1906 }
1907
1908 if (Lexer.isNot(AsmToken::String))
1909 return TokError("unexpected token in '.file' directive");
1910
Chris Lattnerd32e8032010-01-25 19:02:58 +00001911 StringRef Filename = getTok().getString();
1912 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001913 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001914
1915 if (Lexer.isNot(AsmToken::EndOfStatement))
1916 return TokError("unexpected token in '.file' directive");
1917
Chris Lattnerd32e8032010-01-25 19:02:58 +00001918 if (FileNumber == -1)
1919 Out.EmitFileDirective(Filename);
1920 else
1921 Out.EmitDwarfFileDirective(FileNumber, Filename);
1922
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001923 return false;
1924}
1925
1926/// ParseDirectiveLine
1927/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001928bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001929 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1930 if (Lexer.isNot(AsmToken::Integer))
1931 return TokError("unexpected token in '.line' directive");
1932
Sean Callanan18b83232010-01-19 21:44:56 +00001933 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001934 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001935 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001936
1937 // FIXME: Do something with the .line.
1938 }
1939
1940 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001941 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001942
1943 return false;
1944}
1945
1946
1947/// ParseDirectiveLoc
1948/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001949bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001950 if (Lexer.isNot(AsmToken::Integer))
1951 return TokError("unexpected token in '.loc' directive");
1952
1953 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001954 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001955 (void) FileNumber;
1956 // FIXME: Validate file.
1957
Sean Callanan79ed1a82010-01-19 20:22:31 +00001958 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001959 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1960 if (Lexer.isNot(AsmToken::Integer))
1961 return TokError("unexpected token in '.loc' directive");
1962
Sean Callanan18b83232010-01-19 21:44:56 +00001963 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001964 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001965 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001966
1967 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1968 if (Lexer.isNot(AsmToken::Integer))
1969 return TokError("unexpected token in '.loc' directive");
1970
Sean Callanan18b83232010-01-19 21:44:56 +00001971 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001972 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001973 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001974
1975 // FIXME: Do something with the .loc.
1976 }
1977 }
1978
1979 if (Lexer.isNot(AsmToken::EndOfStatement))
1980 return TokError("unexpected token in '.file' directive");
1981
1982 return false;
1983}
1984