MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
asm.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <ostream>
5#include <print>
6#include <string>
7
8#include <mim/phase.h>
9#include <mim/plugin.h>
10#include <mim/world.h>
11
12#include "mim/plug/mach/phase.h"
14
15namespace mim::plug::mach {
16
17/// The `emit` Phase of a native backend: writes GNU assembler code for each external function and each function it
18/// calls. The output goes to `<world>.s` (or `a.s` if the World is unnamed), or to `<file>` of `-X <plugin>:o=<file>`.
19class AsmEmit : public FunPhase {
20public:
22
23 /// The assembler symbol of @p lam.
24 static std::string symbol(Lam* lam) {
25 if (lam->is_external() || !lam->is_set()) return std::format("{}{}", Target::global_prefix(), lam->sym());
26 return lam->unique_name();
27 }
28
29 /// The param_regs() of the known() functions emitted so far.
30 LamMap<ParamRegs>& params() { return params_; }
31
32protected:
33 virtual void emit(std::ostream& os, Lam* fun, bool global) = 0;
34 /// Writes what all functions together need after them, e.g. a constant pool.
35 virtual void finish(std::ostream&) {}
36
37private:
38 void start() final {
39 auto path = std::string(arg_value(args(), "o", "output").value_or(""));
40 if (path.empty()) path = (world().name() ? world().name().str() : std::string("a")) + ".s";
41 auto out = Out(path);
42 os_ = out.os();
43
44 for (auto mut : world().externals().muts())
45 if (!mut->isa<Lam>()) mut->blame("{} backend: cannot emit `{}`", target().name(), mut).bail();
46
47 std::println(*os_, "\t.text");
49 finish(*os_);
50 if (Target::is_elf()) std::println(*os_, "\t.section .note.GNU-stack,\"\",@progbits");
51 os_ = nullptr;
52 }
53
54 /// Emits the functions callees first, so a caller finds the params() of a known callee.
55 void visit(Lam* lam) final { emit(*os_, lam, lam->is_external()); }
56
57 std::ostream* os_ = nullptr;
58 LamMap<ParamRegs> params_;
59};
60
61} // namespace mim::plug::mach
bool is_set() const
Definition def.h:392
bool is_external() const noexcept
Definition def.h:565
Sym sym() const
Definition def.h:629
std::string unique_name() const
name + "_" + Def::gid
Definition def.cpp:646
A function.
Definition lam.h:91
Out()=default
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
std::string_view name() const
Definition phase.h:63
World & world()
Definition phase.h:60
Sym name() const
Definition world.h:113
The emit Phase of a native backend: writes GNU assembler code for each external function and each fun...
Definition asm.h:19
virtual void emit(std::ostream &os, Lam *fun, bool global)=0
void visit(Lam *lam) final
Emits the functions callees first, so a caller finds the params() of a known callee.
Definition asm.h:55
void start() final
Actual entry.
Definition asm.h:38
LamMap< ParamRegs > & params()
The param_regs() of the known() functions emitted so far.
Definition asm.h:30
FunPhase(World &world, flags_t annex, const Target &target)
Definition phase.h:57
static std::string symbol(Lam *lam)
The assembler symbol of lam.
Definition asm.h:24
virtual void finish(std::ostream &)
Writes what all functions together need after them, e.g. a constant pool.
Definition asm.h:35
void start() override
Actual entry.
Definition phase.h:69
FunPhase(World &world, flags_t annex, const Target &target)
Definition phase.h:57
const Target & target() const
Definition phase.h:61
static constexpr std::string_view global_prefix()
The prefix that the object format adds to a C symbol.
Definition target.h:70
static constexpr bool is_elf()
Definition target.h:62
The mach Plugin
Definition asm.h:15
GIDMap< Lam *, To > LamMap
Definition lam.h:210
std::optional< std::string_view > arg_value(fe::View< std::string > args, Keys... keys)
Definition plugin.h:65