blob: a8f2bbddb6700a1c54e197932b8597f62ba1ab0d [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
Sean Callananbf2013e2010-01-20 23:19:55 +000059void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
60 const char *Type) const {
61 SrcMgr.PrintMessage(Loc, Msg, Type);
62}
Sean Callananfd0b0282010-01-21 00:19:58 +000063
64bool AsmParser::EnterIncludeFile(const std::string &Filename) {
65 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
66 if (NewBuf == -1)
67 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000068
Sean Callananfd0b0282010-01-21 00:19:58 +000069 CurBuffer = NewBuf;
70
71 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
72
73 return false;
74}
75
76const AsmToken &AsmParser::Lex() {
77 const AsmToken *tok = &Lexer.Lex();
78
79 if (tok->is(AsmToken::Eof)) {
80 // If this is the end of an included file, pop the parent file off the
81 // include stack.
82 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
83 if (ParentIncludeLoc != SMLoc()) {
84 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
85 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
86 ParentIncludeLoc.getPointer());
87 tok = &Lexer.Lex();
88 }
89 }
90
91 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000092 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000093
Sean Callananfd0b0282010-01-21 00:19:58 +000094 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +000095}
96
Chris Lattner79180e22010-04-05 23:15:42 +000097bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +000098 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000099 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000100 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000101 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000102 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000103 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
104 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000105
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000106 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000107 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000108
Chris Lattnerb717fb02009-07-02 21:53:43 +0000109 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000110
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000111 AsmCond StartingCondState = TheCondState;
112
Chris Lattnerb717fb02009-07-02 21:53:43 +0000113 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000114 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000115 if (!ParseStatement()) continue;
116
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000117 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000118 HadError = true;
119 EatToEndOfStatement();
120 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000121
122 if (TheCondState.TheCond != StartingCondState.TheCond ||
123 TheCondState.Ignore != StartingCondState.Ignore)
124 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000125
Chris Lattner79180e22010-04-05 23:15:42 +0000126 // Finalize the output stream if there are no errors and if the client wants
127 // us to.
128 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000129 Out.Finish();
130
Chris Lattnerb717fb02009-07-02 21:53:43 +0000131 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000132}
133
Chris Lattner2cf5f142009-06-22 01:29:09 +0000134/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
135void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000136 while (Lexer.isNot(AsmToken::EndOfStatement) &&
137 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000138 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000139
140 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000141 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000142 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000143}
144
Chris Lattnerc4193832009-06-22 05:51:26 +0000145
Chris Lattner74ec1a32009-06-22 06:32:03 +0000146/// ParseParenExpr - Parse a paren expression and return it.
147/// NOTE: This assumes the leading '(' has already been consumed.
148///
149/// parenexpr ::= expr)
150///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000151bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000152 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000153 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000154 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000155 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000156 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000157 return false;
158}
Chris Lattnerc4193832009-06-22 05:51:26 +0000159
Daniel Dunbar959fd882009-08-26 22:13:22 +0000160MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000161 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000162 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000163}
164
Chris Lattner74ec1a32009-06-22 06:32:03 +0000165/// ParsePrimaryExpr - Parse a primary expression and return it.
166/// primaryexpr ::= (parenexpr
167/// primaryexpr ::= symbol
168/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000169/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000170/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000171bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000172 switch (Lexer.getKind()) {
173 default:
174 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000175 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000176 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000177 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000178 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000179 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000180 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000181 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000182 case AsmToken::Identifier: {
183 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000184 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
185 MCSymbol *Sym = CreateSymbol(Split.first);
186
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000187 // Mark the symbol as used in an expression.
188 Sym->setUsedInExpr(true);
189
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000190 // Lookup the symbol variant if used.
191 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
192 if (Split.first.size() != getTok().getIdentifier().size())
193 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
194
Chris Lattnerb4307b32010-01-15 19:28:38 +0000195 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000196 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000197
198 // If this is an absolute variable reference, substitute it now to preserve
199 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000200 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000201 if (Variant)
202 return Error(EndLoc, "unexpected modified on variable reference");
203
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000204 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000205 return false;
206 }
207
208 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000209 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000210 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000211 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000212 case AsmToken::Integer: {
213 SMLoc Loc = getTok().getLoc();
214 int64_t IntVal = getTok().getIntVal();
215 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000216 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000217 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000218 // Look for 'b' or 'f' following an Integer as a directional label
219 if (Lexer.getKind() == AsmToken::Identifier) {
220 StringRef IDVal = getTok().getString();
221 if (IDVal == "f" || IDVal == "b"){
222 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
223 IDVal == "f" ? 1 : 0);
224 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
225 getContext());
226 if(IDVal == "b" && Sym->isUndefined())
227 return Error(Loc, "invalid reference to undefined symbol");
228 EndLoc = Lexer.getLoc();
229 Lex(); // Eat identifier.
230 }
231 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000232 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000233 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000234 case AsmToken::Dot: {
235 // This is a '.' reference, which references the current PC. Emit a
236 // temporary label to the streamer and refer to it.
237 MCSymbol *Sym = Ctx.CreateTempSymbol();
238 Out.EmitLabel(Sym);
239 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
240 EndLoc = Lexer.getLoc();
241 Lex(); // Eat identifier.
242 return false;
243 }
244
Daniel Dunbar3f872332009-07-28 16:08:33 +0000245 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000246 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000247 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000248 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000249 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000250 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000251 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000252 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000254 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000255 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000256 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000258 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000260 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000261 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000262 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000264 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000266 }
267}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000268
Chris Lattnerb4307b32010-01-15 19:28:38 +0000269bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000270 SMLoc EndLoc;
271 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000272}
273
Chris Lattner74ec1a32009-06-22 06:32:03 +0000274/// ParseExpression - Parse an expression and return it.
275///
276/// expr ::= expr +,- expr -> lowest.
277/// expr ::= expr |,^,&,! expr -> middle.
278/// expr ::= expr *,/,%,<<,>> expr -> highest.
279/// expr ::= primaryexpr
280///
Chris Lattner54482b42010-01-15 19:39:23 +0000281bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000282 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000283 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000284 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
285 return true;
286
287 // Try to constant fold it up front, if possible.
288 int64_t Value;
289 if (Res->EvaluateAsAbsolute(Value))
290 Res = MCConstantExpr::Create(Value, getContext());
291
292 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000293}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000294
Chris Lattnerb4307b32010-01-15 19:28:38 +0000295bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000296 Res = 0;
297 return ParseParenExpr(Res, EndLoc) ||
298 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000299}
300
Daniel Dunbar475839e2009-06-29 20:37:27 +0000301bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000302 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000303
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000304 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305 if (ParseExpression(Expr))
306 return true;
307
Daniel Dunbare00b0112009-10-16 01:57:52 +0000308 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000309 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310
311 return false;
312}
313
Daniel Dunbar3f872332009-07-28 16:08:33 +0000314static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000315 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000316 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000317 default:
318 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000319
320 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000321 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000322 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000323 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000324 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000325 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000326 return 1;
327
328 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000331 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000333 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000336 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000337 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000338 case AsmToken::ExclaimEqual:
339 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000342 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000343 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000349 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000351 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000352 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000353 return 2;
354
355 // Intermediate Precedence: |, &, ^
356 //
357 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000358 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000359 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000360 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000361 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000362 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000363 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000364 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000365 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000366 return 3;
367
368 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000372 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000381 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000382 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000383 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000384 }
385}
386
387
388/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
389/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000390bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
391 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000392 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000393 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000395
396 // If the next token is lower precedence than we are allowed to eat, return
397 // successfully with what we ate already.
398 if (TokPrec < Precedence)
399 return false;
400
Sean Callanan79ed1a82010-01-19 20:22:31 +0000401 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000402
403 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000404 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000405 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000406
407 // If BinOp binds less tightly with RHS than the operator after RHS, let
408 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000409 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000410 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000411 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000412 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000413 }
414
Daniel Dunbar475839e2009-06-29 20:37:27 +0000415 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000416 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000417 }
418}
419
Chris Lattnerc4193832009-06-22 05:51:26 +0000420
421
422
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000423/// ParseStatement:
424/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000425/// ::= Label* Directive ...Operands... EndOfStatement
426/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000427bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000428 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000429 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000430 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000431 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000432 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000433
434 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000435 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000436 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000437 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000438 int64_t LocalLabelVal = -1;
439 // GUESS allow an integer followed by a ':' as a directional local label
440 if (Lexer.is(AsmToken::Integer)) {
441 LocalLabelVal = getTok().getIntVal();
442 if (LocalLabelVal < 0) {
443 if (!TheCondState.Ignore)
444 return TokError("unexpected token at start of statement");
445 IDVal = "";
446 }
447 else {
448 IDVal = getTok().getString();
449 Lex(); // Consume the integer token to be used as an identifier token.
450 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000451 if (!TheCondState.Ignore)
452 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000453 }
454 }
455 }
456 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000457 if (!TheCondState.Ignore)
458 return TokError("unexpected token at start of statement");
459 IDVal = "";
460 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000461
Chris Lattner7834fac2010-04-17 18:14:27 +0000462 // Handle conditional assembly here before checking for skipping. We
463 // have to do this so that .endif isn't skipped in a ".if 0" block for
464 // example.
465 if (IDVal == ".if")
466 return ParseDirectiveIf(IDLoc);
467 if (IDVal == ".elseif")
468 return ParseDirectiveElseIf(IDLoc);
469 if (IDVal == ".else")
470 return ParseDirectiveElse(IDLoc);
471 if (IDVal == ".endif")
472 return ParseDirectiveEndIf(IDLoc);
473
474 // If we are in a ".if 0" block, ignore this statement.
475 if (TheCondState.Ignore) {
476 EatToEndOfStatement();
477 return false;
478 }
479
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000480 // FIXME: Recurse on local labels?
481
482 // See what kind of statement we have.
483 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000484 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000485 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000486 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000487
488 // Diagnose attempt to use a variable as a label.
489 //
490 // FIXME: Diagnostics. Note the location of the definition as a label.
491 // FIXME: This doesn't diagnose assignment to a symbol which has been
492 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000493 MCSymbol *Sym;
494 if (LocalLabelVal == -1)
495 Sym = CreateSymbol(IDVal);
496 else
497 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000498 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000499 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000500
Daniel Dunbar959fd882009-08-26 22:13:22 +0000501 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000502 Out.EmitLabel(Sym);
503
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000504 // Consume any end of statement token, if present, to avoid spurious
505 // AddBlankLine calls().
506 if (Lexer.is(AsmToken::EndOfStatement)) {
507 Lex();
508 if (Lexer.is(AsmToken::Eof))
509 return false;
510 }
511
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000512 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000513 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000514
Daniel Dunbar3f872332009-07-28 16:08:33 +0000515 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000516 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000517 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000518
Daniel Dunbare2ace502009-08-31 08:09:09 +0000519 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000520
521 default: // Normal instruction or directive.
522 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000523 }
524
525 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000526 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000527 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000529 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000531 // FIXME: This changes behavior based on the -static flag to the
532 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000533 return ParseDirectiveSectionSwitch("__TEXT", "__text",
534 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000536 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000538 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000540 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
541 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000543 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 MCSectionMachO::S_4BYTE_LITERALS,
545 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000547 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 MCSectionMachO::S_8BYTE_LITERALS,
549 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000551 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 MCSectionMachO::S_16BYTE_LITERALS,
553 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
562
563 // FIXME: The assembler manual claims that this has the self modify code
564 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000565 if (IDVal == ".symbol_stub")
566 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
567 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000568 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
569 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 0, 16);
571 // FIXME: PowerPC only?
572 if (IDVal == ".picsymbol_stub")
573 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
574 MCSectionMachO::S_SYMBOL_STUBS |
575 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
576 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000577 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000578 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
581
582 // FIXME: The section names of these two are misspelled in the assembler
583 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
586 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
587 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
590 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
591 4);
592
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000596 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000597 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
598 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000600 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
602 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000605
606
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 return ParseDirectiveSectionSwitch("__OBJC", "__class",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
612 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000614 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
615 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000617 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
618 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000620 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
621 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000623 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
624 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000626 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
627 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000629 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
630 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000632 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
633 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
634 MCSectionMachO::S_LITERAL_POINTERS,
635 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000637 return ParseDirectiveSectionSwitch("__OBJC", "__message_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_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000642 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
643 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000645 return ParseDirectiveSectionSwitch("__OBJC", "__category",
646 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000648 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
649 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000651 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
652 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000654 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
655 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000657 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
658 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000660 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
661 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000663 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
664 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000666 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
667 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000668
Eric Christopherc6177a42010-05-17 22:53:55 +0000669 if (IDVal == ".tdata")
670 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
671 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
672 if (IDVal == ".tlv")
673 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
674 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
675 if (IDVal == ".thread_init_func")
676 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
677 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
678
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000679 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000681 return ParseDirectiveSet();
682
Daniel Dunbara0d14262009-06-24 23:30:00 +0000683 // Data directives
684
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000686 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000688 return ParseDirectiveAscii(true);
689
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000691 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000692 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000693 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000695 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000697 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000698
699 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000701 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000703 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000705 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000707 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000709 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000711 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000713 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000715 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
716
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000718 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000719
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000721 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000723 return ParseDirectiveSpace();
724
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000725 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000726
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000728 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000730 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000732 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000734 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000736 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000738 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000740 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000742 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000744 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000745 if (IDVal == ".type")
746 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000748 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000750 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000752 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000753 if (IDVal == ".weak_def_can_be_hidden")
754 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000755
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000756 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000757 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000758 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000759 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000761 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000763 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000765 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000766 if (IDVal == ".tbss")
767 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000768
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000770 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000771 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000772 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000773 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000774 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000775 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000776 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000777 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000778 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbyf187ac52010-06-28 21:45:58 +0000779 if (IDVal == ".secure_log_unique")
780 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
781 if (IDVal == ".secure_log_reset")
782 return ParseDirectiveDarwinSecureLogReset(IDLoc);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000783
Chris Lattnerebb89b42009-09-27 21:16:52 +0000784 // Look up the handler in the handler table,
785 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
786 if (Handler)
787 return (this->*Handler)(IDVal, IDLoc);
788
Kevin Enderby9c656452009-09-10 20:51:44 +0000789 // Target hook for parsing target specific directives.
790 if (!getTargetParser().ParseDirective(ID))
791 return false;
792
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000793 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000794 EatToEndOfStatement();
795 return false;
796 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000797
Chris Lattnera7f13542010-05-19 23:34:33 +0000798 // Canonicalize the opcode to lower case.
799 SmallString<128> Opcode;
800 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
801 Opcode.push_back(tolower(IDVal[i]));
802
Chris Lattner98986712010-01-14 22:21:20 +0000803 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000804 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000805 ParsedOperands);
806 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
807 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000808
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000809 // If parsing succeeded, match the instruction.
810 if (!HadError) {
811 MCInst Inst;
812 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
813 // Emit the instruction on success.
814 Out.EmitInstruction(Inst);
815 } else {
816 // Otherwise emit a diagnostic about the match failure and set the error
817 // flag.
818 //
819 // FIXME: We should give nicer diagnostics about the exact failure.
820 Error(IDLoc, "unrecognized instruction");
821 HadError = true;
822 }
823 }
Chris Lattner98986712010-01-14 22:21:20 +0000824
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000825 // If there was no error, consume the end-of-statement token. Otherwise this
826 // will be done by our caller.
827 if (!HadError)
828 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000829
830 // Free any parsed operands.
831 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
832 delete ParsedOperands[i];
833
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000834 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000835}
Chris Lattner9a023f72009-06-24 04:43:34 +0000836
Daniel Dunbare2ace502009-08-31 08:09:09 +0000837bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000838 // FIXME: Use better location, we should use proper tokens.
839 SMLoc EqualLoc = Lexer.getLoc();
840
Daniel Dunbar821e3332009-08-31 08:09:28 +0000841 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000842 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000843 return true;
844
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000846 return TokError("unexpected token in assignment");
847
848 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000849 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000850
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000851 // Validate that the LHS is allowed to be a variable (either it has not been
852 // used as a symbol, or it is an absolute symbol).
853 MCSymbol *Sym = getContext().LookupSymbol(Name);
854 if (Sym) {
855 // Diagnose assignment to a label.
856 //
857 // FIXME: Diagnostics. Note the location of the definition as a label.
858 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000859 if (Sym->isUndefined() && !Sym->isUsedInExpr())
860 ; // Allow redefinitions of undefined symbols only used in directives.
861 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000862 return Error(EqualLoc, "redefinition of '" + Name + "'");
863 else if (!Sym->isVariable())
864 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000865 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000866 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
867 Name + "'");
868 } else
869 Sym = CreateSymbol(Name);
870
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000871 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000872
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000873 Sym->setUsedInExpr(true);
874
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000875 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000876 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000877
878 return false;
879}
880
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000881/// ParseIdentifier:
882/// ::= identifier
883/// ::= string
884bool AsmParser::ParseIdentifier(StringRef &Res) {
885 if (Lexer.isNot(AsmToken::Identifier) &&
886 Lexer.isNot(AsmToken::String))
887 return true;
888
Sean Callanan18b83232010-01-19 21:44:56 +0000889 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000890
Sean Callanan79ed1a82010-01-19 20:22:31 +0000891 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000892
893 return false;
894}
895
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000896/// ParseDirectiveSet:
897/// ::= .set identifier ',' expression
898bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000899 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000900
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000901 if (ParseIdentifier(Name))
902 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000903
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000904 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000905 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000906 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000907
Daniel Dunbare2ace502009-08-31 08:09:09 +0000908 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000909}
910
Chris Lattner9a023f72009-06-24 04:43:34 +0000911/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000912/// ::= .section identifier (',' identifier)*
913/// FIXME: This should actually parse out the segment, section, attributes and
914/// sizeof_stub fields.
915bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000916 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000917
Daniel Dunbarace63122009-08-11 03:42:33 +0000918 StringRef SectionName;
919 if (ParseIdentifier(SectionName))
920 return Error(Loc, "expected identifier after '.section' directive");
921
922 // Verify there is a following comma.
923 if (!Lexer.is(AsmToken::Comma))
924 return TokError("unexpected token in '.section' directive");
925
Chris Lattnerff4bc462009-08-10 01:39:42 +0000926 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000927 SectionSpec += ",";
928
929 // Add all the tokens until the end of the line, ParseSectionSpecifier will
930 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000931 StringRef EOL = Lexer.LexUntilEndOfStatement();
932 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000933
Sean Callanan79ed1a82010-01-19 20:22:31 +0000934 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000935 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000936 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000937 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000938
Chris Lattnerff4bc462009-08-10 01:39:42 +0000939
940 StringRef Segment, Section;
941 unsigned TAA, StubSize;
942 std::string ErrorStr =
943 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
944 TAA, StubSize);
945
946 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000947 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000948
Chris Lattner56594f92009-07-31 17:47:16 +0000949 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000950 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000951 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
952 isText ? SectionKind::getText()
953 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000954 return false;
955}
956
Chris Lattnere15c2d72009-08-10 18:05:55 +0000957/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000958bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
959 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000960 unsigned TAA, unsigned Align,
961 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000962 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000963 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000964 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000965
Chris Lattner56594f92009-07-31 17:47:16 +0000966 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000967 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000968 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
969 isText ? SectionKind::getText()
970 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000971
972 // Set the implicit alignment, if any.
973 //
974 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
975 // alignment on the section (e.g., if one manually inserts bytes into the
976 // section, then just issueing the section switch directive will not realign
977 // the section. However, this is arguably more reasonable behavior, and there
978 // is no good reason for someone to intentionally emit incorrectly sized
979 // values into the implicitly aligned sections.
980 if (Align)
981 Out.EmitValueToAlignment(Align, 0, 1, 0);
982
Chris Lattner529fb542009-06-24 05:13:15 +0000983 return false;
984}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000985
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000986bool AsmParser::ParseEscapedString(std::string &Data) {
987 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
988
989 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000990 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000991 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
992 if (Str[i] != '\\') {
993 Data += Str[i];
994 continue;
995 }
996
997 // Recognize escaped characters. Note that this escape semantics currently
998 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
999 ++i;
1000 if (i == e)
1001 return TokError("unexpected backslash at end of string");
1002
1003 // Recognize octal sequences.
1004 if ((unsigned) (Str[i] - '0') <= 7) {
1005 // Consume up to three octal characters.
1006 unsigned Value = Str[i] - '0';
1007
1008 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1009 ++i;
1010 Value = Value * 8 + (Str[i] - '0');
1011
1012 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1013 ++i;
1014 Value = Value * 8 + (Str[i] - '0');
1015 }
1016 }
1017
1018 if (Value > 255)
1019 return TokError("invalid octal escape sequence (out of range)");
1020
1021 Data += (unsigned char) Value;
1022 continue;
1023 }
1024
1025 // Otherwise recognize individual escapes.
1026 switch (Str[i]) {
1027 default:
1028 // Just reject invalid escape sequences for now.
1029 return TokError("invalid escape sequence (unrecognized character)");
1030
1031 case 'b': Data += '\b'; break;
1032 case 'f': Data += '\f'; break;
1033 case 'n': Data += '\n'; break;
1034 case 'r': Data += '\r'; break;
1035 case 't': Data += '\t'; break;
1036 case '"': Data += '"'; break;
1037 case '\\': Data += '\\'; break;
1038 }
1039 }
1040
1041 return false;
1042}
1043
Daniel Dunbara0d14262009-06-24 23:30:00 +00001044/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001045/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001047 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001050 return TokError("expected string in '.ascii' or '.asciz' directive");
1051
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001052 std::string Data;
1053 if (ParseEscapedString(Data))
1054 return true;
1055
Chris Lattneraaec2052010-01-19 19:46:13 +00001056 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001058 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059
Sean Callanan79ed1a82010-01-19 20:22:31 +00001060 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001061
Daniel Dunbar3f872332009-07-28 16:08:33 +00001062 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063 break;
1064
Daniel Dunbar3f872332009-07-28 16:08:33 +00001065 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001066 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001067 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 }
1069 }
1070
Sean Callanan79ed1a82010-01-19 20:22:31 +00001071 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001072 return false;
1073}
1074
1075/// ParseDirectiveValue
1076/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1077bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001078 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001080 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001081 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001082 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083 return true;
1084
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001085 // Special case constant expressions to match code generator.
1086 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1087 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1088 else
1089 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001090
Daniel Dunbar3f872332009-07-28 16:08:33 +00001091 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092 break;
1093
1094 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001095 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001096 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001097 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001098 }
1099 }
1100
Sean Callanan79ed1a82010-01-19 20:22:31 +00001101 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001102 return false;
1103}
1104
1105/// ParseDirectiveSpace
1106/// ::= .space expression [ , expression ]
1107bool AsmParser::ParseDirectiveSpace() {
1108 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001109 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001110 return true;
1111
1112 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001113 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1114 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001116 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117
Daniel Dunbar475839e2009-06-29 20:37:27 +00001118 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119 return true;
1120
Daniel Dunbar3f872332009-07-28 16:08:33 +00001121 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122 return TokError("unexpected token in '.space' directive");
1123 }
1124
Sean Callanan79ed1a82010-01-19 20:22:31 +00001125 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126
1127 if (NumBytes <= 0)
1128 return TokError("invalid number of bytes in '.space' directive");
1129
1130 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001131 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132
1133 return false;
1134}
1135
1136/// ParseDirectiveFill
1137/// ::= .fill expression , expression , expression
1138bool AsmParser::ParseDirectiveFill() {
1139 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001140 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001141 return true;
1142
Daniel Dunbar3f872332009-07-28 16:08:33 +00001143 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001144 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001145 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146
1147 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001148 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001149 return true;
1150
Daniel Dunbar3f872332009-07-28 16:08:33 +00001151 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001152 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001153 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154
1155 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001156 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001157 return true;
1158
Daniel Dunbar3f872332009-07-28 16:08:33 +00001159 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001160 return TokError("unexpected token in '.fill' directive");
1161
Sean Callanan79ed1a82010-01-19 20:22:31 +00001162 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001163
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001164 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1165 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001166
1167 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001168 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001169
1170 return false;
1171}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001172
1173/// ParseDirectiveOrg
1174/// ::= .org expression [ , expression ]
1175bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001176 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001177 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001178 return true;
1179
1180 // Parse optional fill expression.
1181 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001182 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1183 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001184 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001185 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001186
Daniel Dunbar475839e2009-06-29 20:37:27 +00001187 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001188 return true;
1189
Daniel Dunbar3f872332009-07-28 16:08:33 +00001190 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001191 return TokError("unexpected token in '.org' directive");
1192 }
1193
Sean Callanan79ed1a82010-01-19 20:22:31 +00001194 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001195
1196 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1197 // has to be relative to the current section.
1198 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001199
1200 return false;
1201}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001202
1203/// ParseDirectiveAlign
1204/// ::= {.align, ...} expression [ , expression [ , expression ]]
1205bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001206 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207 int64_t Alignment;
1208 if (ParseAbsoluteExpression(Alignment))
1209 return true;
1210
1211 SMLoc MaxBytesLoc;
1212 bool HasFillExpr = false;
1213 int64_t FillExpr = 0;
1214 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001215 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1216 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001217 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001218 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001219
1220 // The fill expression can be omitted while specifying a maximum number of
1221 // alignment bytes, e.g:
1222 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001223 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224 HasFillExpr = true;
1225 if (ParseAbsoluteExpression(FillExpr))
1226 return true;
1227 }
1228
Daniel Dunbar3f872332009-07-28 16:08:33 +00001229 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1230 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001231 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001232 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001233
1234 MaxBytesLoc = Lexer.getLoc();
1235 if (ParseAbsoluteExpression(MaxBytesToFill))
1236 return true;
1237
Daniel Dunbar3f872332009-07-28 16:08:33 +00001238 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001239 return TokError("unexpected token in directive");
1240 }
1241 }
1242
Sean Callanan79ed1a82010-01-19 20:22:31 +00001243 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001244
Daniel Dunbar648ac512010-05-17 21:54:30 +00001245 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001246 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001247
1248 // Compute alignment in bytes.
1249 if (IsPow2) {
1250 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001251 if (Alignment >= 32) {
1252 Error(AlignmentLoc, "invalid alignment value");
1253 Alignment = 31;
1254 }
1255
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001256 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001257 }
1258
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001259 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001260 if (MaxBytesLoc.isValid()) {
1261 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001262 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1263 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001264 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001265 }
1266
1267 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001268 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1269 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001270 MaxBytesToFill = 0;
1271 }
1272 }
1273
Daniel Dunbar648ac512010-05-17 21:54:30 +00001274 // Check whether we should use optimal code alignment for this .align
1275 // directive.
1276 //
1277 // FIXME: This should be using a target hook.
1278 bool UseCodeAlign = false;
1279 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1280 Out.getCurrentSection()))
1281 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1282 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1283 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001284 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001285 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001286 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1287 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001288 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001289
1290 return false;
1291}
1292
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001293/// ParseDirectiveSymbolAttribute
1294/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001295bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001296 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001297 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001298 StringRef Name;
1299
1300 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001301 return TokError("expected identifier in directive");
1302
Daniel Dunbar959fd882009-08-26 22:13:22 +00001303 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001304
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001305 Out.EmitSymbolAttribute(Sym, Attr);
1306
Daniel Dunbar3f872332009-07-28 16:08:33 +00001307 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001308 break;
1309
Daniel Dunbar3f872332009-07-28 16:08:33 +00001310 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001311 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001312 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001313 }
1314 }
1315
Sean Callanan79ed1a82010-01-19 20:22:31 +00001316 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001317 return false;
1318}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001319
Matt Fleming924c5e52010-05-21 11:36:59 +00001320/// ParseDirectiveELFType
1321/// ::= .type identifier , @attribute
1322bool AsmParser::ParseDirectiveELFType() {
1323 StringRef Name;
1324 if (ParseIdentifier(Name))
1325 return TokError("expected identifier in directive");
1326
1327 // Handle the identifier as the key symbol.
1328 MCSymbol *Sym = CreateSymbol(Name);
1329
1330 if (Lexer.isNot(AsmToken::Comma))
1331 return TokError("unexpected token in '.type' directive");
1332 Lex();
1333
1334 if (Lexer.isNot(AsmToken::At))
1335 return TokError("expected '@' before type");
1336 Lex();
1337
1338 StringRef Type;
1339 SMLoc TypeLoc;
1340
1341 TypeLoc = Lexer.getLoc();
1342 if (ParseIdentifier(Type))
1343 return TokError("expected symbol type in directive");
1344
1345 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1346 .Case("function", MCSA_ELF_TypeFunction)
1347 .Case("object", MCSA_ELF_TypeObject)
1348 .Case("tls_object", MCSA_ELF_TypeTLS)
1349 .Case("common", MCSA_ELF_TypeCommon)
1350 .Case("notype", MCSA_ELF_TypeNoType)
1351 .Default(MCSA_Invalid);
1352
1353 if (Attr == MCSA_Invalid)
1354 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1355
1356 if (Lexer.isNot(AsmToken::EndOfStatement))
1357 return TokError("unexpected token in '.type' directive");
1358
1359 Lex();
1360
1361 Out.EmitSymbolAttribute(Sym, Attr);
1362
1363 return false;
1364}
1365
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001366/// ParseDirectiveDarwinSymbolDesc
1367/// ::= .desc identifier , expression
1368bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001369 StringRef Name;
1370 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001371 return TokError("expected identifier in directive");
1372
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001373 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001374 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001375
Daniel Dunbar3f872332009-07-28 16:08:33 +00001376 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001377 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001378 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001379
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001380 int64_t DescValue;
1381 if (ParseAbsoluteExpression(DescValue))
1382 return true;
1383
Daniel Dunbar3f872332009-07-28 16:08:33 +00001384 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001385 return TokError("unexpected token in '.desc' directive");
1386
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001388
1389 // Set the n_desc field of this Symbol to this DescValue
1390 Out.EmitSymbolDesc(Sym, DescValue);
1391
1392 return false;
1393}
1394
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001395/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001396/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1397bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001398 SMLoc IDLoc = Lexer.getLoc();
1399 StringRef Name;
1400 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001401 return TokError("expected identifier in directive");
1402
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001403 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001404 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001405
Daniel Dunbar3f872332009-07-28 16:08:33 +00001406 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001407 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001408 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001409
1410 int64_t Size;
1411 SMLoc SizeLoc = Lexer.getLoc();
1412 if (ParseAbsoluteExpression(Size))
1413 return true;
1414
1415 int64_t Pow2Alignment = 0;
1416 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001417 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001418 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001419 Pow2AlignmentLoc = Lexer.getLoc();
1420 if (ParseAbsoluteExpression(Pow2Alignment))
1421 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001422
1423 // If this target takes alignments in bytes (not log) validate and convert.
1424 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1425 if (!isPowerOf2_64(Pow2Alignment))
1426 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1427 Pow2Alignment = Log2_64(Pow2Alignment);
1428 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001429 }
1430
Daniel Dunbar3f872332009-07-28 16:08:33 +00001431 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001432 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001433
Sean Callanan79ed1a82010-01-19 20:22:31 +00001434 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001435
Chris Lattner1fc3d752009-07-09 17:25:12 +00001436 // NOTE: a size of zero for a .comm should create a undefined symbol
1437 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001438 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001439 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1440 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001441
Eric Christopherc260a3e2010-05-14 01:38:54 +00001442 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001443 // may internally end up wanting an alignment in bytes.
1444 // FIXME: Diagnose overflow.
1445 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001446 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1447 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001448
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001449 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001450 return Error(IDLoc, "invalid symbol redefinition");
1451
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001452 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001453 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001454 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001455 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1456 MCSectionMachO::S_ZEROFILL, 0,
1457 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001458 Sym, Size, 1 << Pow2Alignment);
1459 return false;
1460 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001461
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001462 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001463 return false;
1464}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001465
1466/// ParseDirectiveDarwinZerofill
1467/// ::= .zerofill segname , sectname [, identifier , size_expression [
1468/// , align_expression ]]
1469bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001470 StringRef Segment;
1471 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001472 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001473
Daniel Dunbar3f872332009-07-28 16:08:33 +00001474 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001475 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001476 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001477
1478 StringRef Section;
1479 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001480 return TokError("expected section name after comma in '.zerofill' "
1481 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001482
Chris Lattner9be3fee2009-07-10 22:20:30 +00001483 // If this is the end of the line all that was wanted was to create the
1484 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001485 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001486 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001487 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1488 MCSectionMachO::S_ZEROFILL, 0,
1489 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001490 return false;
1491 }
1492
Daniel Dunbar3f872332009-07-28 16:08:33 +00001493 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001494 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001495 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001496
Chris Lattner7bb7c552010-05-13 00:10:34 +00001497 SMLoc IDLoc = Lexer.getLoc();
1498 StringRef IDStr;
1499 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001500 return TokError("expected identifier in directive");
1501
1502 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001503 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001504
Daniel Dunbar3f872332009-07-28 16:08:33 +00001505 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001506 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001507 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001508
1509 int64_t Size;
1510 SMLoc SizeLoc = Lexer.getLoc();
1511 if (ParseAbsoluteExpression(Size))
1512 return true;
1513
1514 int64_t Pow2Alignment = 0;
1515 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001516 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001517 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001518 Pow2AlignmentLoc = Lexer.getLoc();
1519 if (ParseAbsoluteExpression(Pow2Alignment))
1520 return true;
1521 }
1522
Daniel Dunbar3f872332009-07-28 16:08:33 +00001523 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001524 return TokError("unexpected token in '.zerofill' directive");
1525
Sean Callanan79ed1a82010-01-19 20:22:31 +00001526 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001527
1528 if (Size < 0)
1529 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1530 "than zero");
1531
Eric Christopherc260a3e2010-05-14 01:38:54 +00001532 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001533 // may internally end up wanting an alignment in bytes.
1534 // FIXME: Diagnose overflow.
1535 if (Pow2Alignment < 0)
1536 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1537 "can't be less than zero");
1538
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001539 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001540 return Error(IDLoc, "invalid symbol redefinition");
1541
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001542 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001543 //
1544 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001545 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1546 MCSectionMachO::S_ZEROFILL, 0,
1547 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001548 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001549
1550 return false;
1551}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001552
Eric Christopher482eba02010-05-14 01:50:28 +00001553/// ParseDirectiveDarwinTBSS
1554/// ::= .tbss identifier, size, align
1555bool AsmParser::ParseDirectiveDarwinTBSS() {
1556 SMLoc IDLoc = Lexer.getLoc();
1557 StringRef Name;
1558 if (ParseIdentifier(Name))
1559 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001560
Eric Christopher482eba02010-05-14 01:50:28 +00001561 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001562 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001563
1564 if (Lexer.isNot(AsmToken::Comma))
1565 return TokError("unexpected token in directive");
1566 Lex();
1567
1568 int64_t Size;
1569 SMLoc SizeLoc = Lexer.getLoc();
1570 if (ParseAbsoluteExpression(Size))
1571 return true;
1572
1573 int64_t Pow2Alignment = 0;
1574 SMLoc Pow2AlignmentLoc;
1575 if (Lexer.is(AsmToken::Comma)) {
1576 Lex();
1577 Pow2AlignmentLoc = Lexer.getLoc();
1578 if (ParseAbsoluteExpression(Pow2Alignment))
1579 return true;
1580 }
1581
1582 if (Lexer.isNot(AsmToken::EndOfStatement))
1583 return TokError("unexpected token in '.tbss' directive");
1584
1585 Lex();
1586
1587 if (Size < 0)
1588 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1589 "zero");
1590
1591 // FIXME: Diagnose overflow.
1592 if (Pow2Alignment < 0)
1593 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1594 "than zero");
1595
1596 if (!Sym->isUndefined())
1597 return Error(IDLoc, "invalid symbol redefinition");
1598
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001599 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1600 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1601 0, SectionKind::getThreadBSS()),
1602 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001603
1604 return false;
1605}
1606
Kevin Enderbya5c78322009-07-13 21:03:15 +00001607/// ParseDirectiveDarwinSubsectionsViaSymbols
1608/// ::= .subsections_via_symbols
1609bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001610 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001611 return TokError("unexpected token in '.subsections_via_symbols' directive");
1612
Sean Callanan79ed1a82010-01-19 20:22:31 +00001613 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001614
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001615 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001616
1617 return false;
1618}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001619
1620/// ParseDirectiveAbort
1621/// ::= .abort [ "abort_string" ]
1622bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001623 // FIXME: Use loc from directive.
1624 SMLoc Loc = Lexer.getLoc();
1625
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001626 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001627 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1628 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001629 return TokError("expected string in '.abort' directive");
1630
Sean Callanan18b83232010-01-19 21:44:56 +00001631 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001632
Sean Callanan79ed1a82010-01-19 20:22:31 +00001633 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001634 }
1635
Daniel Dunbar3f872332009-07-28 16:08:33 +00001636 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001637 return TokError("unexpected token in '.abort' directive");
1638
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001640
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001641 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001642 if (Str.empty())
1643 Error(Loc, ".abort detected. Assembly stopping.");
1644 else
1645 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001646
1647 return false;
1648}
Kevin Enderby71148242009-07-14 21:35:03 +00001649
1650/// ParseDirectiveLsym
1651/// ::= .lsym identifier , expression
1652bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001653 StringRef Name;
1654 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001655 return TokError("expected identifier in directive");
1656
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001657 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001658 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001659
Daniel Dunbar3f872332009-07-28 16:08:33 +00001660 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001661 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001662 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001663
Daniel Dunbar821e3332009-08-31 08:09:28 +00001664 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001665 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001666 return true;
1667
Daniel Dunbar3f872332009-07-28 16:08:33 +00001668 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001669 return TokError("unexpected token in '.lsym' directive");
1670
Sean Callanan79ed1a82010-01-19 20:22:31 +00001671 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001672
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001673 // We don't currently support this directive.
1674 //
1675 // FIXME: Diagnostic location!
1676 (void) Sym;
1677 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001678}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001679
1680/// ParseDirectiveInclude
1681/// ::= .include "filename"
1682bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001683 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001684 return TokError("expected string in '.include' directive");
1685
Sean Callanan18b83232010-01-19 21:44:56 +00001686 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001687 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001688 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001689
Daniel Dunbar3f872332009-07-28 16:08:33 +00001690 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001691 return TokError("unexpected token in '.include' directive");
1692
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001693 // Strip the quotes.
1694 Filename = Filename.substr(1, Filename.size()-2);
1695
1696 // Attempt to switch the lexer to the included file before consuming the end
1697 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001698 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001699 PrintMessage(IncludeLoc,
1700 "Could not find include file '" + Filename + "'",
1701 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001702 return true;
1703 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001704
1705 return false;
1706}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001707
1708/// ParseDirectiveDarwinDumpOrLoad
1709/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001710bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001711 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001712 return TokError("expected string in '.dump' or '.load' directive");
1713
Sean Callanan79ed1a82010-01-19 20:22:31 +00001714 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001715
Daniel Dunbar3f872332009-07-28 16:08:33 +00001716 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001717 return TokError("unexpected token in '.dump' or '.load' directive");
1718
Sean Callanan79ed1a82010-01-19 20:22:31 +00001719 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001720
Kevin Enderby5026ae42009-07-20 20:25:37 +00001721 // FIXME: If/when .dump and .load are implemented they will be done in the
1722 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001723 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001724 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001725 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001726 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001727
1728 return false;
1729}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001730
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001731/// ParseDirectiveDarwinSecureLogUnique
1732/// ::= .secure_log_unique "log message"
1733bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1734 std::string LogMessage;
1735
1736 if (Lexer.isNot(AsmToken::String))
1737 LogMessage = "";
1738 else{
1739 LogMessage = getTok().getString();
1740 Lex();
1741 }
1742
1743 if (Lexer.isNot(AsmToken::EndOfStatement))
1744 return TokError("unexpected token in '.secure_log_unique' directive");
1745
1746 if (getContext().getSecureLogUsed() != false)
1747 return Error(IDLoc, ".secure_log_unique specified multiple times");
1748
1749 char *SecureLogFile = getContext().getSecureLogFile();
1750 if (SecureLogFile == NULL)
1751 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1752 "environment variable unset.");
1753
1754 raw_ostream *OS = getContext().getSecureLog();
1755 if (OS == NULL) {
1756 std::string Err;
1757 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1758 if (!Err.empty()) {
1759 delete OS;
1760 return Error(IDLoc, Twine("can't open secure log file: ") +
1761 SecureLogFile + " (" + Err + ")");
1762 }
1763 getContext().setSecureLog(OS);
1764 }
1765
1766 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1767 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1768 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1769 << LogMessage + "\n";
1770
1771 getContext().setSecureLogUsed(true);
1772
1773 return false;
1774}
1775
1776/// ParseDirectiveDarwinSecureLogReset
1777/// ::= .secure_log_reset
1778bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1779 if (Lexer.isNot(AsmToken::EndOfStatement))
1780 return TokError("unexpected token in '.secure_log_reset' directive");
1781
1782 Lex();
1783
1784 getContext().setSecureLogUsed(false);
1785
1786 return false;
1787}
1788
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001789/// ParseDirectiveIf
1790/// ::= .if expression
1791bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001792 TheCondStack.push_back(TheCondState);
1793 TheCondState.TheCond = AsmCond::IfCond;
1794 if(TheCondState.Ignore) {
1795 EatToEndOfStatement();
1796 }
1797 else {
1798 int64_t ExprValue;
1799 if (ParseAbsoluteExpression(ExprValue))
1800 return true;
1801
1802 if (Lexer.isNot(AsmToken::EndOfStatement))
1803 return TokError("unexpected token in '.if' directive");
1804
Sean Callanan79ed1a82010-01-19 20:22:31 +00001805 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001806
1807 TheCondState.CondMet = ExprValue;
1808 TheCondState.Ignore = !TheCondState.CondMet;
1809 }
1810
1811 return false;
1812}
1813
1814/// ParseDirectiveElseIf
1815/// ::= .elseif expression
1816bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1817 if (TheCondState.TheCond != AsmCond::IfCond &&
1818 TheCondState.TheCond != AsmCond::ElseIfCond)
1819 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1820 " an .elseif");
1821 TheCondState.TheCond = AsmCond::ElseIfCond;
1822
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001823 bool LastIgnoreState = false;
1824 if (!TheCondStack.empty())
1825 LastIgnoreState = TheCondStack.back().Ignore;
1826 if (LastIgnoreState || TheCondState.CondMet) {
1827 TheCondState.Ignore = true;
1828 EatToEndOfStatement();
1829 }
1830 else {
1831 int64_t ExprValue;
1832 if (ParseAbsoluteExpression(ExprValue))
1833 return true;
1834
1835 if (Lexer.isNot(AsmToken::EndOfStatement))
1836 return TokError("unexpected token in '.elseif' directive");
1837
Sean Callanan79ed1a82010-01-19 20:22:31 +00001838 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001839 TheCondState.CondMet = ExprValue;
1840 TheCondState.Ignore = !TheCondState.CondMet;
1841 }
1842
1843 return false;
1844}
1845
1846/// ParseDirectiveElse
1847/// ::= .else
1848bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001849 if (Lexer.isNot(AsmToken::EndOfStatement))
1850 return TokError("unexpected token in '.else' directive");
1851
Sean Callanan79ed1a82010-01-19 20:22:31 +00001852 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001853
1854 if (TheCondState.TheCond != AsmCond::IfCond &&
1855 TheCondState.TheCond != AsmCond::ElseIfCond)
1856 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1857 ".elseif");
1858 TheCondState.TheCond = AsmCond::ElseCond;
1859 bool LastIgnoreState = false;
1860 if (!TheCondStack.empty())
1861 LastIgnoreState = TheCondStack.back().Ignore;
1862 if (LastIgnoreState || TheCondState.CondMet)
1863 TheCondState.Ignore = true;
1864 else
1865 TheCondState.Ignore = false;
1866
1867 return false;
1868}
1869
1870/// ParseDirectiveEndIf
1871/// ::= .endif
1872bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001873 if (Lexer.isNot(AsmToken::EndOfStatement))
1874 return TokError("unexpected token in '.endif' directive");
1875
Sean Callanan79ed1a82010-01-19 20:22:31 +00001876 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001877
1878 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1879 TheCondStack.empty())
1880 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1881 ".else");
1882 if (!TheCondStack.empty()) {
1883 TheCondState = TheCondStack.back();
1884 TheCondStack.pop_back();
1885 }
1886
1887 return false;
1888}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001889
1890/// ParseDirectiveFile
1891/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001892bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001893 // FIXME: I'm not sure what this is.
1894 int64_t FileNumber = -1;
1895 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001896 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001898
1899 if (FileNumber < 1)
1900 return TokError("file number less than one");
1901 }
1902
1903 if (Lexer.isNot(AsmToken::String))
1904 return TokError("unexpected token in '.file' directive");
1905
Chris Lattnerd32e8032010-01-25 19:02:58 +00001906 StringRef Filename = getTok().getString();
1907 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001908 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001909
1910 if (Lexer.isNot(AsmToken::EndOfStatement))
1911 return TokError("unexpected token in '.file' directive");
1912
Chris Lattnerd32e8032010-01-25 19:02:58 +00001913 if (FileNumber == -1)
1914 Out.EmitFileDirective(Filename);
1915 else
1916 Out.EmitDwarfFileDirective(FileNumber, Filename);
1917
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001918 return false;
1919}
1920
1921/// ParseDirectiveLine
1922/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001923bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001924 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1925 if (Lexer.isNot(AsmToken::Integer))
1926 return TokError("unexpected token in '.line' directive");
1927
Sean Callanan18b83232010-01-19 21:44:56 +00001928 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001929 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001930 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001931
1932 // FIXME: Do something with the .line.
1933 }
1934
1935 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001936 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001937
1938 return false;
1939}
1940
1941
1942/// ParseDirectiveLoc
1943/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001944bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001945 if (Lexer.isNot(AsmToken::Integer))
1946 return TokError("unexpected token in '.loc' directive");
1947
1948 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001949 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001950 (void) FileNumber;
1951 // FIXME: Validate file.
1952
Sean Callanan79ed1a82010-01-19 20:22:31 +00001953 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001954 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1955 if (Lexer.isNot(AsmToken::Integer))
1956 return TokError("unexpected token in '.loc' directive");
1957
Sean Callanan18b83232010-01-19 21:44:56 +00001958 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001959 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001960 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001961
1962 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1963 if (Lexer.isNot(AsmToken::Integer))
1964 return TokError("unexpected token in '.loc' directive");
1965
Sean Callanan18b83232010-01-19 21:44:56 +00001966 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001967 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001968 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001969
1970 // FIXME: Do something with the .loc.
1971 }
1972 }
1973
1974 if (Lexer.isNot(AsmToken::EndOfStatement))
1975 return TokError("unexpected token in '.file' directive");
1976
1977 return false;
1978}
1979