-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.c
181 lines (157 loc) · 3.75 KB
/
util.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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "kcc.h"
Vector *new_vector() {
Vector *vec = malloc(sizeof(Vector));
vec->data = malloc(sizeof(void *) * 16);
vec->capacity = 16;
vec->len = 0;
return vec;
}
void vec_push(Vector *vec, void *elem) {
if (vec->capacity == vec->len) {
vec->capacity *= 2;
vec->data = realloc(vec->data, sizeof(void *) * vec->capacity);
}
vec->data[vec->len++] = elem;
}
void vec_pushi(Vector *vec, int elem) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-conversion"
#pragma GCC diagnostic ignored "-Wint-to-void-pointer-cast"
vec_push(vec, elem);
#pragma GCC diagnostic pop
}
int vec_geti(Vector *vec, int idx) {
return (int)vec->data[idx];
}
Map *new_map() {
Map *map = malloc(sizeof(Map));
map->keys = new_vector();
map->vals = new_vector();
return map;
}
void map_put(Map *map, char *key, void *val) {
vec_push(map->keys, key);
vec_push(map->vals, val);
}
void map_puti(Map *map, char *key, int val) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-conversion"
#pragma GCC diagnostic ignored "-Wint-to-void-pointer-cast"
map_put(map, key, (void *)val);
#pragma GCC diagnostic pop
}
int map_exists(Map *map, char *key) {
for (int i = 0; i < map->keys->len; i++) {
if (strcmp(map->keys->data[i], key) == 0)
return 1;
}
return 0;
}
void *map_get(Map *map, char *key) {
for (int i = map->keys->len - 1; i >= 0; i--) {
if (strcmp(map->keys->data[i], key) == 0) {
return map->vals->data[i];
}
}
return NULL;
}
int map_geti(Map *map, char *key) {
return (int)map_get(map, key);
}
StringBuilder *new_sb() {
StringBuilder *sb = malloc(sizeof(StringBuilder));
sb->buf = malloc(sizeof(char) * 16);
sb->len = 0;
sb->capacity = 16;
return sb;
}
void sb_add(StringBuilder *sb, char c) {
if (sb->len+1 == sb->capacity) {
sb->capacity *= 2;
sb->buf = realloc(sb->buf, sizeof(char) * sb->capacity);
}
sb->buf[sb->len++] = c;
}
char *sb_string(StringBuilder *sb) {
sb->buf[sb->len] = '\0';
return sb->buf;
}
Type *new_type(int ty) {
Type *t = calloc(1, sizeof(Type));
t->ty = ty;
return t;
}
int is_ptr(Type *ty) {
return ty->ty == PTR || ty->ty == ARY;
}
Type *ptr_of(Type *ty) {
Type *t = new_type(PTR);
t->ptr_of = ty;
return t;
}
Type *ary_of(Type *ty, int len) {
Type *t = new_type(ARY);
t->ptr_of = ty;
t->len = len;
return t;
}
int dig_ptr_of(Type *ty) {
if (is_ptr(ty))
return dig_ptr_of(ty->ptr_of);
return ty->ty;
}
int register_size(Type *ty) {
switch (ty->ty) {
case PTR:
case ARY:
case INT:
return 8;
case CHAR:
return 1;
default:
assert(0 && "invalid Type");
}
}
int size_of(Type *ty) {
switch (ty->ty) {
case ARY:
return size_of(ty->ptr_of) * ty->len;
default:
return register_size(ty);
}
}
int equal_ty(Type *a, Type *b) {
if (a->ty == PTR && b->ty == PTR)
return equal_ty(a->ptr_of, b->ptr_of);
if (a->ty == ARY && b->ty == ARY)
return equal_ty(a->ptr_of, b->ptr_of);
if (a->ty != b->ty)
return 0;
return 1;
}
void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
void debug(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
char *format(char *fmt, ...) {
char buf[2048];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
return strdup(buf);
}