From: Daniel Dunbar Date: Thu, 11 Mar 2010 02:28:59 +0000 (+0000) Subject: MC: Sketch initial MCAsmLayout class, which encapsulates the current layout of an... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=f82f4490b130eca55b08d605456a4ceacccf288a;p=oota-llvm.git MC: Sketch initial MCAsmLayout class, which encapsulates the current layout of an assembly file. The MCAsmLayout is also available for use by MCExpr::EvaluateAs{Absolute,Relocatable}, to allow target specific hooks and "absolutizing" of symbols. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@98227 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/MC/MCAsmLayout.h b/include/llvm/MC/MCAsmLayout.h new file mode 100644 index 00000000000..d2b5e4a507c --- /dev/null +++ b/include/llvm/MC/MCAsmLayout.h @@ -0,0 +1,49 @@ +//===- MCAsmLayout.h - Assembly Layout Object -------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_MC_MCASMLAYOUT_H +#define LLVM_MC_MCASMLAYOUT_H + +namespace llvm { +class MCAssembler; + +/// Encapsulates the layout of an assembly file at a particular point in time. +/// +/// Assembly may requiring compute multiple layouts for a particular assembly +/// file as part of the relaxation process. This class encapsulates the layout +/// at a single point in time in such a way that it is always possible to +/// efficiently compute the exact addresses of any symbol in the assembly file, +/// even during the relaxation process. +class MCAsmLayout { +private: + uint64_t CurrentLocation; + + MCAssembler &Assembler; + +public: + MCAsmLayout(MCAssembler &_Assembler) + : CurrentLocation(0), Assembler(_Assember) {} + + /// Get the assembler object this is a layout for. + MCAssembler &getAssembler() { return Assembler; } + + /// Get the current location value, i.e. that value of the '.' expression. + uin64_t getCurrentLocation() { + return CurrentLocation; + } + + /// Set the current location. + void setCurrentLocation(uint64_t Value) { + CurrentLocation = Value; + } +}; + +} // end namespace llvm + +#endif diff --git a/include/llvm/MC/MCAssembler.h b/include/llvm/MC/MCAssembler.h index ffd77c51eb4..8210db61fca 100644 --- a/include/llvm/MC/MCAssembler.h +++ b/include/llvm/MC/MCAssembler.h @@ -625,6 +625,8 @@ public: MCContext &getContext() const { return Context; } + TargetAsmBackend &getBackend() const { return Backend; } + /// Finish - Do final processing and write the object to the output stream. void Finish(); diff --git a/include/llvm/MC/MCExpr.h b/include/llvm/MC/MCExpr.h index 3f17492c6b4..9daddb2923d 100644 --- a/include/llvm/MC/MCExpr.h +++ b/include/llvm/MC/MCExpr.h @@ -15,6 +15,7 @@ namespace llvm { class MCAsmInfo; +class MCAsmLayout; class MCContext; class MCSymbol; class MCValue; @@ -62,15 +63,19 @@ public: /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value. /// /// @param Res - The absolute value, if evaluation succeeds. + /// @param Layout - The assembler layout object to use for evaluating symbol + /// values. If not given, then only non-symbolic expressions will be + /// evaluated. /// @result - True on success. - bool EvaluateAsAbsolute(int64_t &Res) const; + bool EvaluateAsAbsolute(int64_t &Res, MCAsmLayout *Layout = 0) 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. + /// @param Layout - The assembler layout object to use for evaluating values. /// @result - True on success. - bool EvaluateAsRelocatable(MCValue &Res) const; + bool EvaluateAsRelocatable(MCValue &Res, MCAsmLayout *Layout = 0) const; /// @} @@ -348,7 +353,8 @@ protected: public: virtual void PrintImpl(raw_ostream &OS) const = 0; - virtual bool EvaluateAsRelocatableImpl(MCValue &Res) const = 0; + virtual bool EvaluateAsRelocatableImpl(MCValue &Res, + MCAsmLayout *Layout) const = 0; static bool classof(const MCExpr *E) { diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp index 4439eba5d47..e3640691514 100644 --- a/lib/MC/MCExpr.cpp +++ b/lib/MC/MCExpr.cpp @@ -142,10 +142,10 @@ void MCTargetExpr::Anchor() {} /* *** */ -bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const { +bool MCExpr::EvaluateAsAbsolute(int64_t &Res, MCAsmLayout *Layout) const { MCValue Value; - if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute()) + if (!EvaluateAsRelocatable(Value, Layout) || !Value.isAbsolute()) return false; Res = Value.getConstant(); @@ -174,10 +174,10 @@ static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, return true; } -bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const { +bool MCExpr::EvaluateAsRelocatable(MCValue &Res, MCAsmLayout *Layout) const { switch (getKind()) { case Target: - return cast(this)->EvaluateAsRelocatableImpl(Res); + return cast(this)->EvaluateAsRelocatableImpl(Res, Layout); case Constant: Res = MCValue::get(cast(this)->getValue()); @@ -188,7 +188,7 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const { // Evaluate recursively if this is a variable. if (Sym.isVariable()) - return Sym.getValue()->EvaluateAsRelocatable(Res); + return Sym.getValue()->EvaluateAsRelocatable(Res, Layout); Res = MCValue::get(&Sym, 0, 0); return true; @@ -198,7 +198,7 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const { const MCUnaryExpr *AUE = cast(this); MCValue Value; - if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value)) + if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value, Layout)) return false; switch (AUE->getOpcode()) { @@ -231,8 +231,8 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const { const MCBinaryExpr *ABE = cast(this); MCValue LHSValue, RHSValue; - if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) || - !ABE->getRHS()->EvaluateAsRelocatable(RHSValue)) + if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue, Layout) || + !ABE->getRHS()->EvaluateAsRelocatable(RHSValue, Layout)) return false; // We only support a few operations on non-constant expressions, handle diff --git a/lib/Target/X86/X86MCTargetExpr.cpp b/lib/Target/X86/X86MCTargetExpr.cpp index 17b4fe88c75..de56d313076 100644 --- a/lib/Target/X86/X86MCTargetExpr.cpp +++ b/lib/Target/X86/X86MCTargetExpr.cpp @@ -36,12 +36,13 @@ void X86MCTargetExpr::PrintImpl(raw_ostream &OS) const { } } -bool X86MCTargetExpr::EvaluateAsRelocatableImpl(MCValue &Res) const { +bool X86MCTargetExpr::EvaluateAsRelocatableImpl(MCValue &Res, + MCAsmLayout *Layout) const { // FIXME: I don't know if this is right, it followed MCSymbolRefExpr. // Evaluate recursively if this is a variable. if (Sym->isVariable()) - return Sym->getValue()->EvaluateAsRelocatable(Res); + return Sym->getValue()->EvaluateAsRelocatable(Res, Layout); Res = MCValue::get(Sym, 0, 0); return true; diff --git a/lib/Target/X86/X86MCTargetExpr.h b/lib/Target/X86/X86MCTargetExpr.h index 7de8a5c5738..56011eb053f 100644 --- a/lib/Target/X86/X86MCTargetExpr.h +++ b/lib/Target/X86/X86MCTargetExpr.h @@ -41,7 +41,7 @@ public: MCContext &Ctx); void PrintImpl(raw_ostream &OS) const; - bool EvaluateAsRelocatableImpl(MCValue &Res) const; + bool EvaluateAsRelocatableImpl(MCValue &Res, MCAsmLayout *Layout) const; }; } // end namespace llvm