blob: 1204a0054afa7e29bac62f6f33b085696b45da4a [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 Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfffff912009-10-16 01:34:54 +000024#include "llvm/MC/MCValue.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
27#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000028#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000029using namespace llvm;
30
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000031// Mach-O section uniquing.
32//
33// FIXME: Figure out where this should live, it should be shared by
34// TargetLoweringObjectFile.
35typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
36
Chris Lattnerebb89b42009-09-27 21:16:52 +000037AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
38 const MCAsmInfo &_MAI)
39 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
40 SectionUniquingMap(0) {
41 // Debugging directives.
42 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
43 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
44 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
45}
46
47
48
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000049AsmParser::~AsmParser() {
50 // If we have the MachO uniquing map, free it.
51 delete (MachOUniqueMapTy*)SectionUniquingMap;
52}
53
54const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
55 const StringRef &Section,
56 unsigned TypeAndAttributes,
57 unsigned Reserved2,
58 SectionKind Kind) const {
59 // We unique sections by their segment/section pair. The returned section
60 // may not have the same flags as the requested section, if so this should be
61 // diagnosed by the client as an error.
62
63 // Create the map if it doesn't already exist.
64 if (SectionUniquingMap == 0)
65 SectionUniquingMap = new MachOUniqueMapTy();
66 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
67
68 // Form the name to look up.
69 SmallString<64> Name;
70 Name += Segment;
71 Name.push_back(',');
72 Name += Section;
73
74 // Do the lookup, if we have a hit, return it.
75 const MCSectionMachO *&Entry = Map[Name.str()];
76
77 // FIXME: This should validate the type and attributes.
78 if (Entry) return Entry;
79
80 // Otherwise, return a new section.
81 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
82 Reserved2, Kind, Ctx);
83}
84
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000085void AsmParser::Warning(SMLoc L, const Twine &Msg) {
86 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000087}
88
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000089bool AsmParser::Error(SMLoc L, const Twine &Msg) {
90 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000091 return true;
92}
93
94bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000095 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000096 return true;
97}
98
Chris Lattner27aa7d22009-06-21 20:16:42 +000099bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000100 // Create the initial section.
101 //
102 // FIXME: Support -n.
103 // FIXME: Target hook & command line option for initial section.
104 Out.SwitchSection(getMachOSection("__TEXT", "__text",
105 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
106 0, SectionKind()));
107
108
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000109 // Prime the lexer.
110 Lexer.Lex();
111
Chris Lattnerb717fb02009-07-02 21:53:43 +0000112 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000113
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000114 AsmCond StartingCondState = TheCondState;
115
Chris Lattnerb717fb02009-07-02 21:53:43 +0000116 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000117 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000118 // Handle conditional assembly here before calling ParseStatement()
119 if (Lexer.getKind() == AsmToken::Identifier) {
120 // If we have an identifier, handle it as the key symbol.
121 AsmToken ID = Lexer.getTok();
122 SMLoc IDLoc = ID.getLoc();
123 StringRef IDVal = ID.getString();
124
125 if (IDVal == ".if" ||
126 IDVal == ".elseif" ||
127 IDVal == ".else" ||
128 IDVal == ".endif") {
129 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
130 continue;
131 HadError = true;
132 EatToEndOfStatement();
133 continue;
134 }
135 }
136 if (TheCondState.Ignore) {
137 EatToEndOfStatement();
138 continue;
139 }
140
Chris Lattnerb717fb02009-07-02 21:53:43 +0000141 if (!ParseStatement()) continue;
142
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000143 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000144 HadError = true;
145 EatToEndOfStatement();
146 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000147
148 if (TheCondState.TheCond != StartingCondState.TheCond ||
149 TheCondState.Ignore != StartingCondState.Ignore)
150 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000151
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000152 if (!HadError)
153 Out.Finish();
154
Chris Lattnerb717fb02009-07-02 21:53:43 +0000155 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000156}
157
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000158/// ParseConditionalAssemblyDirectives - parse the conditional assembly
159/// directives
160bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
161 SMLoc DirectiveLoc) {
162 if (Directive == ".if")
163 return ParseDirectiveIf(DirectiveLoc);
164 if (Directive == ".elseif")
165 return ParseDirectiveElseIf(DirectiveLoc);
166 if (Directive == ".else")
167 return ParseDirectiveElse(DirectiveLoc);
168 if (Directive == ".endif")
169 return ParseDirectiveEndIf(DirectiveLoc);
170 return true;
171}
172
Chris Lattner2cf5f142009-06-22 01:29:09 +0000173/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
174void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000175 while (Lexer.isNot(AsmToken::EndOfStatement) &&
176 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000177 Lexer.Lex();
178
179 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000180 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000181 Lexer.Lex();
182}
183
Chris Lattnerc4193832009-06-22 05:51:26 +0000184
Chris Lattner74ec1a32009-06-22 06:32:03 +0000185/// ParseParenExpr - Parse a paren expression and return it.
186/// NOTE: This assumes the leading '(' has already been consumed.
187///
188/// parenexpr ::= expr)
189///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000190bool AsmParser::ParseParenExpr(const MCExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000191 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000192 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000193 return TokError("expected ')' in parentheses expression");
194 Lexer.Lex();
195 return false;
196}
Chris Lattnerc4193832009-06-22 05:51:26 +0000197
Daniel Dunbar959fd882009-08-26 22:13:22 +0000198MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
199 if (MCSymbol *S = Ctx.LookupSymbol(Name))
200 return S;
201
202 // If the label starts with L it is an assembler temporary label.
203 if (Name.startswith("L"))
204 return Ctx.CreateTemporarySymbol(Name);
205
206 return Ctx.CreateSymbol(Name);
207}
208
Chris Lattner74ec1a32009-06-22 06:32:03 +0000209/// ParsePrimaryExpr - Parse a primary expression and return it.
210/// primaryexpr ::= (parenexpr
211/// primaryexpr ::= symbol
212/// primaryexpr ::= number
213/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000214bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000215 switch (Lexer.getKind()) {
216 default:
217 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000218 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000219 Lexer.Lex(); // Eat the operator.
220 if (ParsePrimaryExpr(Res))
221 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000222 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000223 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000224 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000225 case AsmToken::Identifier: {
226 // This is a symbol reference.
227 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Chris Lattnerc4193832009-06-22 05:51:26 +0000228 Lexer.Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000229
230 // If this is an absolute variable reference, substitute it now to preserve
231 // semantics in the face of reassignment.
232 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
233 Res = Sym->getValue();
234 return false;
235 }
236
237 // Otherwise create a symbol ref.
238 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000239 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000240 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000241 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000242 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000243 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000244 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000245 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000246 Lexer.Lex(); // Eat the '('.
247 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000248 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000249 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000250 if (ParsePrimaryExpr(Res))
251 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000252 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000254 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 Lexer.Lex(); // Eat the operator.
256 if (ParsePrimaryExpr(Res))
257 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000258 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000260 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 Lexer.Lex(); // Eat the operator.
262 if (ParsePrimaryExpr(Res))
263 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000264 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000266 }
267}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000268
269/// ParseExpression - Parse an expression and return it.
270///
271/// expr ::= expr +,- expr -> lowest.
272/// expr ::= expr |,^,&,! expr -> middle.
273/// expr ::= expr *,/,%,<<,>> expr -> highest.
274/// expr ::= primaryexpr
275///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000276bool AsmParser::ParseExpression(const MCExpr *&Res) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000277 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000278 return ParsePrimaryExpr(Res) ||
279 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000280}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000281
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000282bool AsmParser::ParseParenExpression(const MCExpr *&Res) {
283 if (ParseParenExpr(Res))
284 return true;
285
286 return false;
287}
288
Daniel Dunbar475839e2009-06-29 20:37:27 +0000289bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000290 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000291
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000292 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 if (ParseExpression(Expr))
294 return true;
295
Daniel Dunbare00b0112009-10-16 01:57:52 +0000296 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000297 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298
299 return false;
300}
301
Daniel Dunbar3f872332009-07-28 16:08:33 +0000302static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000303 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000304 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000305 default:
306 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307
308 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000309 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000310 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000311 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000312 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000313 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000314 return 1;
315
316 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000317 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000318 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000319 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000321 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000322 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000324 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::ExclaimEqual:
327 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000328 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000331 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000332 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000333 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000334 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
342
343 // Intermediate Precedence: |, &, ^
344 //
345 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000348 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000353 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000354 return 3;
355
356 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000361 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000362 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000372 }
373}
374
375
376/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
377/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000378bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000379 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000380 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000381 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000382
383 // If the next token is lower precedence than we are allowed to eat, return
384 // successfully with what we ate already.
385 if (TokPrec < Precedence)
386 return false;
387
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000388 Lexer.Lex();
389
390 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000391 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000392 if (ParsePrimaryExpr(RHS)) return true;
393
394 // If BinOp binds less tightly with RHS than the operator after RHS, let
395 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000396 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000397 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 if (TokPrec < NextTokPrec) {
399 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
400 }
401
Daniel Dunbar475839e2009-06-29 20:37:27 +0000402 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000403 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000404 }
405}
406
Chris Lattnerc4193832009-06-22 05:51:26 +0000407
408
409
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000410/// ParseStatement:
411/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000412/// ::= Label* Directive ...Operands... EndOfStatement
413/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000414bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000415 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000416 Lexer.Lex();
417 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000418 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000419
420 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000421 AsmToken ID = Lexer.getTok();
422 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000423 StringRef IDVal;
424 if (ParseIdentifier(IDVal))
425 return TokError("unexpected token at start of statement");
426
427 // FIXME: Recurse on local labels?
428
429 // See what kind of statement we have.
430 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000431 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000432 // identifier ':' -> Label.
433 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000434
435 // Diagnose attempt to use a variable as a label.
436 //
437 // FIXME: Diagnostics. Note the location of the definition as a label.
438 // FIXME: This doesn't diagnose assignment to a symbol which has been
439 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000440 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000441 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000442 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000443
Daniel Dunbar959fd882009-08-26 22:13:22 +0000444 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000445 Out.EmitLabel(Sym);
446
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000447 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000448 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000449
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000451 // identifier '=' ... -> assignment statement
452 Lexer.Lex();
453
Daniel Dunbare2ace502009-08-31 08:09:09 +0000454 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000455
456 default: // Normal instruction or directive.
457 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000458 }
459
460 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000461 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000462 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000463 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000464 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000465 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000466 // FIXME: This changes behavior based on the -static flag to the
467 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000468 return ParseDirectiveSectionSwitch("__TEXT", "__text",
469 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000471 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000473 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000474 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000475 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
476 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000477 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000478 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000479 MCSectionMachO::S_4BYTE_LITERALS,
480 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000481 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000482 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000483 MCSectionMachO::S_8BYTE_LITERALS,
484 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000485 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000486 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000487 MCSectionMachO::S_16BYTE_LITERALS,
488 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000489 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000490 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000491 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000492 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000494 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000495 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000496 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
497
498 // FIXME: The assembler manual claims that this has the self modify code
499 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000500 if (IDVal == ".symbol_stub")
501 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
502 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000503 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
504 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000505 0, 16);
506 // FIXME: PowerPC only?
507 if (IDVal == ".picsymbol_stub")
508 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
509 MCSectionMachO::S_SYMBOL_STUBS |
510 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
511 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000513 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000514 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000515 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
516
517 // FIXME: The section names of these two are misspelled in the assembler
518 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000519 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000520 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
521 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
522 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000523 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000524 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
525 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
526 4);
527
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000529 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000531 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000532 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
533 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000535 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000536 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
537 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000539 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000540
541
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__OBJC", "__class",
544 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
547 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
550 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
553 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
556 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
559 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
562 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000564 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
565 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
568 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
569 MCSectionMachO::S_LITERAL_POINTERS,
570 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000571 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000572 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
573 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
574 MCSectionMachO::S_LITERAL_POINTERS,
575 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__OBJC", "__category",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
590 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000592 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
593 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000595 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
596 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000598 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
599 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
602 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000603
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000604 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000606 return ParseDirectiveSet();
607
Daniel Dunbara0d14262009-06-24 23:30:00 +0000608 // Data directives
609
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000611 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000613 return ParseDirectiveAscii(true);
614
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000616 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000617 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000618 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000620 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000622 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000623
624 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000626 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000628 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000630 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000632 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000634 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000636 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000638 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000640 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
641
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000643 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000644
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000646 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000648 return ParseDirectiveSpace();
649
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000650 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000651
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000653 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000655 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000657 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000659 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000661 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000663 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000665 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000667 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000669 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000671 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000673 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000675 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
676
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000678 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000680 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000682 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000684 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000686 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000687
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000689 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000691 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000693 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000695 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000697 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000698
Chris Lattnerebb89b42009-09-27 21:16:52 +0000699 // Look up the handler in the handler table,
700 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
701 if (Handler)
702 return (this->*Handler)(IDVal, IDLoc);
703
Kevin Enderby9c656452009-09-10 20:51:44 +0000704 // Target hook for parsing target specific directives.
705 if (!getTargetParser().ParseDirective(ID))
706 return false;
707
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000708 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000709 EatToEndOfStatement();
710 return false;
711 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000712
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000713 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000714 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000715 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000716
Daniel Dunbar3f872332009-07-28 16:08:33 +0000717 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000718 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000719
720 // Eat the end of statement marker.
721 Lexer.Lex();
722
723 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000724 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000725
726 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000727 return false;
728}
Chris Lattner9a023f72009-06-24 04:43:34 +0000729
Daniel Dunbare2ace502009-08-31 08:09:09 +0000730bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000731 // FIXME: Use better location, we should use proper tokens.
732 SMLoc EqualLoc = Lexer.getLoc();
733
Daniel Dunbar821e3332009-08-31 08:09:28 +0000734 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000735 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000736 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000737 return true;
738
Daniel Dunbar3f872332009-07-28 16:08:33 +0000739 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000740 return TokError("unexpected token in assignment");
741
742 // Eat the end of statement marker.
743 Lexer.Lex();
744
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000745 // Validate that the LHS is allowed to be a variable (either it has not been
746 // used as a symbol, or it is an absolute symbol).
747 MCSymbol *Sym = getContext().LookupSymbol(Name);
748 if (Sym) {
749 // Diagnose assignment to a label.
750 //
751 // FIXME: Diagnostics. Note the location of the definition as a label.
752 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
753 if (!Sym->isUndefined() && !Sym->isAbsolute())
754 return Error(EqualLoc, "redefinition of '" + Name + "'");
755 else if (!Sym->isVariable())
756 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
757 else if (!isa<MCConstantExpr>(Sym->getValue()))
758 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
759 Name + "'");
760 } else
761 Sym = CreateSymbol(Name);
762
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000763 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000764
765 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000766 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000767
768 return false;
769}
770
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000771/// ParseIdentifier:
772/// ::= identifier
773/// ::= string
774bool AsmParser::ParseIdentifier(StringRef &Res) {
775 if (Lexer.isNot(AsmToken::Identifier) &&
776 Lexer.isNot(AsmToken::String))
777 return true;
778
779 Res = Lexer.getTok().getIdentifier();
780
781 Lexer.Lex(); // Consume the identifier token.
782
783 return false;
784}
785
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000786/// ParseDirectiveSet:
787/// ::= .set identifier ',' expression
788bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000789 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000790
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000791 if (ParseIdentifier(Name))
792 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000793
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000794 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000795 return TokError("unexpected token in '.set'");
796 Lexer.Lex();
797
Daniel Dunbare2ace502009-08-31 08:09:09 +0000798 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000799}
800
Chris Lattner9a023f72009-06-24 04:43:34 +0000801/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000802/// ::= .section identifier (',' identifier)*
803/// FIXME: This should actually parse out the segment, section, attributes and
804/// sizeof_stub fields.
805bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000806 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000807
Daniel Dunbarace63122009-08-11 03:42:33 +0000808 StringRef SectionName;
809 if (ParseIdentifier(SectionName))
810 return Error(Loc, "expected identifier after '.section' directive");
811
812 // Verify there is a following comma.
813 if (!Lexer.is(AsmToken::Comma))
814 return TokError("unexpected token in '.section' directive");
815
Chris Lattnerff4bc462009-08-10 01:39:42 +0000816 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000817 SectionSpec += ",";
818
819 // Add all the tokens until the end of the line, ParseSectionSpecifier will
820 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000821 StringRef EOL = Lexer.LexUntilEndOfStatement();
822 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000823
Chris Lattnerff4bc462009-08-10 01:39:42 +0000824 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000825 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000826 return TokError("unexpected token in '.section' directive");
827 Lexer.Lex();
828
Chris Lattnerff4bc462009-08-10 01:39:42 +0000829
830 StringRef Segment, Section;
831 unsigned TAA, StubSize;
832 std::string ErrorStr =
833 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
834 TAA, StubSize);
835
836 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000837 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000838
Chris Lattner56594f92009-07-31 17:47:16 +0000839 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000840 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
841 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000842 return false;
843}
844
Chris Lattnere15c2d72009-08-10 18:05:55 +0000845/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000846bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
847 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000848 unsigned TAA, unsigned Align,
849 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000850 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000851 return TokError("unexpected token in section switching directive");
852 Lexer.Lex();
853
Chris Lattner56594f92009-07-31 17:47:16 +0000854 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000855 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
856 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000857
858 // Set the implicit alignment, if any.
859 //
860 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
861 // alignment on the section (e.g., if one manually inserts bytes into the
862 // section, then just issueing the section switch directive will not realign
863 // the section. However, this is arguably more reasonable behavior, and there
864 // is no good reason for someone to intentionally emit incorrectly sized
865 // values into the implicitly aligned sections.
866 if (Align)
867 Out.EmitValueToAlignment(Align, 0, 1, 0);
868
Chris Lattner529fb542009-06-24 05:13:15 +0000869 return false;
870}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000871
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000872bool AsmParser::ParseEscapedString(std::string &Data) {
873 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
874
875 Data = "";
876 StringRef Str = Lexer.getTok().getStringContents();
877 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
878 if (Str[i] != '\\') {
879 Data += Str[i];
880 continue;
881 }
882
883 // Recognize escaped characters. Note that this escape semantics currently
884 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
885 ++i;
886 if (i == e)
887 return TokError("unexpected backslash at end of string");
888
889 // Recognize octal sequences.
890 if ((unsigned) (Str[i] - '0') <= 7) {
891 // Consume up to three octal characters.
892 unsigned Value = Str[i] - '0';
893
894 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
895 ++i;
896 Value = Value * 8 + (Str[i] - '0');
897
898 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
899 ++i;
900 Value = Value * 8 + (Str[i] - '0');
901 }
902 }
903
904 if (Value > 255)
905 return TokError("invalid octal escape sequence (out of range)");
906
907 Data += (unsigned char) Value;
908 continue;
909 }
910
911 // Otherwise recognize individual escapes.
912 switch (Str[i]) {
913 default:
914 // Just reject invalid escape sequences for now.
915 return TokError("invalid escape sequence (unrecognized character)");
916
917 case 'b': Data += '\b'; break;
918 case 'f': Data += '\f'; break;
919 case 'n': Data += '\n'; break;
920 case 'r': Data += '\r'; break;
921 case 't': Data += '\t'; break;
922 case '"': Data += '"'; break;
923 case '\\': Data += '\\'; break;
924 }
925 }
926
927 return false;
928}
929
Daniel Dunbara0d14262009-06-24 23:30:00 +0000930/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000931/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000932bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000933 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000934 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000935 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000936 return TokError("expected string in '.ascii' or '.asciz' directive");
937
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000938 std::string Data;
939 if (ParseEscapedString(Data))
940 return true;
941
942 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000943 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000944 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000945
946 Lexer.Lex();
947
Daniel Dunbar3f872332009-07-28 16:08:33 +0000948 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000949 break;
950
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000952 return TokError("unexpected token in '.ascii' or '.asciz' directive");
953 Lexer.Lex();
954 }
955 }
956
957 Lexer.Lex();
958 return false;
959}
960
961/// ParseDirectiveValue
962/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
963bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000964 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000965 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000966 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +0000967 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000968 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969 return true;
970
Daniel Dunbar883f9202009-08-31 08:08:50 +0000971 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000972
Daniel Dunbar3f872332009-07-28 16:08:33 +0000973 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974 break;
975
976 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000977 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000978 return TokError("unexpected token in directive");
979 Lexer.Lex();
980 }
981 }
982
983 Lexer.Lex();
984 return false;
985}
986
987/// ParseDirectiveSpace
988/// ::= .space expression [ , expression ]
989bool AsmParser::ParseDirectiveSpace() {
990 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000991 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000992 return true;
993
994 int64_t FillExpr = 0;
995 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000996 if (Lexer.isNot(AsmToken::EndOfStatement)) {
997 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998 return TokError("unexpected token in '.space' directive");
999 Lexer.Lex();
1000
Daniel Dunbar475839e2009-06-29 20:37:27 +00001001 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002 return true;
1003
1004 HasFillExpr = true;
1005
Daniel Dunbar3f872332009-07-28 16:08:33 +00001006 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 return TokError("unexpected token in '.space' directive");
1008 }
1009
1010 Lexer.Lex();
1011
1012 if (NumBytes <= 0)
1013 return TokError("invalid number of bytes in '.space' directive");
1014
1015 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1016 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001017 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018
1019 return false;
1020}
1021
1022/// ParseDirectiveFill
1023/// ::= .fill expression , expression , expression
1024bool AsmParser::ParseDirectiveFill() {
1025 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001026 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001027 return true;
1028
Daniel Dunbar3f872332009-07-28 16:08:33 +00001029 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 return TokError("unexpected token in '.fill' directive");
1031 Lexer.Lex();
1032
1033 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001034 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001035 return true;
1036
Daniel Dunbar3f872332009-07-28 16:08:33 +00001037 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001038 return TokError("unexpected token in '.fill' directive");
1039 Lexer.Lex();
1040
1041 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001042 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043 return true;
1044
Daniel Dunbar3f872332009-07-28 16:08:33 +00001045 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046 return TokError("unexpected token in '.fill' directive");
1047
1048 Lexer.Lex();
1049
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001050 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1051 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052
1053 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001054 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055
1056 return false;
1057}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001058
1059/// ParseDirectiveOrg
1060/// ::= .org expression [ , expression ]
1061bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001062 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001063 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001064 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001065 return true;
1066
1067 // Parse optional fill expression.
1068 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001069 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1070 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001071 return TokError("unexpected token in '.org' directive");
1072 Lexer.Lex();
1073
Daniel Dunbar475839e2009-06-29 20:37:27 +00001074 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001075 return true;
1076
Daniel Dunbar3f872332009-07-28 16:08:33 +00001077 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001078 return TokError("unexpected token in '.org' directive");
1079 }
1080
1081 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001082
1083 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1084 // has to be relative to the current section.
1085 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001086
1087 return false;
1088}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001089
1090/// ParseDirectiveAlign
1091/// ::= {.align, ...} expression [ , expression [ , expression ]]
1092bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001093 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001094 int64_t Alignment;
1095 if (ParseAbsoluteExpression(Alignment))
1096 return true;
1097
1098 SMLoc MaxBytesLoc;
1099 bool HasFillExpr = false;
1100 int64_t FillExpr = 0;
1101 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001102 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1103 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001104 return TokError("unexpected token in directive");
1105 Lexer.Lex();
1106
1107 // The fill expression can be omitted while specifying a maximum number of
1108 // alignment bytes, e.g:
1109 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001110 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001111 HasFillExpr = true;
1112 if (ParseAbsoluteExpression(FillExpr))
1113 return true;
1114 }
1115
Daniel Dunbar3f872332009-07-28 16:08:33 +00001116 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1117 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001118 return TokError("unexpected token in directive");
1119 Lexer.Lex();
1120
1121 MaxBytesLoc = Lexer.getLoc();
1122 if (ParseAbsoluteExpression(MaxBytesToFill))
1123 return true;
1124
Daniel Dunbar3f872332009-07-28 16:08:33 +00001125 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001126 return TokError("unexpected token in directive");
1127 }
1128 }
1129
1130 Lexer.Lex();
1131
1132 if (!HasFillExpr) {
1133 // FIXME: Sometimes fill with nop.
1134 FillExpr = 0;
1135 }
1136
1137 // Compute alignment in bytes.
1138 if (IsPow2) {
1139 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001140 if (Alignment >= 32) {
1141 Error(AlignmentLoc, "invalid alignment value");
1142 Alignment = 31;
1143 }
1144
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001145 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001146 }
1147
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001148 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001149 if (MaxBytesLoc.isValid()) {
1150 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001151 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1152 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001153 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001154 }
1155
1156 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001157 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1158 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001159 MaxBytesToFill = 0;
1160 }
1161 }
1162
1163 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1164 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1165
1166 return false;
1167}
1168
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001169/// ParseDirectiveSymbolAttribute
1170/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1171bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001172 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001173 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001174 StringRef Name;
1175
1176 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001177 return TokError("expected identifier in directive");
1178
Daniel Dunbar959fd882009-08-26 22:13:22 +00001179 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001180
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001181 Out.EmitSymbolAttribute(Sym, Attr);
1182
Daniel Dunbar3f872332009-07-28 16:08:33 +00001183 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001184 break;
1185
Daniel Dunbar3f872332009-07-28 16:08:33 +00001186 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001187 return TokError("unexpected token in directive");
1188 Lexer.Lex();
1189 }
1190 }
1191
1192 Lexer.Lex();
1193 return false;
1194}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001195
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001196/// ParseDirectiveDarwinSymbolDesc
1197/// ::= .desc identifier , expression
1198bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001199 StringRef Name;
1200 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001201 return TokError("expected identifier in directive");
1202
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001203 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001204 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001205
Daniel Dunbar3f872332009-07-28 16:08:33 +00001206 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001207 return TokError("unexpected token in '.desc' directive");
1208 Lexer.Lex();
1209
1210 SMLoc DescLoc = Lexer.getLoc();
1211 int64_t DescValue;
1212 if (ParseAbsoluteExpression(DescValue))
1213 return true;
1214
Daniel Dunbar3f872332009-07-28 16:08:33 +00001215 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001216 return TokError("unexpected token in '.desc' directive");
1217
1218 Lexer.Lex();
1219
1220 // Set the n_desc field of this Symbol to this DescValue
1221 Out.EmitSymbolDesc(Sym, DescValue);
1222
1223 return false;
1224}
1225
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001226/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001227/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1228bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001229 SMLoc IDLoc = Lexer.getLoc();
1230 StringRef Name;
1231 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001232 return TokError("expected identifier in directive");
1233
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001234 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001235 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001236
Daniel Dunbar3f872332009-07-28 16:08:33 +00001237 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001238 return TokError("unexpected token in directive");
1239 Lexer.Lex();
1240
1241 int64_t Size;
1242 SMLoc SizeLoc = Lexer.getLoc();
1243 if (ParseAbsoluteExpression(Size))
1244 return true;
1245
1246 int64_t Pow2Alignment = 0;
1247 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001248 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001249 Lexer.Lex();
1250 Pow2AlignmentLoc = Lexer.getLoc();
1251 if (ParseAbsoluteExpression(Pow2Alignment))
1252 return true;
1253 }
1254
Daniel Dunbar3f872332009-07-28 16:08:33 +00001255 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001256 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001257
1258 Lexer.Lex();
1259
Chris Lattner1fc3d752009-07-09 17:25:12 +00001260 // NOTE: a size of zero for a .comm should create a undefined symbol
1261 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001262 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001263 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1264 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001265
1266 // NOTE: The alignment in the directive is a power of 2 value, the assember
1267 // may internally end up wanting an alignment in bytes.
1268 // FIXME: Diagnose overflow.
1269 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001270 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1271 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001272
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001273 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001274 return Error(IDLoc, "invalid symbol redefinition");
1275
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001276 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001277 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001278 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001279 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1280 MCSectionMachO::S_ZEROFILL, 0,
1281 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001282 Sym, Size, 1 << Pow2Alignment);
1283 return false;
1284 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001285
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001286 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001287 return false;
1288}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001289
1290/// ParseDirectiveDarwinZerofill
1291/// ::= .zerofill segname , sectname [, identifier , size_expression [
1292/// , align_expression ]]
1293bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001294 // FIXME: Handle quoted names here.
1295
Daniel Dunbar3f872332009-07-28 16:08:33 +00001296 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001297 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001298 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001299 Lexer.Lex();
1300
Daniel Dunbar3f872332009-07-28 16:08:33 +00001301 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001302 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001303 Lexer.Lex();
1304
Daniel Dunbar3f872332009-07-28 16:08:33 +00001305 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001306 return TokError("expected section name after comma in '.zerofill' "
1307 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001308 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001309 Lexer.Lex();
1310
Chris Lattner9be3fee2009-07-10 22:20:30 +00001311 // If this is the end of the line all that was wanted was to create the
1312 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001313 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001314 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001315 Out.EmitZerofill(getMachOSection(Segment, Section,
1316 MCSectionMachO::S_ZEROFILL, 0,
1317 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001318 return false;
1319 }
1320
Daniel Dunbar3f872332009-07-28 16:08:33 +00001321 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001322 return TokError("unexpected token in directive");
1323 Lexer.Lex();
1324
Daniel Dunbar3f872332009-07-28 16:08:33 +00001325 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001326 return TokError("expected identifier in directive");
1327
1328 // handle the identifier as the key symbol.
1329 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001330 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001331 Lexer.Lex();
1332
Daniel Dunbar3f872332009-07-28 16:08:33 +00001333 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001334 return TokError("unexpected token in directive");
1335 Lexer.Lex();
1336
1337 int64_t Size;
1338 SMLoc SizeLoc = Lexer.getLoc();
1339 if (ParseAbsoluteExpression(Size))
1340 return true;
1341
1342 int64_t Pow2Alignment = 0;
1343 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001344 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001345 Lexer.Lex();
1346 Pow2AlignmentLoc = Lexer.getLoc();
1347 if (ParseAbsoluteExpression(Pow2Alignment))
1348 return true;
1349 }
1350
Daniel Dunbar3f872332009-07-28 16:08:33 +00001351 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001352 return TokError("unexpected token in '.zerofill' directive");
1353
1354 Lexer.Lex();
1355
1356 if (Size < 0)
1357 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1358 "than zero");
1359
1360 // NOTE: The alignment in the directive is a power of 2 value, the assember
1361 // may internally end up wanting an alignment in bytes.
1362 // FIXME: Diagnose overflow.
1363 if (Pow2Alignment < 0)
1364 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1365 "can't be less than zero");
1366
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001367 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001368 return Error(IDLoc, "invalid symbol redefinition");
1369
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001370 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001371 //
1372 // FIXME: Arch specific.
1373 Out.EmitZerofill(getMachOSection(Segment, Section,
1374 MCSectionMachO::S_ZEROFILL, 0,
1375 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001376 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001377
1378 return false;
1379}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001380
1381/// ParseDirectiveDarwinSubsectionsViaSymbols
1382/// ::= .subsections_via_symbols
1383bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001384 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001385 return TokError("unexpected token in '.subsections_via_symbols' directive");
1386
1387 Lexer.Lex();
1388
Kevin Enderbyf96db462009-07-16 17:56:39 +00001389 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001390
1391 return false;
1392}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001393
1394/// ParseDirectiveAbort
1395/// ::= .abort [ "abort_string" ]
1396bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001397 // FIXME: Use loc from directive.
1398 SMLoc Loc = Lexer.getLoc();
1399
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001400 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001401 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1402 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001403 return TokError("expected string in '.abort' directive");
1404
Daniel Dunbar419aded2009-07-28 16:38:40 +00001405 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001406
1407 Lexer.Lex();
1408 }
1409
Daniel Dunbar3f872332009-07-28 16:08:33 +00001410 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001411 return TokError("unexpected token in '.abort' directive");
1412
1413 Lexer.Lex();
1414
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001415 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001416 if (Str.empty())
1417 Error(Loc, ".abort detected. Assembly stopping.");
1418 else
1419 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001420
1421 return false;
1422}
Kevin Enderby71148242009-07-14 21:35:03 +00001423
1424/// ParseDirectiveLsym
1425/// ::= .lsym identifier , expression
1426bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001427 StringRef Name;
1428 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001429 return TokError("expected identifier in directive");
1430
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001431 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001432 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001433
Daniel Dunbar3f872332009-07-28 16:08:33 +00001434 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001435 return TokError("unexpected token in '.lsym' directive");
1436 Lexer.Lex();
1437
Daniel Dunbar821e3332009-08-31 08:09:28 +00001438 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001439 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001440 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001441 return true;
1442
Daniel Dunbar3f872332009-07-28 16:08:33 +00001443 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001444 return TokError("unexpected token in '.lsym' directive");
1445
1446 Lexer.Lex();
1447
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001448 // We don't currently support this directive.
1449 //
1450 // FIXME: Diagnostic location!
1451 (void) Sym;
1452 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001453}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001454
1455/// ParseDirectiveInclude
1456/// ::= .include "filename"
1457bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001458 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001459 return TokError("expected string in '.include' directive");
1460
Daniel Dunbar419aded2009-07-28 16:38:40 +00001461 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001462 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001463 Lexer.Lex();
1464
Daniel Dunbar3f872332009-07-28 16:08:33 +00001465 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001466 return TokError("unexpected token in '.include' directive");
1467
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001468 // Strip the quotes.
1469 Filename = Filename.substr(1, Filename.size()-2);
1470
1471 // Attempt to switch the lexer to the included file before consuming the end
1472 // of statement to avoid losing it when we switch.
1473 if (Lexer.EnterIncludeFile(Filename)) {
1474 Lexer.PrintMessage(IncludeLoc,
1475 "Could not find include file '" + Filename + "'",
1476 "error");
1477 return true;
1478 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001479
1480 return false;
1481}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001482
1483/// ParseDirectiveDarwinDumpOrLoad
1484/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001485bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001486 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001487 return TokError("expected string in '.dump' or '.load' directive");
1488
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001489 Lexer.Lex();
1490
Daniel Dunbar3f872332009-07-28 16:08:33 +00001491 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001492 return TokError("unexpected token in '.dump' or '.load' directive");
1493
1494 Lexer.Lex();
1495
Kevin Enderby5026ae42009-07-20 20:25:37 +00001496 // FIXME: If/when .dump and .load are implemented they will be done in the
1497 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001498 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001499 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001500 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001501 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001502
1503 return false;
1504}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001505
1506/// ParseDirectiveIf
1507/// ::= .if expression
1508bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1509 // Consume the identifier that was the .if directive
1510 Lexer.Lex();
1511
1512 TheCondStack.push_back(TheCondState);
1513 TheCondState.TheCond = AsmCond::IfCond;
1514 if(TheCondState.Ignore) {
1515 EatToEndOfStatement();
1516 }
1517 else {
1518 int64_t ExprValue;
1519 if (ParseAbsoluteExpression(ExprValue))
1520 return true;
1521
1522 if (Lexer.isNot(AsmToken::EndOfStatement))
1523 return TokError("unexpected token in '.if' directive");
1524
1525 Lexer.Lex();
1526
1527 TheCondState.CondMet = ExprValue;
1528 TheCondState.Ignore = !TheCondState.CondMet;
1529 }
1530
1531 return false;
1532}
1533
1534/// ParseDirectiveElseIf
1535/// ::= .elseif expression
1536bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1537 if (TheCondState.TheCond != AsmCond::IfCond &&
1538 TheCondState.TheCond != AsmCond::ElseIfCond)
1539 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1540 " an .elseif");
1541 TheCondState.TheCond = AsmCond::ElseIfCond;
1542
1543 // Consume the identifier that was the .elseif directive
1544 Lexer.Lex();
1545
1546 bool LastIgnoreState = false;
1547 if (!TheCondStack.empty())
1548 LastIgnoreState = TheCondStack.back().Ignore;
1549 if (LastIgnoreState || TheCondState.CondMet) {
1550 TheCondState.Ignore = true;
1551 EatToEndOfStatement();
1552 }
1553 else {
1554 int64_t ExprValue;
1555 if (ParseAbsoluteExpression(ExprValue))
1556 return true;
1557
1558 if (Lexer.isNot(AsmToken::EndOfStatement))
1559 return TokError("unexpected token in '.elseif' directive");
1560
1561 Lexer.Lex();
1562 TheCondState.CondMet = ExprValue;
1563 TheCondState.Ignore = !TheCondState.CondMet;
1564 }
1565
1566 return false;
1567}
1568
1569/// ParseDirectiveElse
1570/// ::= .else
1571bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1572 // Consume the identifier that was the .else directive
1573 Lexer.Lex();
1574
1575 if (Lexer.isNot(AsmToken::EndOfStatement))
1576 return TokError("unexpected token in '.else' directive");
1577
1578 Lexer.Lex();
1579
1580 if (TheCondState.TheCond != AsmCond::IfCond &&
1581 TheCondState.TheCond != AsmCond::ElseIfCond)
1582 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1583 ".elseif");
1584 TheCondState.TheCond = AsmCond::ElseCond;
1585 bool LastIgnoreState = false;
1586 if (!TheCondStack.empty())
1587 LastIgnoreState = TheCondStack.back().Ignore;
1588 if (LastIgnoreState || TheCondState.CondMet)
1589 TheCondState.Ignore = true;
1590 else
1591 TheCondState.Ignore = false;
1592
1593 return false;
1594}
1595
1596/// ParseDirectiveEndIf
1597/// ::= .endif
1598bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1599 // Consume the identifier that was the .endif directive
1600 Lexer.Lex();
1601
1602 if (Lexer.isNot(AsmToken::EndOfStatement))
1603 return TokError("unexpected token in '.endif' directive");
1604
1605 Lexer.Lex();
1606
1607 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1608 TheCondStack.empty())
1609 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1610 ".else");
1611 if (!TheCondStack.empty()) {
1612 TheCondState = TheCondStack.back();
1613 TheCondStack.pop_back();
1614 }
1615
1616 return false;
1617}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001618
1619/// ParseDirectiveFile
1620/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001621bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001622 // FIXME: I'm not sure what this is.
1623 int64_t FileNumber = -1;
1624 if (Lexer.is(AsmToken::Integer)) {
1625 FileNumber = Lexer.getTok().getIntVal();
1626 Lexer.Lex();
1627
1628 if (FileNumber < 1)
1629 return TokError("file number less than one");
1630 }
1631
1632 if (Lexer.isNot(AsmToken::String))
1633 return TokError("unexpected token in '.file' directive");
1634
Bill Wendling9bc0af82009-12-28 01:34:57 +00001635 StringRef ATTRIBUTE_UNUSED FileName = Lexer.getTok().getString();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001636 Lexer.Lex();
1637
1638 if (Lexer.isNot(AsmToken::EndOfStatement))
1639 return TokError("unexpected token in '.file' directive");
1640
1641 // FIXME: Do something with the .file.
1642
1643 return false;
1644}
1645
1646/// ParseDirectiveLine
1647/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001648bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001649 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1650 if (Lexer.isNot(AsmToken::Integer))
1651 return TokError("unexpected token in '.line' directive");
1652
1653 int64_t LineNumber = Lexer.getTok().getIntVal();
1654 (void) LineNumber;
1655 Lexer.Lex();
1656
1657 // FIXME: Do something with the .line.
1658 }
1659
1660 if (Lexer.isNot(AsmToken::EndOfStatement))
1661 return TokError("unexpected token in '.file' directive");
1662
1663 return false;
1664}
1665
1666
1667/// ParseDirectiveLoc
1668/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001669bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001670 if (Lexer.isNot(AsmToken::Integer))
1671 return TokError("unexpected token in '.loc' directive");
1672
1673 // FIXME: What are these fields?
1674 int64_t FileNumber = Lexer.getTok().getIntVal();
1675 (void) FileNumber;
1676 // FIXME: Validate file.
1677
1678 Lexer.Lex();
1679 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1680 if (Lexer.isNot(AsmToken::Integer))
1681 return TokError("unexpected token in '.loc' directive");
1682
1683 int64_t Param2 = Lexer.getTok().getIntVal();
1684 (void) Param2;
1685 Lexer.Lex();
1686
1687 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1688 if (Lexer.isNot(AsmToken::Integer))
1689 return TokError("unexpected token in '.loc' directive");
1690
1691 int64_t Param3 = Lexer.getTok().getIntVal();
1692 (void) Param3;
1693 Lexer.Lex();
1694
1695 // FIXME: Do something with the .loc.
1696 }
1697 }
1698
1699 if (Lexer.isNot(AsmToken::EndOfStatement))
1700 return TokError("unexpected token in '.file' directive");
1701
1702 return false;
1703}
1704