2009-06-21 20:16:42 +00:00
|
|
|
//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class implements the parser for assembly files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AsmParser.h"
|
2009-06-29 20:37:27 +00:00
|
|
|
|
|
|
|
#include "AsmExpr.h"
|
2009-07-27 23:20:52 +00:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2009-06-23 22:01:43 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-06-23 18:41:30 +00:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2009-07-31 17:47:16 +00:00
|
|
|
#include "llvm/MC/MCSection.h"
|
2009-06-23 22:01:43 +00:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2009-06-29 23:43:14 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-06-21 20:54:55 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-07-20 18:55:04 +00:00
|
|
|
#include "llvm/Target/TargetAsmParser.h"
|
2009-06-21 20:16:42 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-27 23:20:52 +00:00
|
|
|
void AsmParser::Warning(SMLoc L, const Twine &Msg) {
|
|
|
|
Lexer.PrintMessage(L, Msg.str(), "warning");
|
2009-06-30 00:49:23 +00:00
|
|
|
}
|
|
|
|
|
2009-07-27 23:20:52 +00:00
|
|
|
bool AsmParser::Error(SMLoc L, const Twine &Msg) {
|
|
|
|
Lexer.PrintMessage(L, Msg.str(), "error");
|
2009-06-21 21:22:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsmParser::TokError(const char *Msg) {
|
2009-06-30 00:49:23 +00:00
|
|
|
Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
|
2009-06-21 21:22:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-21 20:16:42 +00:00
|
|
|
bool AsmParser::Run() {
|
2009-06-21 20:54:55 +00:00
|
|
|
// Prime the lexer.
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-02 21:53:43 +00:00
|
|
|
bool HadError = false;
|
2009-06-21 20:54:55 +00:00
|
|
|
|
2009-07-02 21:53:43 +00:00
|
|
|
// While we have input, parse each statement.
|
2009-07-28 16:08:33 +00:00
|
|
|
while (Lexer.isNot(AsmToken::Eof)) {
|
2009-07-02 21:53:43 +00:00
|
|
|
if (!ParseStatement()) continue;
|
|
|
|
|
|
|
|
// If we had an error, remember it and recover by skipping to the next line.
|
|
|
|
HadError = true;
|
|
|
|
EatToEndOfStatement();
|
|
|
|
}
|
|
|
|
|
|
|
|
return HadError;
|
2009-06-21 20:54:55 +00:00
|
|
|
}
|
|
|
|
|
2009-06-22 01:29:09 +00:00
|
|
|
/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
|
|
|
|
void AsmParser::EatToEndOfStatement() {
|
2009-07-28 16:08:33 +00:00
|
|
|
while (Lexer.isNot(AsmToken::EndOfStatement) &&
|
|
|
|
Lexer.isNot(AsmToken::Eof))
|
2009-06-22 01:29:09 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// Eat EOL.
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.is(AsmToken::EndOfStatement))
|
2009-06-22 01:29:09 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
}
|
|
|
|
|
2009-06-22 05:51:26 +00:00
|
|
|
|
2009-06-22 06:32:03 +00:00
|
|
|
/// ParseParenExpr - Parse a paren expression and return it.
|
|
|
|
/// NOTE: This assumes the leading '(' has already been consumed.
|
|
|
|
///
|
|
|
|
/// parenexpr ::= expr)
|
|
|
|
///
|
2009-06-29 20:37:27 +00:00
|
|
|
bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
|
2009-06-22 06:32:03 +00:00
|
|
|
if (ParseExpression(Res)) return true;
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::RParen))
|
2009-06-22 06:32:03 +00:00
|
|
|
return TokError("expected ')' in parentheses expression");
|
|
|
|
Lexer.Lex();
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-21 20:54:55 +00:00
|
|
|
|
2009-06-22 06:32:03 +00:00
|
|
|
/// ParsePrimaryExpr - Parse a primary expression and return it.
|
|
|
|
/// primaryexpr ::= (parenexpr
|
|
|
|
/// primaryexpr ::= symbol
|
|
|
|
/// primaryexpr ::= number
|
|
|
|
/// primaryexpr ::= ~,+,- primaryexpr
|
2009-06-29 20:37:27 +00:00
|
|
|
bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
|
2009-06-22 05:51:26 +00:00
|
|
|
switch (Lexer.getKind()) {
|
|
|
|
default:
|
|
|
|
return TokError("unknown token in expression");
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Exclaim:
|
2009-06-29 20:37:27 +00:00
|
|
|
Lexer.Lex(); // Eat the operator.
|
|
|
|
if (ParsePrimaryExpr(Res))
|
|
|
|
return true;
|
|
|
|
Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
|
|
|
|
return false;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Identifier: {
|
2009-06-22 05:51:26 +00:00
|
|
|
// This is a label, this should be parsed as part of an expression, to
|
2009-06-29 20:37:27 +00:00
|
|
|
// handle things like LFOO+4.
|
2009-07-28 16:38:40 +00:00
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
|
2009-06-29 23:43:14 +00:00
|
|
|
|
|
|
|
// If this is use of an undefined symbol then mark it external.
|
|
|
|
if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
|
|
|
|
Sym->setExternal(true);
|
|
|
|
|
|
|
|
Res = new AsmSymbolRefExpr(Sym);
|
2009-06-22 05:51:26 +00:00
|
|
|
Lexer.Lex(); // Eat identifier.
|
|
|
|
return false;
|
2009-06-29 23:43:14 +00:00
|
|
|
}
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Integer:
|
2009-07-28 16:38:40 +00:00
|
|
|
Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
|
2009-06-22 05:51:26 +00:00
|
|
|
Lexer.Lex(); // Eat identifier.
|
|
|
|
return false;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::LParen:
|
2009-06-22 06:32:03 +00:00
|
|
|
Lexer.Lex(); // Eat the '('.
|
|
|
|
return ParseParenExpr(Res);
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Minus:
|
2009-06-22 06:32:03 +00:00
|
|
|
Lexer.Lex(); // Eat the operator.
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParsePrimaryExpr(Res))
|
|
|
|
return true;
|
|
|
|
Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
|
|
|
|
return false;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Plus:
|
2009-06-29 20:37:27 +00:00
|
|
|
Lexer.Lex(); // Eat the operator.
|
|
|
|
if (ParsePrimaryExpr(Res))
|
|
|
|
return true;
|
|
|
|
Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
|
|
|
|
return false;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Tilde:
|
2009-06-29 20:37:27 +00:00
|
|
|
Lexer.Lex(); // Eat the operator.
|
|
|
|
if (ParsePrimaryExpr(Res))
|
|
|
|
return true;
|
|
|
|
Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
|
|
|
|
return false;
|
2009-06-22 05:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-22 06:32:03 +00:00
|
|
|
|
|
|
|
/// ParseExpression - Parse an expression and return it.
|
|
|
|
///
|
|
|
|
/// expr ::= expr +,- expr -> lowest.
|
|
|
|
/// expr ::= expr |,^,&,! expr -> middle.
|
|
|
|
/// expr ::= expr *,/,%,<<,>> expr -> highest.
|
|
|
|
/// expr ::= primaryexpr
|
|
|
|
///
|
2009-06-29 20:37:27 +00:00
|
|
|
bool AsmParser::ParseExpression(AsmExpr *&Res) {
|
|
|
|
Res = 0;
|
2009-06-23 05:57:07 +00:00
|
|
|
return ParsePrimaryExpr(Res) ||
|
|
|
|
ParseBinOpRHS(1, Res);
|
2009-06-22 06:32:03 +00:00
|
|
|
}
|
2009-06-23 05:57:07 +00:00
|
|
|
|
2009-06-29 20:37:27 +00:00
|
|
|
bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
|
|
|
|
AsmExpr *Expr;
|
|
|
|
|
2009-06-30 02:10:03 +00:00
|
|
|
SMLoc StartLoc = Lexer.getLoc();
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParseExpression(Expr))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Expr->EvaluateAsAbsolute(Ctx, Res))
|
2009-06-30 02:10:03 +00:00
|
|
|
return Error(StartLoc, "expected absolute expression");
|
2009-06-29 20:37:27 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-30 01:49:52 +00:00
|
|
|
bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
|
|
|
|
AsmExpr *Expr;
|
|
|
|
|
2009-06-30 02:10:03 +00:00
|
|
|
SMLoc StartLoc = Lexer.getLoc();
|
2009-06-30 01:49:52 +00:00
|
|
|
if (ParseExpression(Expr))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Expr->EvaluateAsRelocatable(Ctx, Res))
|
2009-06-30 02:10:03 +00:00
|
|
|
return Error(StartLoc, "expected relocatable expression");
|
2009-06-30 01:49:52 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-02 02:09:07 +00:00
|
|
|
bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
|
|
|
|
AsmExpr *Expr;
|
|
|
|
|
|
|
|
SMLoc StartLoc = Lexer.getLoc();
|
|
|
|
if (ParseParenExpr(Expr))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Expr->EvaluateAsRelocatable(Ctx, Res))
|
|
|
|
return Error(StartLoc, "expected relocatable expression");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
|
2009-06-29 20:37:27 +00:00
|
|
|
AsmBinaryExpr::Opcode &Kind) {
|
2009-06-23 05:57:07 +00:00
|
|
|
switch (K) {
|
|
|
|
default: return 0; // not a binop.
|
2009-06-29 20:37:27 +00:00
|
|
|
|
|
|
|
// Lowest Precedence: &&, ||
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::AmpAmp:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::LAnd;
|
|
|
|
return 1;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::PipePipe:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::LOr;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Plus:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Add;
|
|
|
|
return 2;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Minus:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Sub;
|
|
|
|
return 2;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::EqualEqual:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::EQ;
|
|
|
|
return 2;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::ExclaimEqual:
|
|
|
|
case AsmToken::LessGreater:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::NE;
|
|
|
|
return 2;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Less:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::LT;
|
|
|
|
return 2;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::LessEqual:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::LTE;
|
|
|
|
return 2;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Greater:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::GT;
|
|
|
|
return 2;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::GreaterEqual:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::GTE;
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
// Intermediate Precedence: |, &, ^
|
|
|
|
//
|
|
|
|
// FIXME: gas seems to support '!' as an infix operator?
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Pipe:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Or;
|
|
|
|
return 3;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Caret:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Xor;
|
|
|
|
return 3;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Amp:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::And;
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
// Highest Precedence: *, /, %, <<, >>
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Star:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Mul;
|
|
|
|
return 4;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Slash:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Div;
|
|
|
|
return 4;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Percent:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Mod;
|
|
|
|
return 4;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::LessLess:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Shl;
|
|
|
|
return 4;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::GreaterGreater:
|
2009-06-29 20:37:27 +00:00
|
|
|
Kind = AsmBinaryExpr::Shr;
|
|
|
|
return 4;
|
2009-06-23 05:57:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
|
|
|
|
/// Res contains the LHS of the expression on input.
|
2009-06-29 20:37:27 +00:00
|
|
|
bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
|
2009-06-23 05:57:07 +00:00
|
|
|
while (1) {
|
2009-06-29 21:14:21 +00:00
|
|
|
AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
|
2009-06-29 20:37:27 +00:00
|
|
|
unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
|
2009-06-23 05:57:07 +00:00
|
|
|
|
|
|
|
// If the next token is lower precedence than we are allowed to eat, return
|
|
|
|
// successfully with what we ate already.
|
|
|
|
if (TokPrec < Precedence)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// Eat the next primary expression.
|
2009-06-29 20:37:27 +00:00
|
|
|
AsmExpr *RHS;
|
2009-06-23 05:57:07 +00:00
|
|
|
if (ParsePrimaryExpr(RHS)) return true;
|
|
|
|
|
|
|
|
// If BinOp binds less tightly with RHS than the operator after RHS, let
|
|
|
|
// the pending operator take RHS as its LHS.
|
2009-06-29 20:37:27 +00:00
|
|
|
AsmBinaryExpr::Opcode Dummy;
|
|
|
|
unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
|
2009-06-23 05:57:07 +00:00
|
|
|
if (TokPrec < NextTokPrec) {
|
|
|
|
if (ParseBinOpRHS(Precedence+1, RHS)) return true;
|
|
|
|
}
|
|
|
|
|
2009-06-29 20:37:27 +00:00
|
|
|
// Merge LHS and RHS according to operator.
|
|
|
|
Res = new AsmBinaryExpr(Kind, Res, RHS);
|
2009-06-23 05:57:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-22 05:51:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-21 20:54:55 +00:00
|
|
|
/// ParseStatement:
|
|
|
|
/// ::= EndOfStatement
|
2009-06-22 01:29:09 +00:00
|
|
|
/// ::= Label* Directive ...Operands... EndOfStatement
|
|
|
|
/// ::= Label* Identifier OperandList* EndOfStatement
|
2009-06-21 20:54:55 +00:00
|
|
|
bool AsmParser::ParseStatement() {
|
|
|
|
switch (Lexer.getKind()) {
|
|
|
|
default:
|
2009-06-21 21:22:11 +00:00
|
|
|
return TokError("unexpected token at start of statement");
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::EndOfStatement:
|
2009-06-21 20:54:55 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
return false;
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Identifier:
|
2009-06-21 20:54:55 +00:00
|
|
|
break;
|
|
|
|
// TODO: Recurse on local labels etc.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an identifier, handle it as the key symbol.
|
2009-07-28 16:38:40 +00:00
|
|
|
AsmToken ID = Lexer.getTok();
|
|
|
|
SMLoc IDLoc = ID.getLoc();
|
|
|
|
StringRef IDVal = ID.getString();
|
2009-06-21 20:54:55 +00:00
|
|
|
|
|
|
|
// Consume the identifier, see what is after it.
|
2009-07-28 16:56:42 +00:00
|
|
|
switch (Lexer.Lex().getKind()) {
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Colon: {
|
2009-06-21 20:54:55 +00:00
|
|
|
// identifier ':' -> Label.
|
|
|
|
Lexer.Lex();
|
2009-06-29 23:43:14 +00:00
|
|
|
|
|
|
|
// Diagnose attempt to use a variable as a label.
|
|
|
|
//
|
|
|
|
// FIXME: Diagnostics. Note the location of the definition as a label.
|
|
|
|
// FIXME: This doesn't diagnose assignment to a symbol which has been
|
|
|
|
// implicitly marked as external.
|
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
|
|
|
|
if (Sym->getSection())
|
|
|
|
return Error(IDLoc, "invalid symbol redefinition");
|
|
|
|
if (Ctx.GetSymbolValue(Sym))
|
|
|
|
return Error(IDLoc, "symbol already used as assembler variable");
|
2009-06-24 04:31:49 +00:00
|
|
|
|
|
|
|
// Since we saw a label, create a symbol and emit it.
|
|
|
|
// FIXME: If the label starts with L it is an assembler temporary label.
|
|
|
|
// Why does the client of this api need to know this?
|
2009-06-29 23:43:14 +00:00
|
|
|
Out.EmitLabel(Sym);
|
|
|
|
|
2009-06-21 20:54:55 +00:00
|
|
|
return ParseStatement();
|
2009-06-29 23:43:14 +00:00
|
|
|
}
|
2009-06-25 21:56:11 +00:00
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
case AsmToken::Equal:
|
2009-06-25 21:56:11 +00:00
|
|
|
// identifier '=' ... -> assignment statement
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
return ParseAssignment(IDVal, false);
|
|
|
|
|
|
|
|
default: // Normal instruction or directive.
|
|
|
|
break;
|
2009-06-21 20:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we have a normal instruction or directive.
|
2009-06-22 01:29:09 +00:00
|
|
|
if (IDVal[0] == '.') {
|
2009-06-24 05:13:15 +00:00
|
|
|
// FIXME: This should be driven based on a hash lookup and callback.
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".section")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveDarwinSection();
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".text")
|
2009-06-24 05:13:15 +00:00
|
|
|
// FIXME: This changes behavior based on the -static flag to the
|
|
|
|
// assembler.
|
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__text",
|
|
|
|
"regular,pure_instructions");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".const")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__const");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".static_const")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__static_const");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".cstring")
|
2009-06-25 21:56:11 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__cstring",
|
|
|
|
"cstring_literals");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".literal4")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".literal8")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".literal16")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__literal16",
|
|
|
|
"16byte_literals");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".constructor")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__constructor");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".destructor")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__destructor");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".fvmlib_init0")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".fvmlib_init1")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
|
|
|
|
"self_modifying_code+pure_instructions,5");
|
|
|
|
// FIXME: .picsymbol_stub on PPC.
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".data")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__data");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".static_data")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__static_data");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".non_lazy_symbol_pointer")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
|
|
|
|
"non_lazy_symbol_pointers");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".lazy_symbol_pointer")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
|
|
|
|
"lazy_symbol_pointers");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".dyld")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__dyld");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".mod_init_func")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
|
|
|
|
"mod_init_funcs");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".mod_term_func")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
|
|
|
|
"mod_term_funcs");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".const_data")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: Verify attributes on sections.
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_class")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__class");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_meta_class")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_cat_cls_meth")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_cat_inst_meth")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_protocol")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__protocol");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_string_object")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__string_object");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_cls_meth")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_inst_meth")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_cls_refs")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_message_refs")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_symbols")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__symbols");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_category")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__category");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_class_vars")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_instance_vars")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_module_info")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__module_info");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_class_names")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_meth_var_types")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_meth_var_names")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".objc_selector_strs")
|
2009-06-24 05:13:15 +00:00
|
|
|
return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
|
2009-06-24 04:43:34 +00:00
|
|
|
|
2009-06-25 21:56:11 +00:00
|
|
|
// Assembler features
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".set")
|
2009-06-25 21:56:11 +00:00
|
|
|
return ParseDirectiveSet();
|
|
|
|
|
2009-06-24 23:30:00 +00:00
|
|
|
// Data directives
|
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".ascii")
|
2009-06-24 23:30:00 +00:00
|
|
|
return ParseDirectiveAscii(false);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".asciz")
|
2009-06-24 23:30:00 +00:00
|
|
|
return ParseDirectiveAscii(true);
|
|
|
|
|
|
|
|
// FIXME: Target hooks for size? Also for "word", "hword".
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".byte")
|
2009-06-24 23:30:00 +00:00
|
|
|
return ParseDirectiveValue(1);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".short")
|
2009-06-24 23:30:00 +00:00
|
|
|
return ParseDirectiveValue(2);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".long")
|
2009-06-24 23:30:00 +00:00
|
|
|
return ParseDirectiveValue(4);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".quad")
|
2009-06-24 23:30:00 +00:00
|
|
|
return ParseDirectiveValue(8);
|
2009-06-29 23:46:59 +00:00
|
|
|
|
|
|
|
// FIXME: Target hooks for IsPow2.
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".align")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".align32")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".balign")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".balignw")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".balignl")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".p2align")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".p2alignw")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".p2alignl")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
|
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".org")
|
2009-06-25 22:44:51 +00:00
|
|
|
return ParseDirectiveOrg();
|
2009-06-29 23:46:59 +00:00
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".fill")
|
2009-06-29 23:46:59 +00:00
|
|
|
return ParseDirectiveFill();
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".space")
|
2009-06-24 23:30:00 +00:00
|
|
|
return ParseDirectiveSpace();
|
|
|
|
|
2009-06-30 00:33:19 +00:00
|
|
|
// Symbol attribute directives
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".globl" || IDVal == ".global")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::Global);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".hidden")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".indirect_symbol")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".internal")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".lazy_reference")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".no_dead_strip")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".private_extern")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".protected")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".reference")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".weak")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".weak_definition")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".weak_reference")
|
2009-06-30 00:33:19 +00:00
|
|
|
return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
|
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".comm")
|
2009-07-09 17:25:12 +00:00
|
|
|
return ParseDirectiveComm(/*IsLocal=*/false);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".lcomm")
|
2009-07-09 17:25:12 +00:00
|
|
|
return ParseDirectiveComm(/*IsLocal=*/true);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".zerofill")
|
2009-07-10 22:20:30 +00:00
|
|
|
return ParseDirectiveDarwinZerofill();
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".desc")
|
2009-07-14 18:17:10 +00:00
|
|
|
return ParseDirectiveDarwinSymbolDesc();
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".lsym")
|
2009-07-14 21:35:03 +00:00
|
|
|
return ParseDirectiveDarwinLsym();
|
2009-07-07 20:30:46 +00:00
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".subsections_via_symbols")
|
2009-07-13 21:03:15 +00:00
|
|
|
return ParseDirectiveDarwinSubsectionsViaSymbols();
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".abort")
|
2009-07-13 23:15:14 +00:00
|
|
|
return ParseDirectiveAbort();
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".include")
|
2009-07-14 23:21:55 +00:00
|
|
|
return ParseDirectiveInclude();
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".dump")
|
2009-07-20 20:25:37 +00:00
|
|
|
return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
|
2009-07-27 21:49:56 +00:00
|
|
|
if (IDVal == ".load")
|
2009-07-20 20:25:37 +00:00
|
|
|
return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
|
2009-07-13 21:03:15 +00:00
|
|
|
|
2009-06-30 00:49:23 +00:00
|
|
|
Warning(IDLoc, "ignoring directive for now");
|
2009-06-22 01:29:09 +00:00
|
|
|
EatToEndOfStatement();
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-21 20:54:55 +00:00
|
|
|
|
2009-06-23 18:41:30 +00:00
|
|
|
MCInst Inst;
|
2009-07-28 22:40:46 +00:00
|
|
|
if (getTargetParser().ParseInstruction(IDVal, Inst))
|
2009-06-23 18:41:30 +00:00
|
|
|
return true;
|
2009-06-22 01:29:09 +00:00
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-24 04:43:34 +00:00
|
|
|
return TokError("unexpected token in argument list");
|
2009-06-22 01:29:09 +00:00
|
|
|
|
|
|
|
// Eat the end of statement marker.
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// Instruction is good, process it.
|
2009-07-01 06:35:48 +00:00
|
|
|
Out.EmitInstruction(Inst);
|
2009-06-22 01:29:09 +00:00
|
|
|
|
|
|
|
// Skip to end of line for now.
|
2009-06-21 20:16:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-06-24 04:43:34 +00:00
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
|
2009-06-29 23:43:14 +00:00
|
|
|
// FIXME: Use better location, we should use proper tokens.
|
|
|
|
SMLoc EqualLoc = Lexer.getLoc();
|
|
|
|
|
2009-06-30 02:10:03 +00:00
|
|
|
MCValue Value;
|
|
|
|
if (ParseRelocatableExpression(Value))
|
2009-06-25 21:56:11 +00:00
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-25 21:56:11 +00:00
|
|
|
return TokError("unexpected token in assignment");
|
|
|
|
|
|
|
|
// Eat the end of statement marker.
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-06-29 23:43:14 +00:00
|
|
|
// Diagnose assignment to a label.
|
|
|
|
//
|
|
|
|
// FIXME: Diagnostics. Note the location of the definition as a label.
|
|
|
|
// FIXME: This doesn't diagnose assignment to a symbol which has been
|
|
|
|
// implicitly marked as external.
|
2009-06-25 21:56:11 +00:00
|
|
|
// FIXME: Handle '.'.
|
2009-06-29 23:43:14 +00:00
|
|
|
// FIXME: Diagnose assignment to protected identifier (e.g., register name).
|
2009-06-25 21:56:11 +00:00
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
|
2009-06-29 23:43:14 +00:00
|
|
|
if (Sym->getSection())
|
|
|
|
return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
|
|
|
|
if (Sym->isExternal())
|
|
|
|
return Error(EqualLoc, "invalid assignment to external symbol");
|
|
|
|
|
|
|
|
// Do the assignment.
|
2009-06-30 02:10:03 +00:00
|
|
|
Out.EmitAssignment(Sym, Value, IsDotSet);
|
2009-06-25 21:56:11 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDirectiveSet:
|
|
|
|
/// ::= .set identifier ',' expression
|
|
|
|
bool AsmParser::ParseDirectiveSet() {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-06-25 21:56:11 +00:00
|
|
|
return TokError("expected identifier after '.set' directive");
|
|
|
|
|
2009-07-28 16:38:40 +00:00
|
|
|
StringRef Name = Lexer.getTok().getString();
|
2009-06-25 21:56:11 +00:00
|
|
|
|
2009-07-28 16:56:42 +00:00
|
|
|
if (Lexer.Lex().isNot(AsmToken::Comma))
|
2009-06-25 21:56:11 +00:00
|
|
|
return TokError("unexpected token in '.set'");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
return ParseAssignment(Name, true);
|
|
|
|
}
|
|
|
|
|
2009-06-24 04:43:34 +00:00
|
|
|
/// ParseDirectiveSection:
|
2009-06-24 05:13:15 +00:00
|
|
|
/// ::= .section identifier (',' identifier)*
|
|
|
|
/// FIXME: This should actually parse out the segment, section, attributes and
|
|
|
|
/// sizeof_stub fields.
|
|
|
|
bool AsmParser::ParseDirectiveDarwinSection() {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-06-24 04:43:34 +00:00
|
|
|
return TokError("expected identifier after '.section' directive");
|
|
|
|
|
2009-07-28 16:38:40 +00:00
|
|
|
std::string Section = Lexer.getTok().getString();
|
2009-06-24 04:43:34 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// Accept a comma separated list of modifiers.
|
2009-07-28 16:08:33 +00:00
|
|
|
while (Lexer.is(AsmToken::Comma)) {
|
2009-06-24 04:43:34 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-06-24 04:43:34 +00:00
|
|
|
return TokError("expected identifier in '.section' directive");
|
|
|
|
Section += ',';
|
2009-07-28 16:38:40 +00:00
|
|
|
Section += Lexer.getTok().getString().str();
|
2009-06-24 04:43:34 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-24 04:43:34 +00:00
|
|
|
return TokError("unexpected token in '.section' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-31 17:47:16 +00:00
|
|
|
// FIXME: Arch specific.
|
|
|
|
MCSection *S = Ctx.GetSection(Section);
|
|
|
|
if (S == 0)
|
|
|
|
S = MCSection::Create(Section, Ctx);
|
|
|
|
|
|
|
|
Out.SwitchSection(S);
|
2009-06-24 04:43:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-24 05:13:15 +00:00
|
|
|
bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
|
|
|
|
const char *Directives) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-24 05:13:15 +00:00
|
|
|
return TokError("unexpected token in section switching directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
std::string SectionStr = Section;
|
|
|
|
if (Directives && Directives[0]) {
|
|
|
|
SectionStr += ",";
|
|
|
|
SectionStr += Directives;
|
|
|
|
}
|
|
|
|
|
2009-07-31 17:47:16 +00:00
|
|
|
// FIXME: Arch specific.
|
|
|
|
MCSection *S = Ctx.GetSection(Section);
|
|
|
|
if (S == 0)
|
|
|
|
S = MCSection::Create(Section, Ctx);
|
|
|
|
|
|
|
|
Out.SwitchSection(S);
|
2009-06-24 05:13:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-06-24 23:30:00 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveAscii:
|
2009-06-29 20:37:27 +00:00
|
|
|
/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
|
2009-06-24 23:30:00 +00:00
|
|
|
bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
2009-06-24 23:30:00 +00:00
|
|
|
for (;;) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::String))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("expected string in '.ascii' or '.asciz' directive");
|
|
|
|
|
|
|
|
// FIXME: This shouldn't use a const char* + strlen, the string could have
|
|
|
|
// embedded nulls.
|
|
|
|
// FIXME: Should have accessor for getting string contents.
|
2009-07-28 16:38:40 +00:00
|
|
|
StringRef Str = Lexer.getTok().getString();
|
2009-07-27 21:49:56 +00:00
|
|
|
Out.EmitBytes(Str.substr(1, Str.size() - 2));
|
2009-06-24 23:30:00 +00:00
|
|
|
if (ZeroTerminated)
|
2009-07-27 21:49:56 +00:00
|
|
|
Out.EmitBytes(StringRef("\0", 1));
|
2009-06-24 23:30:00 +00:00
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.is(AsmToken::EndOfStatement))
|
2009-06-24 23:30:00 +00:00
|
|
|
break;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("unexpected token in '.ascii' or '.asciz' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDirectiveValue
|
|
|
|
/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
|
|
|
|
bool AsmParser::ParseDirectiveValue(unsigned Size) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
2009-06-24 23:30:00 +00:00
|
|
|
for (;;) {
|
2009-06-30 02:10:03 +00:00
|
|
|
MCValue Expr;
|
|
|
|
if (ParseRelocatableExpression(Expr))
|
2009-06-24 23:30:00 +00:00
|
|
|
return true;
|
|
|
|
|
2009-06-30 02:10:03 +00:00
|
|
|
Out.EmitValue(Expr, Size);
|
2009-06-24 23:30:00 +00:00
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.is(AsmToken::EndOfStatement))
|
2009-06-24 23:30:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// FIXME: Improve diagnostic.
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDirectiveSpace
|
|
|
|
/// ::= .space expression [ , expression ]
|
|
|
|
bool AsmParser::ParseDirectiveSpace() {
|
|
|
|
int64_t NumBytes;
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParseAbsoluteExpression(NumBytes))
|
2009-06-24 23:30:00 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
int64_t FillExpr = 0;
|
|
|
|
bool HasFillExpr = false;
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("unexpected token in '.space' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParseAbsoluteExpression(FillExpr))
|
2009-06-24 23:30:00 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
HasFillExpr = true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("unexpected token in '.space' directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
if (NumBytes <= 0)
|
|
|
|
return TokError("invalid number of bytes in '.space' directive");
|
|
|
|
|
|
|
|
// FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
|
|
|
|
for (uint64_t i = 0, e = NumBytes; i != e; ++i)
|
|
|
|
Out.EmitValue(MCValue::get(FillExpr), 1);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDirectiveFill
|
|
|
|
/// ::= .fill expression , expression , expression
|
|
|
|
bool AsmParser::ParseDirectiveFill() {
|
|
|
|
int64_t NumValues;
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParseAbsoluteExpression(NumValues))
|
2009-06-24 23:30:00 +00:00
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("unexpected token in '.fill' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
int64_t FillSize;
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParseAbsoluteExpression(FillSize))
|
2009-06-24 23:30:00 +00:00
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("unexpected token in '.fill' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
int64_t FillExpr;
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParseAbsoluteExpression(FillExpr))
|
2009-06-24 23:30:00 +00:00
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-24 23:30:00 +00:00
|
|
|
return TokError("unexpected token in '.fill' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
if (FillSize != 1 && FillSize != 2 && FillSize != 4)
|
|
|
|
return TokError("invalid '.fill' size, expected 1, 2, or 4");
|
|
|
|
|
|
|
|
for (uint64_t i = 0, e = NumValues; i != e; ++i)
|
|
|
|
Out.EmitValue(MCValue::get(FillExpr), FillSize);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-25 22:44:51 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveOrg
|
|
|
|
/// ::= .org expression [ , expression ]
|
|
|
|
bool AsmParser::ParseDirectiveOrg() {
|
2009-06-30 02:10:03 +00:00
|
|
|
MCValue Offset;
|
|
|
|
if (ParseRelocatableExpression(Offset))
|
2009-06-25 22:44:51 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Parse optional fill expression.
|
|
|
|
int64_t FillExpr = 0;
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-25 22:44:51 +00:00
|
|
|
return TokError("unexpected token in '.org' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-06-29 20:37:27 +00:00
|
|
|
if (ParseAbsoluteExpression(FillExpr))
|
2009-06-25 22:44:51 +00:00
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-25 22:44:51 +00:00
|
|
|
return TokError("unexpected token in '.org' directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
Lexer.Lex();
|
2009-06-30 02:10:03 +00:00
|
|
|
|
|
|
|
// FIXME: Only limited forms of relocatable expressions are accepted here, it
|
|
|
|
// has to be relative to the current section.
|
|
|
|
Out.EmitValueToOffset(Offset, FillExpr);
|
2009-06-25 22:44:51 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-29 23:46:59 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveAlign
|
|
|
|
/// ::= {.align, ...} expression [ , expression [ , expression ]]
|
|
|
|
bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
|
|
|
|
int64_t Alignment;
|
|
|
|
if (ParseAbsoluteExpression(Alignment))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
SMLoc MaxBytesLoc;
|
|
|
|
bool HasFillExpr = false;
|
|
|
|
int64_t FillExpr = 0;
|
|
|
|
int64_t MaxBytesToFill = 0;
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-29 23:46:59 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// The fill expression can be omitted while specifying a maximum number of
|
|
|
|
// alignment bytes, e.g:
|
|
|
|
// .align 3,,4
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma)) {
|
2009-06-29 23:46:59 +00:00
|
|
|
HasFillExpr = true;
|
|
|
|
if (ParseAbsoluteExpression(FillExpr))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-29 23:46:59 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
MaxBytesLoc = Lexer.getLoc();
|
|
|
|
if (ParseAbsoluteExpression(MaxBytesToFill))
|
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-06-29 23:46:59 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
if (!HasFillExpr) {
|
|
|
|
// FIXME: Sometimes fill with nop.
|
|
|
|
FillExpr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute alignment in bytes.
|
|
|
|
if (IsPow2) {
|
|
|
|
// FIXME: Diagnose overflow.
|
2009-07-11 22:32:37 +00:00
|
|
|
Alignment = 1LL << Alignment;
|
2009-06-29 23:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Diagnose non-sensical max bytes to fill.
|
|
|
|
if (MaxBytesLoc.isValid()) {
|
|
|
|
if (MaxBytesToFill < 1) {
|
2009-06-30 00:49:23 +00:00
|
|
|
Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
|
|
|
|
"many bytes, ignoring");
|
2009-06-29 23:46:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MaxBytesToFill >= Alignment) {
|
2009-06-30 00:49:23 +00:00
|
|
|
Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
|
|
|
|
"has no effect");
|
2009-06-29 23:46:59 +00:00
|
|
|
MaxBytesToFill = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Target specific behavior about how the "extra" bytes are filled.
|
|
|
|
Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-30 00:33:19 +00:00
|
|
|
/// ParseDirectiveSymbolAttribute
|
|
|
|
/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
|
|
|
|
bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
2009-06-30 00:33:19 +00:00
|
|
|
for (;;) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-06-30 00:33:19 +00:00
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
2009-07-28 16:38:40 +00:00
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
|
2009-06-30 00:33:19 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// If this is use of an undefined symbol then mark it external.
|
|
|
|
if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
|
|
|
|
Sym->setExternal(true);
|
|
|
|
|
|
|
|
Out.EmitSymbolAttribute(Sym, Attr);
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.is(AsmToken::EndOfStatement))
|
2009-06-30 00:33:19 +00:00
|
|
|
break;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-06-30 00:33:19 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-07 20:30:46 +00:00
|
|
|
|
2009-07-14 18:17:10 +00:00
|
|
|
/// ParseDirectiveDarwinSymbolDesc
|
|
|
|
/// ::= .desc identifier , expression
|
|
|
|
bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-07-14 18:17:10 +00:00
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
// handle the identifier as the key symbol.
|
|
|
|
SMLoc IDLoc = Lexer.getLoc();
|
2009-07-28 16:38:40 +00:00
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
|
2009-07-14 18:17:10 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-07-14 18:17:10 +00:00
|
|
|
return TokError("unexpected token in '.desc' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
SMLoc DescLoc = Lexer.getLoc();
|
|
|
|
int64_t DescValue;
|
|
|
|
if (ParseAbsoluteExpression(DescValue))
|
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-14 18:17:10 +00:00
|
|
|
return TokError("unexpected token in '.desc' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// Set the n_desc field of this Symbol to this DescValue
|
|
|
|
Out.EmitSymbolDesc(Sym, DescValue);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-07 20:30:46 +00:00
|
|
|
/// ParseDirectiveComm
|
2009-07-09 17:25:12 +00:00
|
|
|
/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
|
|
|
|
bool AsmParser::ParseDirectiveComm(bool IsLocal) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-07-07 20:30:46 +00:00
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
// handle the identifier as the key symbol.
|
|
|
|
SMLoc IDLoc = Lexer.getLoc();
|
2009-07-28 16:38:40 +00:00
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
|
2009-07-07 20:30:46 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-07-07 20:30:46 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
int64_t Size;
|
|
|
|
SMLoc SizeLoc = Lexer.getLoc();
|
|
|
|
if (ParseAbsoluteExpression(Size))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int64_t Pow2Alignment = 0;
|
|
|
|
SMLoc Pow2AlignmentLoc;
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.is(AsmToken::Comma)) {
|
2009-07-07 20:30:46 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
Pow2AlignmentLoc = Lexer.getLoc();
|
|
|
|
if (ParseAbsoluteExpression(Pow2Alignment))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-09 17:25:12 +00:00
|
|
|
return TokError("unexpected token in '.comm' or '.lcomm' directive");
|
2009-07-07 20:30:46 +00:00
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-09 17:25:12 +00:00
|
|
|
// NOTE: a size of zero for a .comm should create a undefined symbol
|
|
|
|
// but a size of .lcomm creates a bss symbol of size zero.
|
2009-07-07 20:30:46 +00:00
|
|
|
if (Size < 0)
|
2009-07-09 17:25:12 +00:00
|
|
|
return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
|
|
|
|
"be less than zero");
|
2009-07-07 20:30:46 +00:00
|
|
|
|
|
|
|
// NOTE: The alignment in the directive is a power of 2 value, the assember
|
|
|
|
// may internally end up wanting an alignment in bytes.
|
|
|
|
// FIXME: Diagnose overflow.
|
|
|
|
if (Pow2Alignment < 0)
|
2009-07-09 17:25:12 +00:00
|
|
|
return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
|
|
|
|
"alignment, can't be less than zero");
|
2009-07-07 20:30:46 +00:00
|
|
|
|
|
|
|
// TODO: Symbol must be undefined or it is a error to re-defined the symbol
|
|
|
|
if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
|
|
|
|
return Error(IDLoc, "invalid symbol redefinition");
|
|
|
|
|
2009-07-09 17:25:12 +00:00
|
|
|
// Create the Symbol as a common or local common with Size and Pow2Alignment
|
|
|
|
Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
|
2009-07-07 20:30:46 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-10 22:20:30 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveDarwinZerofill
|
|
|
|
/// ::= .zerofill segname , sectname [, identifier , size_expression [
|
|
|
|
/// , align_expression ]]
|
|
|
|
bool AsmParser::ParseDirectiveDarwinZerofill() {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-07-10 22:20:30 +00:00
|
|
|
return TokError("expected segment name after '.zerofill' directive");
|
2009-07-28 16:38:40 +00:00
|
|
|
std::string Section = Lexer.getTok().getString();
|
2009-07-10 22:20:30 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-07-10 22:20:30 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Section += ',';
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-07-10 22:20:30 +00:00
|
|
|
return TokError("expected section name after comma in '.zerofill' "
|
|
|
|
"directive");
|
2009-07-28 16:38:40 +00:00
|
|
|
Section += Lexer.getTok().getString().str();
|
2009-07-10 22:20:30 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// FIXME: we will need to tell GetSection() that this is to be created with or
|
|
|
|
// must have the Mach-O section type of S_ZEROFILL. Something like the code
|
|
|
|
// below could be done but for now it is not as EmitZerofill() does not know
|
|
|
|
// how to deal with a section type in the section name like
|
|
|
|
// ParseDirectiveDarwinSection() allows.
|
|
|
|
// Section += ',';
|
|
|
|
// Section += "zerofill";
|
|
|
|
|
|
|
|
// If this is the end of the line all that was wanted was to create the
|
|
|
|
// the section but with no symbol.
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.is(AsmToken::EndOfStatement)) {
|
2009-07-31 18:27:48 +00:00
|
|
|
// FIXME: Arch specific.
|
|
|
|
MCSection *S = Ctx.GetSection(Section);
|
|
|
|
if (S == 0)
|
|
|
|
S = MCSection::Create(Section, Ctx);
|
|
|
|
|
2009-07-10 22:20:30 +00:00
|
|
|
// Create the zerofill section but no symbol
|
2009-07-31 18:27:48 +00:00
|
|
|
Out.EmitZerofill(S);
|
2009-07-10 22:20:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-07-10 22:20:30 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-07-10 22:20:30 +00:00
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
// handle the identifier as the key symbol.
|
|
|
|
SMLoc IDLoc = Lexer.getLoc();
|
2009-07-28 16:38:40 +00:00
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
|
2009-07-10 22:20:30 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-07-10 22:20:30 +00:00
|
|
|
return TokError("unexpected token in directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
int64_t Size;
|
|
|
|
SMLoc SizeLoc = Lexer.getLoc();
|
|
|
|
if (ParseAbsoluteExpression(Size))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int64_t Pow2Alignment = 0;
|
|
|
|
SMLoc Pow2AlignmentLoc;
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.is(AsmToken::Comma)) {
|
2009-07-10 22:20:30 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
Pow2AlignmentLoc = Lexer.getLoc();
|
|
|
|
if (ParseAbsoluteExpression(Pow2Alignment))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-10 22:20:30 +00:00
|
|
|
return TokError("unexpected token in '.zerofill' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
if (Size < 0)
|
|
|
|
return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
|
|
|
|
"than zero");
|
|
|
|
|
|
|
|
// NOTE: The alignment in the directive is a power of 2 value, the assember
|
|
|
|
// may internally end up wanting an alignment in bytes.
|
|
|
|
// FIXME: Diagnose overflow.
|
|
|
|
if (Pow2Alignment < 0)
|
|
|
|
return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
|
|
|
|
"can't be less than zero");
|
|
|
|
|
|
|
|
// TODO: Symbol must be undefined or it is a error to re-defined the symbol
|
|
|
|
if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
|
|
|
|
return Error(IDLoc, "invalid symbol redefinition");
|
|
|
|
|
2009-07-31 18:27:48 +00:00
|
|
|
// FIXME: Arch specific.
|
|
|
|
MCSection *S = Ctx.GetSection(Section);
|
|
|
|
if (S == 0)
|
|
|
|
S = MCSection::Create(Section, Ctx);
|
|
|
|
|
2009-07-10 22:20:30 +00:00
|
|
|
// Create the zerofill Symbol with Size and Pow2Alignment
|
2009-07-31 18:27:48 +00:00
|
|
|
Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
|
2009-07-10 22:20:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-13 21:03:15 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveDarwinSubsectionsViaSymbols
|
|
|
|
/// ::= .subsections_via_symbols
|
|
|
|
bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-13 21:03:15 +00:00
|
|
|
return TokError("unexpected token in '.subsections_via_symbols' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-16 17:56:39 +00:00
|
|
|
Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
|
2009-07-13 21:03:15 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-13 23:15:14 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveAbort
|
|
|
|
/// ::= .abort [ "abort_string" ]
|
|
|
|
bool AsmParser::ParseDirectiveAbort() {
|
2009-07-27 23:20:52 +00:00
|
|
|
// FIXME: Use loc from directive.
|
|
|
|
SMLoc Loc = Lexer.getLoc();
|
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
StringRef Str = "";
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement)) {
|
|
|
|
if (Lexer.isNot(AsmToken::String))
|
2009-07-13 23:15:14 +00:00
|
|
|
return TokError("expected string in '.abort' directive");
|
|
|
|
|
2009-07-28 16:38:40 +00:00
|
|
|
Str = Lexer.getTok().getString();
|
2009-07-13 23:15:14 +00:00
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-13 23:15:14 +00:00
|
|
|
return TokError("unexpected token in '.abort' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
// FIXME: Handle here.
|
2009-07-27 23:20:52 +00:00
|
|
|
if (Str.empty())
|
|
|
|
Error(Loc, ".abort detected. Assembly stopping.");
|
|
|
|
else
|
|
|
|
Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
|
2009-07-13 23:15:14 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-14 21:35:03 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveLsym
|
|
|
|
/// ::= .lsym identifier , expression
|
|
|
|
bool AsmParser::ParseDirectiveDarwinLsym() {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Identifier))
|
2009-07-14 21:35:03 +00:00
|
|
|
return TokError("expected identifier in directive");
|
|
|
|
|
|
|
|
// handle the identifier as the key symbol.
|
|
|
|
SMLoc IDLoc = Lexer.getLoc();
|
2009-07-28 16:38:40 +00:00
|
|
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
|
2009-07-14 21:35:03 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::Comma))
|
2009-07-14 21:35:03 +00:00
|
|
|
return TokError("unexpected token in '.lsym' directive");
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
MCValue Expr;
|
|
|
|
if (ParseRelocatableExpression(Expr))
|
|
|
|
return true;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-14 21:35:03 +00:00
|
|
|
return TokError("unexpected token in '.lsym' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
|
|
|
// Create the Sym with the value of the Expr
|
|
|
|
Out.EmitLocalSymbol(Sym, Expr);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-14 23:21:55 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveInclude
|
|
|
|
/// ::= .include "filename"
|
|
|
|
bool AsmParser::ParseDirectiveInclude() {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::String))
|
2009-07-14 23:21:55 +00:00
|
|
|
return TokError("expected string in '.include' directive");
|
|
|
|
|
2009-07-28 16:38:40 +00:00
|
|
|
std::string Filename = Lexer.getTok().getString();
|
2009-07-16 06:14:39 +00:00
|
|
|
SMLoc IncludeLoc = Lexer.getLoc();
|
2009-07-14 23:21:55 +00:00
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-14 23:21:55 +00:00
|
|
|
return TokError("unexpected token in '.include' directive");
|
|
|
|
|
2009-07-16 06:14:39 +00:00
|
|
|
// Strip the quotes.
|
|
|
|
Filename = Filename.substr(1, Filename.size()-2);
|
|
|
|
|
|
|
|
// Attempt to switch the lexer to the included file before consuming the end
|
|
|
|
// of statement to avoid losing it when we switch.
|
|
|
|
if (Lexer.EnterIncludeFile(Filename)) {
|
|
|
|
Lexer.PrintMessage(IncludeLoc,
|
|
|
|
"Could not find include file '" + Filename + "'",
|
|
|
|
"error");
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-14 23:21:55 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-15 15:30:11 +00:00
|
|
|
|
|
|
|
/// ParseDirectiveDarwinDumpOrLoad
|
|
|
|
/// ::= ( .dump | .load ) "filename"
|
2009-07-20 20:25:37 +00:00
|
|
|
bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::String))
|
2009-07-15 15:30:11 +00:00
|
|
|
return TokError("expected string in '.dump' or '.load' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
if (Lexer.isNot(AsmToken::EndOfStatement))
|
2009-07-15 15:30:11 +00:00
|
|
|
return TokError("unexpected token in '.dump' or '.load' directive");
|
|
|
|
|
|
|
|
Lexer.Lex();
|
|
|
|
|
2009-07-20 20:25:37 +00:00
|
|
|
// FIXME: If/when .dump and .load are implemented they will be done in the
|
|
|
|
// the assembly parser and not have any need for an MCStreamer API.
|
2009-07-15 15:30:11 +00:00
|
|
|
if (IsDump)
|
2009-07-20 20:25:37 +00:00
|
|
|
Warning(IDLoc, "ignoring directive .dump for now");
|
2009-07-15 15:30:11 +00:00
|
|
|
else
|
2009-07-20 20:25:37 +00:00
|
|
|
Warning(IDLoc, "ignoring directive .load for now");
|
2009-07-15 15:30:11 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|