MimIR
0.6-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
Loading...
Searching...
No Matches
libcall.cpp
Go to the documentation of this file.
1
#include "
mim/plug/mach/libcall.h
"
2
3
#include <
mim/plug/math/math.h
>
4
#include <
mim/plug/mem/mem.h
>
5
6
#include "
mim/plug/mach/mach.h
"
7
8
namespace
mim::plug::mach
{
9
10
namespace
{
11
12
/// The libm name of @p app without the format suffix and its arity, or an empty name.
13
std::pair<std::string, size_t> libm(
const
App* app) {
14
if
(
auto
exp =
Axm::isa<math::exp>
(app)) {
15
if
(
exp
.id() ==
math::exp::lBB
||
exp
.id() ==
math::exp::LBB
)
return
{};
16
auto
name = std::string(
exp
.sub() &
sub_t
(
math::exp::log
) ?
"log"
:
"exp"
);
17
if
(
exp
.sub() &
sub_t
(
math::exp::bin
)) name +=
"2"
;
18
if
(
exp
.sub() &
sub_t
(
math::exp::dec
)) name +=
"10"
;
19
return
{name, 1};
20
}
21
if
(
auto
tri =
Axm::isa<math::tri>
(app)) {
22
auto
name = std::string(
tri
.sub() &
sub_t
(
math::tri::a
) ?
"a"
:
""
);
23
switch
(
math::tri
((fe::to_underlying(
tri
.id()) & 0x3) |
Annex::base<math::tri>
())) {
24
case
math::tri::sin
: name +=
"sin"
;
break
;
25
case
math::tri::cos
: name +=
"cos"
;
break
;
26
case
math::tri::tan
: name +=
"tan"
;
break
;
27
default
:
return
{};
28
}
29
if
(
tri
.sub() &
sub_t
(
math::tri::h
)) name +=
"h"
;
30
return
{name, 1};
31
}
32
if
(
Axm::isa<math::pow>
(app))
return
{
"pow"
, 2};
33
if
(
auto
rt =
Axm::isa<math::rt>
(app);
rt
&&
rt
.id() ==
math::rt::cb
)
return
{
"cbrt"
, 1};
34
if
(
auto
er =
Axm::isa<math::er>
(app))
return
{
er
.id() ==
math::er::f
?
"erf"
:
"erfc"
, 1};
35
if
(
auto
gamma =
Axm::isa<math::gamma>
(app))
return
{
gamma
.id() ==
math::gamma::t
?
"tgamma"
:
"lgamma"
, 1};
36
if
(
auto
round =
Axm::isa<math::round>
(app)) {
37
switch
(
round
.id()) {
38
case
math::round::f
:
return
{
"floor"
, 1};
39
case
math::round::c
:
return
{
"ceil"
, 1};
40
case
math::round::r
:
return
{
"round"
, 1};
41
case
math::round::t
:
return
{
"trunc"
, 1};
42
}
43
}
44
if
(
auto
arith
=
Axm::isa<math::arith>
(app);
arith
&&
arith
.id() ==
math::arith::rem
)
return
{
"fmod"
, 2};
45
if
(
auto
extrema =
Axm::isa<math::extrema>
(app)) {
46
if
(
extrema
.id() ==
math::extrema::fmin
)
return
{
"fmin"
, 2};
47
if
(
extrema
.id() ==
math::extrema::fmax
)
return
{
"fmax"
, 2};
48
}
49
return
{};
50
}
51
52
}
// namespace
53
54
const
Def* LibCall::call(Sym name,
const
App* app,
Defs
args) {
55
auto
&
w
=
new_world
();
56
auto
type
=
rewrite
(app->type());
57
auto
doms =
DefVec
(
args
.size(), [&](
size_t
i) { return args[i]->type(); });
58
doms.emplace_back(
w
.cn(type));
59
// One declaration per type: `malloc` of a `Ptr (T, 0)` for each `T`; all call the same symbol.
60
auto
& decl = decls_[
name
][
w
.cn(doms)];
61
if
(!decl) decl =
w
.mut_con(doms)->set(
name
);
62
return
lift
(decl,
args
, type);
63
}
64
65
const
Def
*
LibCall::rewrite_imm_App
(
const
App
* app) {
66
if
(
liftable
()) {
67
auto
& w =
new_world
();
68
if
(
Axm::isa<mem::strlen>
(app))
return
call(w.sym(
"strlen"
), app,
rewrite
(app->
args
()));
69
if
(
auto
malloc =
Axm::isa<mem::malloc>
(app); malloc ||
Axm::isa<mem::free>
(app)) {
70
check_addr_space
(app, app->
decurry
()->
arg
(2, 1));
71
return
call(w.sym(malloc ?
"malloc"
:
"free"
), app,
rewrite
(app->
args
()));
72
}
73
74
auto
[
name
, n] = libm(app);
75
auto
width =
math::isa_f
(app->
type
());
76
if
(!
name
.empty() && (width == 32 || width == 64)) {
77
auto
args
=
DefVec
(n, [&](
size_t
i) {
return
rewrite
(app->
arg
(n, i)); });
78
return
call(w.sym(*width == 32 ?
name
+
"f"
:
name
), app,
args
);
79
}
80
}
81
return
DS2CPS::rewrite_imm_App
(app);
82
}
83
84
}
// namespace mim::plug::mach
mim::App
Definition
lam.h:215
mim::App::args
auto args(Projector auto f) const noexcept
Definition
lam.h:252
mim::App::decurry
const App * decurry() const
Returns App::callee again as App.
Definition
lam.h:238
mim::App::arg
const Def * arg() const
Definition
lam.h:251
mim::Axm::isa
static auto isa(const Def *def)
Definition
axm.h:112
mim::DS2CPS::lift
const Def * lift(const Def *k, Defs new_args, const Def *new_type, bool filter=false, Sym name={})
Records the call k (new_args..., cont) as pending and returns the Var of cont: Cn new_type as its res...
Definition
ds2cps.cpp:11
mim::DS2CPS::liftable
bool liftable() const
Is the body of a continuation being rewritten, so that lift() is legal?
Definition
ds2cps.h:55
mim::Def
Base class for all Defs.
Definition
def.h:285
mim::Def::type
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition
def.h:1116
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::RWPhase::new_world
World & new_world()
Create new Defs into this.
Definition
phase.h:418
mim::RWPhase::rewrite_imm_App
const Def * rewrite_imm_App(const App *) override
Definition
phase.cpp:317
mim::Rewriter::rewrite
virtual const Def * rewrite(const Def *)
Definition
rewrite.cpp:62
mim::plug::mach::LibCall::rewrite_imm_App
const Def * rewrite_imm_App(const App *) final
Definition
libcall.cpp:65
libcall.h
mach.h
math.h
mem.h
mim::plug::core::extrema
extrema
Definition
autogen.h:255
mim::plug::mach
The mach Plugin
Definition
asm.h:15
mim::plug::mach::check_addr_space
void check_addr_space(const App *app, const Def *a)
Bails unless the address space a of the mem operation app is 0.
Definition
mach.h:31
mim::plug::mach::float_::arith
@ arith
Definition
autogen.h:100
mim::plug::math::tri
tri
Definition
autogen.h:177
mim::plug::math::tri::a
@ a
Definition
autogen.h:195
mim::plug::math::tri::h
@ h
Definition
autogen.h:187
mim::plug::math::tri::cos
@ cos
Definition
autogen.h:181
mim::plug::math::tri::tan
@ tan
Definition
autogen.h:183
mim::plug::math::tri::sin
@ sin
Definition
autogen.h:179
mim::plug::math::rt
rt
Definition
autogen.h:225
mim::plug::math::rt::cb
@ cb
Definition
autogen.h:227
mim::plug::math::arith::rem
@ rem
Definition
autogen.h:142
mim::plug::math::isa_f
std::optional< nat_t > isa_f(const Def *def)
Definition
math.h:55
mim::plug::math::gamma
gamma
Definition
autogen.h:275
mim::plug::math::gamma::t
@ t
Definition
autogen.h:276
mim::plug::math::round
round
Definition
autogen.h:295
mim::plug::math::round::c
@ c
Definition
autogen.h:297
mim::plug::math::round::r
@ r
Definition
autogen.h:298
mim::plug::math::round::f
@ f
Definition
autogen.h:296
mim::plug::math::round::t
@ t
Definition
autogen.h:299
mim::plug::math::extrema::fmax
@ fmax
Definition
autogen.h:163
mim::plug::math::extrema::fmin
@ fmin
Definition
autogen.h:161
mim::plug::math::er
er
Definition
autogen.h:263
mim::plug::math::er::f
@ f
Definition
autogen.h:264
mim::plug::math::exp
exp
Definition
autogen.h:237
mim::plug::math::exp::dec
@ dec
Definition
autogen.h:245
mim::plug::math::exp::LBB
@ LBB
Definition
autogen.h:253
mim::plug::math::exp::bin
@ bin
Definition
autogen.h:242
mim::plug::math::exp::log
@ log
Definition
autogen.h:248
mim::plug::math::exp::lBB
@ lBB
Definition
autogen.h:246
mim::plug::refly::type
type
Definition
autogen.h:37
mim::plug::regex::cls::w
@ w
Definition
autogen.h:91
mim::sub_t
u8 sub_t
Definition
types.h:42
mim::Defs
fe::View< const Def * > Defs
Definition
def.h:94
mim::DefVec
fe::Vector< const Def * > DefVec
Definition
def.h:96
mim::Annex::base
static consteval flags_t base()
Definition
plugin.h:278
src
mim
plug
mach
libcall.cpp
Generated by
1.18.0