[ozulis] fixed some memory leaks
[ozulis.git] / src / ozulis / target-data-factory.cc
blob73f1569615086f69e659b5c43e358561a5878808
1 #include <sys/utsname.h>
2 #include <string.h>
4 #include <ozulis/core/assert.hh>
5 #include <ozulis/target-data.hh>
6 #include <ozulis/target-data-factory.hh>
8 #define ADD_ALIGNMENT(Type, Size, AbiAlign, PrefAlign) \
9 data.typeAlignments.push_back( \
10 TargetData::TypeAlignment( \
11 TargetData::TypeAlignment::Type, Size, AbiAlign, PrefAlign))
13 namespace ozulis
15 const TargetData *
16 TargetDataFactory::native()
18 static const struct {
19 const char * machine;
20 const TargetData * (*create)();
21 } table[] = {
22 {"x86_64", TargetDataFactory::amd64},
23 {"i686", TargetDataFactory::x86},
24 {0, 0}
26 struct utsname u;
28 assert(!uname(&u));
29 for (int i = 0; table[i].machine; i++)
30 if (!strcmp(u.machine, table[i].machine))
31 return table[i].create();
32 assert_msg(false, "unknown machine");
35 const TargetData *
36 TargetDataFactory::x86()
38 static TargetData data;
39 static bool init = true;
41 if (!init)
42 return &data;
44 init = false;
45 data.isLittleEndian = true;
47 ADD_ALIGNMENT(Pointer, 64, 64, 64);
49 ADD_ALIGNMENT(Integer, 1, 8, 8);
50 ADD_ALIGNMENT(Integer, 8, 8, 8);
51 ADD_ALIGNMENT(Integer, 16, 16, 16);
52 ADD_ALIGNMENT(Integer, 32, 32, 32);
53 ADD_ALIGNMENT(Integer, 64, 32, 64);
55 ADD_ALIGNMENT(Float, 32, 32, 32);
56 ADD_ALIGNMENT(Float, 64, 32, 64);
57 ADD_ALIGNMENT(Float, 80, 32, 32);
59 ADD_ALIGNMENT(Vector, 64, 64, 64);
60 ADD_ALIGNMENT(Vector, 128, 128, 128);
62 ADD_ALIGNMENT(Agregate, 0, 0, 64);
64 return &data;
67 const TargetData *
68 TargetDataFactory::amd64()
70 static TargetData data;
71 static bool init = true;
73 if (!init)
74 return &data;
76 init = false;
77 data.isLittleEndian = true;
79 ADD_ALIGNMENT(Pointer, 64, 64, 64);
81 ADD_ALIGNMENT(Integer, 1, 8, 8);
82 ADD_ALIGNMENT(Integer, 8, 8, 8);
83 ADD_ALIGNMENT(Integer, 16, 16, 16);
84 ADD_ALIGNMENT(Integer, 32, 32, 32);
85 ADD_ALIGNMENT(Integer, 64, 32, 64);
87 ADD_ALIGNMENT(Float, 32, 32, 32);
88 ADD_ALIGNMENT(Float, 64, 32, 64);
89 ADD_ALIGNMENT(Float, 80, 32, 32);
91 ADD_ALIGNMENT(Vector, 64, 64, 64);
92 ADD_ALIGNMENT(Vector, 128, 128, 128);
94 ADD_ALIGNMENT(Agregate, 0, 0, 64);
96 return &data;