Exemple #1
0
Value *Codegen::Generate(BinaryExprAST *expr) {
	using namespace boost;

	// treat assignment separately
	if (expr->GetOp() == '=') {
		VariableExprAST *identifier = dynamic_cast<VariableExprAST*>(expr->GetLHS());
		if ( !identifier)
			return BaseError::Throw<Value*>("Left hand of assignment must be a variable");

		Value *val = this->Generate(expr->GetRHS());
		if ( !val)
			return BaseError::Throw<Value*>("Invalid assignment value to variable");

		Value *variable = NamedValues[identifier->GetName()];
		if ( !variable)
			return BaseError::Throw<Value*>("Unknown variable, cannot assign");

		Builder.CreateStore(val, variable);
		return val;
	}

	Value *L = this->Generate(expr->GetLHS());
	Value *R = this->Generate(expr->GetRHS());

	if (L == 0 || R == 0)
		return 0;

	switch (expr->GetOp()) {
		case '+':
			return Builder.CreateFAdd(L, R, "addtmp");
		case '-':
			return Builder.CreateFSub(L, R, "subtmp");
		case '*':
			return Builder.CreateFMul(L, R, "multmp");
		case '/':
			return Builder.CreateFDiv(L, R, "divtmp");
		case '<':
			L = Builder.CreateFCmpULT(L, R, "cmptmp");
			return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), "booltmp");
		default:
			break;
	}

	Function *opFunc = TheModule->getFunction("binary" + expr->GetOp());
	if ( !opFunc)
		return BaseError::Throw<Value*>(str(format("Unknown binary operator '%1%'") % expr->GetOp()));

	Value *args[2] = { L, R };
	return Builder.CreateCall(opFunc, args, "binop");
}