simple constexpr

This commit is contained in:
Philippe Tillet
2019-08-05 13:06:56 -07:00
parent d869d9a924
commit 899b2b72e1
9 changed files with 96 additions and 21 deletions

View File

@@ -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) { }