-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
324 lines (277 loc) · 7.42 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* util.c -- utilities for µIDL
* Copyright 2009, 2010 Kalle A. Sandström <ksandstr@iki.fi>
*
* This file is part of µiX.
*
* µiX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* µiX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with µiX. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <ctype.h>
#include <llvm-c/Core.h>
#include <ccan/strset/strset.h>
#include "defs.h"
#include "llvmutil.h"
static struct strset warn_once_set;
static pthread_key_t llvm_ctx_key;
static bool keys_done = false;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
static void list_dispose(GList *list)
{
GLIST_FOREACH(cur, list) {
g_free(cur->data);
}
g_list_free(list);
}
void free_message_info(struct message_info *inf)
{
if(inf == NULL) return;
g_list_free(inf->params);
list_dispose(inf->untyped);
list_dispose(inf->seq);
list_dispose(inf->string);
list_dispose(inf->mapped);
g_free(inf);
}
void free_method_info(struct method_info *inf)
{
if(inf == NULL) return;
free_message_info(inf->request);
for(int i=0; i<inf->num_reply_msgs; i++) {
free_message_info(inf->replies[i]);
}
g_free(inf);
}
void free_iface_info(struct iface_info *inf, void *unused)
{
if(inf == NULL) return;
GLIST_FOREACH(cur, inf->ops) {
free_method_info(cur->data);
}
g_list_free(inf->ops);
g_list_free(inf->tagmask_ops);
g_free(inf);
}
bool warn_once(const char *fmt, ...)
{
static bool first = true;
if(first) {
first = false;
strset_init(&warn_once_set);
}
va_list al;
va_start(al, fmt);
char *str = g_strdup_vprintf(fmt, al);
va_end(al);
int len = strlen(str);
while(len > 0 && str[len - 1] == '\n') len--;
str[MAX(0, len - 1)] = '\0';
if(strset_get(&warn_once_set, str) == NULL) {
bool ok = strset_add(&warn_once_set, str);
assert(ok || errno != EEXIST);
fprintf(stderr, "warning: %s\n", str);
return true;
} else {
g_free(str);
return false;
}
}
static bool free_member(const char *member, void *unused) {
g_free((char *)member);
return true;
}
void reset_warn_once(void) {
strset_iterate(&warn_once_set, &free_member, NULL);
}
struct msg_param *find_pdecl(GList *list, IDL_tree pdecl)
{
GLIST_FOREACH(cur, list) {
struct msg_param *p = cur->data;
if(p->param_dcl == pdecl) return p;
}
return NULL;
}
char *tmp_vf(struct print_ctx *pr, const char *fmt, va_list al)
{
/* leave the heap out of this, if reasonably possible */
char buf[256];
va_list copy;
va_copy(copy, al);
int n = vsnprintf(buf, sizeof(buf), fmt, copy);
va_end(copy);
char *result;
if(n+1 > sizeof(buf)) {
/* ok, have to involve the heap. */
#ifndef NDEBUG
fprintf(stderr, "%s: output would be %d bytes long\n",
__FUNCTION__, n);
#endif
char *feh = g_strdup_vprintf(fmt, al);
result = g_string_chunk_insert_len(pr->tmpstrchunk, feh, n);
g_free(feh);
} else {
result = g_string_chunk_insert_len(pr->tmpstrchunk, buf, n);
}
return result;
}
char *tmp_f(struct print_ctx *pr, const char *fmt, ...)
{
va_list al;
va_start(al, fmt);
char *result = tmp_vf(pr, fmt, al);
va_end(al);
return result;
}
void print_headers(struct print_ctx *pr, const char * const *strs, int len)
{
for(int i=0; i<len; i++) {
code_f(pr, "#include <%s>", strs[i]);
}
code_f(pr, "\n");
}
void print_file_heading(struct print_ctx *pr)
{
code_f(pr,
"/* THIS FILE WAS GENERATED WITH µidl!\n"
" *\n"
" * Do not modify it, modify the source IDL file `%s' instead.\n"
" */\n", pr->idlfilename);
}
/* TODO: callsites to this here thing should recycle compatible chunks of
* memory, rather than allocating their own for each. this is a low-hanging
* space optimization.
*/
LLVMValueRef build_local_storage(
struct llvm_ctx *ctx,
LLVMTypeRef type,
LLVMValueRef count,
const char *name)
{
assert(ctx->alloc_bb != NULL);
LLVMBuilderRef b = LLVMCreateBuilderInContext(ctx->ctx);
LLVMPositionBuilderAtEnd(b, ctx->alloc_bb);
LLVMValueRef ptr;
if(count == NULL) ptr = LLVMBuildAlloca(b, type, name);
else ptr = LLVMBuildArrayAlloca(b, type, count, name);
LLVMDisposeBuilder(b);
return ptr;
}
/* free(2)'d at end of function. */
LLVMValueRef build_malloc_storage(
struct llvm_ctx *ctx,
LLVMTypeRef type,
LLVMValueRef count,
const char *name)
{
assert(ctx->alloc_bb != NULL);
LLVMBuilderRef b = LLVMCreateBuilderInContext(ctx->ctx);
LLVMPositionBuilderAtEnd(b, ctx->alloc_bb);
LLVMValueRef ptr;
if(count == NULL) ptr = LLVMBuildMalloc(b, type, name);
else ptr = LLVMBuildArrayMalloc(b, type, count, name);
LLVMDisposeBuilder(b);
ctx->malloc_ptrs = g_list_prepend(ctx->malloc_ptrs, ptr);
return ptr;
}
LLVMValueRef build_seq_param_storage(
struct llvm_ctx *ctx,
IDL_tree ptyp,
const char *name)
{
IDL_tree subtype = SEQ_SUBTYPE(ptyp);
uint64_t max_size = SEQ_BOUND_VAL(ptyp);
/* use stack allocation only for buffers smaller than 256 bytes */
uint64_t max_bytes = max_size * (size_in_bits(subtype) / 8);
T typ = llvm_rigid_type(ctx, subtype);
V sz = CONST_INT(max_size);
if(max_bytes >= 256) {
return build_malloc_storage(ctx, typ, sz,
tmp_f(ctx->pr, "%s.heap", name));
} else {
return build_local_storage(ctx, typ, sz,
tmp_f(ctx->pr, "%s.stk", name));
}
}
char *mangle_repo_id(const char *repo_id)
{
int len = strlen(repo_id);
GString *str = g_string_sized_new(len + 16);
bool lower = false;
for(int i=0; i<len; i++) {
char c = repo_id[i];
assert(c != '\0');
if(!isalnum(c) && c != '_') {
c = '_';
lower = false;
} else if(isupper(c)) {
if(lower) g_string_append_c(str, '_');
c = tolower(c);
lower = false;
} else {
lower = true;
}
g_string_append_c(str, c);
}
return g_string_free(str, FALSE);
}
struct member_item *expand_member_list(IDL_tree list)
{
GArray *items = g_array_new(FALSE, FALSE, sizeof(struct member_item));
IDL_LIST_FOREACH(m_cur, list) {
IDL_tree member = IDL_LIST(m_cur).data,
mtype = get_type_spec(IDL_MEMBER(member).type_spec);
IDL_LIST_FOREACH(d_cur, IDL_MEMBER(member).dcls) {
IDL_tree dcl = IDL_LIST(d_cur).data;
struct member_item mi = { .type = mtype };
IDL_tree id;
if(IDL_NODE_TYPE(dcl) == IDLN_IDENT) {
mi.dim = 0;
id = dcl;
} else if(IDL_NODE_TYPE(dcl) == IDLN_TYPE_ARRAY) {
mi.dim = IDL_INTEGER(IDL_LIST(
IDL_TYPE_ARRAY(dcl).size_list).data).value;
id = IDL_TYPE_ARRAY(dcl).ident;
} else {
g_assert_not_reached();
}
mi.name = IDL_IDENT(id).str;
g_array_append_val(items, mi);
}
}
struct member_item term = { .type = NULL };
g_array_append_val(items, term);
return (struct member_item *)g_array_free(items, FALSE);
}
static void make_keys(void)
{
pthread_key_create(&llvm_ctx_key, NULL);
keys_done = true;
}
struct llvm_ctx *get_llvm_ctx(void) {
return keys_done ? pthread_getspecific(llvm_ctx_key) : NULL;
}
struct llvm_ctx *replace_llvm_ctx(struct llvm_ctx *ctx)
{
pthread_once(&key_once, &make_keys);
struct llvm_ctx *old = pthread_getspecific(llvm_ctx_key);
pthread_setspecific(llvm_ctx_key, ctx);
return old;
}