Commit 76bb0bec by Jan Voung

Convert Constant->emit() definitions to allow multiple targets to define them.

Wasn't sure how to allow TargetX8632 and TargetARM32 to both define "ConstantInteger32::emit(GlobalContext *)", and define them differently if both targets happen to be ifdef'ed into the code. Rearranged things so that it's now "TargetFoo::emit(ConstantInteger32 *)", so that each TargetFoo can have a separate definition. Some targets may allow emitting some types of constants while other targets do not (64-bit int for x86-64?). Also they emit constants with a different style. E.g., the prefix for x86 is "$" while the prefix for ARM is "#" and there isn't a prefix for mips(?). Renamed emitWithoutDollar to emitWithoutPrefix. Did this sort of multi-method dispatch via a visitor pattern, which is a bit verbose though. We may be able to remove the emitWithoutDollar/Prefix for ConstantPrimitive by just inlining that into the few places that need it (only needed for ConstantInteger32). This undoes the unreachable methods added by: https://codereview.chromium.org/1017373002/diff/60001/src/IceTargetLoweringX8632.cpp The only place extra was for emitting calls to constants. There was already an inlined instance for OperandX8632Mem. BUG= https://code.google.com/p/nativeclient/issues/detail?id=4076 R=stichnot@chromium.org Review URL: https://codereview.chromium.org/1129263005
parent b2d5084c
...@@ -526,8 +526,12 @@ void InstX8632Call::emit(const Cfg *Func) const { ...@@ -526,8 +526,12 @@ void InstX8632Call::emit(const Cfg *Func) const {
Ostream &Str = Func->getContext()->getStrEmit(); Ostream &Str = Func->getContext()->getStrEmit();
assert(getSrcSize() == 1); assert(getSrcSize() == 1);
Str << "\tcall\t"; Str << "\tcall\t";
if (const auto CallTarget = llvm::dyn_cast<Constant>(getCallTarget())) { if (const auto CI = llvm::dyn_cast<ConstantInteger32>(getCallTarget())) {
CallTarget->emitWithoutDollar(Func->getContext()); // Emit without a leading '$'.
Str << CI->getValue();
} else if (const auto CallTarget =
llvm::dyn_cast<ConstantRelocatable>(getCallTarget())) {
CallTarget->emitWithoutPrefix(Func->getTarget());
} else { } else {
Str << "*"; Str << "*";
getCallTarget()->emit(Func); getCallTarget()->emit(Func);
...@@ -2848,7 +2852,7 @@ void OperandX8632Mem::emit(const Cfg *Func) const { ...@@ -2848,7 +2852,7 @@ void OperandX8632Mem::emit(const Cfg *Func) const {
// Emit a non-zero offset without a leading '$'. // Emit a non-zero offset without a leading '$'.
Str << CI->getValue(); Str << CI->getValue();
} else if (const auto CR = llvm::dyn_cast<ConstantRelocatable>(Offset)) { } else if (const auto CR = llvm::dyn_cast<ConstantRelocatable>(Offset)) {
CR->emitWithoutDollar(Func->getContext()); CR->emitWithoutPrefix(Func->getTarget());
} else { } else {
llvm_unreachable("Invalid offset type for x86 mem operand"); llvm_unreachable("Invalid offset type for x86 mem operand");
} }
......
...@@ -422,27 +422,28 @@ void Variable::dump(const Cfg *Func, Ostream &Str) const { ...@@ -422,27 +422,28 @@ void Variable::dump(const Cfg *Func, Ostream &Str) const {
} }
} }
void ConstantRelocatable::emitWithoutDollar(GlobalContext *Ctx) const { template <> void ConstantInteger32::emit(TargetLowering *Target) const {
if (!ALLOW_DUMP) Target->emit(this);
return;
Ostream &Str = Ctx->getStrEmit();
if (SuppressMangling)
Str << Name;
else
Str << Ctx->mangleName(Name);
if (Offset) {
if (Offset > 0)
Str << "+";
Str << Offset;
}
} }
void ConstantRelocatable::emit(GlobalContext *Ctx) const { template <> void ConstantInteger64::emit(TargetLowering *Target) const {
if (!ALLOW_DUMP) Target->emit(this);
return; }
Ostream &Str = Ctx->getStrEmit();
Str << "$"; template <> void ConstantFloat::emit(TargetLowering *Target) const {
emitWithoutDollar(Ctx); Target->emit(this);
}
template <> void ConstantDouble::emit(TargetLowering *Target) const {
Target->emit(this);
}
void ConstantRelocatable::emit(TargetLowering *Target) const {
Target->emit(this);
}
void ConstantRelocatable::emitWithoutPrefix(TargetLowering *Target) const {
Target->emitWithoutPrefix(this);
} }
void ConstantRelocatable::dump(const Cfg *Func, Ostream &Str) const { void ConstantRelocatable::dump(const Cfg *Func, Ostream &Str) const {
...@@ -458,6 +459,8 @@ void ConstantRelocatable::dump(const Cfg *Func, Ostream &Str) const { ...@@ -458,6 +459,8 @@ void ConstantRelocatable::dump(const Cfg *Func, Ostream &Str) const {
Str << "+" << Offset; Str << "+" << Offset;
} }
void ConstantUndef::emit(TargetLowering *Target) const { Target->emit(this); }
void LiveRange::dump(Ostream &Str) const { void LiveRange::dump(Ostream &Str) const {
if (!ALLOW_DUMP) if (!ALLOW_DUMP)
return; return;
......
...@@ -106,9 +106,8 @@ public: ...@@ -106,9 +106,8 @@ public:
void emitPoolLabel(Ostream &Str) const { void emitPoolLabel(Ostream &Str) const {
Str << ".L$" << getType() << "$" << PoolEntryID; Str << ".L$" << getType() << "$" << PoolEntryID;
} }
void emit(const Cfg *Func) const override { emit(Func->getContext()); } void emit(const Cfg *Func) const override { emit(Func->getTarget()); }
virtual void emit(GlobalContext *Ctx) const = 0; virtual void emit(TargetLowering *Target) const = 0;
virtual void emitWithoutDollar(GlobalContext *Ctx) const = 0;
static bool classof(const Operand *Operand) { static bool classof(const Operand *Operand) {
OperandKind Kind = Operand->getKind(); OperandKind Kind = Operand->getKind();
...@@ -147,10 +146,7 @@ public: ...@@ -147,10 +146,7 @@ public:
} }
PrimType getValue() const { return Value; } PrimType getValue() const { return Value; }
using Constant::emit; using Constant::emit;
// The target needs to implement this for each ConstantPrimitive void emit(TargetLowering *Target) const final;
// specialization.
void emit(GlobalContext *Ctx) const override;
void emitWithoutDollar(GlobalContext *Ctx) const override;
using Constant::dump; using Constant::dump;
void dump(const Cfg *, Ostream &Str) const override { void dump(const Cfg *, Ostream &Str) const override {
if (ALLOW_DUMP) if (ALLOW_DUMP)
...@@ -234,9 +230,9 @@ public: ...@@ -234,9 +230,9 @@ public:
void setSuppressMangling(bool Value) { SuppressMangling = Value; } void setSuppressMangling(bool Value) { SuppressMangling = Value; }
bool getSuppressMangling() const { return SuppressMangling; } bool getSuppressMangling() const { return SuppressMangling; }
using Constant::emit; using Constant::emit;
void emit(TargetLowering *Target) const final;
void emitWithoutPrefix(TargetLowering *Target) const;
using Constant::dump; using Constant::dump;
void emit(GlobalContext *Ctx) const override;
void emitWithoutDollar(GlobalContext *Ctx) const override;
void dump(const Cfg *Func, Ostream &Str) const override; void dump(const Cfg *Func, Ostream &Str) const override;
static bool classof(const Operand *Operand) { static bool classof(const Operand *Operand) {
...@@ -272,10 +268,8 @@ public: ...@@ -272,10 +268,8 @@ public:
} }
using Constant::emit; using Constant::emit;
void emit(TargetLowering *Target) const final;
using Constant::dump; using Constant::dump;
// The target needs to implement this.
void emit(GlobalContext *Ctx) const override;
void emitWithoutDollar(GlobalContext *Ctx) const override;
void dump(const Cfg *, Ostream &Str) const override { void dump(const Cfg *, Ostream &Str) const override {
if (ALLOW_DUMP) if (ALLOW_DUMP)
Str << "undef"; Str << "undef";
......
...@@ -234,6 +234,30 @@ InstCall *TargetLowering::makeHelperCall(const IceString &Name, Variable *Dest, ...@@ -234,6 +234,30 @@ InstCall *TargetLowering::makeHelperCall(const IceString &Name, Variable *Dest,
return Call; return Call;
} }
void TargetLowering::emitWithoutPrefix(const ConstantRelocatable *C) const {
if (!ALLOW_DUMP)
return;
Ostream &Str = Ctx->getStrEmit();
if (C->getSuppressMangling())
Str << C->getName();
else
Str << Ctx->mangleName(C->getName());
RelocOffsetT Offset = C->getOffset();
if (Offset) {
if (Offset > 0)
Str << "+";
Str << Offset;
}
}
void TargetLowering::emit(const ConstantRelocatable *C) const {
if (!ALLOW_DUMP)
return;
Ostream &Str = Ctx->getStrEmit();
Str << getConstantPrefix();
emitWithoutPrefix(C);
}
std::unique_ptr<TargetDataLowering> std::unique_ptr<TargetDataLowering>
TargetDataLowering::createLowering(GlobalContext *Ctx) { TargetDataLowering::createLowering(GlobalContext *Ctx) {
TargetArch Target = Ctx->getFlags().getTargetArch(); TargetArch Target = Ctx->getFlags().getTargetArch();
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "IceDefs.h" #include "IceDefs.h"
#include "IceInst.h" // for the names of the Inst subtypes #include "IceInst.h" // for the names of the Inst subtypes
#include "IceOperand.h"
#include "IceTypes.h" #include "IceTypes.h"
namespace Ice { namespace Ice {
...@@ -207,6 +208,16 @@ public: ...@@ -207,6 +208,16 @@ public:
virtual void emitVariable(const Variable *Var) const = 0; virtual void emitVariable(const Variable *Var) const = 0;
void emitWithoutPrefix(const ConstantRelocatable *CR) const;
void emit(const ConstantRelocatable *CR) const;
virtual const char *getConstantPrefix() const = 0;
virtual void emit(const ConstantUndef *C) const = 0;
virtual void emit(const ConstantInteger32 *C) const = 0;
virtual void emit(const ConstantInteger64 *C) const = 0;
virtual void emit(const ConstantFloat *C) const = 0;
virtual void emit(const ConstantDouble *C) const = 0;
// Performs target-specific argument lowering. // Performs target-specific argument lowering.
virtual void lowerArguments() = 0; virtual void lowerArguments() = 0;
......
...@@ -702,15 +702,28 @@ void TargetARM32::makeRandomRegisterPermutation( ...@@ -702,15 +702,28 @@ void TargetARM32::makeRandomRegisterPermutation(
UnimplementedError(Func->getContext()->getFlags()); UnimplementedError(Func->getContext()->getFlags());
} }
/* TODO(jvoung): avoid duplicate symbols with multiple targets. void TargetARM32::emit(const ConstantInteger32 *C) const {
void ConstantUndef::emitWithoutDollar(GlobalContext *) const { if (!ALLOW_DUMP)
llvm_unreachable("Not expecting to emitWithoutDollar undef"); return;
Ostream &Str = Ctx->getStrEmit();
Str << getConstantPrefix() << C->getValue();
}
void TargetARM32::emit(const ConstantInteger64 *) const {
llvm::report_fatal_error("Not expecting to emit 64-bit integers");
}
void TargetARM32::emit(const ConstantFloat *C) const {
UnimplementedError(Ctx->getFlags());
}
void TargetARM32::emit(const ConstantDouble *C) const {
UnimplementedError(Ctx->getFlags());
} }
void ConstantUndef::emit(GlobalContext *) const { void TargetARM32::emit(const ConstantUndef *) const {
llvm_unreachable("undef value encountered by emitter."); llvm::report_fatal_error("undef value encountered by emitter.");
} }
*/
TargetDataARM32::TargetDataARM32(GlobalContext *Ctx) TargetDataARM32::TargetDataARM32(GlobalContext *Ctx)
: TargetDataLowering(Ctx) {} : TargetDataLowering(Ctx) {}
......
...@@ -53,6 +53,14 @@ public: ...@@ -53,6 +53,14 @@ public:
return (typeWidthInBytes(Ty) + 3) & ~3; return (typeWidthInBytes(Ty) + 3) & ~3;
} }
void emitVariable(const Variable *Var) const override; void emitVariable(const Variable *Var) const override;
const char *getConstantPrefix() const final { return "#"; }
void emit(const ConstantUndef *C) const final;
void emit(const ConstantInteger32 *C) const final;
void emit(const ConstantInteger64 *C) const final;
void emit(const ConstantFloat *C) const final;
void emit(const ConstantDouble *C) const final;
void lowerArguments() override; void lowerArguments() override;
void addProlog(CfgNode *Node) override; void addProlog(CfgNode *Node) override;
void addEpilog(CfgNode *Node) override; void addEpilog(CfgNode *Node) override;
......
...@@ -4650,58 +4650,33 @@ void TargetX8632::makeRandomRegisterPermutation( ...@@ -4650,58 +4650,33 @@ void TargetX8632::makeRandomRegisterPermutation(
} }
} }
template <> void TargetX8632::emit(const ConstantInteger32 *C) const {
void ConstantInteger32::emitWithoutDollar(GlobalContext *Ctx) const {
if (!ALLOW_DUMP) if (!ALLOW_DUMP)
return; return;
Ostream &Str = Ctx->getStrEmit(); Ostream &Str = Ctx->getStrEmit();
Str << (int32_t)getValue(); Str << getConstantPrefix() << C->getValue();
} }
template <> void ConstantInteger32::emit(GlobalContext *Ctx) const { void TargetX8632::emit(const ConstantInteger64 *) const {
if (!ALLOW_DUMP) llvm::report_fatal_error("Not expecting to emit 64-bit integers");
return;
Ostream &Str = Ctx->getStrEmit();
Str << "$";
emitWithoutDollar(Ctx);
}
template <> void ConstantInteger64::emitWithoutDollar(GlobalContext *) const {
llvm_unreachable("Not expecting to emitWithoutDollar 64-bit integers");
}
template <> void ConstantInteger64::emit(GlobalContext *) const {
llvm_unreachable("Not expecting to emit 64-bit integers");
} }
template <> void ConstantFloat::emitWithoutDollar(GlobalContext *) const { void TargetX8632::emit(const ConstantFloat *C) const {
llvm_unreachable("Not expecting to emitWithoutDollar floats");
}
template <> void ConstantFloat::emit(GlobalContext *Ctx) const {
if (!ALLOW_DUMP) if (!ALLOW_DUMP)
return; return;
Ostream &Str = Ctx->getStrEmit(); Ostream &Str = Ctx->getStrEmit();
emitPoolLabel(Str); C->emitPoolLabel(Str);
}
template <> void ConstantDouble::emitWithoutDollar(GlobalContext *) const {
llvm_unreachable("Not expecting to emitWithoutDollar doubles");
} }
template <> void ConstantDouble::emit(GlobalContext *Ctx) const { void TargetX8632::emit(const ConstantDouble *C) const {
if (!ALLOW_DUMP) if (!ALLOW_DUMP)
return; return;
Ostream &Str = Ctx->getStrEmit(); Ostream &Str = Ctx->getStrEmit();
emitPoolLabel(Str); C->emitPoolLabel(Str);
}
void ConstantUndef::emitWithoutDollar(GlobalContext *) const {
llvm_unreachable("Not expecting to emitWithoutDollar undef");
} }
void ConstantUndef::emit(GlobalContext *) const { void TargetX8632::emit(const ConstantUndef *) const {
llvm_unreachable("undef value encountered by emitter."); llvm::report_fatal_error("undef value encountered by emitter.");
} }
TargetDataX8632::TargetDataX8632(GlobalContext *Ctx) TargetDataX8632::TargetDataX8632(GlobalContext *Ctx)
......
...@@ -54,6 +54,14 @@ public: ...@@ -54,6 +54,14 @@ public:
return (typeWidthInBytes(Ty) + 3) & ~3; return (typeWidthInBytes(Ty) + 3) & ~3;
} }
void emitVariable(const Variable *Var) const override; void emitVariable(const Variable *Var) const override;
const char *getConstantPrefix() const final { return "$"; }
void emit(const ConstantUndef *C) const final;
void emit(const ConstantInteger32 *C) const final;
void emit(const ConstantInteger64 *C) const final;
void emit(const ConstantFloat *C) const final;
void emit(const ConstantDouble *C) const final;
void lowerArguments() override; void lowerArguments() override;
void addProlog(CfgNode *Node) override; void addProlog(CfgNode *Node) override;
void addEpilog(CfgNode *Node) override; void addEpilog(CfgNode *Node) override;
...@@ -523,11 +531,6 @@ private: ...@@ -523,11 +531,6 @@ private:
template <typename T> static void emitConstantPool(GlobalContext *Ctx); template <typename T> static void emitConstantPool(GlobalContext *Ctx);
}; };
template <> void ConstantInteger32::emit(GlobalContext *Ctx) const;
template <> void ConstantInteger64::emit(GlobalContext *Ctx) const;
template <> void ConstantFloat::emit(GlobalContext *Ctx) const;
template <> void ConstantDouble::emit(GlobalContext *Ctx) const;
} // end of namespace Ice } // end of namespace Ice
#endif // SUBZERO_SRC_ICETARGETLOWERINGX8632_H #endif // SUBZERO_SRC_ICETARGETLOWERINGX8632_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment