summaryrefslogtreecommitdiff
path: root/src/trans/target.cpp
blob: a48038ddfac6250953d07b0f36b4876d313c10d4 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * MRustC - Rust Compiler
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * trans/target.cpp
 * - Target-specific information
 */
#include "target.hpp"
#include <algorithm>
#include "../expand/cfg.hpp"
#include <fstream>
#include <map>
#include <hir/hir.hpp>
#include <hir_typeck/helpers.hpp>

TargetArch ARCH_X86_64 = {
    "x86_64",
    64, false,
    { /*atomic(u8)=*/true, false, true, true,  true }
    };
TargetArch ARCH_X86 = {
    "x86",
    32, false,
    { /*atomic(u8)=*/true, false, true, false,  true }
};
TargetArch ARCH_ARM32 = {
    "arm",
    32, false,
    { /*atomic(u8)=*/true, false, true, false,  true }
};
TargetSpec  g_target;


bool Target_GetSizeAndAlignOf(const Span& sp, const StaticTraitResolve& resolve, const ::HIR::TypeRef& ty, size_t& out_size, size_t& out_align);

namespace
{
    TargetSpec load_spec_from_file(const ::std::string& filename)
    {
        throw "";
    }
    TargetSpec init_from_spec_name(const ::std::string& target_name)
    {
        if( ::std::ifstream(target_name).is_open() )
        {
            return load_spec_from_file(target_name);
        }
        else if(target_name == "i586-linux-gnu")
        {
            return TargetSpec {
                "unix", "linux", "gnu", CodegenMode::Gnu11, "i586-pc-linux-gnu",
                ARCH_X86
                };
        }
        else if(target_name == "x86_64-linux-gnu")
        {
            return TargetSpec {
                "unix", "linux", "gnu", CodegenMode::Gnu11, "x86_64-pc-linux-gnu",
                ARCH_X86_64
                };
        }
        else if(target_name == "arm-linux-gnu")
        {
            return TargetSpec {
                "unix", "linux", "gnu", CodegenMode::Gnu11, "arm-elf-eabi",
                ARCH_ARM32
                };
        }
        else if(target_name == "x86_64-windows-gnu")
        {
            return TargetSpec {
                "windows", "windows", "gnu", CodegenMode::Gnu11, "x86_64-w64-mingw32",
                ARCH_X86_64
                };
        }
        else if (target_name == "x86-windows-msvc")
        {
            return TargetSpec {
                "windows", "windows", "msvc", CodegenMode::Msvc, "x86",
                ARCH_X86
            };
        }
        //else if (target_name == "x86_64-windows-msvc")
        //{
        //    return TargetSpec {
        //        "windows", "windows", "msvc", CodegenMode::Msvc, "amd64"
        //        ARCH_X86_64
        //        };
        //}
        else
        {
            ::std::cerr << "Unknown target name '" << target_name << "'" << ::std::endl;
            abort();
        }
        throw "";
    }
}

const TargetSpec& Target_GetCurSpec()
{
    return g_target;
}
void Target_SetCfg(const ::std::string& target_name)
{
    g_target = init_from_spec_name(target_name);

    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_vendor", "gnu");
    }
    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;
        });
}

