ComplexValueTy CodeGenFunction::EmitComplexDivSmiths(ComplexValueTy LHS, ComplexValueTy RHS) { auto ElemTy = RHS.Re->getType(); // if(abs(d) <= abs(c)) then auto FabsIntrinsic = GetIntrinsicFunction(llvm::Intrinsic::fabs, ElemTy); auto Predicate = Builder.CreateFCmpOLE(Builder.CreateCall(FabsIntrinsic, RHS.Im), Builder.CreateCall(FabsIntrinsic, RHS.Re)); auto ThenBlock = createBasicBlock("compdiv-then"); auto ElseBlock = createBasicBlock("compdiv-else"); auto MergeBlock = createBasicBlock("compdiv-done"); Builder.CreateCondBr(Predicate, ThenBlock, ElseBlock); llvm::Value *R, *Den, *E, *F; auto ResultRe = llvm::PHINode::Create(ElemTy, 2, "compdiv-re"); auto ResultIm = llvm::PHINode::Create(ElemTy, 2, "compdiv-im"); // r = d / c // den = c + d * r // e = (a + b * r) / den // f = (b - a * r) / den EmitBlock(ThenBlock); R = Builder.CreateFDiv(RHS.Im, RHS.Re); Den = Builder.CreateFAdd(RHS.Re, Builder.CreateFMul(RHS.Im, R)); E = Builder.CreateFDiv(Builder.CreateFAdd(LHS.Re, Builder.CreateFMul(LHS.Im, R)), Den); F = Builder.CreateFDiv(Builder.CreateFSub(LHS.Im, Builder.CreateFMul(LHS.Re, R)), Den); ResultRe->addIncoming(E, ThenBlock); ResultIm->addIncoming(F, ThenBlock); EmitBranch(MergeBlock); // r = c / d // den = c * r + d // e = (a * r + b) / den // f = (b * r - a) / den EmitBlock(ElseBlock); R = Builder.CreateFDiv(RHS.Re, RHS.Im); Den = Builder.CreateFAdd(Builder.CreateFMul(RHS.Re, R), RHS.Im); E = Builder.CreateFDiv(Builder.CreateFAdd( Builder.CreateFMul(LHS.Re, R), LHS.Im), Den); F = Builder.CreateFDiv(Builder.CreateFSub( Builder.CreateFMul(LHS.Im, R), LHS.Re), Den); ResultRe->addIncoming(E, ElseBlock); ResultIm->addIncoming(F, ElseBlock); EmitBranch(MergeBlock); EmitBlock(MergeBlock); Builder.Insert(ResultRe); Builder.Insert(ResultIm); return ComplexValueTy(ResultRe, ResultIm); }
BasicBlock *TacBuilder::createBne(Value *lhs, Value *rhs, BasicBlock *True) { TacBasicBlock *falseBlock = (TacBasicBlock *)createBasicBlock(); TacValue *lhsValue = (TacValue *)lhs; TacValue *rhsValue = (TacValue *)rhs; m_insertBlock->code << "\t\t" << "NE" << "\t" << lhsValue->value() << "\t" << rhsValue->value() << "\t" << ((TacBasicBlock *)True)->name << std::endl; m_insertBlock->code << "\t\t" << "GOTO" << "\t" << falseBlock->name << std::endl; return falseBlock; }
Function::Function(Module& pModule, llvm::Function& function, const ArgInfo& argInfo, TemplateBuilder* pTemplateBuilder) : module_(pModule), function_(function), entryBuilder_(pModule.getLLVMContext()), builder_(pModule.getLLVMContext()), createdEntryBlock_(false), useEntryBuilder_(false), argInfo_(argInfo), templateBuilder_(pTemplateBuilder), #if LOCIC_LLVM_VERSION < 307 debugInfo_(nullptr), #endif exceptionInfo_(nullptr), returnValuePtr_(nullptr), templateArgs_(nullptr), unwindState_(nullptr) { assert(function.isDeclaration()); assert(argInfo_.numArguments() == function_.getFunctionType()->getNumParams()); // Add a bottom level unwind stack. unwindStackStack_.push(UnwindStack()); // Add bottom level action for this function. unwindStack().push_back(UnwindAction::FunctionMarker()); const auto startBB = createBasicBlock(""); builder_.SetInsertPoint(startBB); argValues_.reserve(function_.arg_size()); for (auto arg = function_.arg_begin(); arg != function_.arg_end(); ++arg) { argValues_.push_back(arg); } std::vector<llvm_abi::Type*> argABITypes; argABITypes.reserve(argInfo.argumentTypes().size()); std::vector<llvm::Type*> argLLVMTypes; argLLVMTypes.reserve(argInfo.argumentTypes().size()); for (const auto& typePair : argInfo.argumentTypes()) { argABITypes.push_back(typePair.first); argLLVMTypes.push_back(typePair.second); } SetUseEntryBuilder useEntryBuilder(*this); // Decode arguments according to ABI. decodeABIValues(argValues_, argABITypes, argLLVMTypes); }
BasicBlock *Program::getBasicBlockForInstruction(const arch::Instruction *instruction) { /* If there is a basic block that starts here, just take it. */ BasicBlock *result = getBasicBlockStartingAt(instruction->addr()); if (!result) { /* Maybe this instruction stands next to an existing basic block? */ result = getBasicBlockCovering(instruction->addr() - 1); /* No way? Create new block. */ if (!result) { result = createBasicBlock(instruction->addr()); } } removeRange(result); result->setSuccessorAddress(instruction->endAddr()); addRange(result); return result; }
/* ------------------------------------------------------------------------------ */ void readInstructionsFromFile(char *fname){ int i; ins *ip; FILE *fp; char tk[4]={' ', '\t', '\n', '\0'}; char str[80]; char *s1, *s2, *s3, *s4, *s5; i = 0; fp = fopen(fname,"r"); if(fp == NULL){ printf(" *** Error: Cannot open (%s) as instruction input file \n",fname); Code = NULL; return; } Code = createBasicBlock(0); while(fgets(str,79,fp) != NULL){ #ifdef DEBUG // printf("read::: %s", str); #endif s1 = (char*)strtok(str, tk); if(s1 == NULL){ break; } s2 = (char*)strtok(NULL, tk); if(s2[0] != '='){ printf(" *** Error: Could not locate '=' operator in instruction as second field of input\n"); continue; } s3 = (char*)strtok(NULL, tk); s4 = (char*)strtok(NULL, tk); if(s4 != NULL){ s5 = (char*)strtok(NULL, tk); } else { s5 = NULL; } #ifdef DEBUG // printf(" parsed::: %s %s %s %s %s\n",s1, s2, s3, s4, s5); #endif ip = createInstruction(i); if(s1[0] == '*'){ setInstructionDestOperand(ip,'*'); setInstructionDest(ip, &s1[1]); } else { setInstructionDest(ip, &s1[0]); } if((s3[0] == '*') || (s3[0] == '-')){ setInstructionSource1Operand(ip, s3[0]); setInstructionSource1(ip, &s3[1]); } else { setInstructionSource1(ip, &s3[0]); } if(s4 != NULL){ setInstructionSource2Operand(ip, s4[0]); if(s5 != NULL){ setInstructionSource2(ip, s5); } else { printf(" *** Error: Was expecting a second operand in instruction %d (remove infix operand)\n",i); setInstructionSource2Operand(ip,' '); } } appendInstruction(Code,ip); #ifdef DEBUG // printf(" ...dumping instruction::\n"); // dumpInstruction(ip); // printf("\n"); #endif i++; } #ifdef DEBUG // printf(" Read %d instructions \n",i); // printBasicBlock(Code); #endif fclose(fp); }
/* ------------------------------------------------------------------------------ */ void initDataStructures(){ NumberLocalVariables = 0; LocalVariableMap = createVarMaps(MAX_NUM_LOCAL_VARIABLES); Output = createBasicBlock(0); idx = 0; }