MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
ll.cpp
Go to the documentation of this file.
2
3#include <deque>
4#include <fstream>
5#include <iomanip>
6#include <ranges>
7
8#include <absl/container/btree_set.h>
9
10#include <mim/plug/clos/clos.h>
11#include <mim/plug/math/math.h>
12#include <mim/plug/mem/mem.h>
13
14#include "mim/be/emitter.h"
15#include "mim/util/print.h"
16#include "mim/util/sys.h"
17
18#include "mim/plug/core/core.h"
19
20// Lessons learned:
21// * **Always** follow all ops - even if you actually want to ignore one.
22// Otherwise, you might end up with an incorrect schedule.
23// This was the case for an Extract of type Mem.
24// While we want to ignore the value obtained from that, since there is no Mem value in LLVM,
25// we still want to **first** recursively emit code for its operands and **then** ignore the Extract itself.
26// * i1 has a different meaning in LLVM then in Mim:
27// * Mim: {0, 1} = i1
28// * LLVM: {0, -1} = i1
29// This is a problem when, e.g., using an index of type i1 as LLVM thinks like this:
30// getelementptr ..., i1 1 == getelementptr .., i1 -1
31using namespace std::string_literals;
32
33namespace mim::ll {
34
35namespace clos = mim::plug::clos;
36namespace core = mim::plug::core;
37namespace math = mim::plug::math;
38namespace mem = mim::plug::mem;
39
40namespace {
41bool is_const(const Def* def) {
42 if (def->isa<Bot>()) return true;
43 if (def->isa<Lit>()) return true;
44 if (auto pack = def->isa_imm<Pack>()) return is_const(pack->arity()) && is_const(pack->body());
45
46 if (auto tuple = def->isa<Tuple>()) {
47 auto ops = tuple->ops();
48 return std::ranges::all_of(ops, [](auto def) { return is_const(def); });
49 }
50
51 return false;
52}
53
54const char* math_suffix(const Def* type) {
55 if (auto w = math::isa_f(type)) {
56 switch (*w) {
57 case 32: return "f";
58 case 64: return "";
59 }
60 }
61 error("unsupported foating point type '{}'", type);
62}
63
64const char* llvm_suffix(const Def* type) {
65 if (auto w = math::isa_f(type)) {
66 switch (*w) {
67 case 16: return ".f16";
68 case 32: return ".f32";
69 case 64: return ".f64";
70 }
71 }
72 error("unsupported foating point type '{}'", type);
73}
74
75// [%mem.M 0, T] => T
76// TODO there may be more instances where we have to deal with this trickery
77const Def* isa_mem_sigma_2(const Def* type) {
78 if (auto sigma = type->isa<Sigma>())
79 if (sigma->num_ops() == 2 && Axm::isa<mem::M>(sigma->op(0))) return sigma->op(1);
80 return {};
81}
82} // namespace
83
84struct BB {
85 BB() = default;
86 BB(const BB&) = delete;
87 BB(BB&& other) noexcept = default;
88 BB& operator=(BB other) noexcept { return swap(*this, other), *this; }
89
90 std::deque<std::ostringstream>& head() { return parts[0]; }
91 std::deque<std::ostringstream>& body() { return parts[1]; }
92 std::deque<std::ostringstream>& tail() { return parts[2]; }
93
94 template<class... Args>
95 std::string assign(std::string_view name, const char* s, Args&&... args) {
96 print(print(body().emplace_back(), "{} = ", name), s, std::forward<Args>(args)...);
97 return std::string(name);
98 }
99
100 template<class... Args>
101 void tail(const char* s, Args&&... args) {
102 print(tail().emplace_back(), s, std::forward<Args>(args)...);
103 }
104
105 friend void swap(BB& a, BB& b) noexcept {
106 using std::swap;
107 swap(a.phis, b.phis);
108 swap(a.parts, b.parts);
109 }
110
112 std::array<std::deque<std::ostringstream>, 3> parts;
113};
114
115class Emitter : public mim::Emitter<std::string, std::string, BB, Emitter> {
116public:
118
119 Emitter(World& world, std::ostream& ostream)
120 : Super(world, "llvm_emitter", ostream) {}
121
122 bool is_valid(std::string_view s) { return !s.empty(); }
123 void start() override;
124 void emit_imported(Lam*);
125 void emit_epilogue(Lam*);
126 std::string emit_bb(BB&, const Def*);
127 std::string prepare();
128 void finalize();
129
130 template<class... Args>
131 void declare(const char* s, Args&&... args) {
132 std::ostringstream decl;
133 print(decl << "declare ", s, std::forward<Args>(args)...);
134 decls_.emplace(decl.str());
135 }
136
137private:
138 std::string id(const Def*, bool force_bb = false) const;
139 std::string convert(const Def*);
140 std::string convert_ret_pi(const Pi*);
141
142 absl::btree_set<std::string> decls_;
143 std::ostringstream type_decls_;
144 std::ostringstream vars_decls_;
145 std::ostringstream func_decls_;
146 std::ostringstream func_impls_;
147};
148
149/*
150 * convert
151 */
152
153std::string Emitter::id(const Def* def, bool force_bb /*= false*/) const {
154 if (auto global = def->isa<Global>()) return "@" + global->unique_name();
155
156 if (auto lam = def->isa_mut<Lam>(); lam && !force_bb) {
157 if (lam->type()->ret_pi()) {
158 if (lam->is_external() || !lam->is_set())
159 return "@"s + lam->sym().str(); // TODO or use is_internal or sth like that?
160 return "@"s + lam->unique_name();
161 }
162 }
163
164 return "%"s + def->unique_name();
165}
166
167std::string Emitter::convert(const Def* type) {
168 if (auto i = types_.find(type); i != types_.end()) return i->second;
169
170 assert(!Axm::isa<mem::M>(type));
171 std::ostringstream s;
172 std::string name;
173
174 if (type->isa<Nat>()) {
175 return types_[type] = "i64";
176 } else if (auto size = Idx::isa(type)) {
177 return types_[type] = "i" + std::to_string(*Idx::size2bitwidth(size));
178 } else if (auto w = math::isa_f(type)) {
179 switch (*w) {
180 case 16: return types_[type] = "half";
181 case 32: return types_[type] = "float";
182 case 64: return types_[type] = "double";
183 default: fe::unreachable();
184 }
185 } else if (auto ptr = Axm::isa<mem::Ptr>(type)) {
186 auto [pointee, addr_space] = ptr->args<2>();
187 // TODO addr_space
188 print(s, "{}*", convert(pointee));
189 } else if (auto arr = type->isa<Arr>()) {
190 auto t_elem = convert(arr->body());
191 u64 size = 0;
192 if (auto arity = Lit::isa(arr->arity())) size = *arity;
193 print(s, "[{} x {}]", size, t_elem);
194 } else if (auto pi = type->isa<Pi>()) {
195 assert(Pi::isa_returning(pi) && "should never have to convert type of BB");
196 print(s, "{} (", convert_ret_pi(pi->ret_pi()));
197
198 if (auto t = isa_mem_sigma_2(pi->dom()))
199 s << convert(t);
200 else {
201 auto doms = pi->doms();
202 for (auto sep = ""; auto dom : doms.view().rsubspan(1)) {
203 if (Axm::isa<mem::M>(dom)) continue;
204 s << sep << convert(dom);
205 sep = ", ";
206 }
207 }
208 s << ")*";
209 } else if (auto t = isa_mem_sigma_2(type)) {
210 return convert(t);
211 } else if (auto sigma = type->isa<Sigma>()) {
212 if (sigma->isa_mut()) {
213 name = id(sigma);
214 types_[sigma] = name;
215 print(s, "{} = type", name);
216 }
217
218 print(s, "{{");
219 for (auto sep = ""; auto t : sigma->ops()) {
220 if (Axm::isa<mem::M>(t)) continue;
221 s << sep << convert(t);
222 sep = ", ";
223 }
224 print(s, "}}");
225 } else {
226 fe::unreachable();
227 }
228
229 if (name.empty()) return types_[type] = s.str();
230
231 assert(!s.str().empty());
232 type_decls_ << s.str() << '\n';
233 return types_[type] = name;
234}
235
236std::string Emitter::convert_ret_pi(const Pi* pi) {
237 auto dom = mem::strip_mem_ty(pi->dom());
238 if (dom == world().sigma()) return "void";
239 return convert(dom);
240}
241
242/*
243 * emit
244 */
245
247 Super::start();
248
249 ostream() << type_decls_.str() << '\n';
250 for (auto&& decl : decls_)
251 ostream() << decl << '\n';
252 ostream() << func_decls_.str() << '\n';
253 ostream() << vars_decls_.str() << '\n';
254 ostream() << func_impls_.str() << '\n';
255}
256
258 // TODO merge with declare method
259 print(func_decls_, "declare {} {}(", convert_ret_pi(lam->type()->ret_pi()), id(lam));
260
261 auto doms = lam->doms();
262 for (auto sep = ""; auto dom : doms.view().rsubspan(1)) {
263 if (Axm::isa<mem::M>(dom)) continue;
264 print(func_decls_, "{}{}", sep, convert(dom));
265 sep = ", ";
266 }
267
268 print(func_decls_, ")\n");
269}
270
271std::string Emitter::prepare() {
272 print(func_impls_, "define {} {}(", convert_ret_pi(root()->type()->ret_pi()), id(root()));
273
274 auto vars = root()->vars();
275 for (auto sep = ""; auto var : vars.view().rsubspan(1)) {
276 if (Axm::isa<mem::M>(var->type())) continue;
277 auto name = id(var);
278 locals_[var] = name;
279 print(func_impls_, "{}{} {}", sep, convert(var->type()), name);
280 sep = ", ";
281 }
282
283 print(func_impls_, ") {{\n");
284 return root()->unique_name();
285}
286
288 for (auto& [lam, bb] : lam2bb_) {
289 for (const auto& [phi, args] : bb.phis) {
290 print(bb.head().emplace_back(), "{} = phi {} ", id(phi), convert(phi->type()));
291 for (auto sep = ""; const auto& [arg, pred] : args) {
292 print(bb.head().back(), "{}[ {}, {} ]", sep, arg, pred);
293 sep = ", ";
294 }
295 }
296 }
297
298 for (auto mut : Scheduler::schedule(nest())) {
299 if (auto lam = mut->isa_mut<Lam>()) {
300 assert(lam2bb_.contains(lam));
301 auto& bb = lam2bb_[lam];
302 print(func_impls_, "{}:\n", lam->unique_name());
303
304 ++tab;
305 for (const auto& part : bb.parts)
306 for (const auto& line : part)
307 tab.print(func_impls_, "{}\n", line.str());
308 --tab;
309 func_impls_ << std::endl;
310 }
311 }
312
313 print(func_impls_, "}}\n\n");
314}
315
317 auto app = lam->body()->as<App>();
318 auto& bb = lam2bb_[lam];
319
320 if (app->callee() == root()->ret_var()) { // return
321 std::vector<std::string> values;
322 std::vector<const Def*> types;
323
324 for (auto arg : app->args()) {
325 if (auto val = emit_unsafe(arg); !val.empty()) {
326 values.emplace_back(val);
327 types.emplace_back(arg->type());
328 }
329 }
330
331 switch (values.size()) {
332 case 0: return bb.tail("ret void");
333 case 1: return bb.tail("ret {} {}", convert(types[0]), values[0]);
334 default: {
335 std::string prev = "undef";
336 auto type = convert(world().sigma(types));
337 for (size_t i = 0, n = values.size(); i != n; ++i) {
338 auto v_elem = values[i];
339 auto t_elem = convert(types[i]);
340 auto namei = "%ret_val." + std::to_string(i);
341 bb.tail("{} = insertvalue {} {}, {} {}, {}", namei, type, prev, t_elem, v_elem, i);
342 prev = namei;
343 }
344
345 bb.tail("ret {} {}", type, prev);
346 }
347 }
348 } else if (auto dispatch = Dispatch(app)) {
349 for (auto callee : dispatch.tuple()->projs([](const Def* def) { return def->isa_mut<Lam>(); })) {
350 size_t n = callee->num_tvars();
351 for (size_t i = 0; i != n; ++i) {
352 if (auto arg = emit_unsafe(app->arg(n, i)); !arg.empty()) {
353 auto phi = callee->var(n, i);
354 assert(!Axm::isa<mem::M>(phi->type()));
355 lam2bb_[callee].phis[phi].emplace_back(arg, id(lam, true));
356 locals_[phi] = id(phi);
357 }
358 }
359 }
360
361 auto v_index = emit(dispatch.index());
362 size_t n = dispatch.num_targets();
363 auto bbs = absl::FixedArray<std::string>(n);
364 for (size_t i = 0; i != n; ++i)
365 bbs[i] = emit(dispatch.target(i));
366
367 if (auto branch = Branch(app)) return bb.tail("br i1 {}, label {}, label {}", v_index, bbs[1], bbs[0]);
368
369 auto t_index = convert(dispatch.index()->type());
370 bb.tail("switch {} {}, label {} [ ", t_index, v_index, bbs[0]);
371 for (size_t i = 1; i != n; ++i)
372 print(bb.tail().back(), "{} {}, label {} ", t_index, std::to_string(i), bbs[i]);
373 print(bb.tail().back(), "]");
374 } else if (app->callee()->isa<Bot>()) {
375 return bb.tail("ret ; bottom: unreachable");
376 } else if (auto callee = Lam::isa_mut_basicblock(app->callee())) { // ordinary jump
377 size_t n = callee->num_tvars();
378 for (size_t i = 0; i != n; ++i) {
379 if (auto arg = emit_unsafe(app->arg(n, i)); !arg.empty()) {
380 auto phi = callee->var(n, i);
381 assert(!Axm::isa<mem::M>(phi->type()));
382 lam2bb_[callee].phis[phi].emplace_back(arg, id(lam, true));
383 locals_[phi] = id(phi);
384 }
385 }
386 return bb.tail("br label {}", id(callee));
387 } else if (auto longjmp = Axm::isa<clos::longjmp>(app)) {
388 declare("void @longjmp(i8*, i32) noreturn");
389
390 auto [mem, jbuf, tag] = app->args<3>();
391 emit_unsafe(mem);
392 auto v_jb = emit(jbuf);
393 auto v_tag = emit(tag);
394 bb.tail("call void @longjmp(i8* {}, i32 {})", v_jb, v_tag);
395 return bb.tail("unreachable");
396 } else if (Pi::isa_returning(app->callee_type())) { // function call
397 auto v_callee = emit(app->callee());
398
399 std::vector<std::string> args;
400 auto app_args = app->args();
401 for (auto arg : app_args.view().rsubspan(1))
402 if (auto v_arg = emit_unsafe(arg); !v_arg.empty()) args.emplace_back(convert(arg->type()) + " " + v_arg);
403
404 if (app->args().back()->isa<Bot>()) {
405 // TODO: Perhaps it'd be better to simply η-wrap this prior to the BE...
406 assert(convert_ret_pi(app->callee_type()->ret_pi()) == "void");
407 bb.tail("call void {}({, })", v_callee, args);
408 return bb.tail("unreachable");
409 }
410
411 auto ret_lam = app->args().back()->as_mut<Lam>();
412 size_t num_vars = ret_lam->num_vars();
413 size_t n = 0;
414 DefVec values(num_vars);
415 DefVec types(num_vars);
416 for (auto var : ret_lam->vars()) {
417 if (Axm::isa<mem::M>(var->type())) continue;
418 values[n] = var;
419 types[n] = var->type();
420 ++n;
421 }
422
423 if (n == 0) {
424 bb.tail("call void {}({, })", v_callee, args);
425 } else {
426 auto name = "%" + app->unique_name() + "ret";
427 auto t_ret = convert_ret_pi(ret_lam->type());
428 bb.tail("{} = call {} {}({, })", name, t_ret, v_callee, args);
429
430 for (size_t i = 0, j = 0, e = ret_lam->num_vars(); i != e; ++i) {
431 auto phi = ret_lam->var(i);
432 if (Axm::isa<mem::M>(phi->type())) continue;
433
434 auto namej = name;
435 if (e > 2) {
436 namej += '.' + std::to_string(j);
437 bb.tail("{} = extractvalue {} {}, {}", namej, t_ret, name, j);
438 }
439 assert(!Axm::isa<mem::M>(phi->type()));
440 lam2bb_[ret_lam].phis[phi].emplace_back(namej, id(lam, true));
441 locals_[phi] = id(phi);
442 ++j;
443 }
444 }
445
446 return bb.tail("br label {}", id(ret_lam));
447 }
448}
449
450std::string Emitter::emit_bb(BB& bb, const Def* def) {
451 if (auto lam = def->isa<Lam>()) return id(lam);
452
453 auto name = id(def);
454 std::string op;
455
456 auto emit_tuple = [&](const Def* tuple) {
457 if (isa_mem_sigma_2(tuple->type())) {
458 emit_unsafe(tuple->proj(2, 0));
459 return emit(tuple->proj(2, 1));
460 }
461
462 if (is_const(tuple)) {
463 bool is_array = tuple->type()->isa<Arr>();
464
465 std::string s;
466 s += is_array ? "[" : "{";
467 auto sep = "";
468 for (size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
469 auto e = tuple->proj(n, i);
470 if (auto v_elem = emit_unsafe(e); !v_elem.empty()) {
471 auto t_elem = convert(e->type());
472 s += sep + t_elem + " " + v_elem;
473 sep = ", ";
474 }
475 }
476
477 return s += is_array ? "]" : "}";
478 }
479
480 std::string prev = "undef";
481 auto t = convert(tuple->type());
482 for (size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
483 auto e = tuple->proj(n, src);
484 if (auto elem = emit_unsafe(e); !elem.empty()) {
485 auto elem_t = convert(e->type());
486 // TODO: check dst vs src
487 auto namei = name + "." + std::to_string(dst);
488 prev = bb.assign(namei, "insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
489 dst++;
490 }
491 }
492 return prev;
493 };
494
495 if (def->isa<Var>()) {
496 auto ts = def->type()->projs();
497 if (std::ranges::any_of(ts, [](auto t) { return Axm::isa<mem::M>(t); })) return {};
498 return emit_tuple(def);
499 }
500
501 auto emit_gep_index = [&](const Def* index) {
502 auto v_i = emit(index);
503 auto t_i = convert(index->type());
504
505 if (auto size = Idx::isa(index->type())) {
506 if (auto w = Idx::size2bitwidth(size); w && *w < 64) {
507 v_i = bb.assign(name + ".zext",
508 "zext {} {} to i{} ; add one more bit for gep index as it is treated as signed value",
509 t_i, v_i, *w + 1);
510 t_i = "i" + std::to_string(*w + 1);
511 }
512 }
513
514 return std::pair(v_i, t_i);
515 };
516
517 if (auto lit = def->isa<Lit>()) {
518 if (lit->type()->isa<Nat>() || Idx::isa(lit->type())) {
519 return std::to_string(lit->get());
520 } else if (auto w = math::isa_f(lit->type())) {
521 std::stringstream s;
522 u64 hex;
523
524 switch (*w) {
525 case 16:
526 s << "0xH" << std::setfill('0') << std::setw(4) << std::right << std::hex << lit->get<u16>();
527 return s.str();
528 case 32: {
529 hex = std::bit_cast<u64>(f64(lit->get<f32>()));
530 break;
531 }
532 case 64: hex = lit->get<u64>(); break;
533 default: fe::unreachable();
534 }
535
536 s << "0x" << std::setfill('0') << std::setw(16) << std::right << std::hex << hex;
537 return s.str();
538 }
539 fe::unreachable();
540 } else if (def->isa<Bot>()) {
541 return "undef";
542 } else if (auto top = def->isa<Top>()) {
543 if (Axm::isa<mem::M>(top->type())) return {};
544 // bail out to error below
545 } else if (auto tuple = def->isa<Tuple>()) {
546 return emit_tuple(tuple);
547 } else if (auto pack = def->isa<Pack>()) {
548 if (auto lit = Lit::isa(pack->body()); lit && *lit == 0) return "zeroinitializer";
549 return emit_tuple(pack);
550 } else if (auto sel = Select(def)) {
551 auto t = convert(sel.extract()->type());
552 auto [elem_a, elem_b] = sel.pair()->projs<2>([&](auto e) { return emit_unsafe(e); });
553 auto cond_t = convert(sel.cond()->type());
554 auto cond = emit(sel.cond());
555 return bb.assign(name, "select {} {}, {} {}, {} {}", cond_t, cond, t, elem_b, t, elem_a);
556 } else if (auto extract = def->isa<Extract>()) {
557 auto tuple = extract->tuple();
558 auto index = extract->index();
559 auto v_tup = emit_unsafe(tuple);
560
561 // this exact location is important: after emitting the tuple -> ordering of mem ops
562 // before emitting the index, as it might be a weird value for mem vars.
563 if (Axm::isa<mem::M>(extract->type())) return {};
564
565 auto t_tup = convert(tuple->type());
566 if (auto li = Lit::isa(index)) {
567 if (isa_mem_sigma_2(tuple->type())) return v_tup;
568 // Adjust index, if mem is present.
569 auto v_i = Axm::isa<mem::M>(tuple->proj(0)->type()) ? std::to_string(*li - 1) : std::to_string(*li);
570 return bb.assign(name, "extractvalue {} {}, {}", t_tup, v_tup, v_i);
571 }
572
573 auto t_elem = convert(extract->type());
574 auto [v_i, t_i] = emit_gep_index(index);
575
576 print(lam2bb_[root()].body().emplace_front(),
577 "{}.alloca = alloca {} ; copy to alloca to emulate extract with store + gep + load", name, t_tup);
578 print(bb.body().emplace_back(), "store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup, name);
579 print(bb.body().emplace_back(), "{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}", name, t_tup,
580 t_tup, name, t_i, v_i);
581 return bb.assign(name, "load {}, {}* {}.gep", t_elem, t_elem, name);
582 } else if (auto insert = def->isa<Insert>()) {
583 assert(!Axm::isa<mem::M>(insert->tuple()->proj(0)->type()));
584 auto t_tup = convert(insert->tuple()->type());
585 auto t_val = convert(insert->value()->type());
586 auto v_tup = emit(insert->tuple());
587 auto v_val = emit(insert->value());
588 if (auto idx = Lit::isa(insert->index())) {
589 auto v_idx = emit(insert->index());
590 return bb.assign(name, "insertvalue {} {}, {} {}, {}", t_tup, v_tup, t_val, v_val, v_idx);
591 } else {
592 auto t_elem = convert(insert->value()->type());
593 auto [v_i, t_i] = emit_gep_index(insert->index());
594 print(lam2bb_[root()].body().emplace_front(),
595 "{}.alloca = alloca {} ; copy to alloca to emulate insert with store + gep + load", name, t_tup);
596 print(bb.body().emplace_back(), "store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup, name);
597 print(bb.body().emplace_back(), "{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}", name,
598 t_tup, t_tup, name, t_i, v_i);
599 print(bb.body().emplace_back(), "store {} {}, {}* {}.gep", t_val, v_val, t_val, name);
600 return bb.assign(name, "load {}, {}* {}.alloca", t_tup, t_tup, name);
601 }
602 } else if (auto global = def->isa<Global>()) {
603 auto v_init = emit(global->init());
604 auto [pointee, addr_space] = Axm::as<mem::Ptr>(global->type())->args<2>();
605 print(vars_decls_, "{} = global {} {}\n", name, convert(pointee), v_init);
606 return globals_[global] = name;
607 } else if (auto nat = Axm::isa<core::nat>(def)) {
608 auto [a, b] = nat->args<2>([this](auto def) { return emit(def); });
609
610 switch (nat.id()) {
611 case core::nat::add: op = "add"; break;
612 case core::nat::sub: op = "sub"; break;
613 case core::nat::mul: op = "mul"; break;
614 }
615
616 return bb.assign(name, "{} nsw nuw i64 {}, {}", op, a, b);
617 } else if (auto ncmp = Axm::isa<core::ncmp>(def)) {
618 auto [a, b] = ncmp->args<2>([this](auto def) { return emit(def); });
619 op = "icmp ";
620
621 switch (ncmp.id()) {
622 // clang-format off
623 case core::ncmp::e: op += "eq" ; break;
624 case core::ncmp::ne: op += "ne" ; break;
625 case core::ncmp::g: op += "ugt"; break;
626 case core::ncmp::ge: op += "uge"; break;
627 case core::ncmp::l: op += "ult"; break;
628 case core::ncmp::le: op += "ule"; break;
629 // clang-format on
630 default: fe::unreachable();
631 }
632
633 return bb.assign(name, "{} i64 {}, {}", op, a, b);
634 } else if (auto idx = Axm::isa<core::idx>(def)) {
635 auto x = emit(idx->arg());
636 auto s = *Idx::size2bitwidth(Idx::isa(idx->type()));
637 auto t = convert(idx->type());
638 if (s < 64) return bb.assign(name, "trunc i64 {} to {}", x, t);
639 return x;
640 } else if (auto bit1 = Axm::isa<core::bit1>(def)) {
641 assert(bit1.id() == core::bit1::neg);
642 auto x = emit(bit1->arg());
643 auto t = convert(bit1->type());
644 return bb.assign(name, "xor {} -1, {}", t, x);
645 } else if (auto bit2 = Axm::isa<core::bit2>(def)) {
646 auto [a, b] = bit2->args<2>([this](auto def) { return emit(def); });
647 auto t = convert(bit2->type());
648
649 auto neg = [&](std::string_view x) { return bb.assign(name + ".neg", "xor {} -1, {}", t, x); };
650
651 switch (bit2.id()) {
652 // clang-format off
653 case core::bit2::and_: return bb.assign(name, "and {} {}, {}", t, a, b);
654 case core::bit2:: or_: return bb.assign(name, "or {} {}, {}", t, a, b);
655 case core::bit2::xor_: return bb.assign(name, "xor {} {}, {}", t, a, b);
656 case core::bit2::nand: return neg(bb.assign(name, "and {} {}, {}", t, a, b));
657 case core::bit2:: nor: return neg(bb.assign(name, "or {} {}, {}", t, a, b));
658 case core::bit2::nxor: return neg(bb.assign(name, "xor {} {}, {}", t, a, b));
659 case core::bit2:: iff: return bb.assign(name, "and {} {}, {}", neg(a), b);
660 case core::bit2::niff: return bb.assign(name, "or {} {}, {}", neg(a), b);
661 // clang-format on
662 default: fe::unreachable();
663 }
664 } else if (auto shr = Axm::isa<core::shr>(def)) {
665 auto [a, b] = shr->args<2>([this](auto def) { return emit(def); });
666 auto t = convert(shr->type());
667
668 switch (shr.id()) {
669 case core::shr::a: op = "ashr"; break;
670 case core::shr::l: op = "lshr"; break;
671 }
672
673 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
674 } else if (auto wrap = Axm::isa<core::wrap>(def)) {
675 auto [mode, ab] = wrap->uncurry_args<2>();
676 auto [a, b] = ab->projs<2>([this](auto def) { return emit(def); });
677 auto t = convert(wrap->type());
678 auto lmode = Lit::as(mode);
679
680 switch (wrap.id()) {
681 case core::wrap::add: op = "add"; break;
682 case core::wrap::sub: op = "sub"; break;
683 case core::wrap::mul: op = "mul"; break;
684 case core::wrap::shl: op = "shl"; break;
685 }
686
687 if (lmode & core::Mode::nuw) op += " nuw";
688 if (lmode & core::Mode::nsw) op += " nsw";
689
690 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
691 } else if (auto div = Axm::isa<core::div>(def)) {
692 auto [m, xy] = div->args<2>();
693 auto [x, y] = xy->projs<2>();
694 auto t = convert(x->type());
695 emit_unsafe(m);
696 auto a = emit(x);
697 auto b = emit(y);
698
699 switch (div.id()) {
700 case core::div::sdiv: op = "sdiv"; break;
701 case core::div::udiv: op = "udiv"; break;
702 case core::div::srem: op = "srem"; break;
703 case core::div::urem: op = "urem"; break;
704 }
705
706 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
707 } else if (auto icmp = Axm::isa<core::icmp>(def)) {
708 auto [a, b] = icmp->args<2>([this](auto def) { return emit(def); });
709 auto t = convert(icmp->arg(0)->type());
710 op = "icmp ";
711
712 switch (icmp.id()) {
713 // clang-format off
714 case core::icmp::e: op += "eq" ; break;
715 case core::icmp::ne: op += "ne" ; break;
716 case core::icmp::sg: op += "sgt"; break;
717 case core::icmp::sge: op += "sge"; break;
718 case core::icmp::sl: op += "slt"; break;
719 case core::icmp::sle: op += "sle"; break;
720 case core::icmp::ug: op += "ugt"; break;
721 case core::icmp::uge: op += "uge"; break;
722 case core::icmp::ul: op += "ult"; break;
723 case core::icmp::ule: op += "ule"; break;
724 // clang-format on
725 default: fe::unreachable();
726 }
727
728 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
729 } else if (auto extr = Axm::isa<core::extrema>(def)) {
730 auto [x, y] = extr->args<2>();
731 auto t = convert(x->type());
732 auto a = emit(x);
733 auto b = emit(y);
734 std::string f = "llvm.";
735 switch (extr.id()) {
736 case core::extrema::Sm: f += "smin."; break;
737 case core::extrema::SM: f += "smax."; break;
738 case core::extrema::sm: f += "umin."; break;
739 case core::extrema::sM: f += "umax."; break;
740 }
741 f += t;
742 declare("{} @{}({}, {})", t, f, t, t);
743 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
744 } else if (auto abs = Axm::isa<core::abs>(def)) {
745 auto [m, x] = abs->args<2>();
746 auto t = convert(x->type());
747 auto a = emit(x);
748 std::string f = "llvm.abs." + t;
749 declare("{} @{}({}, {})", t, f, t, "i1");
750 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, "i1", "1");
751 } else if (auto conv = Axm::isa<core::conv>(def)) {
752 auto v_src = emit(conv->arg());
753 auto t_src = convert(conv->arg()->type());
754 auto t_dst = convert(conv->type());
755
756 nat_t w_src = *Idx::size2bitwidth(Idx::isa(conv->arg()->type()));
757 nat_t w_dst = *Idx::size2bitwidth(Idx::isa(conv->type()));
758
759 if (w_src == w_dst) return v_src;
760
761 switch (conv.id()) {
762 case core::conv::s: op = w_src < w_dst ? "sext" : "trunc"; break;
763 case core::conv::u: op = w_src < w_dst ? "zext" : "trunc"; break;
764 }
765
766 return bb.assign(name, "{} {} {} to {}", op, t_src, v_src, t_dst);
767 } else if (auto bitcast = Axm::isa<core::bitcast>(def)) {
768 auto dst_type_ptr = Axm::isa<mem::Ptr>(bitcast->type());
769 auto src_type_ptr = Axm::isa<mem::Ptr>(bitcast->arg()->type());
770 auto v_src = emit(bitcast->arg());
771 auto t_src = convert(bitcast->arg()->type());
772 auto t_dst = convert(bitcast->type());
773
774 if (auto lit = Lit::isa(bitcast->arg()); lit && *lit == 0) return "zeroinitializer";
775 // clang-format off
776 if (src_type_ptr && dst_type_ptr) return bb.assign(name, "bitcast {} {} to {}", t_src, v_src, t_dst);
777 if (src_type_ptr) return bb.assign(name, "ptrtoint {} {} to {}", t_src, v_src, t_dst);
778 if (dst_type_ptr) return bb.assign(name, "inttoptr {} {} to {}", t_src, v_src, t_dst);
779 // clang-format on
780
781 auto size2width = [&](const Def* type) {
782 if (type->isa<Nat>()) return 64_n;
783 if (auto size = Idx::isa(type)) return *Idx::size2bitwidth(size);
784 return 0_n;
785 };
786
787 auto src_size = size2width(bitcast->arg()->type());
788 auto dst_size = size2width(bitcast->type());
789
790 op = "bitcast";
791 if (src_size && dst_size) {
792 if (src_size == dst_size) return v_src;
793 op = (src_size < dst_size) ? "zext" : "trunc";
794 }
795 return bb.assign(name, "{} {} {} to {}", op, t_src, v_src, t_dst);
796 } else if (auto lea = Axm::isa<mem::lea>(def)) {
797 auto [ptr, i] = lea->args<2>();
798 auto pointee = Axm::as<mem::Ptr>(ptr->type())->arg(0);
799 auto v_ptr = emit(ptr);
800 auto t_pointee = convert(pointee);
801 auto t_ptr = convert(ptr->type());
802 if (pointee->isa<Sigma>())
803 return bb.assign(name, "getelementptr inbounds {}, {} {}, i64 0, i32 {}", t_pointee, t_ptr, v_ptr,
804 Lit::as(i));
805
806 assert(pointee->isa<Arr>());
807 auto [v_i, t_i] = emit_gep_index(i);
808
809 return bb.assign(name, "getelementptr inbounds {}, {} {}, i64 0, {} {}", t_pointee, t_ptr, v_ptr, t_i, v_i);
810 } else if (auto malloc = Axm::isa<mem::malloc>(def)) {
811 declare("i8* @malloc(i64)");
812
813 emit_unsafe(malloc->arg(0));
814 auto size = emit(malloc->arg(1));
815 auto ptr_t = convert(Axm::as<mem::Ptr>(def->proj(1)->type()));
816 bb.assign(name + "i8", "call i8* @malloc(i64 {})", size);
817 return bb.assign(name, "bitcast i8* {} to {}", name + "i8", ptr_t);
818 } else if (auto free = Axm::isa<mem::free>(def)) {
819 declare("void @free(i8*)");
820 emit_unsafe(free->arg(0));
821 auto ptr = emit(free->arg(1));
822 auto ptr_t = convert(Axm::as<mem::Ptr>(free->arg(1)->type()));
823
824 bb.assign(name + "i8", "bitcast {} {} to i8*", ptr_t, ptr);
825 bb.tail("call void @free(i8* {})", name + "i8");
826 return {};
827 } else if (auto mslot = Axm::isa<mem::mslot>(def)) {
828 auto [Ta, msi] = mslot->uncurry_args<2>();
829 auto [pointee, addr_space] = Ta->projs<2>();
830 auto [mem, _, __] = msi->projs<3>();
831 emit_unsafe(mslot->arg(0));
832 // TODO array with size
833 // auto v_size = emit(mslot->arg(1));
834 print(bb.body().emplace_back(), "{} = alloca {}", name, convert(pointee));
835 return name;
836 } else if (auto free = Axm::isa<mem::free>(def)) {
837 declare("void @free(i8*)");
838
839 emit_unsafe(free->arg(0));
840 auto v_ptr = emit(free->arg(1));
841 auto t_ptr = convert(Axm::as<mem::Ptr>(free->arg(1)->type()));
842
843 bb.assign(name + "i8", "bitcast {} {} to i8*", t_ptr, v_ptr);
844 bb.tail("call void @free(i8* {})", name + "i8");
845 return {};
846 } else if (auto load = Axm::isa<mem::load>(def)) {
847 emit_unsafe(load->arg(0));
848 auto v_ptr = emit(load->arg(1));
849 auto t_ptr = convert(load->arg(1)->type());
850 auto t_pointee = convert(Axm::as<mem::Ptr>(load->arg(1)->type())->arg(0));
851 return bb.assign(name, "load {}, {} {}", t_pointee, t_ptr, v_ptr);
852 } else if (auto store = Axm::isa<mem::store>(def)) {
853 emit_unsafe(store->arg(0));
854 auto v_ptr = emit(store->arg(1));
855 auto v_val = emit(store->arg(2));
856 auto t_ptr = convert(store->arg(1)->type());
857 auto t_val = convert(store->arg(2)->type());
858 print(bb.body().emplace_back(), "store {} {}, {} {}", t_val, v_val, t_ptr, v_ptr);
859 return {};
860 } else if (auto q = Axm::isa<clos::alloc_jmpbuf>(def)) {
861 declare("i64 @jmpbuf_size()");
862
863 emit_unsafe(q->arg());
864 auto size = name + ".size";
865 bb.assign(size, "call i64 @jmpbuf_size()");
866 return bb.assign(name, "alloca i8, i64 {}", size);
867 } else if (auto setjmp = Axm::isa<clos::setjmp>(def)) {
868 declare("i32 @_setjmp(i8*) returns_twice");
869
870 auto [mem, jmpbuf] = setjmp->arg()->projs<2>();
871 emit_unsafe(mem);
872 auto v_jb = emit(jmpbuf);
873 return bb.assign(name, "call i32 @_setjmp(i8* {})", v_jb);
874 } else if (auto arith = Axm::isa<math::arith>(def)) {
875 auto [mode, ab] = arith->uncurry_args<2>();
876 auto [a, b] = ab->projs<2>([this](auto def) { return emit(def); });
877 auto t = convert(arith->type());
878 auto lmode = Lit::as(mode);
879
880 switch (arith.id()) {
881 case math::arith::add: op = "fadd"; break;
882 case math::arith::sub: op = "fsub"; break;
883 case math::arith::mul: op = "fmul"; break;
884 case math::arith::div: op = "fdiv"; break;
885 case math::arith::rem: op = "frem"; break;
886 }
887
888 if (lmode == math::Mode::fast)
889 op += " fast";
890 else {
891 // clang-format off
892 if (lmode & math::Mode::nnan ) op += " nnan";
893 if (lmode & math::Mode::ninf ) op += " ninf";
894 if (lmode & math::Mode::nsz ) op += " nsz";
895 if (lmode & math::Mode::arcp ) op += " arcp";
896 if (lmode & math::Mode::contract) op += " contract";
897 if (lmode & math::Mode::afn ) op += " afn";
898 if (lmode & math::Mode::reassoc ) op += " reassoc";
899 // clang-format on
900 }
901
902 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
903 } else if (auto tri = Axm::isa<math::tri>(def)) {
904 auto a = emit(tri->arg());
905 auto t = convert(tri->type());
906
907 std::string f;
908
909 if (tri.id() == math::tri::sin) {
910 f = "llvm.sin"s + llvm_suffix(tri->type());
911 } else if (tri.id() == math::tri::cos) {
912 f = "llvm.cos"s + llvm_suffix(tri->type());
913 } else {
914 if (tri.sub() & sub_t(math::tri::a)) f += "a";
915
916 switch (math::tri((tri.id() & 0x3) | Annex::base<math::tri>())) {
917 case math::tri::sin: f += "sin"; break;
918 case math::tri::cos: f += "cos"; break;
919 case math::tri::tan: f += "tan"; break;
920 case math::tri::ahFF: error("this axm is supposed to be unused");
921 default: fe::unreachable();
922 }
923
924 if (tri.sub() & sub_t(math::tri::h)) f += "h";
925 f += math_suffix(tri->type());
926 }
927
928 declare("{} @{}({})", t, f, t);
929 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
930 } else if (auto extrema = Axm::isa<math::extrema>(def)) {
931 auto [a, b] = extrema->args<2>([this](auto def) { return emit(def); });
932 auto t = convert(extrema->type());
933 std::string f = "llvm.";
934 switch (extrema.id()) {
935 case math::extrema::fmin: f += "minnum"; break;
936 case math::extrema::fmax: f += "maxnum"; break;
937 case math::extrema::ieee754min: f += "minimum"; break;
938 case math::extrema::ieee754max: f += "maximum"; break;
939 }
940 f += llvm_suffix(extrema->type());
941
942 declare("{} @{}({}, {})", t, f, t, t);
943 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
944 } else if (auto pow = Axm::isa<math::pow>(def)) {
945 auto [a, b] = pow->args<2>([this](auto def) { return emit(def); });
946 auto t = convert(pow->type());
947 std::string f = "llvm.pow";
948 f += llvm_suffix(pow->type());
949 declare("{} @{}({}, {})", t, f, t, t);
950 return bb.assign(name, "tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
951 } else if (auto rt = Axm::isa<math::rt>(def)) {
952 auto a = emit(rt->arg());
953 auto t = convert(rt->type());
954 std::string f;
955 if (rt.id() == math::rt::sq)
956 f = "llvm.sqrt"s + llvm_suffix(rt->type());
957 else
958 f = "cbrt"s += math_suffix(rt->type());
959 declare("{} @{}({})", t, f, t);
960 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
961 } else if (auto exp = Axm::isa<math::exp>(def)) {
962 auto a = emit(exp->arg());
963 auto t = convert(exp->type());
964 std::string f = "llvm.";
965 f += (exp.sub() & sub_t(math::exp::log)) ? "log" : "exp";
966 f += (exp.sub() & sub_t(math::exp::bin)) ? "2" : (exp.sub() & sub_t(math::exp::dec)) ? "10" : "";
967 f += llvm_suffix(exp->type());
968 // TODO doesn't work for exp10"
969 declare("{} @{}({})", t, f, t);
970 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
971 } else if (auto er = Axm::isa<math::er>(def)) {
972 auto a = emit(er->arg());
973 auto t = convert(er->type());
974 auto f = er.id() == math::er::f ? "erf"s : "erfc"s;
975 f += math_suffix(er->type());
976 declare("{} @{}({})", t, f, t);
977 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
978 } else if (auto gamma = Axm::isa<math::gamma>(def)) {
979 auto a = emit(gamma->arg());
980 auto t = convert(gamma->type());
981 std::string f = gamma.id() == math::gamma::t ? "tgamma" : "lgamma";
982 f += math_suffix(gamma->type());
983 declare("{} @{}({})", t, f, t);
984 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
985 } else if (auto cmp = Axm::isa<math::cmp>(def)) {
986 auto [a, b] = cmp->args<2>([this](auto def) { return emit(def); });
987 auto t = convert(cmp->arg(0)->type());
988 op = "fcmp ";
989
990 switch (cmp.id()) {
991 // clang-format off
992 case math::cmp:: e: op += "oeq"; break;
993 case math::cmp:: l: op += "olt"; break;
994 case math::cmp:: le: op += "ole"; break;
995 case math::cmp:: g: op += "ogt"; break;
996 case math::cmp:: ge: op += "oge"; break;
997 case math::cmp:: ne: op += "one"; break;
998 case math::cmp:: o: op += "ord"; break;
999 case math::cmp:: u: op += "uno"; break;
1000 case math::cmp:: ue: op += "ueq"; break;
1001 case math::cmp:: ul: op += "ult"; break;
1002 case math::cmp::ule: op += "ule"; break;
1003 case math::cmp:: ug: op += "ugt"; break;
1004 case math::cmp::uge: op += "uge"; break;
1005 case math::cmp::une: op += "une"; break;
1006 // clang-format on
1007 default: fe::unreachable();
1008 }
1009
1010 return bb.assign(name, "{} {} {}, {}", op, t, a, b);
1011 } else if (auto conv = Axm::isa<math::conv>(def)) {
1012 auto v_src = emit(conv->arg());
1013 auto t_src = convert(conv->arg()->type());
1014 auto t_dst = convert(conv->type());
1015
1016 auto s_src = math::isa_f(conv->arg()->type());
1017 auto s_dst = math::isa_f(conv->type());
1018
1019 switch (conv.id()) {
1020 case math::conv::f2f: op = s_src < s_dst ? "fpext" : "fptrunc"; break;
1021 case math::conv::s2f: op = "sitofp"; break;
1022 case math::conv::u2f: op = "uitofp"; break;
1023 case math::conv::f2s: op = "fptosi"; break;
1024 case math::conv::f2u: op = "fptoui"; break;
1025 }
1026
1027 return bb.assign(name, "{} {} {} to {}", op, t_src, v_src, t_dst);
1028 } else if (auto abs = Axm::isa<math::abs>(def)) {
1029 auto a = emit(abs->arg());
1030 auto t = convert(abs->type());
1031 std::string f = "llvm.fabs";
1032 f += llvm_suffix(abs->type());
1033 declare("{} @{}({})", t, f, t);
1034 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1035 } else if (auto round = Axm::isa<math::round>(def)) {
1036 auto a = emit(round->arg());
1037 auto t = convert(round->type());
1038 std::string f = "llvm.";
1039 switch (round.id()) {
1040 case math::round::f: f += "floor"; break;
1041 case math::round::c: f += "ceil"; break;
1042 case math::round::r: f += "round"; break;
1043 case math::round::t: f += "trunc"; break;
1044 }
1045 f += llvm_suffix(round->type());
1046 declare("{} @{}({})", t, f, t);
1047 return bb.assign(name, "tail call {} @{}({} {})", t, f, t, a);
1048 }
1049 error("unhandled def in LLVM backend: {} : {}", def, def->type());
1050}
1051
1052void emit(World& world, std::ostream& ostream) {
1053 Emitter emitter(world, ostream);
1054 emitter.run();
1055}
1056
1057int compile(World& world, std::string name) {
1058#ifdef _WIN32
1059 auto exe = name + ".exe"s;
1060#else
1061 auto exe = name;
1062#endif
1063 return compile(world, name + ".ll"s, exe);
1064}
1065
1066int compile(World& world, std::string ll, std::string out) {
1067 std::ofstream ofs(ll);
1068 emit(world, ofs);
1069 ofs.close();
1070 auto cmd = fmt("clang \"{}\" -o \"{}\" -Wno-override-module", ll, out);
1071 return sys::system(cmd);
1072}
1073
1074int compile_and_run(World& world, std::string name, std::string args) {
1075 if (compile(world, name) == 0) return sys::run(name, args);
1076 error("compilation failed");
1077}
1078
1079} // namespace mim::ll
A (possibly paramterized) Array.
Definition tuple.h:117
static auto isa(const Def *def)
Definition axm.h:107
static auto as(const Def *def)
Definition axm.h:129
Matches (ff, tt)#cond arg where cond is not a Literal.
Definition tuple.h:279
Lam * root() const
Definition phase.h:304
Base class for all Defs.
Definition def.h:251
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:587
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:486
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:390
nat_t num_vars() noexcept
Definition def.h:429
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:446
auto vars(F f) noexcept
Definition def.h:429
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:578
Matches a dispatch through a jump table of the form: (target_0, target_1, ...)#index arg where index ...
Definition tuple.h:304
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:206
static constexpr nat_t size2bitwidth(nat_t n)
Definition def.h:893
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
Definition def.cpp:610
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
Definition tuple.h:233
A function.
Definition lam.h:110
static Lam * isa_mut_basicblock(const Def *d)
Only for mutables.
Definition lam.h:145
const Pi * type() const
Definition lam.h:130
const Def * body() const
Definition lam.h:123
static std::optional< T > isa(const Def *def)
Definition def.h:826
static T as(const Def *def)
Definition def.h:832
const Nest & nest() const
Definition phase.h:320
A (possibly paramterized) Tuple.
Definition tuple.h:166
virtual void run()
Entry point and generates some debug output; invokes Phase::start.
Definition phase.cpp:13
virtual void start()=0
Actual entry.
A dependent function type.
Definition lam.h:14
const Pi * ret_pi() const
Yields the last Pi::dom, if Pi::isa_basicblock.
Definition lam.cpp:13
static const Pi * isa_returning(const Def *d)
Is this a continuation (Pi::isa_cn) which has a Pi::ret_pi?
Definition lam.h:49
static Schedule schedule(const Nest &)
Definition schedule.cpp:118
Matches (ff, tt)#cond - where cond is not a Literal.
Definition tuple.h:261
A dependent tuple type.
Definition tuple.h:20
World & world()
Definition pass.h:64
std::string_view name() const
Definition pass.h:67
Data constructor for a Sigma.
Definition tuple.h:68
A variable introduced by a binder (mutable).
Definition def.h:702
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:31
void emit_imported(Lam *)
Definition ll.cpp:257
Emitter(World &world, std::ostream &ostream)
Definition ll.cpp:119
std::string prepare()
Definition ll.cpp:271
void finalize()
Definition ll.cpp:287
void start() override
Actual entry.
Definition ll.cpp:246
std::string emit_bb(BB &, const Def *)
Definition ll.cpp:450
bool is_valid(std::string_view s)
Definition ll.cpp:122
mim::Emitter< std::string, std::string, BB, Emitter > Super
Definition ll.cpp:117
void emit_epilogue(Lam *)
Definition ll.cpp:316
void declare(const char *s, Args &&... args)
Definition ll.cpp:131
Definition ll.h:9
int compile_and_run(World &, std::string name, std::string args={})
Definition ll.cpp:1074
void emit(World &, std::ostream &)
Definition ll.cpp:1052
int compile(World &, std::string name)
Definition ll.cpp:1057
The clos Plugin
Definition clos.h:7
The core Plugin
Definition core.h:8
The math Plugin
Definition math.h:8
The mem Plugin
Definition mem.h:11
int run(std::string cmd, std::string args={})
Wraps sys::system and puts .exe at the back (Windows) and ./ at the front (otherwise) of cmd.
Definition sys.cpp:77
int system(std::string)
Wraps std::system and makes the return value usable.
Definition sys.cpp:71
u64 nat_t
Definition types.h:44
Vector< const Def * > DefVec
Definition def.h:77
u8 sub_t
Definition types.h:49
D bitcast(const S &src)
A bitcast from src of type S to D.
Definition util.h:23
std::ostream & print(std::ostream &os, const char *s)
Base case.
Definition print.cpp:5
double f64
Definition types.h:42
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
Definition print.h:163
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:125
float f32
Definition types.h:41
GIDMap< const Def *, To > DefMap
Definition def.h:73
TExt< true > Top
Definition lattice.h:172
TExt< false > Bot
Definition lattice.h:171
uint64_t u64
Definition types.h:35
@ Nat
Definition def.h:114
@ Pi
Definition def.h:114
@ Arr
Definition def.h:114
@ Sigma
Definition def.h:114
uint16_t u16
Definition types.h:35
static consteval flags_t base()
Definition plugin.h:119
std::deque< std::ostringstream > & tail()
Definition ll.cpp:92
void tail(const char *s, Args &&... args)
Definition ll.cpp:101
DefMap< std::deque< std::pair< std::string, std::string > > > phis
Definition ll.cpp:111
BB(const BB &)=delete
BB()=default
std::deque< std::ostringstream > & body()
Definition ll.cpp:91
BB(BB &&other) noexcept=default
friend void swap(BB &a, BB &b) noexcept
Definition ll.cpp:105
std::array< std::deque< std::ostringstream >, 3 > parts
Definition ll.cpp:112
std::deque< std::ostringstream > & head()
Definition ll.cpp:90
BB & operator=(BB other) noexcept
Definition ll.cpp:88
std::string assign(std::string_view name, const char *s, Args &&... args)
Definition ll.cpp:95