-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeinfo.cpp
299 lines (264 loc) · 9.33 KB
/
typeinfo.cpp
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
// Copyright (c) 2009, Nicholas "Indy" Ray. All rights reserved.
// See the LICENSE file for usage, modification, and distribution terms.
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#include "typeinfo.h"
#include "types.h"
#include "symbol.h"
#include "error.h"
#include <assert.h>
#include <sstream>
#include <llvm/Type.h>
#include <llvm/LLVMContext.h>
#include <llvm/DerivedTypes.h>
#include <llvm/Support/raw_os_ostream.h>
// internal types classes.
void set_car(pointer P, pointer V);
void set_cdr(pointer P, pointer V);
void* get_value(pointer P);
pointer ploy_alloc(const dynamic_type* type, size_t size);
pointer ploy_alloc(const dynamic_type* type);
pointer ploy_static_alloc(const dynamic_type* type, size_t size);
pointer ploy_static_alloc(const dynamic_type* type);
void ploy_free(pointer P);
class typeinfo
{
public:
virtual const llvm::Type* get_llvm_type() = 0;
virtual void print_description(FILE* out) = 0;
virtual ~typeinfo()
{}
};
class primitive_typeinfo : typeinfo
{
const llvm::Type* llvm_type;
public:
primitive_typeinfo(const llvm::Type* typ) : llvm_type(typ)
{}
virtual const llvm::Type* get_llvm_type()
{
return llvm_type;
}
virtual void print_description(FILE* out)
{
std::ostringstream std_desc;
llvm::raw_os_ostream desc(std_desc);
llvm_type->print(desc);
fputs(std_desc.str().c_str(), out);
}
};
class struct_typeinfo : primitive_typeinfo
{
std::vector<const char*> Members; // References to symbol strings, don't need to manage the memory.
public:
struct_typeinfo(std::vector<const char*>& Params, const llvm::Type* typ) : primitive_typeinfo(typ), Members(Params)
{}
};
typeinfo* get_typeinfo(pointer P)
{
assert(is_type(P, DT_TypeInfo));
return (typeinfo*)get_value(P);
}
void typeinfo_finish(pointer P)
{
// Call Destructor
get_typeinfo(P)->~typeinfo();
}
void print_typeinfo(pointer P, symbol_table* tbl, FILE* out)
{
fputs("#Type:", out);
get_typeinfo(P)->print_description(out);
}
const size_t type_info_size = sizeof(typeinfo);
const llvm::Type* typeinfo_get_llvm_type(pointer type)
{
return get_typeinfo(type)->get_llvm_type();
}
pointer make_primitive_typeinfo(const llvm::Type* llvm_T)
{
pointer P = ploy_alloc(get_type(DT_TypeInfo), sizeof(primitive_typeinfo));
typeinfo* T = get_typeinfo(P);
new(T) primitive_typeinfo(llvm_T);
return P;
}
pointer get_int_typeinfo(llvm::LLVMContext& Context)
{
static pointer P = NULL;
if(!P)
{
P = ploy_static_alloc(get_type(DT_TypeInfo), sizeof(primitive_typeinfo));
typeinfo* T = get_typeinfo(P);
new(T) primitive_typeinfo(llvm::Type::getInt32Ty(Context));
}
return P;
}
pointer get_float_typeinfo(llvm::LLVMContext& Context)
{
static pointer P = NULL;
if(!P)
{
P = ploy_static_alloc(get_type(DT_TypeInfo), sizeof(primitive_typeinfo));
typeinfo* T = get_typeinfo(P);
new(T) primitive_typeinfo(llvm::Type::getFloatTy(Context));
}
return P;
}
pointer make_struct_typeinfo(llvm::LLVMContext& Context, std::vector<const char*>& ParamNames, std::vector<const llvm::Type*> ParamTypes)
{
pointer P = ploy_alloc(get_type(DT_TypeInfo), sizeof(struct_typeinfo));
typeinfo* T = get_typeinfo(P);
new(T) struct_typeinfo(ParamNames, llvm::StructType::get(Context, ParamTypes, false));
return P;
}
pointer eval_typeinfo(pointer P, symbol_table* tbl, type_map* type_define_map)
{
// Consider moving llvm type selection into another function,
// and just doing a quick wrap into a typeinfo here.
// This would simplify the recursive stuff.
if(!is_type(P, DT_Pair))
{
// Either an alias or a primitive.
assert_cerror(is_type(P, DT_Symbol), P, "Expecting type symbol or typelist after ':'.");
symbol S = *get_symbol(P);
std::map<symbol, pointer>::iterator it = type_define_map->find(S);
if(it != type_define_map->end())
return it->second;
const llvm::Type* llvm_T;
if(S == symbol_from_string(tbl, "int"))
llvm_T = llvm::Type::getInt32Ty(llvm::getGlobalContext());
else if(S == symbol_from_string(tbl, "float"))
llvm_T = llvm::Type::getFloatTy(llvm::getGlobalContext());
// TODO: move to std library as a typedef.
else if(S == symbol_from_string(tbl, "string"))
llvm_T = llvm::PointerType::getUnqual(
llvm::Type::getInt8Ty(llvm::getGlobalContext()));
else if(S == symbol_from_string(tbl, "pointer"))
llvm_T = llvm::PointerType::getUnqual(
llvm::Type::getInt8Ty(llvm::getGlobalContext()));
else if(S == symbol_from_string(tbl, "void"))
llvm_T = llvm::Type::getVoidTy(llvm::getGlobalContext());
else
compiler_error(P, "Unrecgonized primitive type.");
return make_primitive_typeinfo(llvm_T);
}
else if(pair_cdr(P) == NIL)
{
// Single element list treat it as the first element.
return eval_typeinfo(pair_car(P), tbl, type_define_map);
}
else if(is_type(pair_car(P), DT_Pair))
{
// Try to collapse.
return eval_typeinfo(pair_car(P), tbl, type_define_map);
}
else if(*get_symbol(car(P)) == symbol_from_string(tbl, "int"))
{
//TODO: merge with other int code.
int bitlen = 32;
if(is_type(cdr(P), DT_Pair) &&
is_type(cadr(P), DT_Int))
bitlen = get_int(cadr(P));
// Should be a compiler error, need to move the error funcs somewhere.
assert(!is_type(cdr(P), DT_Pair) ||
is_type(cadr(P), DT_Int)
&& "Bad Type declaration!");
return make_primitive_typeinfo(llvm::IntegerType::get(llvm::getGlobalContext(), bitlen));
}
else if(*get_symbol(car(P)) == symbol_from_string(tbl, "pointer"))
{
pointer cP = eval_typeinfo(cdr(P), tbl, type_define_map);
pointer Ret =
make_primitive_typeinfo(
llvm::PointerType::getUnqual(typeinfo_get_llvm_type(cP)));
destroy_list(cP);
return Ret;
}
// Composite types don't mantain proper non-llvm typeinfo of the members, as they just store members in the llvm type, FIXME.
// Likely to move more powerful type info somewhere else, in which case this is fine.
else if(*get_symbol(car(P)) == symbol_from_string(tbl, "tuple"))
{
std::vector<const llvm::Type*> Params;
pointer Param = cdr(P);
while(Param != NIL)
{
assert(is_type(Param, DT_Pair));
pointer cP = eval_typeinfo(car(Param), tbl, type_define_map);
Params.push_back(typeinfo_get_llvm_type(cP));
destroy_list(cP);
Param = cdr(Param);
}
return make_primitive_typeinfo(llvm::StructType::get(llvm::getGlobalContext(), Params, false));
}
else if(*get_symbol(car(P)) == symbol_from_string(tbl, "struct"))
{
std::vector<const llvm::Type*> Params;
std::vector<const char*> ParamNames;
pointer Param = cdr(P);
while(Param != NIL)
{
assert(is_type(Param, DT_Pair));
assert(is_type(car(Param), DT_Pair));
assert(is_type(caar(Param), DT_String));
pointer cP = eval_typeinfo(cdar(Param), tbl, type_define_map);
Params.push_back(typeinfo_get_llvm_type(cP));
destroy_list(cP);
ParamNames.push_back(string_from_symbol(tbl, *get_symbol(caar(Param))));
Param = cdr(Param);
}
return make_struct_typeinfo(llvm::getGlobalContext(), ParamNames, Params);
}
else if(cdr(P) == NIL)
{
// Try to collapse
return eval_typeinfo(pair_car(P), tbl, type_define_map);
}
else
assert(false);
}
void transform_tree_gen_typedef(pointer P, symbol_table* tbl, type_map* type_define_map)
{
if(P != NIL)
{
if(is_type(P, DT_Pair))
{
if(is_type(car(P), DT_Symbol) && *get_symbol(car(P)) == symbol_from_string(tbl, "define-type"))
{
(*type_define_map)[*get_symbol(cadr(P))] = eval_typeinfo(cddr(P), tbl, type_define_map);
return;
}
while(P != NIL && is_type(P, DT_Pair))
{
transform_tree_gen_typedef(car(P), tbl, type_define_map);
P = cdr(P);
}
}
}
}
void transform_tree_gen_typeinfo(pointer P, symbol_table* tbl, type_map* type_define_map)
{
if(P != NIL)
{
if(is_type(P, DT_Pair))
{
if(is_type(pair_car(P), DT_Symbol))
{
if(*get_symbol(pair_car(P)) == symbol_from_string(tbl, ":"))
{
set_car(P, eval_typeinfo(cadr(P), tbl, type_define_map));
set_cdr(P, cddr(P));
}
else if(string_from_symbol(tbl, *get_symbol(pair_car(P)))[0] == ':')
{
pointer T = create_symbol(tbl, string_from_symbol(tbl, *get_symbol(pair_car(P)))+1);
set_car(P, eval_typeinfo(T, tbl, type_define_map));
destroy_list(T);
}
}
else
{
transform_tree_gen_typeinfo(pair_car(P), tbl, type_define_map);
}
transform_tree_gen_typeinfo(pair_cdr(P), tbl, type_define_map);
}
}
}