-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtypeandname.c
100 lines (86 loc) · 2.49 KB
/
typeandname.c
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
#include <string.h>
#include <stdlib.h>
#include "typeandname.h"
#if defined(__FreeBSD__)
void * _aligned_malloc(size_t size, size_t alignment){
return aligned_alloc(alignment, size);
}
#elif defined(__OpenBSD__)
void * _aligned_malloc(size_t size, size_t alignment){
return aligned_alloc(alignment, size);
}
#elif defined(__linux__) || defined(__CYGWIN__)
#include <malloc.h>
void * _aligned_malloc(size_t size, size_t alignment){
return memalign(alignment, size);
}
#elif defined(_WIN32)
extern void * _aligned_malloc(size_t, size_t);
#elif defined(__APPLE__)
void * _aligned_malloc(size_t size, size_t alignment){
void *mem;
posix_memalign(&mem, size, alignment);
return mem;
}
#else
#error "Please add a definition for some aligned malloc function"
#endif
#ifdef _MSC_VER
#define strdup _strdup
#endif
struct typeandname *newtypeandname(const struct typenode *ty, const char *name)
{
struct typeandname *tan = calloc(sizeof(struct typeandname), 1);
tan->ty = ty;
tan->name = strdup(name);
tan->next = NULL;
return tan;
}
struct typenode *newtypenode(const char *typename, const struct typenode *superclass)
{
struct typenode *result;
result = _aligned_malloc(sizeof(struct typenode), 8);
result->typename = strdup(typename);
result->superclass = superclass;
return result;
}
const struct typenode *getPrimitiveAncestor(const struct typenode *cur)
{
while (getTypePtr(cur)->superclass)
cur = getTypePtr(cur)->superclass;
return cur;
}
int isDerivedFrom(const struct typenode *cur, const struct typenode *base)
{
for(; getTypePtr(cur); cur = getTypePtr(cur)->superclass){
if(typeeq(cur, base)){
return 1;
}
}
return 0;
}
struct typenode* mkretty(const struct typenode *ty, uint8_t ret)
{
uintptr_t tagMask = (8-1);
uintptr_t pointerMask = ~tagMask;
uintptr_t ptr = (uintptr_t)ty;
ret = ret & tagMask;
return (struct typenode*)((ptr & pointerMask) | ret);
}
uint8_t getTypeTag(const struct typenode *ty)
{
uintptr_t tagMask = (8-1);
uintptr_t ptr = (uintptr_t)ty;
return (uint8_t)(ptr & tagMask);
}
struct typenode* getTypePtr(const struct typenode *ty)
{
uintptr_t tagMask = (8-1);
uintptr_t pointerMask = ~tagMask;
uintptr_t ptr = (uintptr_t)ty;
return (struct typenode*)(ptr & pointerMask);
}
int typeeq(const struct typenode *a, const struct typenode *b)
{
return getTypePtr(a) == getTypePtr(b);
}