20 default: std::unreachable();
26 type->blame(
"type `{}` has no static size or alignment", type).bail();
31 auto fields = sigma->projs();
33 sigma->blame(
"type `{}` has no static size or alignment", sigma).bail();
38bool Legalize::is_value(
const Def* args) {
39 for (
auto arg :
args->projs()) {
41 if (res->type()->isa<Nat>() && !
Lit::isa(res))
return false;
46const Def* Legalize::val(
const Def* def) {
53const Def* Legalize::index(
const Def* i) {
64 if (
auto br =
Branch(body)) body = split(br.cond(), br.ff(), br.tt(), br.arg());
69const Def* Legalize::split(
const Def* cond,
const Def* ff,
const Def* tt,
const Def* arg) {
73 auto [a, b] = bit2->args<2>();
74 auto rest = w.mut_con(arg->
type());
76 auto wrap = [&](
const Def* f) {
77 auto eta = w.mut_lam(f->type()->as<
Pi>());
78 return eta->set(
false, w.app<
false>(f, eta->var()));
81 rest->set(
false, split(b, ff, wrap(tt), rest->var()));
82 return split(a, rest, wrap(tt), arg);
84 rest->set(
false, split(b, wrap(ff), tt, rest->var()));
85 return split(a, wrap(ff), rest, arg);
87 return w.app(w.select(cond, tt, ff), arg);
97 if (
auto l =
Lit::isa(val); l && type == w.type_i64() && val->type()->isa<
Nat>())
return w.lit_i64(*l);
104 auto ops =
DefVec(
tuple->ops(), [
this](
const Def* op) { return val(op); });
116 for (
auto arg : app->
args())
118 arg->
blame(
"native backend: argument `{}` of `{}` is no value", arg, decl).bail();
121 auto [p, i] = lea->args<2>();
124 const Def* off =
nullptr;
125 if (!pointee->isa<
Arr>()) {
126 if (!l) i->blame(
"index `{}` into `{}` must be a literal", i, pointee).bail();
127 off = w.lit_i64(field_offset(pointee, *l));
129 off = w.lit_i64(*l * size);
138 auto [T, a] =
load->decurry()->args<2>();
139 auto [m, p] =
load->args<2>();
145 auto [m, p, x] =
store->args<3>();
154 auto [msz, k] = mslot->args<2>();
155 auto [m, size] = msz->projs<2>();
158 if (!n) app->
blame(
"native backend: `mem.mslot` needs a literal size").bail();
163 auto [a, b] = nat->args<2>([
this](
const Def* def) {
return val(def); });
165 auto m = w.bot(w.call<
core::M>(w.lit_nat_0()));
166 return w.call(op,
Defs{m, w.tuple({a, b})})->proj(2, 1);
181 auto [a, b] = ncmp->args<2>([
this](
const Def* def) {
return val(def); });
182 return w.call(unsigned_cmp(ncmp.id()),
Defs{a, b});
191 auto type =
rewrite(cast->type());
192 auto x = val(cast->arg());
193 return x->type() == type ? x : w.call<
core::bitcast>(type, x);
auto args(Projector auto f) const noexcept
const Def * callee() const
A (possibly paramterized) Array.
static auto isa(const Def *def)
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
static std::optional< nat_t > isa_lit(const Def *def)
static std::optional< T > isa(const Def *def)
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
A dependent function type.
World & new_world()
Create new Defs into this.
virtual std::array< const Def *, 2 > rewrite_filter_body(Lam *old_lam, Lam *)
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
const Def * rewrite_imm_App(const App *) override
virtual const Def * rewrite_imm_Extract(const Extract *)
virtual const Def * rewrite(const Def *)
virtual const Def * rewrite_imm_Lit(const Lit *)
Data constructor for a Sigma.
const Def * call(const Def *callee, auto &&arg, Args &&... args)
const Def * extract(const Def *d, const Def *i)
const Lit * lit_nat(nat_t a)
std::array< const Def *, 2 > rewrite_filter_body(Lam *, Lam *) final
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
const Def * rewrite_imm_Lit(const Lit *) final
const Def * rewrite_imm_Extract(const Extract *) final
const Def * rewrite_imm_App(const App *) final
const Def * coerce_leaf(const Def *, const Def *) final
Converts the new leaf new_val into the new new_type, whose representation this phase changed.
std::optional< nat_t > isa_odd_idx(const Def *type)
The literal size of the Idx type if it is no Bool, I8, I16, I32, or I64.
std::optional< Layout > layout(Defs fields, bool overlap=false)
The Layout of fields, unless one of them has no literal size or alignment.
const Def * zext(World &w, const Def *x)
Zero-extends x to I64.
const Def * op(trait o, const Def *type)
void check_addr_space(const App *app, const Def *a)
Bails unless the address space a of the mem operation app is 0.
const Def * pointee(const Def *ptr)
fe::View< const Def * > Defs
fe::Vector< const Def * > DefVec