blob: b4e0f5792aaa93a984b22ce51bbcbc417e276192 [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
16#include "AsmExpr.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000018#include "llvm/MC/MCInst.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000020#include "llvm/MC/MCSymbol.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000021#include "llvm/Support/SourceMgr.h"
22#include "llvm/Support/raw_ostream.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000023using namespace llvm;
24
Daniel Dunbar3fb76832009-06-30 00:49:23 +000025void AsmParser::Warning(SMLoc L, const char *Msg) {
26 Lexer.PrintMessage(L, Msg, "warning");
27}
28
Chris Lattner14ee48a2009-06-21 21:22:11 +000029bool AsmParser::Error(SMLoc L, const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000030 Lexer.PrintMessage(L, Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000031 return true;
32}
33
34bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000035 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000036 return true;
37}
38
Chris Lattner27aa7d22009-06-21 20:16:42 +000039bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000040 // Prime the lexer.
41 Lexer.Lex();
42
Chris Lattnerb717fb02009-07-02 21:53:43 +000043 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000044
Chris Lattnerb717fb02009-07-02 21:53:43 +000045 // While we have input, parse each statement.
46 while (Lexer.isNot(asmtok::Eof)) {
47 if (!ParseStatement()) continue;
48
49 // If we had an error, remember it and recover by skipping to the next line.
50 HadError = true;
51 EatToEndOfStatement();
52 }
53
54 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000055}
56
Chris Lattner2cf5f142009-06-22 01:29:09 +000057/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
58void AsmParser::EatToEndOfStatement() {
59 while (Lexer.isNot(asmtok::EndOfStatement) &&
60 Lexer.isNot(asmtok::Eof))
61 Lexer.Lex();
62
63 // Eat EOL.
64 if (Lexer.is(asmtok::EndOfStatement))
65 Lexer.Lex();
66}
67
Chris Lattnerc4193832009-06-22 05:51:26 +000068
Chris Lattner74ec1a32009-06-22 06:32:03 +000069/// ParseParenExpr - Parse a paren expression and return it.
70/// NOTE: This assumes the leading '(' has already been consumed.
71///
72/// parenexpr ::= expr)
73///
Daniel Dunbar475839e2009-06-29 20:37:27 +000074bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +000075 if (ParseExpression(Res)) return true;
76 if (Lexer.isNot(asmtok::RParen))
77 return TokError("expected ')' in parentheses expression");
78 Lexer.Lex();
79 return false;
80}
Chris Lattnerc4193832009-06-22 05:51:26 +000081
Chris Lattner74ec1a32009-06-22 06:32:03 +000082/// ParsePrimaryExpr - Parse a primary expression and return it.
83/// primaryexpr ::= (parenexpr
84/// primaryexpr ::= symbol
85/// primaryexpr ::= number
86/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +000087bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +000088 switch (Lexer.getKind()) {
89 default:
90 return TokError("unknown token in expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +000091 case asmtok::Exclaim:
92 Lexer.Lex(); // Eat the operator.
93 if (ParsePrimaryExpr(Res))
94 return true;
95 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
96 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000097 case asmtok::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +000098 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +000099 // handle things like LFOO+4.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000100 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
101
102 // If this is use of an undefined symbol then mark it external.
103 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
104 Sym->setExternal(true);
105
106 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000107 Lexer.Lex(); // Eat identifier.
108 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000109 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000110 case asmtok::IntVal:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000111 Res = new AsmConstantExpr(Lexer.getCurIntVal());
Chris Lattnerc4193832009-06-22 05:51:26 +0000112 Lexer.Lex(); // Eat identifier.
113 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000114 case asmtok::LParen:
115 Lexer.Lex(); // Eat the '('.
116 return ParseParenExpr(Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000117 case asmtok::Minus:
118 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000119 if (ParsePrimaryExpr(Res))
120 return true;
121 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
122 return false;
123 case asmtok::Plus:
124 Lexer.Lex(); // Eat the operator.
125 if (ParsePrimaryExpr(Res))
126 return true;
127 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
128 return false;
129 case asmtok::Tilde:
130 Lexer.Lex(); // Eat the operator.
131 if (ParsePrimaryExpr(Res))
132 return true;
133 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
134 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000135 }
136}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000137
138/// ParseExpression - Parse an expression and return it.
139///
140/// expr ::= expr +,- expr -> lowest.
141/// expr ::= expr |,^,&,! expr -> middle.
142/// expr ::= expr *,/,%,<<,>> expr -> highest.
143/// expr ::= primaryexpr
144///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000145bool AsmParser::ParseExpression(AsmExpr *&Res) {
146 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000147 return ParsePrimaryExpr(Res) ||
148 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000149}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000150
Daniel Dunbar475839e2009-06-29 20:37:27 +0000151bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
152 AsmExpr *Expr;
153
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000154 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000155 if (ParseExpression(Expr))
156 return true;
157
158 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000159 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000160
161 return false;
162}
163
Daniel Dunbar15d17072009-06-30 01:49:52 +0000164bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
165 AsmExpr *Expr;
166
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000167 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000168 if (ParseExpression(Expr))
169 return true;
170
171 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000172 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000173
174 return false;
175}
176
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000177bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
178 AsmExpr *Expr;
179
180 SMLoc StartLoc = Lexer.getLoc();
181 if (ParseParenExpr(Expr))
182 return true;
183
184 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
185 return Error(StartLoc, "expected relocatable expression");
186
187 return false;
188}
189
Daniel Dunbar475839e2009-06-29 20:37:27 +0000190static unsigned getBinOpPrecedence(asmtok::TokKind K,
191 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000192 switch (K) {
193 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000194
195 // Lowest Precedence: &&, ||
196 case asmtok::AmpAmp:
197 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000198 return 1;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000199 case asmtok::PipePipe:
200 Kind = AsmBinaryExpr::LOr;
201 return 1;
202
203 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
204 case asmtok::Plus:
205 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000206 return 2;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000207 case asmtok::Minus:
208 Kind = AsmBinaryExpr::Sub;
209 return 2;
210 case asmtok::EqualEqual:
211 Kind = AsmBinaryExpr::EQ;
212 return 2;
213 case asmtok::ExclaimEqual:
214 case asmtok::LessGreater:
215 Kind = AsmBinaryExpr::NE;
216 return 2;
217 case asmtok::Less:
218 Kind = AsmBinaryExpr::LT;
219 return 2;
220 case asmtok::LessEqual:
221 Kind = AsmBinaryExpr::LTE;
222 return 2;
223 case asmtok::Greater:
224 Kind = AsmBinaryExpr::GT;
225 return 2;
226 case asmtok::GreaterEqual:
227 Kind = AsmBinaryExpr::GTE;
228 return 2;
229
230 // Intermediate Precedence: |, &, ^
231 //
232 // FIXME: gas seems to support '!' as an infix operator?
233 case asmtok::Pipe:
234 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000235 return 3;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000236 case asmtok::Caret:
237 Kind = AsmBinaryExpr::Xor;
238 return 3;
239 case asmtok::Amp:
240 Kind = AsmBinaryExpr::And;
241 return 3;
242
243 // Highest Precedence: *, /, %, <<, >>
244 case asmtok::Star:
245 Kind = AsmBinaryExpr::Mul;
246 return 4;
247 case asmtok::Slash:
248 Kind = AsmBinaryExpr::Div;
249 return 4;
250 case asmtok::Percent:
251 Kind = AsmBinaryExpr::Mod;
252 return 4;
253 case asmtok::LessLess:
254 Kind = AsmBinaryExpr::Shl;
255 return 4;
256 case asmtok::GreaterGreater:
257 Kind = AsmBinaryExpr::Shr;
258 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000259 }
260}
261
262
263/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
264/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000266 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000267 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000269
270 // If the next token is lower precedence than we are allowed to eat, return
271 // successfully with what we ate already.
272 if (TokPrec < Precedence)
273 return false;
274
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000275 Lexer.Lex();
276
277 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000278 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000279 if (ParsePrimaryExpr(RHS)) return true;
280
281 // If BinOp binds less tightly with RHS than the operator after RHS, let
282 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000283 AsmBinaryExpr::Opcode Dummy;
284 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000285 if (TokPrec < NextTokPrec) {
286 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
287 }
288
Daniel Dunbar475839e2009-06-29 20:37:27 +0000289 // Merge LHS and RHS according to operator.
290 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000291 }
292}
293
Chris Lattnerc4193832009-06-22 05:51:26 +0000294
295
296
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000297/// ParseStatement:
298/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000299/// ::= Label* Directive ...Operands... EndOfStatement
300/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000301bool AsmParser::ParseStatement() {
302 switch (Lexer.getKind()) {
303 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000304 return TokError("unexpected token at start of statement");
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000305 case asmtok::EndOfStatement:
306 Lexer.Lex();
307 return false;
308 case asmtok::Identifier:
309 break;
310 // TODO: Recurse on local labels etc.
311 }
312
313 // If we have an identifier, handle it as the key symbol.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000314 SMLoc IDLoc = Lexer.getLoc();
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000315 const char *IDVal = Lexer.getCurStrVal();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000316
317 // Consume the identifier, see what is after it.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000318 switch (Lexer.Lex()) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000319 case asmtok::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000320 // identifier ':' -> Label.
321 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000322
323 // Diagnose attempt to use a variable as a label.
324 //
325 // FIXME: Diagnostics. Note the location of the definition as a label.
326 // FIXME: This doesn't diagnose assignment to a symbol which has been
327 // implicitly marked as external.
328 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
329 if (Sym->getSection())
330 return Error(IDLoc, "invalid symbol redefinition");
331 if (Ctx.GetSymbolValue(Sym))
332 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000333
334 // Since we saw a label, create a symbol and emit it.
335 // FIXME: If the label starts with L it is an assembler temporary label.
336 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000337 Out.EmitLabel(Sym);
338
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000339 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000340 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000341
342 case asmtok::Equal:
343 // identifier '=' ... -> assignment statement
344 Lexer.Lex();
345
346 return ParseAssignment(IDVal, false);
347
348 default: // Normal instruction or directive.
349 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000350 }
351
352 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000353 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000354 // FIXME: This should be driven based on a hash lookup and callback.
Chris Lattner9a023f72009-06-24 04:43:34 +0000355 if (!strcmp(IDVal, ".section"))
Chris Lattner529fb542009-06-24 05:13:15 +0000356 return ParseDirectiveDarwinSection();
357 if (!strcmp(IDVal, ".text"))
358 // FIXME: This changes behavior based on the -static flag to the
359 // assembler.
360 return ParseDirectiveSectionSwitch("__TEXT,__text",
361 "regular,pure_instructions");
362 if (!strcmp(IDVal, ".const"))
363 return ParseDirectiveSectionSwitch("__TEXT,__const");
364 if (!strcmp(IDVal, ".static_const"))
365 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
366 if (!strcmp(IDVal, ".cstring"))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000367 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
368 "cstring_literals");
Chris Lattner529fb542009-06-24 05:13:15 +0000369 if (!strcmp(IDVal, ".literal4"))
370 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
371 if (!strcmp(IDVal, ".literal8"))
372 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
373 if (!strcmp(IDVal, ".literal16"))
374 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
375 "16byte_literals");
376 if (!strcmp(IDVal, ".constructor"))
377 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
378 if (!strcmp(IDVal, ".destructor"))
379 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
380 if (!strcmp(IDVal, ".fvmlib_init0"))
381 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
382 if (!strcmp(IDVal, ".fvmlib_init1"))
383 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
384 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
385 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
386 "self_modifying_code+pure_instructions,5");
387 // FIXME: .picsymbol_stub on PPC.
388 if (!strcmp(IDVal, ".data"))
389 return ParseDirectiveSectionSwitch("__DATA,__data");
390 if (!strcmp(IDVal, ".static_data"))
391 return ParseDirectiveSectionSwitch("__DATA,__static_data");
392 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
393 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
394 "non_lazy_symbol_pointers");
395 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
396 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
397 "lazy_symbol_pointers");
398 if (!strcmp(IDVal, ".dyld"))
399 return ParseDirectiveSectionSwitch("__DATA,__dyld");
400 if (!strcmp(IDVal, ".mod_init_func"))
401 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
402 "mod_init_funcs");
403 if (!strcmp(IDVal, ".mod_term_func"))
404 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
405 "mod_term_funcs");
406 if (!strcmp(IDVal, ".const_data"))
407 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
408
409
410 // FIXME: Verify attributes on sections.
411 if (!strcmp(IDVal, ".objc_class"))
412 return ParseDirectiveSectionSwitch("__OBJC,__class");
413 if (!strcmp(IDVal, ".objc_meta_class"))
414 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
415 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
416 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
417 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
418 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
419 if (!strcmp(IDVal, ".objc_protocol"))
420 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
421 if (!strcmp(IDVal, ".objc_string_object"))
422 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
423 if (!strcmp(IDVal, ".objc_cls_meth"))
424 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
425 if (!strcmp(IDVal, ".objc_inst_meth"))
426 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
427 if (!strcmp(IDVal, ".objc_cls_refs"))
428 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
429 if (!strcmp(IDVal, ".objc_message_refs"))
430 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
431 if (!strcmp(IDVal, ".objc_symbols"))
432 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
433 if (!strcmp(IDVal, ".objc_category"))
434 return ParseDirectiveSectionSwitch("__OBJC,__category");
435 if (!strcmp(IDVal, ".objc_class_vars"))
436 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
437 if (!strcmp(IDVal, ".objc_instance_vars"))
438 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
439 if (!strcmp(IDVal, ".objc_module_info"))
440 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
441 if (!strcmp(IDVal, ".objc_class_names"))
442 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
443 if (!strcmp(IDVal, ".objc_meth_var_types"))
444 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
445 if (!strcmp(IDVal, ".objc_meth_var_names"))
446 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
447 if (!strcmp(IDVal, ".objc_selector_strs"))
448 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000449
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000450 // Assembler features
451 if (!strcmp(IDVal, ".set"))
452 return ParseDirectiveSet();
453
Daniel Dunbara0d14262009-06-24 23:30:00 +0000454 // Data directives
455
456 if (!strcmp(IDVal, ".ascii"))
457 return ParseDirectiveAscii(false);
458 if (!strcmp(IDVal, ".asciz"))
459 return ParseDirectiveAscii(true);
460
461 // FIXME: Target hooks for size? Also for "word", "hword".
462 if (!strcmp(IDVal, ".byte"))
463 return ParseDirectiveValue(1);
464 if (!strcmp(IDVal, ".short"))
465 return ParseDirectiveValue(2);
466 if (!strcmp(IDVal, ".long"))
467 return ParseDirectiveValue(4);
468 if (!strcmp(IDVal, ".quad"))
469 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000470
471 // FIXME: Target hooks for IsPow2.
472 if (!strcmp(IDVal, ".align"))
473 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
474 if (!strcmp(IDVal, ".align32"))
475 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
476 if (!strcmp(IDVal, ".balign"))
477 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
478 if (!strcmp(IDVal, ".balignw"))
479 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
480 if (!strcmp(IDVal, ".balignl"))
481 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
482 if (!strcmp(IDVal, ".p2align"))
483 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
484 if (!strcmp(IDVal, ".p2alignw"))
485 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
486 if (!strcmp(IDVal, ".p2alignl"))
487 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
488
Daniel Dunbarc238b582009-06-25 22:44:51 +0000489 if (!strcmp(IDVal, ".org"))
490 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000491
492 if (!strcmp(IDVal, ".fill"))
493 return ParseDirectiveFill();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000494 if (!strcmp(IDVal, ".space"))
495 return ParseDirectiveSpace();
496
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000497 // Symbol attribute directives
498 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
499 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
500 if (!strcmp(IDVal, ".hidden"))
501 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
502 if (!strcmp(IDVal, ".indirect_symbol"))
503 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
504 if (!strcmp(IDVal, ".internal"))
505 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
506 if (!strcmp(IDVal, ".lazy_reference"))
507 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
508 if (!strcmp(IDVal, ".no_dead_strip"))
509 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
510 if (!strcmp(IDVal, ".private_extern"))
511 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
512 if (!strcmp(IDVal, ".protected"))
513 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
514 if (!strcmp(IDVal, ".reference"))
515 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
516 if (!strcmp(IDVal, ".weak"))
517 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
518 if (!strcmp(IDVal, ".weak_definition"))
519 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
520 if (!strcmp(IDVal, ".weak_reference"))
521 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
522
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000523 if (!strcmp(IDVal, ".comm"))
Chris Lattner1fc3d752009-07-09 17:25:12 +0000524 return ParseDirectiveComm(/*IsLocal=*/false);
525 if (!strcmp(IDVal, ".lcomm"))
526 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000527 if (!strcmp(IDVal, ".zerofill"))
528 return ParseDirectiveDarwinZerofill();
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000529 if (!strcmp(IDVal, ".desc"))
530 return ParseDirectiveDarwinSymbolDesc();
Kevin Enderby71148242009-07-14 21:35:03 +0000531 if (!strcmp(IDVal, ".lsym"))
532 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000533
Kevin Enderbya5c78322009-07-13 21:03:15 +0000534 if (!strcmp(IDVal, ".subsections_via_symbols"))
535 return ParseDirectiveDarwinSubsectionsViaSymbols();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000536 if (!strcmp(IDVal, ".abort"))
537 return ParseDirectiveAbort();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000538
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000539 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000540 EatToEndOfStatement();
541 return false;
542 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000543
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000544 MCInst Inst;
Daniel Dunbard9627e12009-06-30 23:38:38 +0000545 if (ParseX86InstOperands(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000546 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000547
548 if (Lexer.isNot(asmtok::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000549 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000550
551 // Eat the end of statement marker.
552 Lexer.Lex();
553
554 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000555 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000556
557 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000558 return false;
559}
Chris Lattner9a023f72009-06-24 04:43:34 +0000560
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000561bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000562 // FIXME: Use better location, we should use proper tokens.
563 SMLoc EqualLoc = Lexer.getLoc();
564
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000565 MCValue Value;
566 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000567 return true;
568
569 if (Lexer.isNot(asmtok::EndOfStatement))
570 return TokError("unexpected token in assignment");
571
572 // Eat the end of statement marker.
573 Lexer.Lex();
574
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000575 // Diagnose assignment to a label.
576 //
577 // FIXME: Diagnostics. Note the location of the definition as a label.
578 // FIXME: This doesn't diagnose assignment to a symbol which has been
579 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000580 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000581 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000582 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000583 if (Sym->getSection())
584 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
585 if (Sym->isExternal())
586 return Error(EqualLoc, "invalid assignment to external symbol");
587
588 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000589 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000590
591 return false;
592}
593
594/// ParseDirectiveSet:
595/// ::= .set identifier ',' expression
596bool AsmParser::ParseDirectiveSet() {
597 if (Lexer.isNot(asmtok::Identifier))
598 return TokError("expected identifier after '.set' directive");
599
600 const char *Name = Lexer.getCurStrVal();
601
602 if (Lexer.Lex() != asmtok::Comma)
603 return TokError("unexpected token in '.set'");
604 Lexer.Lex();
605
606 return ParseAssignment(Name, true);
607}
608
Chris Lattner9a023f72009-06-24 04:43:34 +0000609/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000610/// ::= .section identifier (',' identifier)*
611/// FIXME: This should actually parse out the segment, section, attributes and
612/// sizeof_stub fields.
613bool AsmParser::ParseDirectiveDarwinSection() {
Chris Lattner9a023f72009-06-24 04:43:34 +0000614 if (Lexer.isNot(asmtok::Identifier))
615 return TokError("expected identifier after '.section' directive");
616
617 std::string Section = Lexer.getCurStrVal();
618 Lexer.Lex();
619
620 // Accept a comma separated list of modifiers.
621 while (Lexer.is(asmtok::Comma)) {
622 Lexer.Lex();
623
624 if (Lexer.isNot(asmtok::Identifier))
625 return TokError("expected identifier in '.section' directive");
626 Section += ',';
627 Section += Lexer.getCurStrVal();
628 Lexer.Lex();
629 }
630
631 if (Lexer.isNot(asmtok::EndOfStatement))
632 return TokError("unexpected token in '.section' directive");
633 Lexer.Lex();
634
635 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
636 return false;
637}
638
Chris Lattner529fb542009-06-24 05:13:15 +0000639bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
640 const char *Directives) {
641 if (Lexer.isNot(asmtok::EndOfStatement))
642 return TokError("unexpected token in section switching directive");
643 Lexer.Lex();
644
645 std::string SectionStr = Section;
646 if (Directives && Directives[0]) {
647 SectionStr += ",";
648 SectionStr += Directives;
649 }
650
651 Out.SwitchSection(Ctx.GetSection(Section));
652 return false;
653}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000654
655/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000656/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000657bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
658 if (Lexer.isNot(asmtok::EndOfStatement)) {
659 for (;;) {
660 if (Lexer.isNot(asmtok::String))
661 return TokError("expected string in '.ascii' or '.asciz' directive");
662
663 // FIXME: This shouldn't use a const char* + strlen, the string could have
664 // embedded nulls.
665 // FIXME: Should have accessor for getting string contents.
666 const char *Str = Lexer.getCurStrVal();
667 Out.EmitBytes(Str + 1, strlen(Str) - 2);
668 if (ZeroTerminated)
669 Out.EmitBytes("\0", 1);
670
671 Lexer.Lex();
672
673 if (Lexer.is(asmtok::EndOfStatement))
674 break;
675
676 if (Lexer.isNot(asmtok::Comma))
677 return TokError("unexpected token in '.ascii' or '.asciz' directive");
678 Lexer.Lex();
679 }
680 }
681
682 Lexer.Lex();
683 return false;
684}
685
686/// ParseDirectiveValue
687/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
688bool AsmParser::ParseDirectiveValue(unsigned Size) {
689 if (Lexer.isNot(asmtok::EndOfStatement)) {
690 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000691 MCValue Expr;
692 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000693 return true;
694
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000695 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000696
697 if (Lexer.is(asmtok::EndOfStatement))
698 break;
699
700 // FIXME: Improve diagnostic.
701 if (Lexer.isNot(asmtok::Comma))
702 return TokError("unexpected token in directive");
703 Lexer.Lex();
704 }
705 }
706
707 Lexer.Lex();
708 return false;
709}
710
711/// ParseDirectiveSpace
712/// ::= .space expression [ , expression ]
713bool AsmParser::ParseDirectiveSpace() {
714 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000715 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000716 return true;
717
718 int64_t FillExpr = 0;
719 bool HasFillExpr = false;
720 if (Lexer.isNot(asmtok::EndOfStatement)) {
721 if (Lexer.isNot(asmtok::Comma))
722 return TokError("unexpected token in '.space' directive");
723 Lexer.Lex();
724
Daniel Dunbar475839e2009-06-29 20:37:27 +0000725 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000726 return true;
727
728 HasFillExpr = true;
729
730 if (Lexer.isNot(asmtok::EndOfStatement))
731 return TokError("unexpected token in '.space' directive");
732 }
733
734 Lexer.Lex();
735
736 if (NumBytes <= 0)
737 return TokError("invalid number of bytes in '.space' directive");
738
739 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
740 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
741 Out.EmitValue(MCValue::get(FillExpr), 1);
742
743 return false;
744}
745
746/// ParseDirectiveFill
747/// ::= .fill expression , expression , expression
748bool AsmParser::ParseDirectiveFill() {
749 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000750 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000751 return true;
752
753 if (Lexer.isNot(asmtok::Comma))
754 return TokError("unexpected token in '.fill' directive");
755 Lexer.Lex();
756
757 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000758 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000759 return true;
760
761 if (Lexer.isNot(asmtok::Comma))
762 return TokError("unexpected token in '.fill' directive");
763 Lexer.Lex();
764
765 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000766 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000767 return true;
768
769 if (Lexer.isNot(asmtok::EndOfStatement))
770 return TokError("unexpected token in '.fill' directive");
771
772 Lexer.Lex();
773
774 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
775 return TokError("invalid '.fill' size, expected 1, 2, or 4");
776
777 for (uint64_t i = 0, e = NumValues; i != e; ++i)
778 Out.EmitValue(MCValue::get(FillExpr), FillSize);
779
780 return false;
781}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000782
783/// ParseDirectiveOrg
784/// ::= .org expression [ , expression ]
785bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000786 MCValue Offset;
787 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000788 return true;
789
790 // Parse optional fill expression.
791 int64_t FillExpr = 0;
792 if (Lexer.isNot(asmtok::EndOfStatement)) {
793 if (Lexer.isNot(asmtok::Comma))
794 return TokError("unexpected token in '.org' directive");
795 Lexer.Lex();
796
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000798 return true;
799
800 if (Lexer.isNot(asmtok::EndOfStatement))
801 return TokError("unexpected token in '.org' directive");
802 }
803
804 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000805
806 // FIXME: Only limited forms of relocatable expressions are accepted here, it
807 // has to be relative to the current section.
808 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000809
810 return false;
811}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000812
813/// ParseDirectiveAlign
814/// ::= {.align, ...} expression [ , expression [ , expression ]]
815bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
816 int64_t Alignment;
817 if (ParseAbsoluteExpression(Alignment))
818 return true;
819
820 SMLoc MaxBytesLoc;
821 bool HasFillExpr = false;
822 int64_t FillExpr = 0;
823 int64_t MaxBytesToFill = 0;
824 if (Lexer.isNot(asmtok::EndOfStatement)) {
825 if (Lexer.isNot(asmtok::Comma))
826 return TokError("unexpected token in directive");
827 Lexer.Lex();
828
829 // The fill expression can be omitted while specifying a maximum number of
830 // alignment bytes, e.g:
831 // .align 3,,4
832 if (Lexer.isNot(asmtok::Comma)) {
833 HasFillExpr = true;
834 if (ParseAbsoluteExpression(FillExpr))
835 return true;
836 }
837
838 if (Lexer.isNot(asmtok::EndOfStatement)) {
839 if (Lexer.isNot(asmtok::Comma))
840 return TokError("unexpected token in directive");
841 Lexer.Lex();
842
843 MaxBytesLoc = Lexer.getLoc();
844 if (ParseAbsoluteExpression(MaxBytesToFill))
845 return true;
846
847 if (Lexer.isNot(asmtok::EndOfStatement))
848 return TokError("unexpected token in directive");
849 }
850 }
851
852 Lexer.Lex();
853
854 if (!HasFillExpr) {
855 // FIXME: Sometimes fill with nop.
856 FillExpr = 0;
857 }
858
859 // Compute alignment in bytes.
860 if (IsPow2) {
861 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000862 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000863 }
864
865 // Diagnose non-sensical max bytes to fill.
866 if (MaxBytesLoc.isValid()) {
867 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000868 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
869 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000870 return false;
871 }
872
873 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000874 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
875 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000876 MaxBytesToFill = 0;
877 }
878 }
879
880 // FIXME: Target specific behavior about how the "extra" bytes are filled.
881 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
882
883 return false;
884}
885
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000886/// ParseDirectiveSymbolAttribute
887/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
888bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
889 if (Lexer.isNot(asmtok::EndOfStatement)) {
890 for (;;) {
891 if (Lexer.isNot(asmtok::Identifier))
892 return TokError("expected identifier in directive");
893
894 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
895 Lexer.Lex();
896
897 // If this is use of an undefined symbol then mark it external.
898 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
899 Sym->setExternal(true);
900
901 Out.EmitSymbolAttribute(Sym, Attr);
902
903 if (Lexer.is(asmtok::EndOfStatement))
904 break;
905
906 if (Lexer.isNot(asmtok::Comma))
907 return TokError("unexpected token in directive");
908 Lexer.Lex();
909 }
910 }
911
912 Lexer.Lex();
913 return false;
914}
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000915
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000916/// ParseDirectiveDarwinSymbolDesc
917/// ::= .desc identifier , expression
918bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
919 if (Lexer.isNot(asmtok::Identifier))
920 return TokError("expected identifier in directive");
921
922 // handle the identifier as the key symbol.
923 SMLoc IDLoc = Lexer.getLoc();
924 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
925 Lexer.Lex();
926
927 if (Lexer.isNot(asmtok::Comma))
928 return TokError("unexpected token in '.desc' directive");
929 Lexer.Lex();
930
931 SMLoc DescLoc = Lexer.getLoc();
932 int64_t DescValue;
933 if (ParseAbsoluteExpression(DescValue))
934 return true;
935
936 if (Lexer.isNot(asmtok::EndOfStatement))
937 return TokError("unexpected token in '.desc' directive");
938
939 Lexer.Lex();
940
941 // Set the n_desc field of this Symbol to this DescValue
942 Out.EmitSymbolDesc(Sym, DescValue);
943
944 return false;
945}
946
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000947/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +0000948/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
949bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000950 if (Lexer.isNot(asmtok::Identifier))
951 return TokError("expected identifier in directive");
952
953 // handle the identifier as the key symbol.
954 SMLoc IDLoc = Lexer.getLoc();
955 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
956 Lexer.Lex();
957
958 if (Lexer.isNot(asmtok::Comma))
959 return TokError("unexpected token in directive");
960 Lexer.Lex();
961
962 int64_t Size;
963 SMLoc SizeLoc = Lexer.getLoc();
964 if (ParseAbsoluteExpression(Size))
965 return true;
966
967 int64_t Pow2Alignment = 0;
968 SMLoc Pow2AlignmentLoc;
969 if (Lexer.is(asmtok::Comma)) {
970 Lexer.Lex();
971 Pow2AlignmentLoc = Lexer.getLoc();
972 if (ParseAbsoluteExpression(Pow2Alignment))
973 return true;
974 }
975
976 if (Lexer.isNot(asmtok::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +0000977 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000978
979 Lexer.Lex();
980
Chris Lattner1fc3d752009-07-09 17:25:12 +0000981 // NOTE: a size of zero for a .comm should create a undefined symbol
982 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000983 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +0000984 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
985 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000986
987 // NOTE: The alignment in the directive is a power of 2 value, the assember
988 // may internally end up wanting an alignment in bytes.
989 // FIXME: Diagnose overflow.
990 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +0000991 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
992 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000993
994 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
995 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
996 return Error(IDLoc, "invalid symbol redefinition");
997
Chris Lattner1fc3d752009-07-09 17:25:12 +0000998 // Create the Symbol as a common or local common with Size and Pow2Alignment
999 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001000
1001 return false;
1002}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001003
1004/// ParseDirectiveDarwinZerofill
1005/// ::= .zerofill segname , sectname [, identifier , size_expression [
1006/// , align_expression ]]
1007bool AsmParser::ParseDirectiveDarwinZerofill() {
1008 if (Lexer.isNot(asmtok::Identifier))
1009 return TokError("expected segment name after '.zerofill' directive");
1010 std::string Section = Lexer.getCurStrVal();
1011 Lexer.Lex();
1012
1013 if (Lexer.isNot(asmtok::Comma))
1014 return TokError("unexpected token in directive");
1015 Section += ',';
1016 Lexer.Lex();
1017
1018 if (Lexer.isNot(asmtok::Identifier))
1019 return TokError("expected section name after comma in '.zerofill' "
1020 "directive");
1021 Section += Lexer.getCurStrVal();
1022 Lexer.Lex();
1023
1024 // FIXME: we will need to tell GetSection() that this is to be created with or
1025 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1026 // below could be done but for now it is not as EmitZerofill() does not know
1027 // how to deal with a section type in the section name like
1028 // ParseDirectiveDarwinSection() allows.
1029 // Section += ',';
1030 // Section += "zerofill";
1031
1032 // If this is the end of the line all that was wanted was to create the
1033 // the section but with no symbol.
1034 if (Lexer.is(asmtok::EndOfStatement)) {
1035 // Create the zerofill section but no symbol
1036 Out.EmitZerofill(Ctx.GetSection(Section.c_str()));
1037 return false;
1038 }
1039
1040 if (Lexer.isNot(asmtok::Comma))
1041 return TokError("unexpected token in directive");
1042 Lexer.Lex();
1043
1044 if (Lexer.isNot(asmtok::Identifier))
1045 return TokError("expected identifier in directive");
1046
1047 // handle the identifier as the key symbol.
1048 SMLoc IDLoc = Lexer.getLoc();
1049 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
1050 Lexer.Lex();
1051
1052 if (Lexer.isNot(asmtok::Comma))
1053 return TokError("unexpected token in directive");
1054 Lexer.Lex();
1055
1056 int64_t Size;
1057 SMLoc SizeLoc = Lexer.getLoc();
1058 if (ParseAbsoluteExpression(Size))
1059 return true;
1060
1061 int64_t Pow2Alignment = 0;
1062 SMLoc Pow2AlignmentLoc;
1063 if (Lexer.is(asmtok::Comma)) {
1064 Lexer.Lex();
1065 Pow2AlignmentLoc = Lexer.getLoc();
1066 if (ParseAbsoluteExpression(Pow2Alignment))
1067 return true;
1068 }
1069
1070 if (Lexer.isNot(asmtok::EndOfStatement))
1071 return TokError("unexpected token in '.zerofill' directive");
1072
1073 Lexer.Lex();
1074
1075 if (Size < 0)
1076 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1077 "than zero");
1078
1079 // NOTE: The alignment in the directive is a power of 2 value, the assember
1080 // may internally end up wanting an alignment in bytes.
1081 // FIXME: Diagnose overflow.
1082 if (Pow2Alignment < 0)
1083 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1084 "can't be less than zero");
1085
1086 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1087 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1088 return Error(IDLoc, "invalid symbol redefinition");
1089
1090 // Create the zerofill Symbol with Size and Pow2Alignment
1091 Out.EmitZerofill(Ctx.GetSection(Section.c_str()), Sym, Size, Pow2Alignment);
1092
1093 return false;
1094}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001095
1096/// ParseDirectiveDarwinSubsectionsViaSymbols
1097/// ::= .subsections_via_symbols
1098bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1099 if (Lexer.isNot(asmtok::EndOfStatement))
1100 return TokError("unexpected token in '.subsections_via_symbols' directive");
1101
1102 Lexer.Lex();
1103
1104 Out.SubsectionsViaSymbols();
1105
1106 return false;
1107}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001108
1109/// ParseDirectiveAbort
1110/// ::= .abort [ "abort_string" ]
1111bool AsmParser::ParseDirectiveAbort() {
1112 const char *Str = NULL;
1113 if (Lexer.isNot(asmtok::EndOfStatement)) {
1114 if (Lexer.isNot(asmtok::String))
1115 return TokError("expected string in '.abort' directive");
1116
1117 Str = Lexer.getCurStrVal();
1118
1119 Lexer.Lex();
1120 }
1121
1122 if (Lexer.isNot(asmtok::EndOfStatement))
1123 return TokError("unexpected token in '.abort' directive");
1124
1125 Lexer.Lex();
1126
1127 Out.AbortAssembly(Str);
1128
1129 return false;
1130}
Kevin Enderby71148242009-07-14 21:35:03 +00001131
1132/// ParseDirectiveLsym
1133/// ::= .lsym identifier , expression
1134bool AsmParser::ParseDirectiveDarwinLsym() {
1135 if (Lexer.isNot(asmtok::Identifier))
1136 return TokError("expected identifier in directive");
1137
1138 // handle the identifier as the key symbol.
1139 SMLoc IDLoc = Lexer.getLoc();
1140 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
1141 Lexer.Lex();
1142
1143 if (Lexer.isNot(asmtok::Comma))
1144 return TokError("unexpected token in '.lsym' directive");
1145 Lexer.Lex();
1146
1147 MCValue Expr;
1148 if (ParseRelocatableExpression(Expr))
1149 return true;
1150
1151 if (Lexer.isNot(asmtok::EndOfStatement))
1152 return TokError("unexpected token in '.lsym' directive");
1153
1154 Lexer.Lex();
1155
1156 // Create the Sym with the value of the Expr
1157 Out.EmitLocalSymbol(Sym, Expr);
1158
1159 return false;
1160}