📄 vhdl.cpp
字号:
// Out << "extern "; // printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); // // if (I->hasLinkOnceLinkage()) // Out << " __attribute__((common))"; // else if (I->hasWeakLinkage()) // Out << " __ATTRIBUTE_WEAK__"; // Out << ";\n"; // } //} // Output the global variable definitions and contents... if (!M.global_empty()) { Out << "\n\n; Global Variable Definitions and Initialization \n"; for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) if (!I->isExternal()) { Out << " GLOBAL VAR "; //if (I->hasInternalLinkage()) // Out << "static "; printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); //printType(Out, I->getType()->getElementType()); //Out << " " << I; //if (I->hasLinkOnceLinkage()) // Out << " __attribute__((common))"; //else if (I->hasWeakLinkage()) // Out << " __ATTRIBUTE_WEAK__"; // If the initializer is not null, emit the initializer. If it is null, // we try to avoid emitting large amounts of zeros. The problem with // this, however, occurs when the variable has weak linkage. In this // case, the assembler will complain about the variable being both weak // and common, so we disable this optimization. if (!I->getInitializer()->isNullValue()) { Out << " = " ; writeOperand(I->getInitializer()); } else if (I->hasWeakLinkage()) { // We have to specify an initializer, but it doesn't have to be // complete. If the value is an aggregate, print out { 0 }, and let // the compiler figure out the rest of the zeros. Out << " = " ; if (isa<StructType>(I->getInitializer()->getType()) || isa<ArrayType>(I->getInitializer()->getType())) { Out << "{ 0 }"; } else { // Just print it out normally. writeOperand(I->getInitializer()); } } Out << ";\n"; } } // Output all floating point constants that cannot be printed accurately... //printFloatingPointConstants(M); if (!M.empty()) Out << "\n\n; Function Bodies \n"; return false;}/// Output all floating point constants that cannot be printed accurately...void VWriter::printFloatingPointConstants(Function &F) { union { double D; uint64_t U; } DBLUnion; union { float F; unsigned U; } FLTUnion; // Scan the module for floating point constants. If any FP constant is used // in the function, we want to redirect it here so that we do not depend on // the precision of the printed form, unless the printed form preserves // precision. // static unsigned FPCounter = 0; for (constant_iterator I = constant_begin(&F), E = constant_end(&F); I != E; ++I) if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I)) if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe. !FPConstantMap.count(FPC)) { double Val = FPC->getValue(); FPConstantMap[FPC] = FPCounter; // Number the FP constants if (FPC->getType() == Type::DoubleTy) { DBLUnion.D = Val; Out << "static const ConstantDoubleTy FPConstant" << FPCounter++ << " = 0x" << std::hex << DBLUnion.U << std::dec << "ULL; /* " << Val << " */\n"; } else if (FPC->getType() == Type::FloatTy) { FLTUnion.F = Val; Out << "static const ConstantFloatTy FPConstant" << FPCounter++ << " = 0x" << std::hex << FLTUnion.U << std::dec << "U; /* " << Val << " */\n"; } else assert(0 && "Unknown float type!"); } Out << "\n"; }/// printSymbolTable - Run through symbol table looking for type names. If a/// type name is found, emit it's declaration...///void VWriter::printModuleTypes(const SymbolTable &ST) { // We are only interested in the type plane of the symbol table... SymbolTable::type_const_iterator I = ST.type_begin(); SymbolTable::type_const_iterator End = ST.type_end(); if (I == End) return; // Print out forward declarations for structure types before anything else! Out << "/* Structure forward decls */\n"; for (; I != End; ++I) if (const Type *STy = dyn_cast<StructType>(I->second)) { std::string Name = "struct l_" + Mangler::makeNameProper(I->first); Out << Name << ";\n"; TypeNames.insert(std::make_pair(STy, Name)); } Out << "\n"; // Now we can print out typedefs... Out << "/* Typedefs */\n"; for (I = ST.type_begin(); I != End; ++I) { const Type *Ty = cast<Type>(I->second); std::string Name = "l_" + Mangler::makeNameProper(I->first); Out << "typedef "; printType(Out, Ty, Name); Out << ";\n"; } Out << "\n"; // Keep track of which structures have been printed so far... std::set<const StructType *> StructPrinted; // Loop over all structures then push them into the stack so they are // printed in the correct order. // Out << "/* Structure contents */\n"; for (I = ST.type_begin(); I != End; ++I) if (const StructType *STy = dyn_cast<StructType>(I->second)) // Only print out used types! printContainedStructs(STy, StructPrinted);}// Push the struct onto the stack and recursively push all structs// this one depends on.void VWriter::printContainedStructs(const Type *Ty, std::set<const StructType*> &StructPrinted){ if (const StructType *STy = dyn_cast<StructType>(Ty)) { //Check to see if we have already printed this struct if (StructPrinted.count(STy) == 0) { // Print all contained types first... for (StructType::element_iterator I = STy->element_begin(), E = STy->element_end(); I != E; ++I) { const Type *Ty1 = I->get(); if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1)) printContainedStructs(*I, StructPrinted); } //Print structure type out.. StructPrinted.insert(STy); std::string Name = TypeNames[STy]; printType(Out, STy, Name, true); Out << ";\n\n"; } // If it is an array, check contained types and continue } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)){ const Type *Ty1 = ATy->getElementType(); if (isa<StructType>(Ty1) || isa<ArrayType>(Ty1)) printContainedStructs(Ty1, StructPrinted); }}void VWriter::printFunctionSignature(const Function *F, bool Prototype) { if (F->hasInternalLinkage()) Out << "static "; // Loop over the arguments, printing them... const FunctionType *FT = cast<FunctionType>(F->getFunctionType()); std::stringstream FunctionInnards; // Print out the name... FunctionInnards << Mang->getValueName(F) << "("; if (!F->isExternal()) { if (!F->arg_empty()) { std::string ArgName; if (F->arg_begin()->hasName() || !Prototype) ArgName = Mang->getValueName(F->arg_begin()); printType(FunctionInnards, F->arg_begin()->getType(), ArgName); for (Function::const_arg_iterator I = ++F->arg_begin(), E = F->arg_end(); I != E; ++I) { FunctionInnards << ", "; if (I->hasName() || !Prototype) ArgName = Mang->getValueName(I); else ArgName = ""; printType(FunctionInnards, I->getType(), ArgName); } } } else { // Loop over the arguments, printing them... for (FunctionType::param_iterator I = FT->param_begin(), E = FT->param_end(); I != E; ++I) { if (I != FT->param_begin()) FunctionInnards << ", "; printType(FunctionInnards, *I); } } // Finish printing arguments... if this is a vararg function, print the ..., // unless there are no known types, in which case, we just emit (). // if (FT->isVarArg() && FT->getNumParams()) { if (FT->getNumParams()) FunctionInnards << ", "; FunctionInnards << "..."; // Output varargs portion of signature! } else if (!FT->isVarArg() && FT->getNumParams() == 0) { FunctionInnards << "void"; // ret() -> ret(void) in C. } FunctionInnards << ")"; // Print out the return type and the entire signature for that matter printType(Out, F->getReturnType(), FunctionInnards.str());}void VWriter::printFunction(Function &F) { //std::cout << "printingFunction -- sort of visiting -- THIS IS STILL BROKEN\n"; //std::cout << " function name " << F.getName() << "\n"; // printFunctionSignature(&F, false); Out << "FUNCTION "<< F.getName() << "\n"; // cut out the local variable stuff -- do I need to know ?? // print local variable information for the function //for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) // if (const AllocaInst *AI = isDirectAlloca(&*I)) { // Out << " "; // printType(Out, AI->getAllocatedType(), Mang->getValueName(AI)); // Out << "; /* Address exposed local */\n"; // } else if (I->getType() != Type::VoidTy && !isInlinableInst(*I)) { // Out << " "; // printType(Out, I->getType(), Mang->getValueName(&*I)); // Out << ";\n"; // // if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well... // Out << " "; // printType(Out, I->getType(), // Mang->getValueName(&*I)+"__PHI_TEMPORARY"); // Out << ";\n"; // } // } // //Out << "\n"; // print the basic blocks level = 0; for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { if (Loop *L = LI->getLoopFor(BB)) { if (L->getHeader() == BB && L->getParentLoop() == 0) printLoop(L); } else { printBasicBlock(BB); if (++BB!=E) Out << "\n"; --BB; } } Out << "FUNCTION END " << F.getName() << "\n\n";}void VWriter::printSpace() { for(int i = 0; i < level; i++) { Out << " "; }}void VWriter::printLoop(Loop *L) { //Out << " do { /* Syntactic loop '" << L->getHeader()->getName() // << "' to make GCC happy */\n"; printSpace(); Out << "LOOP %" << L->getHeader()->getName() << "\n"; level++; for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) { BasicBlock *BB = L->getBlocks()[i]; Loop *BBLoop = LI->getLoopFor(BB); if (BBLoop == L) printBasicBlock(BB); else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L) printLoop(BBLoop); } level--; printSpace(); Out << "LOOP END %" << L->getHeader()->getName() << "\n"; //Out << " } while (1); /* end of syntactic loop '" //<< L->getHeader()->getName() << "' */\n";}void VWriter::printBasicBlock(BasicBlock *BB) { // Don't print the label for the basic block if there are no uses, or if // the only terminator use is the predecessor basic block's terminator. // We have to scan the use list because PHI nodes use basic blocks too but // do not require a label to be generated. // bool NeedsLabel = false; for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) if (isGotoCodeNecessary(*PI, BB)) { NeedsLabel = true; break; } //if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n"; //if (NeedsLabel) Out << " BLOCK %"<< BB->getName() << "\n"; printSpace(); Out << "BLOCK %"<< BB->getName() << "\n"; // Output all of the instructions in the basic block... for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II) { /* if (!isInlinableInst(*II) && !isDirectAlloca(II)) { if (II->getType() != Type::VoidTy) outputLValue(II); else Out << " "; visit(*II); Out << ";\n"; } */ printSpace(); Out << " " << *II ; } // Don't emit prefix or suffix for the terminator... //visit(*BB->getTerminator()); printSpace(); Out << " " << (*BB->getTerminator()) ; printSpace(); Out << "BLOCK END %"<< BB->getName() << "\n";}// Specific Instruction type classes... note that all of the casts are// necessary because we use the instruction classes as opaque types...//void VWriter::visitReturnInst(ReturnInst &I) { // Don't output a void return if this is the last basic block in the function if (I.getNumOperands() == 0 && &*--I.getParent()->getParent()->end() == I.getParent() && !I.getParent()->size() == 1) { return; } Out << " return"; if (I.getNumOperands()) { Out << " "; writeOperand(I.getOperand(0)); } Out << ";\n";}void VWriter::visitSwitchInst(SwitchInst &SI) { Out << " switch ("; writeOperand(SI.getOperand(0)); Out << ") {\n default:\n"; printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2); Out << ";\n"; for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) { Out << " case "; writeOperand(SI.getOperand(i)); Out << ":\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -