示例#1
0
Value* NotEqualExpression::getValue() {
    assert(leftExpression != NULL);
    assert(rightExpression != NULL);
    assert(leftExpression->getType() == rightExpression->getType());

    IRBuilder<>* builder = codegen::getBuilder();
    int c;
    if(leftExpression->getType() != COLEXPRESSION) {
        c = leftExpression->getType();
    } else {
        c = convertDTtoET(leftExpression->getDataType());
    }
    switch(c) {
        case LONGVALUEEXPRESSION:
            return builder->CreateICmpNE(leftExpression->getValue(), rightExpression->getValue());
        case DOUBLEVALUEEXPRESSION:
            return builder->CreateFCmpONE(leftExpression->getValue(), rightExpression->getValue());
        case STRINGVALUEEXPRESSION:
        case DATEVALUEEXPRESSION:
            return codegen::stringCmp(leftExpression->getValue(), rightExpression->getValue(), NOTEQUALEXPRESSION);
        default:
            cout << "Unknown expression type!" << endl;
            exit(-1);
    }

    return NULL;
}
示例#2
0
void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
                                         IRBuilder<> &IRB, Value *Addr,
                                         uint32_t TypeSize, bool IsWrite) {
  Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);

  Type *ShadowTy  = IntegerType::get(
      *C, std::max(8U, TypeSize >> MappingScale));
  Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
  Value *ShadowPtr = memToShadow(AddrLong, IRB);
  Value *CmpVal = Constant::getNullValue(ShadowTy);
  Value *ShadowValue = IRB.CreateLoad(
      IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));

  Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);

  Instruction *CheckTerm = splitBlockAndInsertIfThen(
      cast<Instruction>(Cmp)->getNextNode(), Cmp);
  IRBuilder<> IRB2(CheckTerm);

  size_t Granularity = 1 << MappingScale;
  if (TypeSize < 8 * Granularity) {
    // Addr & (Granularity - 1)
    Value *Lower3Bits = IRB2.CreateAnd(
        AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
    // (Addr & (Granularity - 1)) + size - 1
    Value *LastAccessedByte = IRB2.CreateAdd(
        Lower3Bits, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
    // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
    LastAccessedByte = IRB2.CreateIntCast(
        LastAccessedByte, IRB.getInt8Ty(), false);
    // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
    Value *Cmp2 = IRB2.CreateICmpSGE(LastAccessedByte, ShadowValue);

    CheckTerm = splitBlockAndInsertIfThen(CheckTerm, Cmp2);
  }

  IRBuilder<> IRB1(CheckTerm);
  Instruction *Crash = generateCrashCode(IRB1, AddrLong, IsWrite, TypeSize);
  Crash->setDebugLoc(OrigIns->getDebugLoc());
  ReplaceInstWithInst(CheckTerm, new UnreachableInst(*C));
}
void AddressSanitizer::instrumentAddress(AsanFunctionContext &AFC,
                                         Instruction *OrigIns,
                                         IRBuilder<> &IRB, Value *Addr,
                                         uint32_t TypeSize, bool IsWrite) {
  Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);

  Type *ShadowTy  = IntegerType::get(
      *C, std::max(8U, TypeSize >> MappingScale));
  Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
  Value *ShadowPtr = memToShadow(AddrLong, IRB);
  Value *CmpVal = Constant::getNullValue(ShadowTy);
  Value *ShadowValue = IRB.CreateLoad(
      IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));

  Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
  size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
  size_t Granularity = 1 << MappingScale;
  TerminatorInst *CrashTerm = 0;

  if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
    TerminatorInst *CheckTerm = splitBlockAndInsertIfThen(Cmp, false);
    assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
    BasicBlock *NextBB = CheckTerm->getSuccessor(0);
    IRB.SetInsertPoint(CheckTerm);
    Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
    BasicBlock *CrashBlock = BasicBlock::Create(*C, "", &AFC.F, NextBB);
    CrashTerm = new UnreachableInst(*C, CrashBlock);
    BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
    ReplaceInstWithInst(CheckTerm, NewTerm);
  } else {
    CrashTerm = splitBlockAndInsertIfThen(Cmp, true);
  }

  Instruction *Crash =
      generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex);
  Crash->setDebugLoc(OrigIns->getDebugLoc());
}
示例#4
0
		static Value* emitCond(IRBuilder<>& builder, Value* arg) {
			return builder.CreateICmpNE(builder.CreateAnd(arg, builder.getInt64(1)), builder.getInt64(0));
		}
