llvm.org GIT mirror llvm / a9f5e05
Add support for the switch instruction to the CWriter git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5847 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 17 years ago
2 changed file(s) with 46 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
66 #include "llvm/Constants.h"
77 #include "llvm/DerivedTypes.h"
88 #include "llvm/Module.h"
9 #include "llvm/iMemory.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/iPHINode.h"
12 #include "llvm/iOther.h"
13 #include "llvm/iOperators.h"
9 #include "llvm/Instructions.h"
1410 #include "llvm/Pass.h"
1511 #include "llvm/SymbolTable.h"
1612 #include "llvm/SlotCalculator.h"
107103
108104 void visitReturnInst(ReturnInst &I);
109105 void visitBranchInst(BranchInst &I);
106 void visitSwitchInst(SwitchInst &I);
110107
111108 void visitPHINode(PHINode &I) {}
112109 void visitBinaryOperator(Instruction &I);
155152 string CWriter::getValueName(const Value *V) {
156153 if (V->hasName()) { // Print out the label if it exists...
157154 if (isa(V) && // Do not mangle globals...
158 cast(V)->hasExternalLinkage())// && // Unless it's internal or
159 //!MangledGlobals.count(V)) // Unless the name would collide if we don't
155 cast(V)->hasExternalLinkage()) // Unless it's internal or
156 //!MangledGlobals.count(V)) // Unless the name would collide if we don't
160157 return makeNameProper(V->getName());
161158
162159 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
866863 Out << ";\n";
867864 }
868865
866 void CWriter::visitSwitchInst(SwitchInst &SI) {
867 Out << " switch (";
868 writeOperand(SI.getOperand(0));
869 Out << ") {\n default: goto ";
870 writeOperand(SI.getDefaultDest());
871 Out << ";\n";
872 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
873 Out << " case ";
874 writeOperand(SI.getOperand(i));
875 Out << ":\n";
876 BasicBlock *Succ = cast(SI.getOperand(i+1));
877 printBranchToBlock(SI.getParent(), Succ, 2);
878 if (Succ == SI.getParent()->getNext())
879 Out << " break;\n";
880 }
881 Out << " }\n";
882 }
883
884
869885 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
870886 // If PHI nodes need copies, we need the copy code...
871887 if (isa(To->front()) ||
66 #include "llvm/Constants.h"
77 #include "llvm/DerivedTypes.h"
88 #include "llvm/Module.h"
9 #include "llvm/iMemory.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/iPHINode.h"
12 #include "llvm/iOther.h"
13 #include "llvm/iOperators.h"
9 #include "llvm/Instructions.h"
1410 #include "llvm/Pass.h"
1511 #include "llvm/SymbolTable.h"
1612 #include "llvm/SlotCalculator.h"
107103
108104 void visitReturnInst(ReturnInst &I);
109105 void visitBranchInst(BranchInst &I);
106 void visitSwitchInst(SwitchInst &I);
110107
111108 void visitPHINode(PHINode &I) {}
112109 void visitBinaryOperator(Instruction &I);
155152 string CWriter::getValueName(const Value *V) {
156153 if (V->hasName()) { // Print out the label if it exists...
157154 if (isa(V) && // Do not mangle globals...
158 cast(V)->hasExternalLinkage())// && // Unless it's internal or
159 //!MangledGlobals.count(V)) // Unless the name would collide if we don't
155 cast(V)->hasExternalLinkage()) // Unless it's internal or
156 //!MangledGlobals.count(V)) // Unless the name would collide if we don't
160157 return makeNameProper(V->getName());
161158
162159 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
866863 Out << ";\n";
867864 }
868865
866 void CWriter::visitSwitchInst(SwitchInst &SI) {
867 Out << " switch (";
868 writeOperand(SI.getOperand(0));
869 Out << ") {\n default: goto ";
870 writeOperand(SI.getDefaultDest());
871 Out << ";\n";
872 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
873 Out << " case ";
874 writeOperand(SI.getOperand(i));
875 Out << ":\n";
876 BasicBlock *Succ = cast(SI.getOperand(i+1));
877 printBranchToBlock(SI.getParent(), Succ, 2);
878 if (Succ == SI.getParent()->getNext())
879 Out << " break;\n";
880 }
881 Out << " }\n";
882 }
883
884
869885 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
870886 // If PHI nodes need copies, we need the copy code...
871887 if (isa(To->front()) ||