namespace {
    // Returns NULL when the repr can't be determined
    ::std::unique_ptr<StructRepr> make_struct_repr(const Span& sp, const StaticTraitResolve& resolve, const ::HIR::TypeRef& ty)
    {
        ::std::vector<StructRepr::Ent>  ents;
        bool packed = false;
        bool allow_sort = false;
        if( const auto* te = ty.m_data.opt_Path() )
        {
            const auto& str = *te->binding.as_Struct();
            auto monomorph_cb = monomorphise_type_get_cb(sp, nullptr, &te->path.m_data.as_Generic().m_params, nullptr);
            auto monomorph = [&](const auto& tpl) {
                auto rv = monomorphise_type_with(sp, tpl, monomorph_cb);
                resolve.expand_associated_types(sp, rv);
                return rv;
                };
            TU_MATCHA( (str.m_data), (se),
            (Unit,
                ),
            (Tuple,
                unsigned int idx = 0;
                for(const auto& e : se)
                {
                    auto ty = monomorph(e.ent);
                    size_t  size, align;
                    if( !Target_GetSizeAndAlignOf(sp, resolve, ty, size,align) )
                        return nullptr;
                    ents.push_back(StructRepr::Ent { idx++, size, align, mv$(ty) });
                }
                ),
            (Named,
                unsigned int idx = 0;
                for(const auto& e : se)
                {
                    auto ty = monomorph(e.second.ent);
                    size_t  size, align;
                    if( !Target_GetSizeAndAlignOf(sp, resolve, ty, size,align) )
                        return nullptr;
                    ents.push_back(StructRepr::Ent { idx++, size, align, mv$(ty) });
                }
                )
            )
            switch(str.m_repr)
            {
            case ::HIR::Struct::Repr::Packed:
                packed = true;
                TODO(sp, "make_struct_repr - repr(packed)");    // needs codegen help
                break;
            case ::HIR::Struct::Repr::C:
                // No sorting, no packing
                break;
            case ::HIR::Struct::Repr::Rust:
                allow_sort = true;
                break;
            }
        }
        else if( const auto* te = ty.m_data.opt_Tuple() )
        {
            unsigned int idx = 0;
            for(const auto& t : *te)
            {
                size_t  size, align;
                if( !Target_GetSizeAndAlignOf(sp, resolve, t, size,align) )
                    return nullptr;
                ents.push_back(StructRepr::Ent { idx++, size, align, t.clone() });
            }
        }
        else
        {
            BUG(sp, "Unexpected type in creating struct repr");
        }


        if( allow_sort )
        {
            // TODO: Sort by alignment then size (largest first)
            // - Requires codegen to use this information
        }

        StructRepr  rv;
        size_t  cur_ofs = 0;
        size_t  max_align = 1;
        for(auto& e : ents)
        {
            // Increase offset to fit alignment
            if( !packed )
            {
                while( cur_ofs % e.align != 0 )
                {
                    rv.ents.push_back({ ~0u, 1, 1, ::HIR::TypeRef( ::HIR::CoreType::U8 ) });
                    cur_ofs ++;
                }
            }
            max_align = ::std::max(max_align, e.align);

            rv.ents.push_back(mv$(e));
            cur_ofs += e.size;
        }
        if( !packed )
        {
            while( cur_ofs % max_align != 0 )
            {
                rv.ents.push_back({ ~0u, 1, 1, ::HIR::TypeRef( ::HIR::CoreType::U8 ) });
                cur_ofs ++;
            }
        }
        return box$(rv);
    }
}
const StructRepr* Target_GetStructRepr(const Span& sp, const StaticTraitResolve& resolve, const ::HIR::TypeRef& ty)
{
    // TODO: Thread safety
    // Map of generic paths to struct representations.
    static ::std::map<::HIR::TypeRef, ::std::unique_ptr<StructRepr>>  s_cache;

    auto it = s_cache.find(ty);
    if( it != s_cache.end() )
    {
        return it->second.get();
    }

    auto ires = s_cache.insert(::std::make_pair( ty.clone(), make_struct_repr(sp, resolve, ty) ));
    return ires.first->second.get();
}

// TODO: Include NonZero and other repr optimisations here

bool Target_GetSizeAndAlignOf(const Span& sp, const StaticTraitResolve& resolve, 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,
        TU_MATCHA( (te.binding), (be),
        (Unbound,
            BUG(sp, "Unbound type path " << ty << " encountered");
            ),
        (Opaque,
            return false;
            ),
        (Struct,
            if( const auto* repr = Target_GetStructRepr(sp, resolve, ty) )
            {
                out_size = 0;
                out_align = 1;
                for(const auto& e : repr->ents)
                {
                    out_size += e.size;
                    out_align = ::std::max(out_align, e.align);
                }
                return true;
            }
            else
            {
                return false;
            }
            ),
        (Enum,
            // Search for alternate repr
            // If not found, determine the variant size
            // Get data size and alignment.
            return false;
            ),
        (Union,
            // Max alignment and max data size
            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,
        size_t  size;
        if( !Target_GetSizeAndAlignOf(sp, resolve, *te.inner, size,out_align) )
            return false;
        size *= te.size_val;
        ),
    (Slice,
        BUG(sp, "sizeof on a slice - unsized");
        ),
    (Tuple,
        // Same code as structs :)
        if( const auto* repr = Target_GetStructRepr(sp, resolve, ty) )
        {
            out_size = 0;
            out_align = 1;
            for(const auto& e : repr->ents)
            {
                out_size += e.size;
                out_align = ::std::max(out_align, e.align);
            }
            return true;
        }
        else
        {
            return false;
        }
        ),
    (Borrow,
        // - Alignment is machine native
        out_align = g_target.m_arch.m_pointer_bits / 8;
        // - Size depends on Sized-nes of the parameter
        if( resolve.type_is_sized(sp, *te.inner) )
        {
            out_size = g_target.m_arch.m_pointer_bits / 8;
            return true;
        }
        // TODO: Handle different types of Unsized (ones with different pointer sizes)
        out_size = g_target.m_arch.m_pointer_bits / 8 * 2;
        return true;
        ),
    (Pointer,
        // - Alignment is machine native
        out_align = g_target.m_arch.m_pointer_bits / 8;
        // - Size depends on Sized-nes of the parameter
        if( resolve.type_is_sized(sp, *te.inner) )
        {
            out_size = g_target.m_arch.m_pointer_bits / 8;
            return true;
        }
        // TODO: Handle different types of Unsized (ones with different pointer sizes)
        out_size = g_target.m_arch.m_pointer_bits / 8 * 2;
        return true;
        ),
    (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 StaticTraitResolve& resolve, const ::HIR::TypeRef& ty, size_t& out_size)
{
    size_t  ignore_align;
    return Target_GetSizeAndAlignOf(sp, resolve, ty, out_size, ignore_align);
}
bool Target_GetAlignOf(const Span& sp, const StaticTraitResolve& resolve, const ::HIR::TypeRef& ty, size_t& out_align)
{
    size_t  ignore_size;
    return Target_GetSizeAndAlignOf(sp, resolve, ty, ignore_size, out_align);
}