示例#5
0
文件: llvm.cpp 项目: WoodMath/mathmap
void
code_emitter::emit_stmts (statement_t *stmt, unsigned int slice_flag)
{
    for (; stmt != NULL; stmt = stmt->next)
    {
	if (!must_emit_stmt(stmt, slice_flag))
	    continue;

	switch (stmt->kind)
	{
	    case STMT_NIL :
		g_assert(slice_flag == SLICE_IGNORE);
		break;

	    case STMT_ASSIGN :
#ifdef DEBUG_OUTPUT
		compiler_print_assign_statement(stmt);
		printf("\n");
#endif
		if (stmt->v.assign.rhs->kind == RHS_OP
		    && stmt->v.assign.rhs->v.op.op->index == OP_OUTPUT_TUPLE)
		    builder->CreateRet(emit_primary(&stmt->v.assign.rhs->v.op.args[0]));
		else
		    set_value(stmt->v.assign.lhs, emit_rhs(stmt->v.assign.rhs));
		break;

	    case STMT_IF_COND :
		{
		    Value *condition_number = emit_rhs(stmt->v.if_cond.condition);
		    Value *condition;
		    map<rhs_t*, Value*> rhs_map;

		    if (condition_number->getType() == Type::Int32Ty)
			condition = builder->CreateICmpNE(condition_number, make_int_const(0));
		    else if (condition_number->getType() == Type::FloatTy)
			condition = builder->CreateFCmpONE(condition_number, make_float_const(0.0));
		    else
			g_assert_not_reached();

		    BasicBlock *then_bb = BasicBlock::Create("then", current_function);
		    BasicBlock *else_bb = BasicBlock::Create("else");
		    BasicBlock *merge_bb = BasicBlock::Create("ifcont");

		    builder->CreateCondBr(condition, then_bb, else_bb);

		    builder->SetInsertPoint(then_bb);
		    emit_stmts(stmt->v.if_cond.consequent, slice_flag);
		    emit_phi_rhss(stmt->v.if_cond.exit, true, &rhs_map, slice_flag);
		    builder->CreateBr(merge_bb);
		    then_bb = builder->GetInsertBlock();

		    current_function->getBasicBlockList().push_back(else_bb);
		    builder->SetInsertPoint(else_bb);
		    emit_stmts(stmt->v.if_cond.alternative, slice_flag);
		    emit_phi_rhss(stmt->v.if_cond.exit, false, &rhs_map, slice_flag);
		    builder->CreateBr(merge_bb);
		    else_bb = builder->GetInsertBlock();

		    current_function->getBasicBlockList().push_back(merge_bb);
		    builder->SetInsertPoint(merge_bb);

		    emit_phis(stmt->v.if_cond.exit, then_bb, else_bb, rhs_map, slice_flag);
		}
		break;

	    case STMT_WHILE_LOOP:
		{
		    BasicBlock *start_bb = builder->GetInsertBlock();
		    BasicBlock *entry_bb = BasicBlock::Create("entry", current_function);
		    BasicBlock *body_bb = BasicBlock::Create("body");
		    BasicBlock *exit_bb = BasicBlock::Create("exit");
		    map<rhs_t*, Value*> rhs_map;

		    emit_phi_rhss(stmt->v.while_loop.entry, true, &rhs_map, slice_flag);

		    builder->CreateBr(entry_bb);

		    builder->SetInsertPoint(entry_bb);

		    emit_phis(stmt->v.while_loop.entry, start_bb, NULL, rhs_map, slice_flag);

		    Value *invariant_number = emit_rhs(stmt->v.while_loop.invariant);
		    Value *invariant;

		    if (invariant_number->getType() == Type::Int32Ty)
			invariant = builder->CreateICmpNE(invariant_number, make_int_const(0));
		    else if (invariant_number->getType() == Type::FloatTy)
			invariant = builder->CreateFCmpONE(invariant_number, make_float_const(0.0));
		    else
			g_assert_not_reached();

		    builder->CreateCondBr(invariant, body_bb, exit_bb);

		    current_function->getBasicBlockList().push_back(body_bb);
		    builder->SetInsertPoint(body_bb);
		    emit_stmts(stmt->v.while_loop.body, slice_flag);
		    body_bb = builder->GetInsertBlock();
		    emit_phi_rhss(stmt->v.while_loop.entry, false, &rhs_map, slice_flag);
		    emit_phis(stmt->v.while_loop.entry, NULL, body_bb, rhs_map, slice_flag);
		    builder->CreateBr(entry_bb);

		    current_function->getBasicBlockList().push_back(exit_bb);
		    builder->SetInsertPoint(exit_bb);
		}
		break;

	    default:
		g_assert_not_reached();
		break;
	}
    }
}