Enables llvm dyn_cast for Assemblers.
IceCfg::getAssembler() is a template that simply static_casts the CFG's assembler. This could potentially be problematic in the future, so we enabled the (relatively) cheap llvm dyn_cast operator for Assemblers.
This CL also renames assembler_mips32.h to IceAssemblerMIPS32.h.
BUG=
R=stichnot@chromium.org
Review URL: https://codereview.chromium.org/1211863004.
diff --git a/src/IceAssembler.h b/src/IceAssembler.h
index b6ddf08..c2a40d6 100644
--- a/src/IceAssembler.h
+++ b/src/IceAssembler.h
@@ -144,11 +144,18 @@
};
class Assembler {
+ Assembler() = delete;
Assembler(const Assembler &) = delete;
Assembler &operator=(const Assembler &) = delete;
public:
- Assembler() : Allocator(), Buffer(*this) {}
+ enum AssemblerKind {
+ Asm_ARM32,
+ Asm_MIPS32,
+ Asm_X8632,
+ Asm_X8664,
+ };
+
virtual ~Assembler() = default;
// Allocate a chunk of bytes using the per-Assembler allocator.
@@ -203,7 +210,15 @@
void setPreliminary(bool Value) { Preliminary = Value; }
bool getPreliminary() const { return Preliminary; }
+ AssemblerKind getKind() const { return Kind; }
+
+protected:
+ explicit Assembler(AssemblerKind Kind)
+ : Kind(Kind), Allocator(), Buffer(*this) {}
+
private:
+ const AssemblerKind Kind;
+
ArenaAllocator<32 * 1024> Allocator;
// FunctionName and IsInternal are transferred from the original Cfg
// object, since the Cfg object may be deleted by the time the
diff --git a/src/IceAssemblerARM32.h b/src/IceAssemblerARM32.h
index a032204..f67b630 100644
--- a/src/IceAssemblerARM32.h
+++ b/src/IceAssemblerARM32.h
@@ -34,7 +34,8 @@
AssemblerARM32 &operator=(const AssemblerARM32 &) = delete;
public:
- explicit AssemblerARM32(bool use_far_branches = false) : Assembler() {
+ explicit AssemblerARM32(bool use_far_branches = false)
+ : Assembler(Asm_ARM32) {
// This mode is only needed and implemented for MIPS and ARM.
assert(!use_far_branches);
(void)use_far_branches;
@@ -68,6 +69,10 @@
(void)Kind;
llvm_unreachable("Not yet implemented.");
}
+
+ static bool classof(const Assembler *Asm) {
+ return Asm->getKind() == Asm_ARM32;
+ }
};
} // end of namespace ARM32
diff --git a/src/assembler_mips32.h b/src/IceAssemblerMIPS32.h
similarity index 84%
rename from src/assembler_mips32.h
rename to src/IceAssemblerMIPS32.h
index b5bca57..fc83919 100644
--- a/src/assembler_mips32.h
+++ b/src/IceAssemblerMIPS32.h
@@ -1,4 +1,4 @@
-//===- subzero/src/assembler_mips.h - Assembler for MIPS --------*- C++ -*-===//
+//===- subzero/src/IceAssemblerMIPS32.h - Assembler for MIPS ----*- C++ -*-===//
//
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
@@ -19,8 +19,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef SUBZERO_SRC_ASSEMBLER_MIPS32_H
-#define SUBZERO_SRC_ASSEMBLER_MIPS32_H
+#ifndef SUBZERO_SRC_ICEASSEMBLERMIPS32_H
+#define SUBZERO_SRC_ICEASSEMBLERMIPS32_H
#include "IceAssembler.h"
#include "IceDefs.h"
@@ -34,7 +34,8 @@
AssemblerMIPS32 &operator=(const AssemblerMIPS32 &) = delete;
public:
- explicit AssemblerMIPS32(bool use_far_branches = false) : Assembler() {
+ explicit AssemblerMIPS32(bool use_far_branches = false)
+ : Assembler(Asm_MIPS32) {
// This mode is only needed and implemented for MIPS32 and ARM.
assert(!use_far_branches);
(void)use_far_branches;
@@ -67,9 +68,13 @@
(void)Kind;
llvm::report_fatal_error("Not yet implemented.");
}
+
+ static bool classof(const Assembler *Asm) {
+ return Asm->getKind() == Asm_MIPS32;
+ }
};
} // end of namespace MIPS32
} // end of namespace Ice
-#endif // SUBZERO_SRC_ASSEMBLER_MIPS32_H
+#endif // SUBZERO_SRC_ICEASSEMBLERMIPS32_H
diff --git a/src/IceAssemblerX8632.h b/src/IceAssemblerX8632.h
index 132e72d..8b13fc6 100644
--- a/src/IceAssemblerX8632.h
+++ b/src/IceAssemblerX8632.h
@@ -337,7 +337,8 @@
AssemblerX8632 &operator=(const AssemblerX8632 &) = delete;
public:
- explicit AssemblerX8632(bool use_far_branches = false) : Assembler() {
+ explicit AssemblerX8632(bool use_far_branches = false)
+ : Assembler(Asm_X8632) {
// This mode is only needed and implemented for MIPS and ARM.
assert(!use_far_branches);
(void)use_far_branches;
@@ -377,6 +378,10 @@
return Kind == llvm::ELF::R_386_PC32;
}
+ static bool classof(const Assembler *Asm) {
+ return Asm->getKind() == Asm_X8632;
+ }
+
// Operations to emit GPR instructions (and dispatch on operand type).
typedef void (AssemblerX8632::*TypedEmitGPR)(Type, GPRRegister);
typedef void (AssemblerX8632::*TypedEmitAddr)(Type, const Address &);
diff --git a/src/IceAssemblerX8664.h b/src/IceAssemblerX8664.h
index de19b65..0deda30 100644
--- a/src/IceAssemblerX8664.h
+++ b/src/IceAssemblerX8664.h
@@ -25,7 +25,8 @@
AssemblerX8664 &operator=(const AssemblerX8664 &) = delete;
public:
- explicit AssemblerX8664(bool use_far_branches = false) : Assembler() {
+ explicit AssemblerX8664(bool use_far_branches = false)
+ : Assembler(Asm_X8664) {
assert(!use_far_branches);
(void)use_far_branches;
llvm::report_fatal_error("Not yet implemented");
@@ -40,6 +41,10 @@
llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const override;
void bindCfgNodeLabel(SizeT NodeNumber) override;
bool fixupIsPCRel(FixupKind Kind) const override;
+
+ static bool classof(const Assembler *Asm) {
+ return Asm->getKind() == Asm_X8664;
+ }
};
} // end of namespace X8664
diff --git a/src/IceCfg.h b/src/IceCfg.h
index 83c4ddd..800bf40 100644
--- a/src/IceCfg.h
+++ b/src/IceCfg.h
@@ -127,7 +127,7 @@
VariablesMetadata *getVMetadata() const { return VMetadata.get(); }
Liveness *getLiveness() const { return Live.get(); }
template <typename T = Assembler> T *getAssembler() const {
- return static_cast<T *>(TargetAssembler.get());
+ return llvm::dyn_cast<T>(TargetAssembler.get());
}
Assembler *releaseAssembler() { return TargetAssembler.release(); }
std::unique_ptr<VariableDeclarationList> getGlobalInits() {
diff --git a/src/IceTargetLowering.cpp b/src/IceTargetLowering.cpp
index 74ce567..25e013b 100644
--- a/src/IceTargetLowering.cpp
+++ b/src/IceTargetLowering.cpp
@@ -18,9 +18,9 @@
#include "IceTargetLowering.h"
#include "IceAssemblerARM32.h"
+#include "IceAssemblerMIPS32.h"
#include "IceAssemblerX8632.h"
#include "IceAssemblerX8664.h"
-#include "assembler_mips32.h"
#include "IceCfg.h" // setError()
#include "IceCfgNode.h"
#include "IceGlobalInits.h"
@@ -519,9 +519,8 @@
Var.getInitializers()) {
switch (Init->getKind()) {
case VariableDeclaration::Initializer::DataInitializerKind: {
- const auto &Data =
- llvm::cast<VariableDeclaration::DataInitializer>(Init.get())
- ->getContents();
+ const auto &Data = llvm::cast<VariableDeclaration::DataInitializer>(
+ Init.get())->getContents();
for (SizeT i = 0; i < Init->getNumBytes(); ++i) {
Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n";
}