void CLIPSCallInstructionBuilder::addFields(CallInst* target, KnowledgeConstruction *kc, char* parent) { CLIPSInstructionBuilder::addFields((Instruction*)target, kc, parent); if(target->isTailCall()) addTrueField("IsTailCall"); if(target->isNoInline()) addTrueField("IsNoInline"); if(target->canReturnTwice()) addTrueField("CanReturnTwice"); if(target-> doesNotAccessMemory()) addTrueField("DoesNotAccessMemory"); if(target-> onlyReadsMemory()) addTrueField("OnlyReadsMemory"); if(target-> doesNotReturn()) addTrueField("DoesNotReturn"); if(target-> doesNotThrow()) addTrueField("DoesNotThrow"); if(target-> hasStructRetAttr()) addTrueField("HasStructRetAttr"); if(target-> hasByValArgument()) addTrueField("HasByValArgument"); if(target-> isInlineAsm()) addTrueField("IsInlineAsm"); Function* fn = target->getCalledFunction(); openField("CalledFunction"); if(fn != NULL) appendValue(fn->getName()); else appendValue("indirect"); closeField(); if(target->getNumArgOperands() > 0) { openField("ArgumentOperands"); FunctionNamer& namer = getNamer(); for(unsigned i = 0; i < target->getNumArgOperands(); ++i) { //add function args appendValue(kc->route(target->getArgOperand(i), namer)); } closeField(); } }
void CLIPSInvokeInstructionBuilder::addFields(InvokeInst* target, KnowledgeConstruction *kc, char* parent) { CLIPSTerminatorInstructionBuilder::addFields((TerminatorInst*)target, kc, parent); if(target->isNoInline()) addTrueField("IsNoInline"); if(target->doesNotAccessMemory()) addTrueField("DoesNotAccessMemory"); if(target->onlyReadsMemory()) addTrueField("OnlyReadsMemory"); if(target->doesNotReturn()) addTrueField("DoesNotReturn"); if(target->hasStructRetAttr()) addTrueField("HasStructRetAttr"); if(target->hasByValArgument()) addTrueField("HasByValArgument"); addField("NormalDestination", target->getNormalDest()->getName()); addField("UnwindDestination", target->getUnwindDest()->getName()); Function* fn = target->getCalledFunction(); openField("CalledFunction"); if(fn != NULL) { appendValue(fn->getName()); } else { appendValue("indirect"); } closeField(); if(target->getNumArgOperands() > 0) { openField("Arguments"); FunctionNamer& namer = getNamer(); for(unsigned i = 0;i < target->getNumArgOperands(); ++i) { appendValue(kc->route(target->getArgOperand(i), namer)); } closeField(); } }
void CLIPSSwitchInstructionBuilder::addFields(SwitchInst* inst, KnowledgeConstruction *kc, char* parent) { CLIPSValueBuilder::addFields((Value*)inst, kc, parent); std::string par(parent); FunctionNamer& namer = getNamer(); PointerAddress pa = namer.registerInstructionWithBasicBlock(par); addField("TimeIndex", pa); addField("Operation", (char*) inst->getOpcodeName()); if(inst->mayWriteToMemory()) addTrueField("MayWriteToMemory"); if(inst->mayReadFromMemory()) addTrueField("MayReadFromMemory"); if(inst->mayReadOrWriteMemory()) addTrueField("MayReadOrWriteMemory"); if(inst->mayHaveSideEffects()) addTrueField("MayHaveSideEffects"); if(inst->isBinaryOp()) addTrueField("IsBinaryOp"); if(inst->isTerminator()) addTrueField("IsTerminator"); if(inst->isShift()) addTrueField("IsShift"); if(inst->isCast()) addTrueField("IsCast"); if(inst->isArithmeticShift()) addTrueField("IsArithmeticShift"); if(inst->isLogicalShift()) addTrueField("IsLogicalShift"); if(inst->isAssociative()) addTrueField("IsAssociative"); if(inst->isCommutative()) addTrueField("IsCommutative"); addField("Condition", kc->route(inst->getCondition(), getNamer())); addField("DefaultDestination", inst->getDefaultDest()->getName()); unsigned succCount = inst->getNumSuccessors(); if(succCount > 0) { addField("NumSuccessors", succCount); openField("Operands"); for(unsigned i = 0; i < succCount; ++i) { appendValue(i); BasicBlock* target = inst->getSuccessor(i); appendValue(target->getName()); } closeField(); } }
void CLIPSInstructionBuilder::addFields(Instruction* instruction, KnowledgeConstruction *kc, char* parent, bool addDestinationRegisters) { User* tmp = (User*)instruction; CLIPSUserBuilder::addFields(tmp, kc, parent); FunctionNamer& namer = getNamer(); std::string par (parent); PointerAddress pa = namer.registerInstructionWithBasicBlock(par); addField("TimeIndex", pa); addField("Operation", (char*) instruction->getOpcodeName()); if(instruction->mayWriteToMemory()) addTrueField("MayWriteToMemory"); if(instruction->mayReadFromMemory()) addTrueField("MayReadFromMemory"); if(instruction->mayReadOrWriteMemory()) addTrueField("MayReadOrWriteMemory"); if(instruction->mayHaveSideEffects()) addTrueField("MayHaveSideEffects"); if(instruction->isBinaryOp()) addTrueField("IsBinaryOp"); if(instruction->isTerminator()) addTrueField("IsTerminator"); if(instruction->isShift()) addTrueField("IsShift"); if(instruction->isCast()) addTrueField("IsCast"); if(instruction->isArithmeticShift()) addTrueField("IsArithmeticShift"); if(instruction->isLogicalShift()) addTrueField("IsLogicalShift"); if(instruction->isAssociative()) addTrueField("IsAssociative"); if(instruction->isCommutative()) addTrueField("IsCommutative"); if(!instruction->getType()->isVoidTy() && instruction->hasName() && addDestinationRegisters) { addField("DestinationRegisters", instruction->getName()); } if(!instruction->use_empty()) { // DenseMap<BasicBlock*,unsigned> counterArgument; //BasicBlock* directParent = instruction->getParent(); //we could have an instruction not used in the block it's created :D openField("Consumers"); for(Value::use_iterator i = instruction->use_begin(), e = instruction->use_end(); i != e; ++i) { User* target = *i; PointerAddress ptr = (PointerAddress)target; if(namer.pointerRegistered(ptr)) { appendValue(namer.nameFromPointer(ptr)); } else if(isa<Function>(target) || isa<Instruction>(target)) { appendValue(target->getName()); } else { appendValue(kc->route(target, namer)); } } closeField(); /* openField("BlocksUsedIn"); for(DenseMap<BasicBlock*, unsigned>::iterator a = counterArgument.begin(), b = counterArgument.end(); a != b; ++a) { BasicBlock* bb = a->first; appendValue(bb->getName()); //the name of the basic block appendValue(a->second); //the number of iterations } closeField(); */ } }
void CLIPSExtractValueInstructionBuilder::addFields(ExtractValueInst* inst, KnowledgeConstruction *kc, char* parent) { CLIPSUnaryInstructionBuilder::addFields((UnaryInstruction*)inst, kc, parent); addField("AggregateOperand", kc->route(inst->getAggregateOperand(), getNamer())); openField("Indices"); for(ExtractValueInst::idx_iterator i = inst->idx_begin(), e = inst->idx_end(); i != e; ++i) { char* buf = CharBuffer(32); sprintf(buf, "%d", *i); appendValue(buf); free(buf); } closeField(); }
void CLIPSBasicBlockBuilder::addFields(BasicBlock* bb, KnowledgeConstructor *kc, char* parent, bool constructInstructions) { CLIPSValueBuilder::addFields((Value*)bb, kc, parent); char* name = (char*)bb->getName().data(); FunctionNamer& namer = getNamer(); namer.registerBasicBlock(name); if(bb->isLandingPad()) addTrueField("IsLandingPad"); if(bb->hasAddressTaken()) addTrueField("HasAddressTaken"); pred_iterator pi = pred_begin(bb), pe = pred_end(bb); if(pi != pe) { openField("Predecessors"); for(; pi != pe; ++pi) { BasicBlock* pred = *pi; appendValue(pred->getName()); } closeField(); } succ_iterator si = succ_begin(bb), se = succ_end(bb); if(si != se) { openField("Successors"); for(; si != se; ++si) { BasicBlock* succ = *si; appendValue(succ->getName()); } closeField(); } if(constructInstructions && !bb->empty()) { std::string tmp; raw_string_ostream data(tmp); openField("contents"); for(BasicBlock::iterator i = bb->begin(), e = bb->end(); i != e; ++i) { Instruction* inst = i; std::string res = kc->route(inst, namer, name); appendValue(res); data << " " << res; } closeField(); addField("Produces", data.str()); } }
/******************* FUNCTION *********************/ void JsonState::printFormattedField(const char * name, const char* format, ... ) { char buffer[1024]; //format the chain va_list param; va_start (param, format); vsnprintf (buffer,sizeof(buffer), format, param); va_end (param); //print openField(name); *out << buffer; closeField(name); }
void CLIPSStoreInstructionBuilder::addFields(StoreInst* target, KnowledgeConstruction *kc, char* parent) { CLIPSInstructionBuilder::addFields((Instruction*)target, kc, parent, false); addField("Alignment", target->getAlignment()); if(target->isAtomic()) addTrueField("IsAtomic"); if(target->isSimple()) addTrueField("IsSimple"); if(target->isUnordered()) addTrueField("IsUnordered"); if(target->isVolatile()) addTrueField("IsVolatile"); openField("DestinationRegisters"); Value* pointer = target->getPointerOperand(); if(pointer->hasName()) { appendValue(pointer->getName()); } else { appendValue(kc->route(pointer, getNamer())); } closeField(); }
/** * Use printf syntaxe to format new field. * @param name Name of the field to print. * @param format Define the format to use in internal sprint method. **/ void JsonState::printFormattedField(const char * name, const char* format, ... ) { char buffer[SPRINTF_BUFFER_SIZE]; //format the chain va_list param; va_start (param, format); size_t size = vsnprintf (buffer,sizeof(buffer), format, param); va_end (param); //check buffer overflow if (size <= SPRINTF_BUFFER_SIZE) abort(); //print openField(name); bufferdStream << buffer; closeField(name); }
/* void CLIPSInstructionBuilder::build(Instruction* inst, KnowledgeConstruction *kc, char* parent) { open(); addFields(inst, kc, parent); close(); std::string &str = getCompletedString(); kc.addToKnowledgeBase((PointerAddress)inst, str); } */ void CLIPSPHINodeBuilder::addFields(PHINode* instruction, KnowledgeConstruction *kc, char* parent) { //I don't think we want to do User's addField as it's not necessary //We should do Value instead CLIPSValueBuilder::addFields((Value*)instruction, kc, parent); std::string par(parent); FunctionNamer& namer = getNamer(); PointerAddress pa = namer.registerInstructionWithBasicBlock(par); addField("TimeIndex", pa); addField("Operation", instruction->getOpcodeName()); if(instruction->mayWriteToMemory()) addTrueField("MayWriteToMemory"); if(instruction->mayReadFromMemory()) addTrueField("MayReadFromMemory"); if(instruction->mayReadOrWriteMemory()) addTrueField("MayReadOrWriteMemory"); if(instruction->mayHaveSideEffects()) addTrueField("MayHaveSideEffects"); if(instruction->isBinaryOp()) addTrueField("IsBinaryOp"); if(instruction->isTerminator()) addTrueField("IsTerminator"); if(instruction->isShift()) addTrueField("IsShift"); if(instruction->isCast()) addTrueField("IsCast"); if(instruction->isArithmeticShift()) addTrueField("IsArithmeticShift"); if(instruction->isLogicalShift()) addTrueField("IsLogicalShift"); if(instruction->isAssociative()) addTrueField("IsAssociative"); if(instruction->isCommutative()) addTrueField("IsCommutative"); //TODO: insert code to add the potential constant value addField("DestinationRegisters", instruction->getName()); openField("Operands"); //we use pattern matching within CLIPS to our advantage :D for(unsigned i = 0; i < instruction->getNumIncomingValues(); ++i) { Value* target = instruction->getIncomingValue(i); BasicBlock* from = instruction->getIncomingBlock(i); if(isa<UndefValue>(target)) appendValue("undef"); else if(Instruction* inst = dyn_cast<Instruction>(target)) appendValue(inst->getName()); else appendValue(kc->route(target, namer)); appendValue(from->getName()); } closeField(); if(!instruction->use_empty()) { //DenseMap<BasicBlock*,unsigned> counterArgument; //BasicBlock* directParent = instruction->getParent(); //we could have an instruction not used in the block it's created :D openField("Consumers"); for(Value::use_iterator i = instruction->use_begin(), e = instruction->use_end(); i != e; ++i) { User* target = *i; /* //this part was taken from isUsedOutsideOfBlock in llvm::Instruction It has been modified though PHINode* PN = dyn_cast<PHINode>(target); if(PN == 0) { Instruction* j = cast<Instruction>(target); BasicBlock* target = j->getParent(); if(!counterArgument.count(target)) // { std::pair<BasicBlock*,unsigned> pair(target, 1); counterArgument.insert(pair); } else { std::pair<BasicBlock*,unsigned>& pair = counterArgument.FindAndConstruct(target); pair.second = pair.second + 1; } } else { //PHI nodes are evaluated within the basic block denoted in each cell BasicBlock* incBlock = PN->getIncomingBlock(i); if(incBlock != directParent && !counterArgument.count(incBlock)) { std::pair<BasicBlock*,unsigned> pair(incBlock, 1); counterArgument.insert(pair); } else { std::pair<BasicBlock*,unsigned>& pair = counterArgument.FindAndConstruct(incBlock); pair.second = pair.second + 1; } } //end graft */ PointerAddress ptr = (PointerAddress)target; if(namer.pointerRegistered(ptr)) { appendValue(namer.nameFromPointer(ptr)); } else if(isa<Function>(target) || isa<Instruction>(target)) { appendValue(target->getName()); } else { appendValue(kc->route(target, namer)); } } closeField(); /* openField("BlocksUsedIn"); for(DenseMap<BasicBlock*, unsigned>::iterator a = counterArgument.begin(), b = counterArgument.end(); a != b; ++a) { BasicBlock* bb = a->first; appendValue(bb->getName()); //the name of the basic block appendValue(a->second); //the number of iterations } closeField(); */ } }
/******************* FUNCTION *********************/ void JsonState::closeFieldStruct(const char* name) { closeStruct(); closeField(name); }
/** * Close a field array opened by openFieldArray(). * @param name Define the field to terminate (only for checking). **/ void JsonState::closeFieldArray(const char * name) { closeArray(); closeField(name); }