simple constexpr
This commit is contained in:
@@ -127,6 +127,42 @@ metaparameter* metaparameter::create(context &ctx, type *ty, const std::vector<u
|
||||
return result;
|
||||
}
|
||||
|
||||
// constant expression
|
||||
constant_expression::constant_expression(op_t op, constant_int* lhs, constant_int* rhs)
|
||||
: constant_int(lhs->get_type(), 0),
|
||||
op_(op), lhs_(lhs), rhs_(rhs) { }
|
||||
|
||||
|
||||
constant_expression *constant_expression::create(op_t op, constant_int* lhs, constant_int* rhs) {
|
||||
context_impl *impl = lhs->get_type()->get_context().p_impl.get();
|
||||
constant_expression *& result = impl->expr_constants_[std::make_tuple((int)op, lhs, rhs)];
|
||||
if(!result)
|
||||
result = new constant_expression(op, lhs, rhs);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint64_t constant_expression::get_value() const {
|
||||
uint64_t lhs = lhs_->get_value();
|
||||
uint64_t rhs = rhs_->get_value();
|
||||
switch(op_) {
|
||||
case llop::Add : return lhs + rhs;
|
||||
case llop::Sub : return lhs - rhs;
|
||||
case llop::Mul : return lhs * rhs;
|
||||
case llop::UDiv : return lhs / rhs;
|
||||
case llop::SDiv : return lhs / rhs;
|
||||
case llop::URem : return lhs % rhs;
|
||||
case llop::SRem : return lhs % rhs;
|
||||
case llop::Shl : return lhs << rhs;
|
||||
case llop::LShr : return lhs >> rhs;
|
||||
case llop::AShr : return lhs >> rhs;
|
||||
case llop::And : return lhs && rhs;
|
||||
case llop::Or : return lhs || rhs;
|
||||
case llop::Xor : return lhs ^ rhs;
|
||||
default: throw std::runtime_error("unsupported constexpr binary operator");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// undef value
|
||||
undef_value::undef_value(type *ty)
|
||||
: constant(ty, 0) { }
|
||||
|
Reference in New Issue
Block a user