blob: 9414f9918c62ad0ab531e2f41f50d709de0e48e6 [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
43 while (Lexer.isNot(asmtok::Eof))
44 if (ParseStatement())
45 return true;
46
47 return false;
48}
49
Chris Lattner2cf5f142009-06-22 01:29:09 +000050/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
51void AsmParser::EatToEndOfStatement() {
52 while (Lexer.isNot(asmtok::EndOfStatement) &&
53 Lexer.isNot(asmtok::Eof))
54 Lexer.Lex();
55
56 // Eat EOL.
57 if (Lexer.is(asmtok::EndOfStatement))
58 Lexer.Lex();
59}
60
Chris Lattnerc4193832009-06-22 05:51:26 +000061
Chris Lattner74ec1a32009-06-22 06:32:03 +000062/// ParseParenExpr - Parse a paren expression and return it.
63/// NOTE: This assumes the leading '(' has already been consumed.
64///
65/// parenexpr ::= expr)
66///
Daniel Dunbar475839e2009-06-29 20:37:27 +000067bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +000068 if (ParseExpression(Res)) return true;
69 if (Lexer.isNot(asmtok::RParen))
70 return TokError("expected ')' in parentheses expression");
71 Lexer.Lex();
72 return false;
73}
Chris Lattnerc4193832009-06-22 05:51:26 +000074
Chris Lattner74ec1a32009-06-22 06:32:03 +000075/// ParsePrimaryExpr - Parse a primary expression and return it.
76/// primaryexpr ::= (parenexpr
77/// primaryexpr ::= symbol
78/// primaryexpr ::= number
79/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +000080bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +000081 switch (Lexer.getKind()) {
82 default:
83 return TokError("unknown token in expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +000084 case asmtok::Exclaim:
85 Lexer.Lex(); // Eat the operator.
86 if (ParsePrimaryExpr(Res))
87 return true;
88 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
89 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000090 case asmtok::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +000091 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +000092 // handle things like LFOO+4.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000093 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
94
95 // If this is use of an undefined symbol then mark it external.
96 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
97 Sym->setExternal(true);
98
99 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000100 Lexer.Lex(); // Eat identifier.
101 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000102 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000103 case asmtok::IntVal:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000104 Res = new AsmConstantExpr(Lexer.getCurIntVal());
Chris Lattnerc4193832009-06-22 05:51:26 +0000105 Lexer.Lex(); // Eat identifier.
106 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000107 case asmtok::LParen:
108 Lexer.Lex(); // Eat the '('.
109 return ParseParenExpr(Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000110 case asmtok::Minus:
111 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000112 if (ParsePrimaryExpr(Res))
113 return true;
114 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
115 return false;
116 case asmtok::Plus:
117 Lexer.Lex(); // Eat the operator.
118 if (ParsePrimaryExpr(Res))
119 return true;
120 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
121 return false;
122 case asmtok::Tilde:
123 Lexer.Lex(); // Eat the operator.
124 if (ParsePrimaryExpr(Res))
125 return true;
126 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
127 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000128 }
129}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000130
131/// ParseExpression - Parse an expression and return it.
132///
133/// expr ::= expr +,- expr -> lowest.
134/// expr ::= expr |,^,&,! expr -> middle.
135/// expr ::= expr *,/,%,<<,>> expr -> highest.
136/// expr ::= primaryexpr
137///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000138bool AsmParser::ParseExpression(AsmExpr *&Res) {
139 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000140 return ParsePrimaryExpr(Res) ||
141 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000142}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000143
Daniel Dunbar475839e2009-06-29 20:37:27 +0000144bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
145 AsmExpr *Expr;
146
147 if (ParseExpression(Expr))
148 return true;
149
150 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
151 return TokError("expected absolute expression");
152
153 return false;
154}
155
156static unsigned getBinOpPrecedence(asmtok::TokKind K,
157 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000158 switch (K) {
159 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000160
161 // Lowest Precedence: &&, ||
162 case asmtok::AmpAmp:
163 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000164 return 1;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000165 case asmtok::PipePipe:
166 Kind = AsmBinaryExpr::LOr;
167 return 1;
168
169 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
170 case asmtok::Plus:
171 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000172 return 2;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000173 case asmtok::Minus:
174 Kind = AsmBinaryExpr::Sub;
175 return 2;
176 case asmtok::EqualEqual:
177 Kind = AsmBinaryExpr::EQ;
178 return 2;
179 case asmtok::ExclaimEqual:
180 case asmtok::LessGreater:
181 Kind = AsmBinaryExpr::NE;
182 return 2;
183 case asmtok::Less:
184 Kind = AsmBinaryExpr::LT;
185 return 2;
186 case asmtok::LessEqual:
187 Kind = AsmBinaryExpr::LTE;
188 return 2;
189 case asmtok::Greater:
190 Kind = AsmBinaryExpr::GT;
191 return 2;
192 case asmtok::GreaterEqual:
193 Kind = AsmBinaryExpr::GTE;
194 return 2;
195
196 // Intermediate Precedence: |, &, ^
197 //
198 // FIXME: gas seems to support '!' as an infix operator?
199 case asmtok::Pipe:
200 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000201 return 3;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000202 case asmtok::Caret:
203 Kind = AsmBinaryExpr::Xor;
204 return 3;
205 case asmtok::Amp:
206 Kind = AsmBinaryExpr::And;
207 return 3;
208
209 // Highest Precedence: *, /, %, <<, >>
210 case asmtok::Star:
211 Kind = AsmBinaryExpr::Mul;
212 return 4;
213 case asmtok::Slash:
214 Kind = AsmBinaryExpr::Div;
215 return 4;
216 case asmtok::Percent:
217 Kind = AsmBinaryExpr::Mod;
218 return 4;
219 case asmtok::LessLess:
220 Kind = AsmBinaryExpr::Shl;
221 return 4;
222 case asmtok::GreaterGreater:
223 Kind = AsmBinaryExpr::Shr;
224 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000225 }
226}
227
228
229/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
230/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000231bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000232 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000233 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000234 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000235
236 // If the next token is lower precedence than we are allowed to eat, return
237 // successfully with what we ate already.
238 if (TokPrec < Precedence)
239 return false;
240
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000241 Lexer.Lex();
242
243 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000244 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000245 if (ParsePrimaryExpr(RHS)) return true;
246
247 // If BinOp binds less tightly with RHS than the operator after RHS, let
248 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000249 AsmBinaryExpr::Opcode Dummy;
250 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000251 if (TokPrec < NextTokPrec) {
252 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
253 }
254
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 // Merge LHS and RHS according to operator.
256 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000257 }
258}
259
Chris Lattnerc4193832009-06-22 05:51:26 +0000260
261
262
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000263/// ParseStatement:
264/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000265/// ::= Label* Directive ...Operands... EndOfStatement
266/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000267bool AsmParser::ParseStatement() {
268 switch (Lexer.getKind()) {
269 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000270 return TokError("unexpected token at start of statement");
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000271 case asmtok::EndOfStatement:
272 Lexer.Lex();
273 return false;
274 case asmtok::Identifier:
275 break;
276 // TODO: Recurse on local labels etc.
277 }
278
279 // If we have an identifier, handle it as the key symbol.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000280 SMLoc IDLoc = Lexer.getLoc();
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000281 const char *IDVal = Lexer.getCurStrVal();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000282
283 // Consume the identifier, see what is after it.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000284 switch (Lexer.Lex()) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000285 case asmtok::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000286 // identifier ':' -> Label.
287 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000288
289 // Diagnose attempt to use a variable as a label.
290 //
291 // FIXME: Diagnostics. Note the location of the definition as a label.
292 // FIXME: This doesn't diagnose assignment to a symbol which has been
293 // implicitly marked as external.
294 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
295 if (Sym->getSection())
296 return Error(IDLoc, "invalid symbol redefinition");
297 if (Ctx.GetSymbolValue(Sym))
298 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000299
300 // Since we saw a label, create a symbol and emit it.
301 // FIXME: If the label starts with L it is an assembler temporary label.
302 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000303 Out.EmitLabel(Sym);
304
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000305 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000306 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000307
308 case asmtok::Equal:
309 // identifier '=' ... -> assignment statement
310 Lexer.Lex();
311
312 return ParseAssignment(IDVal, false);
313
314 default: // Normal instruction or directive.
315 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000316 }
317
318 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000319 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000320 // FIXME: This should be driven based on a hash lookup and callback.
Chris Lattner9a023f72009-06-24 04:43:34 +0000321 if (!strcmp(IDVal, ".section"))
Chris Lattner529fb542009-06-24 05:13:15 +0000322 return ParseDirectiveDarwinSection();
323 if (!strcmp(IDVal, ".text"))
324 // FIXME: This changes behavior based on the -static flag to the
325 // assembler.
326 return ParseDirectiveSectionSwitch("__TEXT,__text",
327 "regular,pure_instructions");
328 if (!strcmp(IDVal, ".const"))
329 return ParseDirectiveSectionSwitch("__TEXT,__const");
330 if (!strcmp(IDVal, ".static_const"))
331 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
332 if (!strcmp(IDVal, ".cstring"))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000333 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
334 "cstring_literals");
Chris Lattner529fb542009-06-24 05:13:15 +0000335 if (!strcmp(IDVal, ".literal4"))
336 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
337 if (!strcmp(IDVal, ".literal8"))
338 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
339 if (!strcmp(IDVal, ".literal16"))
340 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
341 "16byte_literals");
342 if (!strcmp(IDVal, ".constructor"))
343 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
344 if (!strcmp(IDVal, ".destructor"))
345 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
346 if (!strcmp(IDVal, ".fvmlib_init0"))
347 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
348 if (!strcmp(IDVal, ".fvmlib_init1"))
349 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
350 if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
351 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
352 "self_modifying_code+pure_instructions,5");
353 // FIXME: .picsymbol_stub on PPC.
354 if (!strcmp(IDVal, ".data"))
355 return ParseDirectiveSectionSwitch("__DATA,__data");
356 if (!strcmp(IDVal, ".static_data"))
357 return ParseDirectiveSectionSwitch("__DATA,__static_data");
358 if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
359 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
360 "non_lazy_symbol_pointers");
361 if (!strcmp(IDVal, ".lazy_symbol_pointer"))
362 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
363 "lazy_symbol_pointers");
364 if (!strcmp(IDVal, ".dyld"))
365 return ParseDirectiveSectionSwitch("__DATA,__dyld");
366 if (!strcmp(IDVal, ".mod_init_func"))
367 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
368 "mod_init_funcs");
369 if (!strcmp(IDVal, ".mod_term_func"))
370 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
371 "mod_term_funcs");
372 if (!strcmp(IDVal, ".const_data"))
373 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
374
375
376 // FIXME: Verify attributes on sections.
377 if (!strcmp(IDVal, ".objc_class"))
378 return ParseDirectiveSectionSwitch("__OBJC,__class");
379 if (!strcmp(IDVal, ".objc_meta_class"))
380 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
381 if (!strcmp(IDVal, ".objc_cat_cls_meth"))
382 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
383 if (!strcmp(IDVal, ".objc_cat_inst_meth"))
384 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
385 if (!strcmp(IDVal, ".objc_protocol"))
386 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
387 if (!strcmp(IDVal, ".objc_string_object"))
388 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
389 if (!strcmp(IDVal, ".objc_cls_meth"))
390 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
391 if (!strcmp(IDVal, ".objc_inst_meth"))
392 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
393 if (!strcmp(IDVal, ".objc_cls_refs"))
394 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
395 if (!strcmp(IDVal, ".objc_message_refs"))
396 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
397 if (!strcmp(IDVal, ".objc_symbols"))
398 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
399 if (!strcmp(IDVal, ".objc_category"))
400 return ParseDirectiveSectionSwitch("__OBJC,__category");
401 if (!strcmp(IDVal, ".objc_class_vars"))
402 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
403 if (!strcmp(IDVal, ".objc_instance_vars"))
404 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
405 if (!strcmp(IDVal, ".objc_module_info"))
406 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
407 if (!strcmp(IDVal, ".objc_class_names"))
408 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
409 if (!strcmp(IDVal, ".objc_meth_var_types"))
410 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
411 if (!strcmp(IDVal, ".objc_meth_var_names"))
412 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
413 if (!strcmp(IDVal, ".objc_selector_strs"))
414 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000415
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000416 // Assembler features
417 if (!strcmp(IDVal, ".set"))
418 return ParseDirectiveSet();
419
Daniel Dunbara0d14262009-06-24 23:30:00 +0000420 // Data directives
421
422 if (!strcmp(IDVal, ".ascii"))
423 return ParseDirectiveAscii(false);
424 if (!strcmp(IDVal, ".asciz"))
425 return ParseDirectiveAscii(true);
426
427 // FIXME: Target hooks for size? Also for "word", "hword".
428 if (!strcmp(IDVal, ".byte"))
429 return ParseDirectiveValue(1);
430 if (!strcmp(IDVal, ".short"))
431 return ParseDirectiveValue(2);
432 if (!strcmp(IDVal, ".long"))
433 return ParseDirectiveValue(4);
434 if (!strcmp(IDVal, ".quad"))
435 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000436
437 // FIXME: Target hooks for IsPow2.
438 if (!strcmp(IDVal, ".align"))
439 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
440 if (!strcmp(IDVal, ".align32"))
441 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
442 if (!strcmp(IDVal, ".balign"))
443 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
444 if (!strcmp(IDVal, ".balignw"))
445 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
446 if (!strcmp(IDVal, ".balignl"))
447 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
448 if (!strcmp(IDVal, ".p2align"))
449 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
450 if (!strcmp(IDVal, ".p2alignw"))
451 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
452 if (!strcmp(IDVal, ".p2alignl"))
453 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
454
Daniel Dunbarc238b582009-06-25 22:44:51 +0000455 if (!strcmp(IDVal, ".org"))
456 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000457
458 if (!strcmp(IDVal, ".fill"))
459 return ParseDirectiveFill();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000460 if (!strcmp(IDVal, ".space"))
461 return ParseDirectiveSpace();
462
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000463 // Symbol attribute directives
464 if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
465 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
466 if (!strcmp(IDVal, ".hidden"))
467 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
468 if (!strcmp(IDVal, ".indirect_symbol"))
469 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
470 if (!strcmp(IDVal, ".internal"))
471 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
472 if (!strcmp(IDVal, ".lazy_reference"))
473 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
474 if (!strcmp(IDVal, ".no_dead_strip"))
475 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
476 if (!strcmp(IDVal, ".private_extern"))
477 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
478 if (!strcmp(IDVal, ".protected"))
479 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
480 if (!strcmp(IDVal, ".reference"))
481 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
482 if (!strcmp(IDVal, ".weak"))
483 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
484 if (!strcmp(IDVal, ".weak_definition"))
485 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
486 if (!strcmp(IDVal, ".weak_reference"))
487 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
488
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000489 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000490 EatToEndOfStatement();
491 return false;
492 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000493
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000494 MCInst Inst;
495 if (ParseX86InstOperands(Inst))
496 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000497
498 if (Lexer.isNot(asmtok::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000499 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000500
501 // Eat the end of statement marker.
502 Lexer.Lex();
503
504 // Instruction is good, process it.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000505 outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
Chris Lattner2cf5f142009-06-22 01:29:09 +0000506 << " operands.\n";
507
508 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000509 return false;
510}
Chris Lattner9a023f72009-06-24 04:43:34 +0000511
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000512bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000513 // FIXME: Use better location, we should use proper tokens.
514 SMLoc EqualLoc = Lexer.getLoc();
515
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000516 int64_t Value;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000517 if (ParseAbsoluteExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000518 return true;
519
520 if (Lexer.isNot(asmtok::EndOfStatement))
521 return TokError("unexpected token in assignment");
522
523 // Eat the end of statement marker.
524 Lexer.Lex();
525
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000526 // Diagnose assignment to a label.
527 //
528 // FIXME: Diagnostics. Note the location of the definition as a label.
529 // FIXME: This doesn't diagnose assignment to a symbol which has been
530 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000531 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000532 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000533 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000534 if (Sym->getSection())
535 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
536 if (Sym->isExternal())
537 return Error(EqualLoc, "invalid assignment to external symbol");
538
539 // Do the assignment.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000540 Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
541
542 return false;
543}
544
545/// ParseDirectiveSet:
546/// ::= .set identifier ',' expression
547bool AsmParser::ParseDirectiveSet() {
548 if (Lexer.isNot(asmtok::Identifier))
549 return TokError("expected identifier after '.set' directive");
550
551 const char *Name = Lexer.getCurStrVal();
552
553 if (Lexer.Lex() != asmtok::Comma)
554 return TokError("unexpected token in '.set'");
555 Lexer.Lex();
556
557 return ParseAssignment(Name, true);
558}
559
Chris Lattner9a023f72009-06-24 04:43:34 +0000560/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000561/// ::= .section identifier (',' identifier)*
562/// FIXME: This should actually parse out the segment, section, attributes and
563/// sizeof_stub fields.
564bool AsmParser::ParseDirectiveDarwinSection() {
Chris Lattner9a023f72009-06-24 04:43:34 +0000565 if (Lexer.isNot(asmtok::Identifier))
566 return TokError("expected identifier after '.section' directive");
567
568 std::string Section = Lexer.getCurStrVal();
569 Lexer.Lex();
570
571 // Accept a comma separated list of modifiers.
572 while (Lexer.is(asmtok::Comma)) {
573 Lexer.Lex();
574
575 if (Lexer.isNot(asmtok::Identifier))
576 return TokError("expected identifier in '.section' directive");
577 Section += ',';
578 Section += Lexer.getCurStrVal();
579 Lexer.Lex();
580 }
581
582 if (Lexer.isNot(asmtok::EndOfStatement))
583 return TokError("unexpected token in '.section' directive");
584 Lexer.Lex();
585
586 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
587 return false;
588}
589
Chris Lattner529fb542009-06-24 05:13:15 +0000590bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
591 const char *Directives) {
592 if (Lexer.isNot(asmtok::EndOfStatement))
593 return TokError("unexpected token in section switching directive");
594 Lexer.Lex();
595
596 std::string SectionStr = Section;
597 if (Directives && Directives[0]) {
598 SectionStr += ",";
599 SectionStr += Directives;
600 }
601
602 Out.SwitchSection(Ctx.GetSection(Section));
603 return false;
604}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000605
606/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000607/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000608bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
609 if (Lexer.isNot(asmtok::EndOfStatement)) {
610 for (;;) {
611 if (Lexer.isNot(asmtok::String))
612 return TokError("expected string in '.ascii' or '.asciz' directive");
613
614 // FIXME: This shouldn't use a const char* + strlen, the string could have
615 // embedded nulls.
616 // FIXME: Should have accessor for getting string contents.
617 const char *Str = Lexer.getCurStrVal();
618 Out.EmitBytes(Str + 1, strlen(Str) - 2);
619 if (ZeroTerminated)
620 Out.EmitBytes("\0", 1);
621
622 Lexer.Lex();
623
624 if (Lexer.is(asmtok::EndOfStatement))
625 break;
626
627 if (Lexer.isNot(asmtok::Comma))
628 return TokError("unexpected token in '.ascii' or '.asciz' directive");
629 Lexer.Lex();
630 }
631 }
632
633 Lexer.Lex();
634 return false;
635}
636
637/// ParseDirectiveValue
638/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
639bool AsmParser::ParseDirectiveValue(unsigned Size) {
640 if (Lexer.isNot(asmtok::EndOfStatement)) {
641 for (;;) {
642 int64_t Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 if (ParseAbsoluteExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000644 return true;
645
646 Out.EmitValue(MCValue::get(Expr), Size);
647
648 if (Lexer.is(asmtok::EndOfStatement))
649 break;
650
651 // FIXME: Improve diagnostic.
652 if (Lexer.isNot(asmtok::Comma))
653 return TokError("unexpected token in directive");
654 Lexer.Lex();
655 }
656 }
657
658 Lexer.Lex();
659 return false;
660}
661
662/// ParseDirectiveSpace
663/// ::= .space expression [ , expression ]
664bool AsmParser::ParseDirectiveSpace() {
665 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000666 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000667 return true;
668
669 int64_t FillExpr = 0;
670 bool HasFillExpr = false;
671 if (Lexer.isNot(asmtok::EndOfStatement)) {
672 if (Lexer.isNot(asmtok::Comma))
673 return TokError("unexpected token in '.space' directive");
674 Lexer.Lex();
675
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000677 return true;
678
679 HasFillExpr = true;
680
681 if (Lexer.isNot(asmtok::EndOfStatement))
682 return TokError("unexpected token in '.space' directive");
683 }
684
685 Lexer.Lex();
686
687 if (NumBytes <= 0)
688 return TokError("invalid number of bytes in '.space' directive");
689
690 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
691 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
692 Out.EmitValue(MCValue::get(FillExpr), 1);
693
694 return false;
695}
696
697/// ParseDirectiveFill
698/// ::= .fill expression , expression , expression
699bool AsmParser::ParseDirectiveFill() {
700 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000701 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000702 return true;
703
704 if (Lexer.isNot(asmtok::Comma))
705 return TokError("unexpected token in '.fill' directive");
706 Lexer.Lex();
707
708 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000709 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000710 return true;
711
712 if (Lexer.isNot(asmtok::Comma))
713 return TokError("unexpected token in '.fill' directive");
714 Lexer.Lex();
715
716 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000717 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000718 return true;
719
720 if (Lexer.isNot(asmtok::EndOfStatement))
721 return TokError("unexpected token in '.fill' directive");
722
723 Lexer.Lex();
724
725 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
726 return TokError("invalid '.fill' size, expected 1, 2, or 4");
727
728 for (uint64_t i = 0, e = NumValues; i != e; ++i)
729 Out.EmitValue(MCValue::get(FillExpr), FillSize);
730
731 return false;
732}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000733
734/// ParseDirectiveOrg
735/// ::= .org expression [ , expression ]
736bool AsmParser::ParseDirectiveOrg() {
737 int64_t Offset;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000738 if (ParseAbsoluteExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000739 return true;
740
741 // Parse optional fill expression.
742 int64_t FillExpr = 0;
743 if (Lexer.isNot(asmtok::EndOfStatement)) {
744 if (Lexer.isNot(asmtok::Comma))
745 return TokError("unexpected token in '.org' directive");
746 Lexer.Lex();
747
Daniel Dunbar475839e2009-06-29 20:37:27 +0000748 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000749 return true;
750
751 if (Lexer.isNot(asmtok::EndOfStatement))
752 return TokError("unexpected token in '.org' directive");
753 }
754
755 Lexer.Lex();
756
757 Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
758
759 return false;
760}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000761
762/// ParseDirectiveAlign
763/// ::= {.align, ...} expression [ , expression [ , expression ]]
764bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
765 int64_t Alignment;
766 if (ParseAbsoluteExpression(Alignment))
767 return true;
768
769 SMLoc MaxBytesLoc;
770 bool HasFillExpr = false;
771 int64_t FillExpr = 0;
772 int64_t MaxBytesToFill = 0;
773 if (Lexer.isNot(asmtok::EndOfStatement)) {
774 if (Lexer.isNot(asmtok::Comma))
775 return TokError("unexpected token in directive");
776 Lexer.Lex();
777
778 // The fill expression can be omitted while specifying a maximum number of
779 // alignment bytes, e.g:
780 // .align 3,,4
781 if (Lexer.isNot(asmtok::Comma)) {
782 HasFillExpr = true;
783 if (ParseAbsoluteExpression(FillExpr))
784 return true;
785 }
786
787 if (Lexer.isNot(asmtok::EndOfStatement)) {
788 if (Lexer.isNot(asmtok::Comma))
789 return TokError("unexpected token in directive");
790 Lexer.Lex();
791
792 MaxBytesLoc = Lexer.getLoc();
793 if (ParseAbsoluteExpression(MaxBytesToFill))
794 return true;
795
796 if (Lexer.isNot(asmtok::EndOfStatement))
797 return TokError("unexpected token in directive");
798 }
799 }
800
801 Lexer.Lex();
802
803 if (!HasFillExpr) {
804 // FIXME: Sometimes fill with nop.
805 FillExpr = 0;
806 }
807
808 // Compute alignment in bytes.
809 if (IsPow2) {
810 // FIXME: Diagnose overflow.
811 Alignment = 1 << Alignment;
812 }
813
814 // Diagnose non-sensical max bytes to fill.
815 if (MaxBytesLoc.isValid()) {
816 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000817 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
818 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000819 return false;
820 }
821
822 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000823 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
824 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000825 MaxBytesToFill = 0;
826 }
827 }
828
829 // FIXME: Target specific behavior about how the "extra" bytes are filled.
830 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
831
832 return false;
833}
834
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000835/// ParseDirectiveSymbolAttribute
836/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
837bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
838 if (Lexer.isNot(asmtok::EndOfStatement)) {
839 for (;;) {
840 if (Lexer.isNot(asmtok::Identifier))
841 return TokError("expected identifier in directive");
842
843 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
844 Lexer.Lex();
845
846 // If this is use of an undefined symbol then mark it external.
847 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
848 Sym->setExternal(true);
849
850 Out.EmitSymbolAttribute(Sym, Attr);
851
852 if (Lexer.is(asmtok::EndOfStatement))
853 break;
854
855 if (Lexer.isNot(asmtok::Comma))
856 return TokError("unexpected token in directive");
857 Lexer.Lex();
858 }
859 }
860
861 Lexer.Lex();
862 return false;
863}