MimIR
0.6-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
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
"
13
#include "
mim/plug/mach/target.h
"
14
15
namespace
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>`.
19
class
AsmEmit
:
public
FunPhase
{
20
public
:
21
using
FunPhase::FunPhase
;
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
32
protected
:
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
37
private
:
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"
);
48
FunPhase::start
();
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
mim::Def::is_set
bool is_set() const
Definition
def.h:392
mim::Def::is_external
bool is_external() const noexcept
Definition
def.h:565
mim::Def::sym
Sym sym() const
Definition
def.h:629
mim::Def::unique_name
std::string unique_name() const
name + "_" + Def::gid
Definition
def.cpp:646
mim::Lam
A function.
Definition
lam.h:91
mim::Out::Out
Out()=default
mim::Phase::args
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition
phase.cpp:23
mim::Phase::name
std::string_view name() const
Definition
phase.h:63
mim::Phase::world
World & world()
Definition
phase.h:60
mim::World::name
Sym name() const
Definition
world.h:113
mim::plug::mach::AsmEmit
The emit Phase of a native backend: writes GNU assembler code for each external function and each fun...
Definition
asm.h:19
mim::plug::mach::AsmEmit::emit
virtual void emit(std::ostream &os, Lam *fun, bool global)=0
mim::plug::mach::AsmEmit::visit
void visit(Lam *lam) final
Emits the functions callees first, so a caller finds the params() of a known callee.
Definition
asm.h:55
mim::plug::mach::AsmEmit::start
void start() final
Actual entry.
Definition
asm.h:38
mim::plug::mach::AsmEmit::params
LamMap< ParamRegs > & params()
The param_regs() of the known() functions emitted so far.
Definition
asm.h:30
mim::plug::mach::AsmEmit::FunPhase
FunPhase(World &world, flags_t annex, const Target &target)
Definition
phase.h:57
mim::plug::mach::AsmEmit::symbol
static std::string symbol(Lam *lam)
The assembler symbol of lam.
Definition
asm.h:24
mim::plug::mach::AsmEmit::finish
virtual void finish(std::ostream &)
Writes what all functions together need after them, e.g. a constant pool.
Definition
asm.h:35
mim::plug::mach::FunPhase::start
void start() override
Actual entry.
Definition
phase.h:69
mim::plug::mach::FunPhase::FunPhase
FunPhase(World &world, flags_t annex, const Target &target)
Definition
phase.h:57
mim::plug::mach::FunPhase::target
const Target & target() const
Definition
phase.h:61
mim::plug::mach::Target::global_prefix
static constexpr std::string_view global_prefix()
The prefix that the object format adds to a C symbol.
Definition
target.h:70
mim::plug::mach::Target::is_elf
static constexpr bool is_elf()
Definition
target.h:62
mim::plug::mach
The mach Plugin
Definition
asm.h:15
mim::LamMap
GIDMap< Lam *, To > LamMap
Definition
lam.h:210
mim::arg_value
std::optional< std::string_view > arg_value(fe::View< std::string > args, Keys... keys)
Definition
plugin.h:65
phase.h
phase.h
target.h
plugin.h
world.h
include
mim
plug
mach
asm.h
Generated by
1.18.0