blob: 3b05f1569c7a7b57558fb1b5d377250737d711d8 [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
14#include "AsmParser.h"
Daniel Dunbar475839e2009-06-29 20:37:27 +000015
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000016#include "llvm/ADT/SmallString.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 Lattner98986712010-01-14 22:21:20 +000021#include "llvm/MC/MCParsedAsmOperand.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000022#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000024#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfffff912009-10-16 01:34:54 +000025#include "llvm/MC/MCValue.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000027#include "llvm/Support/SourceMgr.h"
28#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 Dunbar7c0a3342009-08-26 22:49:51 +000035// Mach-O section uniquing.
36//
37// FIXME: Figure out where this should live, it should be shared by
38// TargetLoweringObjectFile.
39typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
40
Chris Lattnerebb89b42009-09-27 21:16:52 +000041AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
42 const MCAsmInfo &_MAI)
43 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
44 SectionUniquingMap(0) {
45 // Debugging directives.
46 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
47 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
48 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
49}
50
51
52
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000053AsmParser::~AsmParser() {
54 // If we have the MachO uniquing map, free it.
55 delete (MachOUniqueMapTy*)SectionUniquingMap;
56}
57
58const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
59 const StringRef &Section,
60 unsigned TypeAndAttributes,
61 unsigned Reserved2,
62 SectionKind Kind) const {
63 // We unique sections by their segment/section pair. The returned section
64 // may not have the same flags as the requested section, if so this should be
65 // diagnosed by the client as an error.
66
67 // Create the map if it doesn't already exist.
68 if (SectionUniquingMap == 0)
69 SectionUniquingMap = new MachOUniqueMapTy();
70 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
71
72 // Form the name to look up.
73 SmallString<64> Name;
74 Name += Segment;
75 Name.push_back(',');
76 Name += Section;
77
78 // Do the lookup, if we have a hit, return it.
79 const MCSectionMachO *&Entry = Map[Name.str()];
80
81 // FIXME: This should validate the type and attributes.
82 if (Entry) return Entry;
83
84 // Otherwise, return a new section.
85 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
86 Reserved2, Kind, Ctx);
87}
88
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000089void AsmParser::Warning(SMLoc L, const Twine &Msg) {
90 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000091}
92
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000093bool AsmParser::Error(SMLoc L, const Twine &Msg) {
94 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000095 return true;
96}
97
98bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000099 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000100 return true;
101}
102
Chris Lattner27aa7d22009-06-21 20:16:42 +0000103bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000104 // Create the initial section.
105 //
106 // FIXME: Support -n.
107 // FIXME: Target hook & command line option for initial section.
108 Out.SwitchSection(getMachOSection("__TEXT", "__text",
109 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
110 0, SectionKind()));
111
112
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000113 // Prime the lexer.
114 Lexer.Lex();
115
Chris Lattnerb717fb02009-07-02 21:53:43 +0000116 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000117
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000118 AsmCond StartingCondState = TheCondState;
119
Chris Lattnerb717fb02009-07-02 21:53:43 +0000120 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000121 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000122 // Handle conditional assembly here before calling ParseStatement()
123 if (Lexer.getKind() == AsmToken::Identifier) {
124 // If we have an identifier, handle it as the key symbol.
125 AsmToken ID = Lexer.getTok();
126 SMLoc IDLoc = ID.getLoc();
127 StringRef IDVal = ID.getString();
128
129 if (IDVal == ".if" ||
130 IDVal == ".elseif" ||
131 IDVal == ".else" ||
132 IDVal == ".endif") {
133 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
134 continue;
135 HadError = true;
136 EatToEndOfStatement();
137 continue;
138 }
139 }
140 if (TheCondState.Ignore) {
141 EatToEndOfStatement();
142 continue;
143 }
144
Chris Lattnerb717fb02009-07-02 21:53:43 +0000145 if (!ParseStatement()) continue;
146
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000147 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000148 HadError = true;
149 EatToEndOfStatement();
150 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000151
152 if (TheCondState.TheCond != StartingCondState.TheCond ||
153 TheCondState.Ignore != StartingCondState.Ignore)
154 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000155
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000156 if (!HadError)
157 Out.Finish();
158
Chris Lattnerb717fb02009-07-02 21:53:43 +0000159 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000160}
161
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000162/// ParseConditionalAssemblyDirectives - parse the conditional assembly
163/// directives
164bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
165 SMLoc DirectiveLoc) {
166 if (Directive == ".if")
167 return ParseDirectiveIf(DirectiveLoc);
168 if (Directive == ".elseif")
169 return ParseDirectiveElseIf(DirectiveLoc);
170 if (Directive == ".else")
171 return ParseDirectiveElse(DirectiveLoc);
172 if (Directive == ".endif")
173 return ParseDirectiveEndIf(DirectiveLoc);
174 return true;
175}
176
Chris Lattner2cf5f142009-06-22 01:29:09 +0000177/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
178void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000179 while (Lexer.isNot(AsmToken::EndOfStatement) &&
180 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000181 Lexer.Lex();
182
183 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000184 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000185 Lexer.Lex();
186}
187
Chris Lattnerc4193832009-06-22 05:51:26 +0000188
Chris Lattner74ec1a32009-06-22 06:32:03 +0000189/// ParseParenExpr - Parse a paren expression and return it.
190/// NOTE: This assumes the leading '(' has already been consumed.
191///
192/// parenexpr ::= expr)
193///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000194bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000195 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000196 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000197 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000198 EndLoc = Lexer.getLoc();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000199 Lexer.Lex();
200 return false;
201}
Chris Lattnerc4193832009-06-22 05:51:26 +0000202
Daniel Dunbar959fd882009-08-26 22:13:22 +0000203MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
204 if (MCSymbol *S = Ctx.LookupSymbol(Name))
205 return S;
206
207 // If the label starts with L it is an assembler temporary label.
208 if (Name.startswith("L"))
209 return Ctx.CreateTemporarySymbol(Name);
210
211 return Ctx.CreateSymbol(Name);
212}
213
Chris Lattner74ec1a32009-06-22 06:32:03 +0000214/// ParsePrimaryExpr - Parse a primary expression and return it.
215/// primaryexpr ::= (parenexpr
216/// primaryexpr ::= symbol
217/// primaryexpr ::= number
218/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000219bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000220 switch (Lexer.getKind()) {
221 default:
222 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000223 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000224 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000225 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000226 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000227 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000228 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000229 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000230 case AsmToken::Identifier: {
231 // This is a symbol reference.
232 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000233 EndLoc = Lexer.getLoc();
Chris Lattnerc4193832009-06-22 05:51:26 +0000234 Lexer.Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000235
236 // If this is an absolute variable reference, substitute it now to preserve
237 // semantics in the face of reassignment.
238 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
239 Res = Sym->getValue();
240 return false;
241 }
242
243 // Otherwise create a symbol ref.
244 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000245 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000246 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000247 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000248 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000249 EndLoc = Lexer.getLoc();
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000250 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000251 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000252 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000253 Lexer.Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000255 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000256 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000257 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000259 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000260 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000261 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000263 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000264 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000265 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000266 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000267 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000269 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000271 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000272 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000273 }
274}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000275
Chris Lattnerb4307b32010-01-15 19:28:38 +0000276bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000277 SMLoc EndLoc;
278 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000279}
280
Chris Lattner74ec1a32009-06-22 06:32:03 +0000281/// ParseExpression - Parse an expression and return it.
282///
283/// expr ::= expr +,- expr -> lowest.
284/// expr ::= expr |,^,&,! expr -> middle.
285/// expr ::= expr *,/,%,<<,>> expr -> highest.
286/// expr ::= primaryexpr
287///
Chris Lattner54482b42010-01-15 19:39:23 +0000288bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000289 Res = 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000290 return ParsePrimaryExpr(Res, EndLoc) ||
291 ParseBinOpRHS(1, Res, EndLoc);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000292}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000293
Chris Lattnerb4307b32010-01-15 19:28:38 +0000294bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
295 if (ParseParenExpr(Res, EndLoc))
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000296 return true;
297
298 return false;
299}
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
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401 Lexer.Lex();
402
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)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000429 Lexer.Lex();
430 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000431 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000432
433 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000434 AsmToken ID = Lexer.getTok();
435 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000436 StringRef IDVal;
437 if (ParseIdentifier(IDVal))
438 return TokError("unexpected token at start of statement");
439
440 // FIXME: Recurse on local labels?
441
442 // See what kind of statement we have.
443 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000444 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000445 // identifier ':' -> Label.
446 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000447
448 // Diagnose attempt to use a variable as a label.
449 //
450 // FIXME: Diagnostics. Note the location of the definition as a label.
451 // FIXME: This doesn't diagnose assignment to a symbol which has been
452 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000453 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000454 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000455 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000456
Daniel Dunbar959fd882009-08-26 22:13:22 +0000457 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000458 Out.EmitLabel(Sym);
459
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000460 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000461 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000462
Daniel Dunbar3f872332009-07-28 16:08:33 +0000463 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000464 // identifier '=' ... -> assignment statement
465 Lexer.Lex();
466
Daniel Dunbare2ace502009-08-31 08:09:09 +0000467 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000468
469 default: // Normal instruction or directive.
470 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000471 }
472
473 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000474 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000475 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000477 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000479 // FIXME: This changes behavior based on the -static flag to the
480 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000481 return ParseDirectiveSectionSwitch("__TEXT", "__text",
482 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000483 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000484 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000485 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000486 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000488 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
489 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000491 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000492 MCSectionMachO::S_4BYTE_LITERALS,
493 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000495 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000496 MCSectionMachO::S_8BYTE_LITERALS,
497 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000498 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000499 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 MCSectionMachO::S_16BYTE_LITERALS,
501 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000502 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000503 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000504 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000505 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000506 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000507 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000508 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000509 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
510
511 // FIXME: The assembler manual claims that this has the self modify code
512 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000513 if (IDVal == ".symbol_stub")
514 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
515 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000516 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
517 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000518 0, 16);
519 // FIXME: PowerPC only?
520 if (IDVal == ".picsymbol_stub")
521 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
522 MCSectionMachO::S_SYMBOL_STUBS |
523 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
524 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000526 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000528 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
529
530 // FIXME: The section names of these two are misspelled in the assembler
531 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000533 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
534 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
535 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
538 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
539 4);
540
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000544 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
546 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000548 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
550 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000553
554
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 return ParseDirectiveSectionSwitch("__OBJC", "__class",
557 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
560 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000562 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
582 MCSectionMachO::S_LITERAL_POINTERS,
583 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
586 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
587 MCSectionMachO::S_LITERAL_POINTERS,
588 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000590 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
591 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000593 return ParseDirectiveSectionSwitch("__OBJC", "__category",
594 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000596 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
597 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000602 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
603 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000605 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
606 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000608 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
609 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000611 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
612 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000614 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
615 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000616
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000617 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000619 return ParseDirectiveSet();
620
Daniel Dunbara0d14262009-06-24 23:30:00 +0000621 // Data directives
622
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000624 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000626 return ParseDirectiveAscii(true);
627
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000629 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000630 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000631 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000633 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000635 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000636
637 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000639 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000641 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000643 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000645 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000647 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000649 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000651 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000653 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
654
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000656 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000657
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000659 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000661 return ParseDirectiveSpace();
662
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000663 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000664
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000666 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000668 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000670 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000672 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000674 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000676 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000678 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000680 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000682 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000684 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000686 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000688 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
689
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000691 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000693 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000695 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000697 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000699 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000700
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000702 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000704 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000706 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000708 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000710 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000711
Chris Lattnerebb89b42009-09-27 21:16:52 +0000712 // Look up the handler in the handler table,
713 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
714 if (Handler)
715 return (this->*Handler)(IDVal, IDLoc);
716
Kevin Enderby9c656452009-09-10 20:51:44 +0000717 // Target hook for parsing target specific directives.
718 if (!getTargetParser().ParseDirective(ID))
719 return false;
720
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000721 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000722 EatToEndOfStatement();
723 return false;
724 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000725
Chris Lattner98986712010-01-14 22:21:20 +0000726
727 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
728 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
729 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000730 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000731
Daniel Dunbar3f872332009-07-28 16:08:33 +0000732 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000733 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000734 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000735
736 // Eat the end of statement marker.
737 Lexer.Lex();
738
Chris Lattner98986712010-01-14 22:21:20 +0000739
740 MCInst Inst;
741
742 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
743
744 // Free any parsed operands.
745 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
746 delete ParsedOperands[i];
747
748 if (MatchFail) {
749 // FIXME: We should give nicer diagnostics about the exact failure.
750 Error(IDLoc, "unrecognized instruction");
751 return true;
752 }
753
Chris Lattner2cf5f142009-06-22 01:29:09 +0000754 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000755 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000756
757 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000758 return false;
759}
Chris Lattner9a023f72009-06-24 04:43:34 +0000760
Daniel Dunbare2ace502009-08-31 08:09:09 +0000761bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000762 // FIXME: Use better location, we should use proper tokens.
763 SMLoc EqualLoc = Lexer.getLoc();
764
Daniel Dunbar821e3332009-08-31 08:09:28 +0000765 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000766 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000767 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000768 return true;
769
Daniel Dunbar3f872332009-07-28 16:08:33 +0000770 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000771 return TokError("unexpected token in assignment");
772
773 // Eat the end of statement marker.
774 Lexer.Lex();
775
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000776 // Validate that the LHS is allowed to be a variable (either it has not been
777 // used as a symbol, or it is an absolute symbol).
778 MCSymbol *Sym = getContext().LookupSymbol(Name);
779 if (Sym) {
780 // Diagnose assignment to a label.
781 //
782 // FIXME: Diagnostics. Note the location of the definition as a label.
783 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
784 if (!Sym->isUndefined() && !Sym->isAbsolute())
785 return Error(EqualLoc, "redefinition of '" + Name + "'");
786 else if (!Sym->isVariable())
787 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
788 else if (!isa<MCConstantExpr>(Sym->getValue()))
789 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
790 Name + "'");
791 } else
792 Sym = CreateSymbol(Name);
793
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000794 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000795
796 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000797 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000798
799 return false;
800}
801
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000802/// ParseIdentifier:
803/// ::= identifier
804/// ::= string
805bool AsmParser::ParseIdentifier(StringRef &Res) {
806 if (Lexer.isNot(AsmToken::Identifier) &&
807 Lexer.isNot(AsmToken::String))
808 return true;
809
810 Res = Lexer.getTok().getIdentifier();
811
812 Lexer.Lex(); // Consume the identifier token.
813
814 return false;
815}
816
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000817/// ParseDirectiveSet:
818/// ::= .set identifier ',' expression
819bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000820 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000821
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000822 if (ParseIdentifier(Name))
823 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000824
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000825 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000826 return TokError("unexpected token in '.set'");
827 Lexer.Lex();
828
Daniel Dunbare2ace502009-08-31 08:09:09 +0000829 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000830}
831
Chris Lattner9a023f72009-06-24 04:43:34 +0000832/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000833/// ::= .section identifier (',' identifier)*
834/// FIXME: This should actually parse out the segment, section, attributes and
835/// sizeof_stub fields.
836bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000837 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000838
Daniel Dunbarace63122009-08-11 03:42:33 +0000839 StringRef SectionName;
840 if (ParseIdentifier(SectionName))
841 return Error(Loc, "expected identifier after '.section' directive");
842
843 // Verify there is a following comma.
844 if (!Lexer.is(AsmToken::Comma))
845 return TokError("unexpected token in '.section' directive");
846
Chris Lattnerff4bc462009-08-10 01:39:42 +0000847 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000848 SectionSpec += ",";
849
850 // Add all the tokens until the end of the line, ParseSectionSpecifier will
851 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000852 StringRef EOL = Lexer.LexUntilEndOfStatement();
853 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000854
Chris Lattnerff4bc462009-08-10 01:39:42 +0000855 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000856 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000857 return TokError("unexpected token in '.section' directive");
858 Lexer.Lex();
859
Chris Lattnerff4bc462009-08-10 01:39:42 +0000860
861 StringRef Segment, Section;
862 unsigned TAA, StubSize;
863 std::string ErrorStr =
864 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
865 TAA, StubSize);
866
867 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000868 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000869
Chris Lattner56594f92009-07-31 17:47:16 +0000870 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000871 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
872 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000873 return false;
874}
875
Chris Lattnere15c2d72009-08-10 18:05:55 +0000876/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000877bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
878 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000879 unsigned TAA, unsigned Align,
880 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000881 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000882 return TokError("unexpected token in section switching directive");
883 Lexer.Lex();
884
Chris Lattner56594f92009-07-31 17:47:16 +0000885 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000886 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
887 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000888
889 // Set the implicit alignment, if any.
890 //
891 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
892 // alignment on the section (e.g., if one manually inserts bytes into the
893 // section, then just issueing the section switch directive will not realign
894 // the section. However, this is arguably more reasonable behavior, and there
895 // is no good reason for someone to intentionally emit incorrectly sized
896 // values into the implicitly aligned sections.
897 if (Align)
898 Out.EmitValueToAlignment(Align, 0, 1, 0);
899
Chris Lattner529fb542009-06-24 05:13:15 +0000900 return false;
901}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000902
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000903bool AsmParser::ParseEscapedString(std::string &Data) {
904 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
905
906 Data = "";
907 StringRef Str = Lexer.getTok().getStringContents();
908 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
909 if (Str[i] != '\\') {
910 Data += Str[i];
911 continue;
912 }
913
914 // Recognize escaped characters. Note that this escape semantics currently
915 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
916 ++i;
917 if (i == e)
918 return TokError("unexpected backslash at end of string");
919
920 // Recognize octal sequences.
921 if ((unsigned) (Str[i] - '0') <= 7) {
922 // Consume up to three octal characters.
923 unsigned Value = Str[i] - '0';
924
925 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
926 ++i;
927 Value = Value * 8 + (Str[i] - '0');
928
929 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
930 ++i;
931 Value = Value * 8 + (Str[i] - '0');
932 }
933 }
934
935 if (Value > 255)
936 return TokError("invalid octal escape sequence (out of range)");
937
938 Data += (unsigned char) Value;
939 continue;
940 }
941
942 // Otherwise recognize individual escapes.
943 switch (Str[i]) {
944 default:
945 // Just reject invalid escape sequences for now.
946 return TokError("invalid escape sequence (unrecognized character)");
947
948 case 'b': Data += '\b'; break;
949 case 'f': Data += '\f'; break;
950 case 'n': Data += '\n'; break;
951 case 'r': Data += '\r'; break;
952 case 't': Data += '\t'; break;
953 case '"': Data += '"'; break;
954 case '\\': Data += '\\'; break;
955 }
956 }
957
958 return false;
959}
960
Daniel Dunbara0d14262009-06-24 23:30:00 +0000961/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000962/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000964 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000965 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000966 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000967 return TokError("expected string in '.ascii' or '.asciz' directive");
968
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000969 std::string Data;
970 if (ParseEscapedString(Data))
971 return true;
972
Chris Lattneraaec2052010-01-19 19:46:13 +0000973 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +0000975 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976
977 Lexer.Lex();
978
Daniel Dunbar3f872332009-07-28 16:08:33 +0000979 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 break;
981
Daniel Dunbar3f872332009-07-28 16:08:33 +0000982 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000983 return TokError("unexpected token in '.ascii' or '.asciz' directive");
984 Lexer.Lex();
985 }
986 }
987
988 Lexer.Lex();
989 return false;
990}
991
992/// ParseDirectiveValue
993/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
994bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000995 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000997 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +0000998 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000999 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001000 return true;
1001
Chris Lattneraaec2052010-01-19 19:46:13 +00001002 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001003
Daniel Dunbar3f872332009-07-28 16:08:33 +00001004 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001005 break;
1006
1007 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001008 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001009 return TokError("unexpected token in directive");
1010 Lexer.Lex();
1011 }
1012 }
1013
1014 Lexer.Lex();
1015 return false;
1016}
1017
1018/// ParseDirectiveSpace
1019/// ::= .space expression [ , expression ]
1020bool AsmParser::ParseDirectiveSpace() {
1021 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001022 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023 return true;
1024
1025 int64_t FillExpr = 0;
1026 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001027 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1028 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return TokError("unexpected token in '.space' directive");
1030 Lexer.Lex();
1031
Daniel Dunbar475839e2009-06-29 20:37:27 +00001032 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033 return true;
1034
1035 HasFillExpr = true;
1036
Daniel Dunbar3f872332009-07-28 16:08:33 +00001037 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001038 return TokError("unexpected token in '.space' directive");
1039 }
1040
1041 Lexer.Lex();
1042
1043 if (NumBytes <= 0)
1044 return TokError("invalid number of bytes in '.space' directive");
1045
1046 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001047 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048
1049 return false;
1050}
1051
1052/// ParseDirectiveFill
1053/// ::= .fill expression , expression , expression
1054bool AsmParser::ParseDirectiveFill() {
1055 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001056 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057 return true;
1058
Daniel Dunbar3f872332009-07-28 16:08:33 +00001059 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060 return TokError("unexpected token in '.fill' directive");
1061 Lexer.Lex();
1062
1063 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001064 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001065 return true;
1066
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return TokError("unexpected token in '.fill' directive");
1069 Lexer.Lex();
1070
1071 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001072 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 return true;
1074
Daniel Dunbar3f872332009-07-28 16:08:33 +00001075 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076 return TokError("unexpected token in '.fill' directive");
1077
1078 Lexer.Lex();
1079
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001080 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1081 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082
1083 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001084 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1085 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086
1087 return false;
1088}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001089
1090/// ParseDirectiveOrg
1091/// ::= .org expression [ , expression ]
1092bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001093 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001094 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001095 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001096 return true;
1097
1098 // Parse optional fill expression.
1099 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001100 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1101 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001102 return TokError("unexpected token in '.org' directive");
1103 Lexer.Lex();
1104
Daniel Dunbar475839e2009-06-29 20:37:27 +00001105 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001106 return true;
1107
Daniel Dunbar3f872332009-07-28 16:08:33 +00001108 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001109 return TokError("unexpected token in '.org' directive");
1110 }
1111
1112 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001113
1114 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1115 // has to be relative to the current section.
1116 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001117
1118 return false;
1119}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001120
1121/// ParseDirectiveAlign
1122/// ::= {.align, ...} expression [ , expression [ , expression ]]
1123bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001124 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001125 int64_t Alignment;
1126 if (ParseAbsoluteExpression(Alignment))
1127 return true;
1128
1129 SMLoc MaxBytesLoc;
1130 bool HasFillExpr = false;
1131 int64_t FillExpr = 0;
1132 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001133 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1134 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001135 return TokError("unexpected token in directive");
1136 Lexer.Lex();
1137
1138 // The fill expression can be omitted while specifying a maximum number of
1139 // alignment bytes, e.g:
1140 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001141 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001142 HasFillExpr = true;
1143 if (ParseAbsoluteExpression(FillExpr))
1144 return true;
1145 }
1146
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1148 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001149 return TokError("unexpected token in directive");
1150 Lexer.Lex();
1151
1152 MaxBytesLoc = Lexer.getLoc();
1153 if (ParseAbsoluteExpression(MaxBytesToFill))
1154 return true;
1155
Daniel Dunbar3f872332009-07-28 16:08:33 +00001156 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001157 return TokError("unexpected token in directive");
1158 }
1159 }
1160
1161 Lexer.Lex();
1162
1163 if (!HasFillExpr) {
1164 // FIXME: Sometimes fill with nop.
1165 FillExpr = 0;
1166 }
1167
1168 // Compute alignment in bytes.
1169 if (IsPow2) {
1170 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001171 if (Alignment >= 32) {
1172 Error(AlignmentLoc, "invalid alignment value");
1173 Alignment = 31;
1174 }
1175
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001176 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177 }
1178
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001179 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001180 if (MaxBytesLoc.isValid()) {
1181 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001182 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1183 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001184 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001185 }
1186
1187 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001188 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1189 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001190 MaxBytesToFill = 0;
1191 }
1192 }
1193
1194 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1195 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1196
1197 return false;
1198}
1199
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001200/// ParseDirectiveSymbolAttribute
1201/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1202bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001203 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001204 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001205 StringRef Name;
1206
1207 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001208 return TokError("expected identifier in directive");
1209
Daniel Dunbar959fd882009-08-26 22:13:22 +00001210 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001211
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001212 Out.EmitSymbolAttribute(Sym, Attr);
1213
Daniel Dunbar3f872332009-07-28 16:08:33 +00001214 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001215 break;
1216
Daniel Dunbar3f872332009-07-28 16:08:33 +00001217 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001218 return TokError("unexpected token in directive");
1219 Lexer.Lex();
1220 }
1221 }
1222
1223 Lexer.Lex();
1224 return false;
1225}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001226
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001227/// ParseDirectiveDarwinSymbolDesc
1228/// ::= .desc identifier , expression
1229bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001230 StringRef Name;
1231 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001232 return TokError("expected identifier in directive");
1233
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001234 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001235 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001236
Daniel Dunbar3f872332009-07-28 16:08:33 +00001237 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001238 return TokError("unexpected token in '.desc' directive");
1239 Lexer.Lex();
1240
1241 SMLoc DescLoc = Lexer.getLoc();
1242 int64_t DescValue;
1243 if (ParseAbsoluteExpression(DescValue))
1244 return true;
1245
Daniel Dunbar3f872332009-07-28 16:08:33 +00001246 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001247 return TokError("unexpected token in '.desc' directive");
1248
1249 Lexer.Lex();
1250
1251 // Set the n_desc field of this Symbol to this DescValue
1252 Out.EmitSymbolDesc(Sym, DescValue);
1253
1254 return false;
1255}
1256
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001257/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001258/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1259bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001260 SMLoc IDLoc = Lexer.getLoc();
1261 StringRef Name;
1262 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001263 return TokError("expected identifier in directive");
1264
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001265 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001266 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001267
Daniel Dunbar3f872332009-07-28 16:08:33 +00001268 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001269 return TokError("unexpected token in directive");
1270 Lexer.Lex();
1271
1272 int64_t Size;
1273 SMLoc SizeLoc = Lexer.getLoc();
1274 if (ParseAbsoluteExpression(Size))
1275 return true;
1276
1277 int64_t Pow2Alignment = 0;
1278 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001279 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001280 Lexer.Lex();
1281 Pow2AlignmentLoc = Lexer.getLoc();
1282 if (ParseAbsoluteExpression(Pow2Alignment))
1283 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001284
1285 // If this target takes alignments in bytes (not log) validate and convert.
1286 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1287 if (!isPowerOf2_64(Pow2Alignment))
1288 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1289 Pow2Alignment = Log2_64(Pow2Alignment);
1290 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001291 }
1292
Daniel Dunbar3f872332009-07-28 16:08:33 +00001293 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001294 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001295
1296 Lexer.Lex();
1297
Chris Lattner1fc3d752009-07-09 17:25:12 +00001298 // NOTE: a size of zero for a .comm should create a undefined symbol
1299 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001300 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001301 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1302 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001303
1304 // NOTE: The alignment in the directive is a power of 2 value, the assember
1305 // may internally end up wanting an alignment in bytes.
1306 // FIXME: Diagnose overflow.
1307 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001308 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1309 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001310
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001311 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001312 return Error(IDLoc, "invalid symbol redefinition");
1313
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001314 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001315 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001316 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001317 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1318 MCSectionMachO::S_ZEROFILL, 0,
1319 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001320 Sym, Size, 1 << Pow2Alignment);
1321 return false;
1322 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001323
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001324 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001325 return false;
1326}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001327
1328/// ParseDirectiveDarwinZerofill
1329/// ::= .zerofill segname , sectname [, identifier , size_expression [
1330/// , align_expression ]]
1331bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001332 // FIXME: Handle quoted names here.
1333
Daniel Dunbar3f872332009-07-28 16:08:33 +00001334 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001335 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001336 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001337 Lexer.Lex();
1338
Daniel Dunbar3f872332009-07-28 16:08:33 +00001339 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001340 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001341 Lexer.Lex();
1342
Daniel Dunbar3f872332009-07-28 16:08:33 +00001343 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001344 return TokError("expected section name after comma in '.zerofill' "
1345 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001346 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001347 Lexer.Lex();
1348
Chris Lattner9be3fee2009-07-10 22:20:30 +00001349 // If this is the end of the line all that was wanted was to create the
1350 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001351 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001352 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001353 Out.EmitZerofill(getMachOSection(Segment, Section,
1354 MCSectionMachO::S_ZEROFILL, 0,
1355 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001356 return false;
1357 }
1358
Daniel Dunbar3f872332009-07-28 16:08:33 +00001359 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001360 return TokError("unexpected token in directive");
1361 Lexer.Lex();
1362
Daniel Dunbar3f872332009-07-28 16:08:33 +00001363 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001364 return TokError("expected identifier in directive");
1365
1366 // handle the identifier as the key symbol.
1367 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001368 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001369 Lexer.Lex();
1370
Daniel Dunbar3f872332009-07-28 16:08:33 +00001371 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001372 return TokError("unexpected token in directive");
1373 Lexer.Lex();
1374
1375 int64_t Size;
1376 SMLoc SizeLoc = Lexer.getLoc();
1377 if (ParseAbsoluteExpression(Size))
1378 return true;
1379
1380 int64_t Pow2Alignment = 0;
1381 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001382 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001383 Lexer.Lex();
1384 Pow2AlignmentLoc = Lexer.getLoc();
1385 if (ParseAbsoluteExpression(Pow2Alignment))
1386 return true;
1387 }
1388
Daniel Dunbar3f872332009-07-28 16:08:33 +00001389 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001390 return TokError("unexpected token in '.zerofill' directive");
1391
1392 Lexer.Lex();
1393
1394 if (Size < 0)
1395 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1396 "than zero");
1397
1398 // NOTE: The alignment in the directive is a power of 2 value, the assember
1399 // may internally end up wanting an alignment in bytes.
1400 // FIXME: Diagnose overflow.
1401 if (Pow2Alignment < 0)
1402 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1403 "can't be less than zero");
1404
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001405 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001406 return Error(IDLoc, "invalid symbol redefinition");
1407
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001408 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001409 //
1410 // FIXME: Arch specific.
1411 Out.EmitZerofill(getMachOSection(Segment, Section,
1412 MCSectionMachO::S_ZEROFILL, 0,
1413 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001414 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001415
1416 return false;
1417}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001418
1419/// ParseDirectiveDarwinSubsectionsViaSymbols
1420/// ::= .subsections_via_symbols
1421bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001422 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001423 return TokError("unexpected token in '.subsections_via_symbols' directive");
1424
1425 Lexer.Lex();
1426
Kevin Enderbyf96db462009-07-16 17:56:39 +00001427 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001428
1429 return false;
1430}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001431
1432/// ParseDirectiveAbort
1433/// ::= .abort [ "abort_string" ]
1434bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001435 // FIXME: Use loc from directive.
1436 SMLoc Loc = Lexer.getLoc();
1437
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001438 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001439 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1440 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001441 return TokError("expected string in '.abort' directive");
1442
Daniel Dunbar419aded2009-07-28 16:38:40 +00001443 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001444
1445 Lexer.Lex();
1446 }
1447
Daniel Dunbar3f872332009-07-28 16:08:33 +00001448 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001449 return TokError("unexpected token in '.abort' directive");
1450
1451 Lexer.Lex();
1452
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001453 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001454 if (Str.empty())
1455 Error(Loc, ".abort detected. Assembly stopping.");
1456 else
1457 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001458
1459 return false;
1460}
Kevin Enderby71148242009-07-14 21:35:03 +00001461
1462/// ParseDirectiveLsym
1463/// ::= .lsym identifier , expression
1464bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001465 StringRef Name;
1466 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001467 return TokError("expected identifier in directive");
1468
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001469 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001470 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001471
Daniel Dunbar3f872332009-07-28 16:08:33 +00001472 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001473 return TokError("unexpected token in '.lsym' directive");
1474 Lexer.Lex();
1475
Daniel Dunbar821e3332009-08-31 08:09:28 +00001476 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001477 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001478 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001479 return true;
1480
Daniel Dunbar3f872332009-07-28 16:08:33 +00001481 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001482 return TokError("unexpected token in '.lsym' directive");
1483
1484 Lexer.Lex();
1485
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001486 // We don't currently support this directive.
1487 //
1488 // FIXME: Diagnostic location!
1489 (void) Sym;
1490 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001491}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001492
1493/// ParseDirectiveInclude
1494/// ::= .include "filename"
1495bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001496 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001497 return TokError("expected string in '.include' directive");
1498
Daniel Dunbar419aded2009-07-28 16:38:40 +00001499 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001500 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001501 Lexer.Lex();
1502
Daniel Dunbar3f872332009-07-28 16:08:33 +00001503 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001504 return TokError("unexpected token in '.include' directive");
1505
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001506 // Strip the quotes.
1507 Filename = Filename.substr(1, Filename.size()-2);
1508
1509 // Attempt to switch the lexer to the included file before consuming the end
1510 // of statement to avoid losing it when we switch.
1511 if (Lexer.EnterIncludeFile(Filename)) {
1512 Lexer.PrintMessage(IncludeLoc,
1513 "Could not find include file '" + Filename + "'",
1514 "error");
1515 return true;
1516 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001517
1518 return false;
1519}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001520
1521/// ParseDirectiveDarwinDumpOrLoad
1522/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001523bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001524 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001525 return TokError("expected string in '.dump' or '.load' directive");
1526
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001527 Lexer.Lex();
1528
Daniel Dunbar3f872332009-07-28 16:08:33 +00001529 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001530 return TokError("unexpected token in '.dump' or '.load' directive");
1531
1532 Lexer.Lex();
1533
Kevin Enderby5026ae42009-07-20 20:25:37 +00001534 // FIXME: If/when .dump and .load are implemented they will be done in the
1535 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001536 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001537 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001538 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001539 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001540
1541 return false;
1542}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001543
1544/// ParseDirectiveIf
1545/// ::= .if expression
1546bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1547 // Consume the identifier that was the .if directive
1548 Lexer.Lex();
1549
1550 TheCondStack.push_back(TheCondState);
1551 TheCondState.TheCond = AsmCond::IfCond;
1552 if(TheCondState.Ignore) {
1553 EatToEndOfStatement();
1554 }
1555 else {
1556 int64_t ExprValue;
1557 if (ParseAbsoluteExpression(ExprValue))
1558 return true;
1559
1560 if (Lexer.isNot(AsmToken::EndOfStatement))
1561 return TokError("unexpected token in '.if' directive");
1562
1563 Lexer.Lex();
1564
1565 TheCondState.CondMet = ExprValue;
1566 TheCondState.Ignore = !TheCondState.CondMet;
1567 }
1568
1569 return false;
1570}
1571
1572/// ParseDirectiveElseIf
1573/// ::= .elseif expression
1574bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1575 if (TheCondState.TheCond != AsmCond::IfCond &&
1576 TheCondState.TheCond != AsmCond::ElseIfCond)
1577 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1578 " an .elseif");
1579 TheCondState.TheCond = AsmCond::ElseIfCond;
1580
1581 // Consume the identifier that was the .elseif directive
1582 Lexer.Lex();
1583
1584 bool LastIgnoreState = false;
1585 if (!TheCondStack.empty())
1586 LastIgnoreState = TheCondStack.back().Ignore;
1587 if (LastIgnoreState || TheCondState.CondMet) {
1588 TheCondState.Ignore = true;
1589 EatToEndOfStatement();
1590 }
1591 else {
1592 int64_t ExprValue;
1593 if (ParseAbsoluteExpression(ExprValue))
1594 return true;
1595
1596 if (Lexer.isNot(AsmToken::EndOfStatement))
1597 return TokError("unexpected token in '.elseif' directive");
1598
1599 Lexer.Lex();
1600 TheCondState.CondMet = ExprValue;
1601 TheCondState.Ignore = !TheCondState.CondMet;
1602 }
1603
1604 return false;
1605}
1606
1607/// ParseDirectiveElse
1608/// ::= .else
1609bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1610 // Consume the identifier that was the .else directive
1611 Lexer.Lex();
1612
1613 if (Lexer.isNot(AsmToken::EndOfStatement))
1614 return TokError("unexpected token in '.else' directive");
1615
1616 Lexer.Lex();
1617
1618 if (TheCondState.TheCond != AsmCond::IfCond &&
1619 TheCondState.TheCond != AsmCond::ElseIfCond)
1620 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1621 ".elseif");
1622 TheCondState.TheCond = AsmCond::ElseCond;
1623 bool LastIgnoreState = false;
1624 if (!TheCondStack.empty())
1625 LastIgnoreState = TheCondStack.back().Ignore;
1626 if (LastIgnoreState || TheCondState.CondMet)
1627 TheCondState.Ignore = true;
1628 else
1629 TheCondState.Ignore = false;
1630
1631 return false;
1632}
1633
1634/// ParseDirectiveEndIf
1635/// ::= .endif
1636bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1637 // Consume the identifier that was the .endif directive
1638 Lexer.Lex();
1639
1640 if (Lexer.isNot(AsmToken::EndOfStatement))
1641 return TokError("unexpected token in '.endif' directive");
1642
1643 Lexer.Lex();
1644
1645 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1646 TheCondStack.empty())
1647 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1648 ".else");
1649 if (!TheCondStack.empty()) {
1650 TheCondState = TheCondStack.back();
1651 TheCondStack.pop_back();
1652 }
1653
1654 return false;
1655}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001656
1657/// ParseDirectiveFile
1658/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001659bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001660 // FIXME: I'm not sure what this is.
1661 int64_t FileNumber = -1;
1662 if (Lexer.is(AsmToken::Integer)) {
1663 FileNumber = Lexer.getTok().getIntVal();
1664 Lexer.Lex();
1665
1666 if (FileNumber < 1)
1667 return TokError("file number less than one");
1668 }
1669
1670 if (Lexer.isNot(AsmToken::String))
1671 return TokError("unexpected token in '.file' directive");
1672
Bill Wendling9bc0af82009-12-28 01:34:57 +00001673 StringRef ATTRIBUTE_UNUSED FileName = Lexer.getTok().getString();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001674 Lexer.Lex();
1675
1676 if (Lexer.isNot(AsmToken::EndOfStatement))
1677 return TokError("unexpected token in '.file' directive");
1678
1679 // FIXME: Do something with the .file.
1680
1681 return false;
1682}
1683
1684/// ParseDirectiveLine
1685/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001686bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001687 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1688 if (Lexer.isNot(AsmToken::Integer))
1689 return TokError("unexpected token in '.line' directive");
1690
1691 int64_t LineNumber = Lexer.getTok().getIntVal();
1692 (void) LineNumber;
1693 Lexer.Lex();
1694
1695 // FIXME: Do something with the .line.
1696 }
1697
1698 if (Lexer.isNot(AsmToken::EndOfStatement))
1699 return TokError("unexpected token in '.file' directive");
1700
1701 return false;
1702}
1703
1704
1705/// ParseDirectiveLoc
1706/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001707bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001708 if (Lexer.isNot(AsmToken::Integer))
1709 return TokError("unexpected token in '.loc' directive");
1710
1711 // FIXME: What are these fields?
1712 int64_t FileNumber = Lexer.getTok().getIntVal();
1713 (void) FileNumber;
1714 // FIXME: Validate file.
1715
1716 Lexer.Lex();
1717 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1718 if (Lexer.isNot(AsmToken::Integer))
1719 return TokError("unexpected token in '.loc' directive");
1720
1721 int64_t Param2 = Lexer.getTok().getIntVal();
1722 (void) Param2;
1723 Lexer.Lex();
1724
1725 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1726 if (Lexer.isNot(AsmToken::Integer))
1727 return TokError("unexpected token in '.loc' directive");
1728
1729 int64_t Param3 = Lexer.getTok().getIntVal();
1730 (void) Param3;
1731 Lexer.Lex();
1732
1733 // FIXME: Do something with the .loc.
1734 }
1735 }
1736
1737 if (Lexer.isNot(AsmToken::EndOfStatement))
1738 return TokError("unexpected token in '.file' directive");
1739
1740 return false;
1741}
1742