Initial skeleton of Subzero.
This includes just enough code to build the high-level ICE IR and dump it back out again. There is a script szdiff.py that does a fuzzy diff of the input and output for verification. See the comment in szdiff.py for a description of the fuzziness.
Building llvm2ice requires LLVM headers, libs, and tools (e.g. FileCheck) to be present. These default to something like llvm_i686_linux_work/Release+Asserts/ based on the checked-out and built pnacl-llvm code; I'll try to figure out how to more automatically detect the build configuration.
"make check" runs the lit tests.
This CL has under 2000 lines of "interesting" Ice*.{h,cpp} code, plus 600 lines of llvm2ice.cpp driver code, and the rest is tests.
Here is the high-level mapping of source files to functionality:
IceDefs.h, IceTypes.h, IceTypes.cpp:
Commonly used types and utilities.
IceCfg.h, IceCfg.cpp:
Operations at the function level.
IceCfgNode.h, IceCfgNode.cpp:
Operations on basic blocks (nodes).
IceInst.h, IceInst.cpp:
Operations on instructions.
IceOperand.h, IceOperand.cpp:
Operations on operands, such as stack locations, physical registers, and constants.
BUG= none
R=jfb@chromium.org
Review URL: https://codereview.chromium.org/205613002
diff --git a/src/IceCfgNode.cpp b/src/IceCfgNode.cpp
new file mode 100644
index 0000000..fe8b70e0
--- /dev/null
+++ b/src/IceCfgNode.cpp
@@ -0,0 +1,109 @@
+//===- subzero/src/IceCfgNode.cpp - Basic block (node) implementation -----===//
+//
+// The Subzero Code Generator
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the CfgNode class, including the
+// complexities of instruction insertion and in-edge calculation.
+//
+//===----------------------------------------------------------------------===//
+
+#include "IceCfg.h"
+#include "IceCfgNode.h"
+#include "IceInst.h"
+#include "IceOperand.h"
+
+namespace Ice {
+
+CfgNode::CfgNode(Cfg *Func, SizeT LabelNumber, IceString Name)
+ : Func(Func), Number(LabelNumber), Name(Name) {}
+
+// Returns the name the node was created with. If no name was given,
+// it synthesizes a (hopefully) unique name.
+IceString CfgNode::getName() const {
+ if (!Name.empty())
+ return Name;
+ char buf[30];
+ snprintf(buf, llvm::array_lengthof(buf), "__%u", getIndex());
+ return buf;
+}
+
+// Adds an instruction to either the Phi list or the regular
+// instruction list. Validates that all Phis are added before all
+// regular instructions.
+void CfgNode::appendInst(Inst *Inst) {
+ if (InstPhi *Phi = llvm::dyn_cast<InstPhi>(Inst)) {
+ if (!Insts.empty()) {
+ Func->setError("Phi instruction added to the middle of a block");
+ return;
+ }
+ Phis.push_back(Phi);
+ } else {
+ Insts.push_back(Inst);
+ }
+ Inst->updateVars(this);
+}
+
+// When a node is created, the OutEdges are immediately knows, but the
+// InEdges have to be built up incrementally. After the CFG has been
+// constructed, the computePredecessors() pass finalizes it by
+// creating the InEdges list.
+void CfgNode::computePredecessors() {
+ OutEdges = (*Insts.rbegin())->getTerminatorEdges();
+ for (NodeList::const_iterator I = OutEdges.begin(), E = OutEdges.end();
+ I != E; ++I) {
+ CfgNode *Node = *I;
+ Node->InEdges.push_back(this);
+ }
+}
+
+// ======================== Dump routines ======================== //
+
+void CfgNode::dump(Cfg *Func) const {
+ Func->setCurrentNode(this);
+ Ostream &Str = Func->getContext()->getStrDump();
+ if (Func->getContext()->isVerbose(IceV_Instructions)) {
+ Str << getName() << ":\n";
+ }
+ // Dump list of predecessor nodes.
+ if (Func->getContext()->isVerbose(IceV_Preds) && !InEdges.empty()) {
+ Str << " // preds = ";
+ for (NodeList::const_iterator I = InEdges.begin(), E = InEdges.end();
+ I != E; ++I) {
+ if (I != InEdges.begin())
+ Str << ", ";
+ Str << "%" << (*I)->getName();
+ }
+ Str << "\n";
+ }
+ // Dump each instruction.
+ if (Func->getContext()->isVerbose(IceV_Instructions)) {
+ for (PhiList::const_iterator I = Phis.begin(), E = Phis.end(); I != E;
+ ++I) {
+ const Inst *Inst = *I;
+ Inst->dumpDecorated(Func);
+ }
+ InstList::const_iterator I = Insts.begin(), E = Insts.end();
+ while (I != E) {
+ Inst *Inst = *I++;
+ Inst->dumpDecorated(Func);
+ }
+ }
+ // Dump list of successor nodes.
+ if (Func->getContext()->isVerbose(IceV_Succs)) {
+ Str << " // succs = ";
+ for (NodeList::const_iterator I = OutEdges.begin(), E = OutEdges.end();
+ I != E; ++I) {
+ if (I != OutEdges.begin())
+ Str << ", ";
+ Str << "%" << (*I)->getName();
+ }
+ Str << "\n";
+ }
+}
+
+} // end of namespace Ice