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
|
#include <stdlib.h>
#include "dynamic_typing.h"
struct entry {
void *start;
void *end;
struct type *type;
};
static struct entry *ENTRIES;
static unsigned long N_ENTRIES;
void annotate_type(void *ptr, struct type *type) {
ENTRIES = realloc(ENTRIES, (++N_ENTRIES) * sizeof(ENTRIES[0]));
ENTRIES[N_ENTRIES - 1] = (struct entry){
.start = ptr,
.end = ptr + 1,
.type = type,
};
}
struct type *dynamic_typeof(void *ptr) {
for (unsigned long i = N_ENTRIES; i --> 0;)
if (ENTRIES[i].start <= ptr && ptr < ENTRIES[i].end)
return ENTRIES[i].type;
return 0;
}
struct type *make_basic(const char *type, unsigned long size) {
struct type *t = calloc(1, sizeof(*t));
t->kind = TY_BASIC;
t->basic = type;
t->size = size;
return t;
}
struct type *make_array(struct type *base, unsigned long len) {
struct type *t = calloc(1, sizeof(*t));
t->kind = TY_ARRAY;
t->base = base;
t->length = len;
t->size = len * t->base->size;
return t;
}
struct type *make_pointer(struct type *base) {
struct type *t = calloc(1, sizeof(*t));
t->kind = TY_POINTER;
t->base = base;
t->size = sizeof(void*);
return t;
}
// function not supported yet
// for struct & union, the members are initialized by the caller
struct type *make_struct(unsigned long size) {
struct type *t = calloc(1, sizeof(*t));
t->kind = TY_STRUCT;
return t;
}
struct type *make_union(unsigned long size) {
struct type *t = calloc(1, sizeof(*t));
t->kind = TY_UNION;
return t;
}
void prepend_member(struct type *aggregate, struct type *member, const char *name, unsigned long offset) {
member->offset = offset;
member->next_member = aggregate->members;
member->field_name = name;
aggregate->members = member;
}
|