diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/llvm-mc/AsmExpr.cpp | 162 | ||||
-rw-r--r-- | tools/llvm-mc/AsmExpr.h | 179 | ||||
-rw-r--r-- | tools/llvm-mc/AsmParser.cpp | 74 | ||||
-rw-r--r-- | tools/llvm-mc/AsmParser.h | 10 | ||||
-rw-r--r-- | tools/llvm-mc/CMakeLists.txt | 1 |
5 files changed, 42 insertions, 384 deletions
diff --git a/tools/llvm-mc/AsmExpr.cpp b/tools/llvm-mc/AsmExpr.cpp deleted file mode 100644 index 226dd13..0000000 --- a/tools/llvm-mc/AsmExpr.cpp +++ /dev/null @@ -1,162 +0,0 @@ -//===- AsmExpr.cpp - Assembly file expressions ----------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "AsmExpr.h" -#include "llvm/MC/MCContext.h" -#include "llvm/MC/MCSymbol.h" -#include "llvm/MC/MCValue.h" -using namespace llvm; - -AsmExpr::~AsmExpr() { -} - -bool AsmExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const { - MCValue Value; - - if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isAbsolute()) - return false; - - Res = Value.getConstant(); - return true; -} - -static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, - const MCSymbol *RHS_B, int64_t RHS_Cst, - MCValue &Res) { - // We can't add or subtract two symbols. - if ((LHS.getSymA() && RHS_A) || - (LHS.getSymB() && RHS_B)) - return false; - - const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A; - const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B; - if (B) { - // If we have a negated symbol, then we must have also have a non-negated - // symbol in order to encode the expression. We can do this check later to - // permit expressions which eventually fold to a representable form -- such - // as (a + (0 - b)) -- if necessary. - if (!A) - return false; - } - Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst); - return true; -} - -bool AsmExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const { - switch (getKind()) { - default: - assert(0 && "Invalid assembly expression kind!"); - - case Constant: - Res = MCValue::get(cast<AsmConstantExpr>(this)->getValue()); - return true; - - case SymbolRef: { - MCSymbol *Sym = cast<AsmSymbolRefExpr>(this)->getSymbol(); - if (const MCValue *Value = Ctx.GetSymbolValue(Sym)) - Res = *Value; - else - Res = MCValue::get(Sym, 0, 0); - return true; - } - - case Unary: { - const AsmUnaryExpr *AUE = cast<AsmUnaryExpr>(this); - MCValue Value; - - if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value)) - return false; - - switch (AUE->getOpcode()) { - case AsmUnaryExpr::LNot: - if (!Value.isAbsolute()) - return false; - Res = MCValue::get(!Value.getConstant()); - break; - case AsmUnaryExpr::Minus: - /// -(a - b + const) ==> (b - a - const) - if (Value.getSymA() && !Value.getSymB()) - return false; - Res = MCValue::get(Value.getSymB(), Value.getSymA(), - -Value.getConstant()); - break; - case AsmUnaryExpr::Not: - if (!Value.isAbsolute()) - return false; - Res = MCValue::get(~Value.getConstant()); - break; - case AsmUnaryExpr::Plus: - Res = Value; - break; - } - - return true; - } - - case Binary: { - const AsmBinaryExpr *ABE = cast<AsmBinaryExpr>(this); - MCValue LHSValue, RHSValue; - - if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) || - !ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue)) - return false; - - // We only support a few operations on non-constant expressions, handle - // those first. - if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) { - switch (ABE->getOpcode()) { - default: - return false; - case AsmBinaryExpr::Sub: - // Negate RHS and add. - return EvaluateSymbolicAdd(LHSValue, - RHSValue.getSymB(), RHSValue.getSymA(), - -RHSValue.getConstant(), - Res); - - case AsmBinaryExpr::Add: - return EvaluateSymbolicAdd(LHSValue, - RHSValue.getSymA(), RHSValue.getSymB(), - RHSValue.getConstant(), - Res); - } - } - - // FIXME: We need target hooks for the evaluation. It may be limited in - // width, and gas defines the result of comparisons differently from Apple - // as (the result is sign extended). - int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant(); - int64_t Result = 0; - switch (ABE->getOpcode()) { - case AsmBinaryExpr::Add: Result = LHS + RHS; break; - case AsmBinaryExpr::And: Result = LHS & RHS; break; - case AsmBinaryExpr::Div: Result = LHS / RHS; break; - case AsmBinaryExpr::EQ: Result = LHS == RHS; break; - case AsmBinaryExpr::GT: Result = LHS > RHS; break; - case AsmBinaryExpr::GTE: Result = LHS >= RHS; break; - case AsmBinaryExpr::LAnd: Result = LHS && RHS; break; - case AsmBinaryExpr::LOr: Result = LHS || RHS; break; - case AsmBinaryExpr::LT: Result = LHS < RHS; break; - case AsmBinaryExpr::LTE: Result = LHS <= RHS; break; - case AsmBinaryExpr::Mod: Result = LHS % RHS; break; - case AsmBinaryExpr::Mul: Result = LHS * RHS; break; - case AsmBinaryExpr::NE: Result = LHS != RHS; break; - case AsmBinaryExpr::Or: Result = LHS | RHS; break; - case AsmBinaryExpr::Shl: Result = LHS << RHS; break; - case AsmBinaryExpr::Shr: Result = LHS >> RHS; break; - case AsmBinaryExpr::Sub: Result = LHS - RHS; break; - case AsmBinaryExpr::Xor: Result = LHS ^ RHS; break; - } - - Res = MCValue::get(Result); - return true; - } - } -} - diff --git a/tools/llvm-mc/AsmExpr.h b/tools/llvm-mc/AsmExpr.h deleted file mode 100644 index 84e58ff..0000000 --- a/tools/llvm-mc/AsmExpr.h +++ /dev/null @@ -1,179 +0,0 @@ -//===- AsmExpr.h - Assembly file expressions --------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef ASMEXPR_H -#define ASMEXPR_H - -#include "llvm/Support/Casting.h" -#include "llvm/Support/DataTypes.h" - -namespace llvm { -class MCContext; -class MCSymbol; -class MCValue; - -/// AsmExpr - Base class for the full range of assembler expressions which are -/// needed for parsing. -class AsmExpr { -public: - enum AsmExprKind { - Binary, ///< Binary expressions. - Constant, ///< Constant expressions. - SymbolRef, ///< References to labels and assigned expressions. - Unary ///< Unary expressions. - }; - -private: - AsmExprKind Kind; - -protected: - AsmExpr(AsmExprKind _Kind) : Kind(_Kind) {} - -public: - virtual ~AsmExpr(); - - AsmExprKind getKind() const { return Kind; } - - /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value. - /// - /// @param Res - The absolute value, if evaluation succeeds. - /// @result - True on success. - bool EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const; - - /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable - /// value, i.e. an expression of the fixed form (a - b + constant). - /// - /// @param Res - The relocatable value, if evaluation succeeds. - /// @result - True on success. - bool EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const; - - static bool classof(const AsmExpr *) { return true; } -}; - -//// AsmConstantExpr - Represent a constant integer expression. -class AsmConstantExpr : public AsmExpr { - int64_t Value; - -public: - AsmConstantExpr(int64_t _Value) - : AsmExpr(AsmExpr::Constant), Value(_Value) {} - - int64_t getValue() const { return Value; } - - static bool classof(const AsmExpr *E) { - return E->getKind() == AsmExpr::Constant; - } - static bool classof(const AsmConstantExpr *) { return true; } -}; - -/// AsmSymbolRefExpr - Represent a reference to a symbol from inside an -/// expression. -/// -/// A symbol reference in an expression may be a use of a label, a use of an -/// assembler variable (defined constant), or constitute an implicit definition -/// of the symbol as external. -class AsmSymbolRefExpr : public AsmExpr { - MCSymbol *Symbol; - -public: - AsmSymbolRefExpr(MCSymbol *_Symbol) - : AsmExpr(AsmExpr::SymbolRef), Symbol(_Symbol) {} - - MCSymbol *getSymbol() const { return Symbol; } - - static bool classof(const AsmExpr *E) { - return E->getKind() == AsmExpr::SymbolRef; - } - static bool classof(const AsmSymbolRefExpr *) { return true; } -}; - -/// AsmUnaryExpr - Unary assembler expressions. -class AsmUnaryExpr : public AsmExpr { -public: - enum Opcode { - LNot, ///< Logical negation. - Minus, ///< Unary minus. - Not, ///< Bitwise negation. - Plus ///< Unary plus. - }; - -private: - Opcode Op; - AsmExpr *Expr; - -public: - AsmUnaryExpr(Opcode _Op, AsmExpr *_Expr) - : AsmExpr(AsmExpr::Unary), Op(_Op), Expr(_Expr) {} - ~AsmUnaryExpr() { - delete Expr; - } - - Opcode getOpcode() const { return Op; } - - AsmExpr *getSubExpr() const { return Expr; } - - static bool classof(const AsmExpr *E) { - return E->getKind() == AsmExpr::Unary; - } - static bool classof(const AsmUnaryExpr *) { return true; } -}; - -/// AsmBinaryExpr - Binary assembler expressions. -class AsmBinaryExpr : public AsmExpr { -public: - enum Opcode { - Add, ///< Addition. - And, ///< Bitwise and. - Div, ///< Division. - EQ, ///< Equality comparison. - GT, ///< Greater than comparison. - GTE, ///< Greater than or equal comparison. - LAnd, ///< Logical and. - LOr, ///< Logical or. - LT, ///< Less than comparison. - LTE, ///< Less than or equal comparison. - Mod, ///< Modulus. - Mul, ///< Multiplication. - NE, ///< Inequality comparison. - Or, ///< Bitwise or. - Shl, ///< Bitwise shift left. - Shr, ///< Bitwise shift right. - Sub, ///< Subtraction. - Xor ///< Bitwise exclusive or. - }; - -private: - Opcode Op; - AsmExpr *LHS, *RHS; - -public: - AsmBinaryExpr(Opcode _Op, AsmExpr *_LHS, AsmExpr *_RHS) - : AsmExpr(AsmExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {} - ~AsmBinaryExpr() { - delete LHS; - delete RHS; - } - - Opcode getOpcode() const { return Op; } - - /// getLHS - Get the left-hand side expression of the binary operator. - AsmExpr *getLHS() const { return LHS; } - - /// getRHS - Get the right-hand side expression of the binary operator. - AsmExpr *getRHS() const { return RHS; } - - static bool classof(const AsmExpr *E) { - return E->getKind() == AsmExpr::Binary; - } - static bool classof(const AsmBinaryExpr *) { return true; } -}; - -} // end namespace llvm - -#endif diff --git a/tools/llvm-mc/AsmParser.cpp b/tools/llvm-mc/AsmParser.cpp index 032b25e..cb69bb1 100644 --- a/tools/llvm-mc/AsmParser.cpp +++ b/tools/llvm-mc/AsmParser.cpp @@ -13,10 +13,10 @@ #include "AsmParser.h" -#include "AsmExpr.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCInst.h" #include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCStreamer.h" @@ -173,7 +173,7 @@ void AsmParser::EatToEndOfStatement() { /// /// parenexpr ::= expr) /// -bool AsmParser::ParseParenExpr(AsmExpr *&Res) { +bool AsmParser::ParseParenExpr(MCExpr *&Res) { if (ParseExpression(Res)) return true; if (Lexer.isNot(AsmToken::RParen)) return TokError("expected ')' in parentheses expression"); @@ -197,7 +197,7 @@ MCSymbol *AsmParser::CreateSymbol(StringRef Name) { /// primaryexpr ::= symbol /// primaryexpr ::= number /// primaryexpr ::= ~,+,- primaryexpr -bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { +bool AsmParser::ParsePrimaryExpr(MCExpr *&Res) { switch (Lexer.getKind()) { default: return TokError("unknown token in expression"); @@ -205,7 +205,7 @@ bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { Lexer.Lex(); // Eat the operator. if (ParsePrimaryExpr(Res)) return true; - Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res); + Res = new MCUnaryExpr(MCUnaryExpr::LNot, Res); return false; case AsmToken::String: case AsmToken::Identifier: { @@ -213,12 +213,12 @@ bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { // handle things like LFOO+4. MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier()); - Res = new AsmSymbolRefExpr(Sym); + Res = new MCSymbolRefExpr(Sym); Lexer.Lex(); // Eat identifier. return false; } case AsmToken::Integer: - Res = new AsmConstantExpr(Lexer.getTok().getIntVal()); + Res = new MCConstantExpr(Lexer.getTok().getIntVal()); Lexer.Lex(); // Eat token. return false; case AsmToken::LParen: @@ -228,19 +228,19 @@ bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { Lexer.Lex(); // Eat the operator. if (ParsePrimaryExpr(Res)) return true; - Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res); + Res = new MCUnaryExpr(MCUnaryExpr::Minus, Res); return false; case AsmToken::Plus: Lexer.Lex(); // Eat the operator. if (ParsePrimaryExpr(Res)) return true; - Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res); + Res = new MCUnaryExpr(MCUnaryExpr::Plus, Res); return false; case AsmToken::Tilde: Lexer.Lex(); // Eat the operator. if (ParsePrimaryExpr(Res)) return true; - Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res); + Res = new MCUnaryExpr(MCUnaryExpr::Not, Res); return false; } } @@ -252,14 +252,14 @@ bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { /// expr ::= expr *,/,%,<<,>> expr -> highest. /// expr ::= primaryexpr /// -bool AsmParser::ParseExpression(AsmExpr *&Res) { +bool AsmParser::ParseExpression(MCExpr *&Res) { Res = 0; return ParsePrimaryExpr(Res) || ParseBinOpRHS(1, Res); } bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { - AsmExpr *Expr; + MCExpr *Expr; SMLoc StartLoc = Lexer.getLoc(); if (ParseExpression(Expr)) @@ -272,7 +272,7 @@ bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { } bool AsmParser::ParseRelocatableExpression(MCValue &Res) { - AsmExpr *Expr; + MCExpr *Expr; SMLoc StartLoc = Lexer.getLoc(); if (ParseExpression(Expr)) @@ -285,7 +285,7 @@ bool AsmParser::ParseRelocatableExpression(MCValue &Res) { } bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) { - AsmExpr *Expr; + MCExpr *Expr; SMLoc StartLoc = Lexer.getLoc(); if (ParseParenExpr(Expr)) @@ -298,73 +298,73 @@ bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) { } static unsigned getBinOpPrecedence(AsmToken::TokenKind K, - AsmBinaryExpr::Opcode &Kind) { + MCBinaryExpr::Opcode &Kind) { switch (K) { default: return 0; // not a binop. // Lowest Precedence: &&, || case AsmToken::AmpAmp: - Kind = AsmBinaryExpr::LAnd; + Kind = MCBinaryExpr::LAnd; return 1; case AsmToken::PipePipe: - Kind = AsmBinaryExpr::LOr; + Kind = MCBinaryExpr::LOr; return 1; // Low Precedence: +, -, ==, !=, <>, <, <=, >, >= case AsmToken::Plus: - Kind = AsmBinaryExpr::Add; + Kind = MCBinaryExpr::Add; return 2; case AsmToken::Minus: - Kind = AsmBinaryExpr::Sub; + Kind = MCBinaryExpr::Sub; return 2; case AsmToken::EqualEqual: - Kind = AsmBinaryExpr::EQ; + Kind = MCBinaryExpr::EQ; return 2; case AsmToken::ExclaimEqual: case AsmToken::LessGreater: - Kind = AsmBinaryExpr::NE; + Kind = MCBinaryExpr::NE; return 2; case AsmToken::Less: - Kind = AsmBinaryExpr::LT; + Kind = MCBinaryExpr::LT; return 2; case AsmToken::LessEqual: - Kind = AsmBinaryExpr::LTE; + Kind = MCBinaryExpr::LTE; return 2; case AsmToken::Greater: - Kind = AsmBinaryExpr::GT; + Kind = MCBinaryExpr::GT; return 2; case AsmToken::GreaterEqual: - Kind = AsmBinaryExpr::GTE; + Kind = MCBinaryExpr::GTE; return 2; // Intermediate Precedence: |, &, ^ // // FIXME: gas seems to support '!' as an infix operator? case AsmToken::Pipe: - Kind = AsmBinaryExpr::Or; + Kind = MCBinaryExpr::Or; return 3; case AsmToken::Caret: - Kind = AsmBinaryExpr::Xor; + Kind = MCBinaryExpr::Xor; return 3; case AsmToken::Amp: - Kind = AsmBinaryExpr::And; + Kind = MCBinaryExpr::And; return 3; // Highest Precedence: *, /, %, <<, >> case AsmToken::Star: - Kind = AsmBinaryExpr::Mul; + Kind = MCBinaryExpr::Mul; return 4; case AsmToken::Slash: - Kind = AsmBinaryExpr::Div; + Kind = MCBinaryExpr::Div; return 4; case AsmToken::Percent: - Kind = AsmBinaryExpr::Mod; + Kind = MCBinaryExpr::Mod; return 4; case AsmToken::LessLess: - Kind = AsmBinaryExpr::Shl; + Kind = MCBinaryExpr::Shl; return 4; case AsmToken::GreaterGreater: - Kind = AsmBinaryExpr::Shr; + Kind = MCBinaryExpr::Shr; return 4; } } @@ -372,9 +372,9 @@ static unsigned getBinOpPrecedence(AsmToken::TokenKind K, /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. /// Res contains the LHS of the expression on input. -bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) { +bool AsmParser::ParseBinOpRHS(unsigned Precedence, MCExpr *&Res) { while (1) { - AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add; + MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); // If the next token is lower precedence than we are allowed to eat, return @@ -385,19 +385,19 @@ bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) { Lexer.Lex(); // Eat the next primary expression. - AsmExpr *RHS; + MCExpr *RHS; 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. - AsmBinaryExpr::Opcode Dummy; + MCBinaryExpr::Opcode Dummy; unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); if (TokPrec < NextTokPrec) { if (ParseBinOpRHS(Precedence+1, RHS)) return true; } // Merge LHS and RHS according to operator. - Res = new AsmBinaryExpr(Kind, Res, RHS); + Res = new MCBinaryExpr(Kind, Res, RHS); } } diff --git a/tools/llvm-mc/AsmParser.h b/tools/llvm-mc/AsmParser.h index 8ab4b43..99e5bbe 100644 --- a/tools/llvm-mc/AsmParser.h +++ b/tools/llvm-mc/AsmParser.h @@ -22,9 +22,9 @@ #include "llvm/MC/MCStreamer.h" namespace llvm { -class AsmExpr; class AsmCond; class MCContext; +class MCExpr; class MCInst; class MCStreamer; class MCValue; @@ -66,7 +66,7 @@ public: virtual bool Error(SMLoc L, const Twine &Msg); - virtual bool ParseExpression(AsmExpr *&Res); + virtual bool ParseExpression(MCExpr *&Res); virtual bool ParseAbsoluteExpression(int64_t &Res); @@ -104,9 +104,9 @@ private: /// @see ParseRelocatableExpression, ParseParenExpr. bool ParseParenRelocatableExpression(MCValue &Res); - bool ParsePrimaryExpr(AsmExpr *&Res); - bool ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res); - bool ParseParenExpr(AsmExpr *&Res); + bool ParsePrimaryExpr(MCExpr *&Res); + bool ParseBinOpRHS(unsigned Precedence, MCExpr *&Res); + bool ParseParenExpr(MCExpr *&Res); /// ParseIdentifier - Parse an identifier or string (as a quoted identifier) /// and set \arg Res to the identifier contents. diff --git a/tools/llvm-mc/CMakeLists.txt b/tools/llvm-mc/CMakeLists.txt index 8781846..ce9d63b 100644 --- a/tools/llvm-mc/CMakeLists.txt +++ b/tools/llvm-mc/CMakeLists.txt @@ -2,7 +2,6 @@ set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} support MC) add_llvm_tool(llvm-mc llvm-mc.cpp - AsmExpr.cpp AsmLexer.cpp AsmParser.cpp ) |