summaryrefslogtreecommitdiff
path: root/src/trans/target.cpp
blob: d9b3486e1e51ec0f943d36df7279281e79d3ff34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * trans/target.cpp
 * - Target-specific information
 */
#include "target.hpp"
#include <algorithm>
#include "../expand/cfg.hpp"

// TODO: Replace with target selection
#define POINTER_SIZE_BYTES  8

TargetSpec  g_target = {
    "unix",
    "linux",
    "gnu",
    CodegenMode::Gnu11,
    TargetArch {
        "x86_64",
        64, false,
        { true, false, true, true,  true }
        }
    };

void Target_SetCfg()
{
    if(g_target.m_family == "unix") {
        Cfg_SetFlag("unix");
    }
    else if( g_target.m_family == "windows") {
        Cfg_SetFlag("windows");
    }
    Cfg_SetValue("target_family", g_target.m_family);

    if( g_target.m_os_name == "linux" )
    {
        Cfg_SetFlag("linux");
    }
    Cfg_SetValue("target_env", g_target.m_env_name);

    Cfg_SetValue("target_os", g_target.m_os_name);
    Cfg_SetValue("target_pointer_width", FMT(g_target.m_arch.m_pointer_bits));
    Cfg_SetValue("target_endian", g_target.m_arch.m_big_endian ? "big" : "little");
    Cfg_SetValue("target_arch", g_target.m_arch.m_name);
    Cfg_SetValueCb("target_has_atomic", [&](const ::std::string& s) {
        if(s == "8")    return g_target.m_arch.m_atomics.u8;    // Has an atomic byte
        if(s == "ptr")  return g_target.m_arch.m_atomics.ptr;   // Has an atomic pointer-sized value
        return false;
        });
    Cfg_SetValueCb("target_feature", [](const ::std::string& s) {
        return false;
        });
}

bool Target_GetSizeAndAlignOf(const Span& sp, const ::HIR::TypeRef& ty, size_t& out_size, size_t& out_align)
{
    TU_MATCHA( (ty.m_data), (te),
    (Infer,
        BUG(sp, "sizeof on _ type");
        ),
    (Diverge,
        out_size = 0;
        out_align = 0;
        return true;
        ),
    (Primitive,
        switch(te)
        {
        case ::HIR::CoreType::Bool:
        case ::HIR::CoreType::U8:
        case ::HIR::CoreType::I8:
            out_size = 1;
            out_align = 1;
            return true;
        case ::HIR::CoreType::U16:
        case ::HIR::CoreType::I16:
            out_size = 2;
            out_align = 2;
            return true;
        case ::HIR::CoreType::U32:
        case ::HIR::CoreType::I32:
        case ::HIR::CoreType::Char:
            out_size = 4;
            out_align = 4;
            return true;
        case ::HIR::CoreType::U64:
        case ::HIR::CoreType::I64:
            out_size = 8;
            out_align = 8;
            return true;
        case ::HIR::CoreType::U128:
        case ::HIR::CoreType::I128:
            out_size = 16;
            // TODO: If i128 is emulated, this can be 8
            out_align = 16;
            return true;
        case ::HIR::CoreType::Usize:
        case ::HIR::CoreType::Isize:
            out_size = g_target.m_arch.m_pointer_bits / 8;
            out_align = g_target.m_arch.m_pointer_bits / 8;
            return true;
        case ::HIR::CoreType::F32:
            out_size = 4;
            out_align = 4;
            return true;
        case ::HIR::CoreType::F64:
            out_size = 8;
            out_align = 8;
            return true;
        case ::HIR::CoreType::Str:
            BUG(sp, "sizeof on a `str` - unsized");
        }
        ),
    (Path,
        // TODO:
        return false;
        ),
    (Generic,
        // Unknown - return false
        return false;
        ),
    (TraitObject,
        BUG(sp, "sizeof on a trait object - unsized");
        ),
    (ErasedType,
        BUG(sp, "sizeof on an erased type - shouldn't exist");
        ),
    (Array,
        // TODO: 
        size_t  size;
        if( !Target_GetSizeAndAlignOf(sp, *te.inner, size,out_align) )
            return false;
        size *= te.size_val;
        ),
    (Slice,
        BUG(sp, "sizeof on a slice - unsized");
        ),
    (Tuple,
        out_size = 0;
        out_align = 0;

        // TODO: Struct reordering
        for(const auto& t : te)
        {
            size_t  size, align;
            if( !Target_GetSizeAndAlignOf(sp, t, size,align) )
                return false;
            if( out_size % align != 0 )
            {
                out_size += align;
                out_size %= align;
            }
            out_size += size;
            out_align = ::std::max(out_align, align);
        }
        ),
    (Borrow,
        // TODO
        ),
    (Pointer,
        // TODO
        ),
    (Function,
        // Pointer size
        out_size = g_target.m_arch.m_pointer_bits / 8;
        out_align = g_target.m_arch.m_pointer_bits / 8;
        return true;
        ),
    (Closure,
        // TODO.
        )
    )
    return false;
}
bool Target_GetSizeOf(const Span& sp, const ::HIR::TypeRef& ty, size_t& out_size)
{
    size_t  ignore_align;
    return Target_GetSizeAndAlignOf(sp, ty, out_size, ignore_align);
}
bool Target_GetAlignOf(const Span& sp, const ::HIR::TypeRef& ty, size_t& out_align)
{
    size_t  ignore_size;
    return Target_GetSizeAndAlignOf(sp, ty, ignore_size, out_align);
}