blob: d4af4bd0df6e119046984f6f6b7c1e4ade5548a7 [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
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000032// Mach-O section uniquing.
33//
34// FIXME: Figure out where this should live, it should be shared by
35// TargetLoweringObjectFile.
36typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
37
Chris Lattnerebb89b42009-09-27 21:16:52 +000038AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
39 const MCAsmInfo &_MAI)
40 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
41 SectionUniquingMap(0) {
42 // Debugging directives.
43 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
44 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
45 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
46}
47
48
49
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000050AsmParser::~AsmParser() {
51 // If we have the MachO uniquing map, free it.
52 delete (MachOUniqueMapTy*)SectionUniquingMap;
53}
54
55const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
56 const StringRef &Section,
57 unsigned TypeAndAttributes,
58 unsigned Reserved2,
59 SectionKind Kind) const {
60 // We unique sections by their segment/section pair. The returned section
61 // may not have the same flags as the requested section, if so this should be
62 // diagnosed by the client as an error.
63
64 // Create the map if it doesn't already exist.
65 if (SectionUniquingMap == 0)
66 SectionUniquingMap = new MachOUniqueMapTy();
67 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
68
69 // Form the name to look up.
70 SmallString<64> Name;
71 Name += Segment;
72 Name.push_back(',');
73 Name += Section;
74
75 // Do the lookup, if we have a hit, return it.
76 const MCSectionMachO *&Entry = Map[Name.str()];
77
78 // FIXME: This should validate the type and attributes.
79 if (Entry) return Entry;
80
81 // Otherwise, return a new section.
82 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
83 Reserved2, Kind, Ctx);
84}
85
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000086void AsmParser::Warning(SMLoc L, const Twine &Msg) {
87 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000088}
89
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000090bool AsmParser::Error(SMLoc L, const Twine &Msg) {
91 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000092 return true;
93}
94
95bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000096 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000097 return true;
98}
99
Chris Lattner27aa7d22009-06-21 20:16:42 +0000100bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000101 // Create the initial section.
102 //
103 // FIXME: Support -n.
104 // FIXME: Target hook & command line option for initial section.
105 Out.SwitchSection(getMachOSection("__TEXT", "__text",
106 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
107 0, SectionKind()));
108
109
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000110 // Prime the lexer.
111 Lexer.Lex();
112
Chris Lattnerb717fb02009-07-02 21:53:43 +0000113 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000114
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000115 AsmCond StartingCondState = TheCondState;
116
Chris Lattnerb717fb02009-07-02 21:53:43 +0000117 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000118 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000119 // Handle conditional assembly here before calling ParseStatement()
120 if (Lexer.getKind() == AsmToken::Identifier) {
121 // If we have an identifier, handle it as the key symbol.
122 AsmToken ID = Lexer.getTok();
123 SMLoc IDLoc = ID.getLoc();
124 StringRef IDVal = ID.getString();
125
126 if (IDVal == ".if" ||
127 IDVal == ".elseif" ||
128 IDVal == ".else" ||
129 IDVal == ".endif") {
130 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
131 continue;
132 HadError = true;
133 EatToEndOfStatement();
134 continue;
135 }
136 }
137 if (TheCondState.Ignore) {
138 EatToEndOfStatement();
139 continue;
140 }
141
Chris Lattnerb717fb02009-07-02 21:53:43 +0000142 if (!ParseStatement()) continue;
143
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000144 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000145 HadError = true;
146 EatToEndOfStatement();
147 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000148
149 if (TheCondState.TheCond != StartingCondState.TheCond ||
150 TheCondState.Ignore != StartingCondState.Ignore)
151 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000152
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000153 if (!HadError)
154 Out.Finish();
155
Chris Lattnerb717fb02009-07-02 21:53:43 +0000156 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000157}
158
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000159/// ParseConditionalAssemblyDirectives - parse the conditional assembly
160/// directives
161bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
162 SMLoc DirectiveLoc) {
163 if (Directive == ".if")
164 return ParseDirectiveIf(DirectiveLoc);
165 if (Directive == ".elseif")
166 return ParseDirectiveElseIf(DirectiveLoc);
167 if (Directive == ".else")
168 return ParseDirectiveElse(DirectiveLoc);
169 if (Directive == ".endif")
170 return ParseDirectiveEndIf(DirectiveLoc);
171 return true;
172}
173
Chris Lattner2cf5f142009-06-22 01:29:09 +0000174/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
175void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000176 while (Lexer.isNot(AsmToken::EndOfStatement) &&
177 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000178 Lexer.Lex();
179
180 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000181 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000182 Lexer.Lex();
183}
184
Chris Lattnerc4193832009-06-22 05:51:26 +0000185
Chris Lattner74ec1a32009-06-22 06:32:03 +0000186/// ParseParenExpr - Parse a paren expression and return it.
187/// NOTE: This assumes the leading '(' has already been consumed.
188///
189/// parenexpr ::= expr)
190///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000191bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000192 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000193 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000194 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000195 EndLoc = Lexer.getLoc();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000196 Lexer.Lex();
197 return false;
198}
Chris Lattnerc4193832009-06-22 05:51:26 +0000199
Daniel Dunbar959fd882009-08-26 22:13:22 +0000200MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
201 if (MCSymbol *S = Ctx.LookupSymbol(Name))
202 return S;
203
204 // If the label starts with L it is an assembler temporary label.
205 if (Name.startswith("L"))
206 return Ctx.CreateTemporarySymbol(Name);
207
208 return Ctx.CreateSymbol(Name);
209}
210
Chris Lattner74ec1a32009-06-22 06:32:03 +0000211/// ParsePrimaryExpr - Parse a primary expression and return it.
212/// primaryexpr ::= (parenexpr
213/// primaryexpr ::= symbol
214/// primaryexpr ::= number
215/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000216bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000217 switch (Lexer.getKind()) {
218 default:
219 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000220 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000221 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000222 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000223 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000224 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000225 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000226 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000227 case AsmToken::Identifier: {
228 // This is a symbol reference.
229 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000230 EndLoc = Lexer.getLoc();
Chris Lattnerc4193832009-06-22 05:51:26 +0000231 Lexer.Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000232
233 // If this is an absolute variable reference, substitute it now to preserve
234 // semantics in the face of reassignment.
235 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
236 Res = Sym->getValue();
237 return false;
238 }
239
240 // Otherwise create a symbol ref.
241 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000242 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000243 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000244 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000245 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000246 EndLoc = Lexer.getLoc();
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000247 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000248 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000249 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000250 Lexer.Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000251 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000252 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000253 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000256 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000260 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000262 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000264 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000266 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000267 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000268 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000270 }
271}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000272
Chris Lattnerb4307b32010-01-15 19:28:38 +0000273bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000274 SMLoc EndLoc;
275 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000276}
277
Chris Lattner74ec1a32009-06-22 06:32:03 +0000278/// ParseExpression - Parse an expression and return it.
279///
280/// expr ::= expr +,- expr -> lowest.
281/// expr ::= expr |,^,&,! expr -> middle.
282/// expr ::= expr *,/,%,<<,>> expr -> highest.
283/// expr ::= primaryexpr
284///
Chris Lattner54482b42010-01-15 19:39:23 +0000285bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000286 Res = 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000287 return ParsePrimaryExpr(Res, EndLoc) ||
288 ParseBinOpRHS(1, Res, EndLoc);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000289}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000290
Chris Lattnerb4307b32010-01-15 19:28:38 +0000291bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
292 if (ParseParenExpr(Res, EndLoc))
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000293 return true;
294
295 return false;
296}
297
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000299 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000300
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000301 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000302 if (ParseExpression(Expr))
303 return true;
304
Daniel Dunbare00b0112009-10-16 01:57:52 +0000305 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000306 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307
308 return false;
309}
310
Daniel Dunbar3f872332009-07-28 16:08:33 +0000311static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000312 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000313 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000314 default:
315 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000316
317 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000318 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000319 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000320 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000321 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000322 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000323 return 1;
324
325 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000327 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000328 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000333 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::ExclaimEqual:
336 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000342 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000343 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000349 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 return 2;
351
352 // Intermediate Precedence: |, &, ^
353 //
354 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000357 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000358 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000359 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000361 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000362 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000363 return 3;
364
365 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000372 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000381 }
382}
383
384
385/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
386/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000387bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
388 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000389 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000392
393 // If the next token is lower precedence than we are allowed to eat, return
394 // successfully with what we ate already.
395 if (TokPrec < Precedence)
396 return false;
397
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 Lexer.Lex();
399
400 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000401 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000402 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000403
404 // If BinOp binds less tightly with RHS than the operator after RHS, let
405 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000407 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000408 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000409 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000410 }
411
Daniel Dunbar475839e2009-06-29 20:37:27 +0000412 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000413 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000414 }
415}
416
Chris Lattnerc4193832009-06-22 05:51:26 +0000417
418
419
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000420/// ParseStatement:
421/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000422/// ::= Label* Directive ...Operands... EndOfStatement
423/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000424bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000425 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000426 Lexer.Lex();
427 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000428 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000429
430 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000431 AsmToken ID = Lexer.getTok();
432 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000433 StringRef IDVal;
434 if (ParseIdentifier(IDVal))
435 return TokError("unexpected token at start of statement");
436
437 // FIXME: Recurse on local labels?
438
439 // See what kind of statement we have.
440 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000441 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000442 // identifier ':' -> Label.
443 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000444
445 // Diagnose attempt to use a variable as a label.
446 //
447 // FIXME: Diagnostics. Note the location of the definition as a label.
448 // FIXME: This doesn't diagnose assignment to a symbol which has been
449 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000450 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000451 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000452 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000453
Daniel Dunbar959fd882009-08-26 22:13:22 +0000454 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000455 Out.EmitLabel(Sym);
456
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000457 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000458 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000459
Daniel Dunbar3f872332009-07-28 16:08:33 +0000460 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000461 // identifier '=' ... -> assignment statement
462 Lexer.Lex();
463
Daniel Dunbare2ace502009-08-31 08:09:09 +0000464 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000465
466 default: // Normal instruction or directive.
467 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000468 }
469
470 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000471 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000472 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000473 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000474 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000475 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000476 // FIXME: This changes behavior based on the -static flag to the
477 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000478 return ParseDirectiveSectionSwitch("__TEXT", "__text",
479 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000481 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000483 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000485 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
486 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000488 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000489 MCSectionMachO::S_4BYTE_LITERALS,
490 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000491 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000492 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 MCSectionMachO::S_8BYTE_LITERALS,
494 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000495 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000496 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000497 MCSectionMachO::S_16BYTE_LITERALS,
498 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000502 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000504 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000505 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000506 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
507
508 // FIXME: The assembler manual claims that this has the self modify code
509 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000510 if (IDVal == ".symbol_stub")
511 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
512 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000513 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
514 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000515 0, 16);
516 // FIXME: PowerPC only?
517 if (IDVal == ".picsymbol_stub")
518 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
519 MCSectionMachO::S_SYMBOL_STUBS |
520 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
521 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000523 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000525 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
526
527 // FIXME: The section names of these two are misspelled in the assembler
528 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000530 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
531 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
532 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
535 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
536 4);
537
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000539 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000541 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
543 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000545 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
547 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000550
551
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000553 return ParseDirectiveSectionSwitch("__OBJC", "__class",
554 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
557 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
560 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000562 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
579 MCSectionMachO::S_LITERAL_POINTERS,
580 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000582 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
583 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
584 MCSectionMachO::S_LITERAL_POINTERS,
585 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000586 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000587 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
588 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000590 return ParseDirectiveSectionSwitch("__OBJC", "__category",
591 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000593 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
594 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000596 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
597 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000602 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
603 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".objc_meth_var_types")
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_names")
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_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
612 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000613
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000614 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000616 return ParseDirectiveSet();
617
Daniel Dunbara0d14262009-06-24 23:30:00 +0000618 // Data directives
619
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000621 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000623 return ParseDirectiveAscii(true);
624
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000626 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000627 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000628 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000630 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000632 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000633
634 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000636 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000638 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000640 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000642 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000644 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000646 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000648 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000650 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
651
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000653 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000654
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000656 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000658 return ParseDirectiveSpace();
659
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000660 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000661
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000663 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000665 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000667 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000669 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000671 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000673 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000675 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000677 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000679 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000681 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000683 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000685 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
686
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000688 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000690 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000692 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000694 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000696 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000697
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000699 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000701 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000703 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000705 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000707 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000708
Chris Lattnerebb89b42009-09-27 21:16:52 +0000709 // Look up the handler in the handler table,
710 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
711 if (Handler)
712 return (this->*Handler)(IDVal, IDLoc);
713
Kevin Enderby9c656452009-09-10 20:51:44 +0000714 // Target hook for parsing target specific directives.
715 if (!getTargetParser().ParseDirective(ID))
716 return false;
717
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000718 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000719 EatToEndOfStatement();
720 return false;
721 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000722
Chris Lattner98986712010-01-14 22:21:20 +0000723
724 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
725 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
726 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000727 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000728
Daniel Dunbar3f872332009-07-28 16:08:33 +0000729 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000730 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000731 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000732
733 // Eat the end of statement marker.
734 Lexer.Lex();
735
Chris Lattner98986712010-01-14 22:21:20 +0000736
737 MCInst Inst;
738
739 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
740
741 // Free any parsed operands.
742 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
743 delete ParsedOperands[i];
744
745 if (MatchFail) {
746 // FIXME: We should give nicer diagnostics about the exact failure.
747 Error(IDLoc, "unrecognized instruction");
748 return true;
749 }
750
Chris Lattner2cf5f142009-06-22 01:29:09 +0000751 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000752 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000753
754 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000755 return false;
756}
Chris Lattner9a023f72009-06-24 04:43:34 +0000757
Daniel Dunbare2ace502009-08-31 08:09:09 +0000758bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000759 // FIXME: Use better location, we should use proper tokens.
760 SMLoc EqualLoc = Lexer.getLoc();
761
Daniel Dunbar821e3332009-08-31 08:09:28 +0000762 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000763 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000764 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000765 return true;
766
Daniel Dunbar3f872332009-07-28 16:08:33 +0000767 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000768 return TokError("unexpected token in assignment");
769
770 // Eat the end of statement marker.
771 Lexer.Lex();
772
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000773 // Validate that the LHS is allowed to be a variable (either it has not been
774 // used as a symbol, or it is an absolute symbol).
775 MCSymbol *Sym = getContext().LookupSymbol(Name);
776 if (Sym) {
777 // Diagnose assignment to a label.
778 //
779 // FIXME: Diagnostics. Note the location of the definition as a label.
780 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
781 if (!Sym->isUndefined() && !Sym->isAbsolute())
782 return Error(EqualLoc, "redefinition of '" + Name + "'");
783 else if (!Sym->isVariable())
784 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
785 else if (!isa<MCConstantExpr>(Sym->getValue()))
786 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
787 Name + "'");
788 } else
789 Sym = CreateSymbol(Name);
790
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000791 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000792
793 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000794 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000795
796 return false;
797}
798
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000799/// ParseIdentifier:
800/// ::= identifier
801/// ::= string
802bool AsmParser::ParseIdentifier(StringRef &Res) {
803 if (Lexer.isNot(AsmToken::Identifier) &&
804 Lexer.isNot(AsmToken::String))
805 return true;
806
807 Res = Lexer.getTok().getIdentifier();
808
809 Lexer.Lex(); // Consume the identifier token.
810
811 return false;
812}
813
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000814/// ParseDirectiveSet:
815/// ::= .set identifier ',' expression
816bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000817 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000818
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000819 if (ParseIdentifier(Name))
820 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000821
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000822 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000823 return TokError("unexpected token in '.set'");
824 Lexer.Lex();
825
Daniel Dunbare2ace502009-08-31 08:09:09 +0000826 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000827}
828
Chris Lattner9a023f72009-06-24 04:43:34 +0000829/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000830/// ::= .section identifier (',' identifier)*
831/// FIXME: This should actually parse out the segment, section, attributes and
832/// sizeof_stub fields.
833bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000834 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000835
Daniel Dunbarace63122009-08-11 03:42:33 +0000836 StringRef SectionName;
837 if (ParseIdentifier(SectionName))
838 return Error(Loc, "expected identifier after '.section' directive");
839
840 // Verify there is a following comma.
841 if (!Lexer.is(AsmToken::Comma))
842 return TokError("unexpected token in '.section' directive");
843
Chris Lattnerff4bc462009-08-10 01:39:42 +0000844 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000845 SectionSpec += ",";
846
847 // Add all the tokens until the end of the line, ParseSectionSpecifier will
848 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000849 StringRef EOL = Lexer.LexUntilEndOfStatement();
850 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000851
Chris Lattnerff4bc462009-08-10 01:39:42 +0000852 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000853 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000854 return TokError("unexpected token in '.section' directive");
855 Lexer.Lex();
856
Chris Lattnerff4bc462009-08-10 01:39:42 +0000857
858 StringRef Segment, Section;
859 unsigned TAA, StubSize;
860 std::string ErrorStr =
861 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
862 TAA, StubSize);
863
864 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000865 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000866
Chris Lattner56594f92009-07-31 17:47:16 +0000867 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000868 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
869 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000870 return false;
871}
872
Chris Lattnere15c2d72009-08-10 18:05:55 +0000873/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000874bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
875 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000876 unsigned TAA, unsigned Align,
877 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000878 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000879 return TokError("unexpected token in section switching directive");
880 Lexer.Lex();
881
Chris Lattner56594f92009-07-31 17:47:16 +0000882 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000883 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
884 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000885
886 // Set the implicit alignment, if any.
887 //
888 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
889 // alignment on the section (e.g., if one manually inserts bytes into the
890 // section, then just issueing the section switch directive will not realign
891 // the section. However, this is arguably more reasonable behavior, and there
892 // is no good reason for someone to intentionally emit incorrectly sized
893 // values into the implicitly aligned sections.
894 if (Align)
895 Out.EmitValueToAlignment(Align, 0, 1, 0);
896
Chris Lattner529fb542009-06-24 05:13:15 +0000897 return false;
898}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000899
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000900bool AsmParser::ParseEscapedString(std::string &Data) {
901 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
902
903 Data = "";
904 StringRef Str = Lexer.getTok().getStringContents();
905 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
906 if (Str[i] != '\\') {
907 Data += Str[i];
908 continue;
909 }
910
911 // Recognize escaped characters. Note that this escape semantics currently
912 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
913 ++i;
914 if (i == e)
915 return TokError("unexpected backslash at end of string");
916
917 // Recognize octal sequences.
918 if ((unsigned) (Str[i] - '0') <= 7) {
919 // Consume up to three octal characters.
920 unsigned Value = Str[i] - '0';
921
922 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
923 ++i;
924 Value = Value * 8 + (Str[i] - '0');
925
926 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
927 ++i;
928 Value = Value * 8 + (Str[i] - '0');
929 }
930 }
931
932 if (Value > 255)
933 return TokError("invalid octal escape sequence (out of range)");
934
935 Data += (unsigned char) Value;
936 continue;
937 }
938
939 // Otherwise recognize individual escapes.
940 switch (Str[i]) {
941 default:
942 // Just reject invalid escape sequences for now.
943 return TokError("invalid escape sequence (unrecognized character)");
944
945 case 'b': Data += '\b'; break;
946 case 'f': Data += '\f'; break;
947 case 'n': Data += '\n'; break;
948 case 'r': Data += '\r'; break;
949 case 't': Data += '\t'; break;
950 case '"': Data += '"'; break;
951 case '\\': Data += '\\'; break;
952 }
953 }
954
955 return false;
956}
957
Daniel Dunbara0d14262009-06-24 23:30:00 +0000958/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000959/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000961 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000962 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964 return TokError("expected string in '.ascii' or '.asciz' directive");
965
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000966 std::string Data;
967 if (ParseEscapedString(Data))
968 return true;
969
970 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000971 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000972 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000973
974 Lexer.Lex();
975
Daniel Dunbar3f872332009-07-28 16:08:33 +0000976 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977 break;
978
Daniel Dunbar3f872332009-07-28 16:08:33 +0000979 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 return TokError("unexpected token in '.ascii' or '.asciz' directive");
981 Lexer.Lex();
982 }
983 }
984
985 Lexer.Lex();
986 return false;
987}
988
989/// ParseDirectiveValue
990/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
991bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000992 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000993 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000994 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +0000995 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000996 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000997 return true;
998
Daniel Dunbar883f9202009-08-31 08:08:50 +0000999 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001000
Daniel Dunbar3f872332009-07-28 16:08:33 +00001001 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002 break;
1003
1004 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001005 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001006 return TokError("unexpected token in directive");
1007 Lexer.Lex();
1008 }
1009 }
1010
1011 Lexer.Lex();
1012 return false;
1013}
1014
1015/// ParseDirectiveSpace
1016/// ::= .space expression [ , expression ]
1017bool AsmParser::ParseDirectiveSpace() {
1018 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001019 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 return true;
1021
1022 int64_t FillExpr = 0;
1023 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001024 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1025 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 return TokError("unexpected token in '.space' directive");
1027 Lexer.Lex();
1028
Daniel Dunbar475839e2009-06-29 20:37:27 +00001029 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 return true;
1031
1032 HasFillExpr = true;
1033
Daniel Dunbar3f872332009-07-28 16:08:33 +00001034 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001035 return TokError("unexpected token in '.space' directive");
1036 }
1037
1038 Lexer.Lex();
1039
1040 if (NumBytes <= 0)
1041 return TokError("invalid number of bytes in '.space' directive");
1042
1043 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1044 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001045 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046
1047 return false;
1048}
1049
1050/// ParseDirectiveFill
1051/// ::= .fill expression , expression , expression
1052bool AsmParser::ParseDirectiveFill() {
1053 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001054 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 return true;
1056
Daniel Dunbar3f872332009-07-28 16:08:33 +00001057 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001058 return TokError("unexpected token in '.fill' directive");
1059 Lexer.Lex();
1060
1061 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001062 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063 return true;
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 '.fill' directive");
1067 Lexer.Lex();
1068
1069 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001070 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return true;
1072
Daniel Dunbar3f872332009-07-28 16:08:33 +00001073 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001074 return TokError("unexpected token in '.fill' directive");
1075
1076 Lexer.Lex();
1077
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001078 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1079 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080
1081 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001082 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083
1084 return false;
1085}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001086
1087/// ParseDirectiveOrg
1088/// ::= .org expression [ , expression ]
1089bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001090 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001091 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001092 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001093 return true;
1094
1095 // Parse optional fill expression.
1096 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001097 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1098 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001099 return TokError("unexpected token in '.org' directive");
1100 Lexer.Lex();
1101
Daniel Dunbar475839e2009-06-29 20:37:27 +00001102 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001103 return true;
1104
Daniel Dunbar3f872332009-07-28 16:08:33 +00001105 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001106 return TokError("unexpected token in '.org' directive");
1107 }
1108
1109 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001110
1111 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1112 // has to be relative to the current section.
1113 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001114
1115 return false;
1116}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001117
1118/// ParseDirectiveAlign
1119/// ::= {.align, ...} expression [ , expression [ , expression ]]
1120bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001121 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001122 int64_t Alignment;
1123 if (ParseAbsoluteExpression(Alignment))
1124 return true;
1125
1126 SMLoc MaxBytesLoc;
1127 bool HasFillExpr = false;
1128 int64_t FillExpr = 0;
1129 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001130 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1131 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001132 return TokError("unexpected token in directive");
1133 Lexer.Lex();
1134
1135 // The fill expression can be omitted while specifying a maximum number of
1136 // alignment bytes, e.g:
1137 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001138 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001139 HasFillExpr = true;
1140 if (ParseAbsoluteExpression(FillExpr))
1141 return true;
1142 }
1143
Daniel Dunbar3f872332009-07-28 16:08:33 +00001144 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1145 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001146 return TokError("unexpected token in directive");
1147 Lexer.Lex();
1148
1149 MaxBytesLoc = Lexer.getLoc();
1150 if (ParseAbsoluteExpression(MaxBytesToFill))
1151 return true;
1152
Daniel Dunbar3f872332009-07-28 16:08:33 +00001153 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001154 return TokError("unexpected token in directive");
1155 }
1156 }
1157
1158 Lexer.Lex();
1159
1160 if (!HasFillExpr) {
1161 // FIXME: Sometimes fill with nop.
1162 FillExpr = 0;
1163 }
1164
1165 // Compute alignment in bytes.
1166 if (IsPow2) {
1167 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001168 if (Alignment >= 32) {
1169 Error(AlignmentLoc, "invalid alignment value");
1170 Alignment = 31;
1171 }
1172
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001173 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001174 }
1175
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001176 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177 if (MaxBytesLoc.isValid()) {
1178 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001179 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1180 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001181 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001182 }
1183
1184 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001185 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1186 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001187 MaxBytesToFill = 0;
1188 }
1189 }
1190
1191 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1192 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1193
1194 return false;
1195}
1196
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001197/// ParseDirectiveSymbolAttribute
1198/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1199bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001200 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001201 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001202 StringRef Name;
1203
1204 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001205 return TokError("expected identifier in directive");
1206
Daniel Dunbar959fd882009-08-26 22:13:22 +00001207 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001208
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001209 Out.EmitSymbolAttribute(Sym, Attr);
1210
Daniel Dunbar3f872332009-07-28 16:08:33 +00001211 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001212 break;
1213
Daniel Dunbar3f872332009-07-28 16:08:33 +00001214 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001215 return TokError("unexpected token in directive");
1216 Lexer.Lex();
1217 }
1218 }
1219
1220 Lexer.Lex();
1221 return false;
1222}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001223
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001224/// ParseDirectiveDarwinSymbolDesc
1225/// ::= .desc identifier , expression
1226bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001227 StringRef Name;
1228 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001229 return TokError("expected identifier in directive");
1230
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001231 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001232 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001233
Daniel Dunbar3f872332009-07-28 16:08:33 +00001234 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001235 return TokError("unexpected token in '.desc' directive");
1236 Lexer.Lex();
1237
1238 SMLoc DescLoc = Lexer.getLoc();
1239 int64_t DescValue;
1240 if (ParseAbsoluteExpression(DescValue))
1241 return true;
1242
Daniel Dunbar3f872332009-07-28 16:08:33 +00001243 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001244 return TokError("unexpected token in '.desc' directive");
1245
1246 Lexer.Lex();
1247
1248 // Set the n_desc field of this Symbol to this DescValue
1249 Out.EmitSymbolDesc(Sym, DescValue);
1250
1251 return false;
1252}
1253
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001254/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001255/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1256bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001257 SMLoc IDLoc = Lexer.getLoc();
1258 StringRef Name;
1259 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001260 return TokError("expected identifier in directive");
1261
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001262 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001263 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001264
Daniel Dunbar3f872332009-07-28 16:08:33 +00001265 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001266 return TokError("unexpected token in directive");
1267 Lexer.Lex();
1268
1269 int64_t Size;
1270 SMLoc SizeLoc = Lexer.getLoc();
1271 if (ParseAbsoluteExpression(Size))
1272 return true;
1273
1274 int64_t Pow2Alignment = 0;
1275 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001276 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001277 Lexer.Lex();
1278 Pow2AlignmentLoc = Lexer.getLoc();
1279 if (ParseAbsoluteExpression(Pow2Alignment))
1280 return true;
1281 }
1282
Daniel Dunbar3f872332009-07-28 16:08:33 +00001283 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001284 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001285
1286 Lexer.Lex();
1287
Chris Lattner1fc3d752009-07-09 17:25:12 +00001288 // NOTE: a size of zero for a .comm should create a undefined symbol
1289 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001290 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001291 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1292 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001293
1294 // NOTE: The alignment in the directive is a power of 2 value, the assember
1295 // may internally end up wanting an alignment in bytes.
1296 // FIXME: Diagnose overflow.
1297 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001298 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1299 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001300
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001301 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001302 return Error(IDLoc, "invalid symbol redefinition");
1303
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001304 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001305 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001306 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001307 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1308 MCSectionMachO::S_ZEROFILL, 0,
1309 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001310 Sym, Size, 1 << Pow2Alignment);
1311 return false;
1312 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001313
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001314 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001315 return false;
1316}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001317
1318/// ParseDirectiveDarwinZerofill
1319/// ::= .zerofill segname , sectname [, identifier , size_expression [
1320/// , align_expression ]]
1321bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001322 // FIXME: Handle quoted names here.
1323
Daniel Dunbar3f872332009-07-28 16:08:33 +00001324 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001325 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001326 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001327 Lexer.Lex();
1328
Daniel Dunbar3f872332009-07-28 16:08:33 +00001329 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001330 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001331 Lexer.Lex();
1332
Daniel Dunbar3f872332009-07-28 16:08:33 +00001333 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001334 return TokError("expected section name after comma in '.zerofill' "
1335 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001336 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001337 Lexer.Lex();
1338
Chris Lattner9be3fee2009-07-10 22:20:30 +00001339 // If this is the end of the line all that was wanted was to create the
1340 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001341 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001342 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001343 Out.EmitZerofill(getMachOSection(Segment, Section,
1344 MCSectionMachO::S_ZEROFILL, 0,
1345 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001346 return false;
1347 }
1348
Daniel Dunbar3f872332009-07-28 16:08:33 +00001349 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001350 return TokError("unexpected token in directive");
1351 Lexer.Lex();
1352
Daniel Dunbar3f872332009-07-28 16:08:33 +00001353 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001354 return TokError("expected identifier in directive");
1355
1356 // handle the identifier as the key symbol.
1357 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001358 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001359 Lexer.Lex();
1360
Daniel Dunbar3f872332009-07-28 16:08:33 +00001361 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001362 return TokError("unexpected token in directive");
1363 Lexer.Lex();
1364
1365 int64_t Size;
1366 SMLoc SizeLoc = Lexer.getLoc();
1367 if (ParseAbsoluteExpression(Size))
1368 return true;
1369
1370 int64_t Pow2Alignment = 0;
1371 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001372 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001373 Lexer.Lex();
1374 Pow2AlignmentLoc = Lexer.getLoc();
1375 if (ParseAbsoluteExpression(Pow2Alignment))
1376 return true;
1377 }
1378
Daniel Dunbar3f872332009-07-28 16:08:33 +00001379 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001380 return TokError("unexpected token in '.zerofill' directive");
1381
1382 Lexer.Lex();
1383
1384 if (Size < 0)
1385 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1386 "than zero");
1387
1388 // NOTE: The alignment in the directive is a power of 2 value, the assember
1389 // may internally end up wanting an alignment in bytes.
1390 // FIXME: Diagnose overflow.
1391 if (Pow2Alignment < 0)
1392 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1393 "can't be less than zero");
1394
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001395 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001396 return Error(IDLoc, "invalid symbol redefinition");
1397
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001398 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001399 //
1400 // FIXME: Arch specific.
1401 Out.EmitZerofill(getMachOSection(Segment, Section,
1402 MCSectionMachO::S_ZEROFILL, 0,
1403 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001404 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001405
1406 return false;
1407}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001408
1409/// ParseDirectiveDarwinSubsectionsViaSymbols
1410/// ::= .subsections_via_symbols
1411bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001412 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001413 return TokError("unexpected token in '.subsections_via_symbols' directive");
1414
1415 Lexer.Lex();
1416
Kevin Enderbyf96db462009-07-16 17:56:39 +00001417 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001418
1419 return false;
1420}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001421
1422/// ParseDirectiveAbort
1423/// ::= .abort [ "abort_string" ]
1424bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001425 // FIXME: Use loc from directive.
1426 SMLoc Loc = Lexer.getLoc();
1427
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001428 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001429 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1430 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001431 return TokError("expected string in '.abort' directive");
1432
Daniel Dunbar419aded2009-07-28 16:38:40 +00001433 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001434
1435 Lexer.Lex();
1436 }
1437
Daniel Dunbar3f872332009-07-28 16:08:33 +00001438 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001439 return TokError("unexpected token in '.abort' directive");
1440
1441 Lexer.Lex();
1442
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001443 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001444 if (Str.empty())
1445 Error(Loc, ".abort detected. Assembly stopping.");
1446 else
1447 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001448
1449 return false;
1450}
Kevin Enderby71148242009-07-14 21:35:03 +00001451
1452/// ParseDirectiveLsym
1453/// ::= .lsym identifier , expression
1454bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001455 StringRef Name;
1456 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001457 return TokError("expected identifier in directive");
1458
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001459 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001460 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001461
Daniel Dunbar3f872332009-07-28 16:08:33 +00001462 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001463 return TokError("unexpected token in '.lsym' directive");
1464 Lexer.Lex();
1465
Daniel Dunbar821e3332009-08-31 08:09:28 +00001466 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001467 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001468 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001469 return true;
1470
Daniel Dunbar3f872332009-07-28 16:08:33 +00001471 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001472 return TokError("unexpected token in '.lsym' directive");
1473
1474 Lexer.Lex();
1475
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001476 // We don't currently support this directive.
1477 //
1478 // FIXME: Diagnostic location!
1479 (void) Sym;
1480 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001481}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001482
1483/// ParseDirectiveInclude
1484/// ::= .include "filename"
1485bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001486 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001487 return TokError("expected string in '.include' directive");
1488
Daniel Dunbar419aded2009-07-28 16:38:40 +00001489 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001490 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001491 Lexer.Lex();
1492
Daniel Dunbar3f872332009-07-28 16:08:33 +00001493 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001494 return TokError("unexpected token in '.include' directive");
1495
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001496 // Strip the quotes.
1497 Filename = Filename.substr(1, Filename.size()-2);
1498
1499 // Attempt to switch the lexer to the included file before consuming the end
1500 // of statement to avoid losing it when we switch.
1501 if (Lexer.EnterIncludeFile(Filename)) {
1502 Lexer.PrintMessage(IncludeLoc,
1503 "Could not find include file '" + Filename + "'",
1504 "error");
1505 return true;
1506 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001507
1508 return false;
1509}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001510
1511/// ParseDirectiveDarwinDumpOrLoad
1512/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001513bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001514 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001515 return TokError("expected string in '.dump' or '.load' directive");
1516
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001517 Lexer.Lex();
1518
Daniel Dunbar3f872332009-07-28 16:08:33 +00001519 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001520 return TokError("unexpected token in '.dump' or '.load' directive");
1521
1522 Lexer.Lex();
1523
Kevin Enderby5026ae42009-07-20 20:25:37 +00001524 // FIXME: If/when .dump and .load are implemented they will be done in the
1525 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001526 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001527 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001528 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001529 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001530
1531 return false;
1532}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001533
1534/// ParseDirectiveIf
1535/// ::= .if expression
1536bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1537 // Consume the identifier that was the .if directive
1538 Lexer.Lex();
1539
1540 TheCondStack.push_back(TheCondState);
1541 TheCondState.TheCond = AsmCond::IfCond;
1542 if(TheCondState.Ignore) {
1543 EatToEndOfStatement();
1544 }
1545 else {
1546 int64_t ExprValue;
1547 if (ParseAbsoluteExpression(ExprValue))
1548 return true;
1549
1550 if (Lexer.isNot(AsmToken::EndOfStatement))
1551 return TokError("unexpected token in '.if' directive");
1552
1553 Lexer.Lex();
1554
1555 TheCondState.CondMet = ExprValue;
1556 TheCondState.Ignore = !TheCondState.CondMet;
1557 }
1558
1559 return false;
1560}
1561
1562/// ParseDirectiveElseIf
1563/// ::= .elseif expression
1564bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1565 if (TheCondState.TheCond != AsmCond::IfCond &&
1566 TheCondState.TheCond != AsmCond::ElseIfCond)
1567 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1568 " an .elseif");
1569 TheCondState.TheCond = AsmCond::ElseIfCond;
1570
1571 // Consume the identifier that was the .elseif directive
1572 Lexer.Lex();
1573
1574 bool LastIgnoreState = false;
1575 if (!TheCondStack.empty())
1576 LastIgnoreState = TheCondStack.back().Ignore;
1577 if (LastIgnoreState || TheCondState.CondMet) {
1578 TheCondState.Ignore = true;
1579 EatToEndOfStatement();
1580 }
1581 else {
1582 int64_t ExprValue;
1583 if (ParseAbsoluteExpression(ExprValue))
1584 return true;
1585
1586 if (Lexer.isNot(AsmToken::EndOfStatement))
1587 return TokError("unexpected token in '.elseif' directive");
1588
1589 Lexer.Lex();
1590 TheCondState.CondMet = ExprValue;
1591 TheCondState.Ignore = !TheCondState.CondMet;
1592 }
1593
1594 return false;
1595}
1596
1597/// ParseDirectiveElse
1598/// ::= .else
1599bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1600 // Consume the identifier that was the .else directive
1601 Lexer.Lex();
1602
1603 if (Lexer.isNot(AsmToken::EndOfStatement))
1604 return TokError("unexpected token in '.else' directive");
1605
1606 Lexer.Lex();
1607
1608 if (TheCondState.TheCond != AsmCond::IfCond &&
1609 TheCondState.TheCond != AsmCond::ElseIfCond)
1610 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1611 ".elseif");
1612 TheCondState.TheCond = AsmCond::ElseCond;
1613 bool LastIgnoreState = false;
1614 if (!TheCondStack.empty())
1615 LastIgnoreState = TheCondStack.back().Ignore;
1616 if (LastIgnoreState || TheCondState.CondMet)
1617 TheCondState.Ignore = true;
1618 else
1619 TheCondState.Ignore = false;
1620
1621 return false;
1622}
1623
1624/// ParseDirectiveEndIf
1625/// ::= .endif
1626bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1627 // Consume the identifier that was the .endif directive
1628 Lexer.Lex();
1629
1630 if (Lexer.isNot(AsmToken::EndOfStatement))
1631 return TokError("unexpected token in '.endif' directive");
1632
1633 Lexer.Lex();
1634
1635 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1636 TheCondStack.empty())
1637 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1638 ".else");
1639 if (!TheCondStack.empty()) {
1640 TheCondState = TheCondStack.back();
1641 TheCondStack.pop_back();
1642 }
1643
1644 return false;
1645}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001646
1647/// ParseDirectiveFile
1648/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001649bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001650 // FIXME: I'm not sure what this is.
1651 int64_t FileNumber = -1;
1652 if (Lexer.is(AsmToken::Integer)) {
1653 FileNumber = Lexer.getTok().getIntVal();
1654 Lexer.Lex();
1655
1656 if (FileNumber < 1)
1657 return TokError("file number less than one");
1658 }
1659
1660 if (Lexer.isNot(AsmToken::String))
1661 return TokError("unexpected token in '.file' directive");
1662
Bill Wendling9bc0af82009-12-28 01:34:57 +00001663 StringRef ATTRIBUTE_UNUSED FileName = Lexer.getTok().getString();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001664 Lexer.Lex();
1665
1666 if (Lexer.isNot(AsmToken::EndOfStatement))
1667 return TokError("unexpected token in '.file' directive");
1668
1669 // FIXME: Do something with the .file.
1670
1671 return false;
1672}
1673
1674/// ParseDirectiveLine
1675/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001676bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001677 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1678 if (Lexer.isNot(AsmToken::Integer))
1679 return TokError("unexpected token in '.line' directive");
1680
1681 int64_t LineNumber = Lexer.getTok().getIntVal();
1682 (void) LineNumber;
1683 Lexer.Lex();
1684
1685 // FIXME: Do something with the .line.
1686 }
1687
1688 if (Lexer.isNot(AsmToken::EndOfStatement))
1689 return TokError("unexpected token in '.file' directive");
1690
1691 return false;
1692}
1693
1694
1695/// ParseDirectiveLoc
1696/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001697bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001698 if (Lexer.isNot(AsmToken::Integer))
1699 return TokError("unexpected token in '.loc' directive");
1700
1701 // FIXME: What are these fields?
1702 int64_t FileNumber = Lexer.getTok().getIntVal();
1703 (void) FileNumber;
1704 // FIXME: Validate file.
1705
1706 Lexer.Lex();
1707 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1708 if (Lexer.isNot(AsmToken::Integer))
1709 return TokError("unexpected token in '.loc' directive");
1710
1711 int64_t Param2 = Lexer.getTok().getIntVal();
1712 (void) Param2;
1713 Lexer.Lex();
1714
1715 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1716 if (Lexer.isNot(AsmToken::Integer))
1717 return TokError("unexpected token in '.loc' directive");
1718
1719 int64_t Param3 = Lexer.getTok().getIntVal();
1720 (void) Param3;
1721 Lexer.Lex();
1722
1723 // FIXME: Do something with the .loc.
1724 }
1725 }
1726
1727 if (Lexer.isNot(AsmToken::EndOfStatement))
1728 return TokError("unexpected token in '.file' directive");
1729
1730 return false;
1731}
1732