-
Notifications
You must be signed in to change notification settings - Fork 1
/
CirAttr.c
389 lines (352 loc) · 9.95 KB
/
CirAttr.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "cir_internal.h"
#include <assert.h>
#include <stdalign.h>
#define CIRATTR_INT 1
#define CIRATTR_STR 2
#define CIRATTR_NAME 3
#define CIRATTR_CONS 4
// data1:
// bits 31 to 29: type
// bits: 28 to 0: numArgs
#define data1ToType(x) (((x) >> 29) & 0x07)
#define typeToData1(x) (((x) & 0x07) << 29)
#define data1ToNumArgs(x) ((x) & 0x1fffffff)
#define numArgsToData1(x) ((x) & 0x1fffffff)
#define MAX_CONS_ARGS 0x1fffffff
typedef struct CirAttr {
uint32_t data1;
} CirAttr;
typedef struct CirAttrInt {
uint32_t data1;
int32_t i;
} CirAttrInt;
typedef struct CirAttrStr {
uint32_t data1;
const char *s;
} CirAttrStr;
typedef struct CirAttrName {
uint32_t data1;
CirName name;
} CirAttrName;
typedef struct CirAttrCons {
uint32_t data1;
CirName name;
const CirAttr *args[];
} CirAttrCons;
const CirAttr *
CirAttr_int(int32_t i)
{
CirAttrInt *attr = CirMem_balloc(sizeof(*attr), alignof(*attr));
attr->data1 = typeToData1(CIRATTR_INT);
attr->i = i;
return (CirAttr *)attr;
}
const CirAttr *
CirAttr_str(const char *s)
{
CirAttrStr *attr = CirMem_balloc(sizeof(*attr), alignof(*attr));
attr->data1 = typeToData1(CIRATTR_STR);
attr->s = s;
return (CirAttr *)attr;
}
const CirAttr *
CirAttr_name(CirName name)
{
CirAttrName *attr = CirMem_balloc(sizeof(*attr), alignof(*attr));
attr->data1 = typeToData1(CIRATTR_NAME);
attr->name = name;
return (CirAttr *)attr;
}
const CirAttr *
CirAttr_cons(CirName name, const CirAttr **args, size_t len)
{
if (len > MAX_CONS_ARGS)
cir_bug("Too many attr args");
CirAttrCons *attr = CirMem_balloc(sizeof(*attr) + sizeof(CirAttr *) * len, alignof(*attr));
attr->data1 = typeToData1(CIRATTR_CONS) | numArgsToData1(len);
attr->name = name;
for (uint32_t i = 0; i < len; i++)
attr->args[i] = args[i];
return (CirAttr *)attr;
}
bool
CirAttr_isInt(const CirAttr *attr)
{
return data1ToType(attr->data1) == CIRATTR_INT;
}
bool
CirAttr_isStr(const CirAttr *attr)
{
return data1ToType(attr->data1) == CIRATTR_STR;
}
bool
CirAttr_isName(const CirAttr *attr)
{
return data1ToType(attr->data1) == CIRATTR_NAME;
}
bool
CirAttr_isCons(const CirAttr *attr)
{
return data1ToType(attr->data1) == CIRATTR_CONS;
}
CirName
CirAttr_getName(const CirAttr *attr)
{
switch (data1ToType(attr->data1)) {
case CIRATTR_NAME:
return ((const CirAttrName *)attr)->name;
case CIRATTR_CONS:
return ((const CirAttrCons *)attr)->name;
default:
cir_bug("CirAttr_getName: not a name");
}
}
size_t
CirAttr_getNumArgs(const CirAttr *attr)
{
if (data1ToType(attr->data1) != CIRATTR_CONS)
cir_bug("CirAttr_getNumArgs: attr is not cons type");
return data1ToNumArgs(attr->data1);
}
const CirAttr * const *
CirAttr_getArgs(const CirAttr *attr)
{
if (data1ToType(attr->data1) != CIRATTR_CONS)
cir_bug("CirAttr_getArgs: attr is not cons type");
return ((const CirAttrCons *)attr)->args;
}
void
CirAttrArray__add(CirAttrArray *arr, const CirAttr *item)
{
CirArray_grow(arr, 1);
CirName name = CirAttr_getName(item);
size_t i;
for (i = 0; i < arr->len; i++) {
CirName itemName = CirAttr_getName(arr->items[i]);
if (itemName == name) {
// Do not add if already in there
return;
} else if (itemName > name) {
break;
}
}
for (size_t j = arr->len; j > i; j--) {
arr->items[j] = arr->items[j - 1];
}
arr->items[i] = item;
arr->len++;
}
void
CirAttrArray__merge(CirAttrArray *arr, const CirAttr * const *srcA, size_t lenA, const CirAttr * const *srcB, size_t lenB)
{
size_t aIdx = 0, bIdx = 0, i = 0;
CirArray_alloc(arr, lenA + lenB);
while (aIdx < lenA && bIdx < lenB) {
CirName nameA = CirAttr_getName(srcA[aIdx]);
CirName nameB = CirAttr_getName(srcB[bIdx]);
if (nameA < nameB) {
// Choose nameA
arr->items[i++] = srcA[aIdx];
aIdx++;
} else if (nameA == nameB) {
// Choose nameA
arr->items[i++] = srcA[aIdx];
aIdx++;
bIdx++;
} else { // nameA > nameB
// Choose nameB
arr->items[i++] = srcB[bIdx];
bIdx++;
}
}
while (aIdx < lenA)
arr->items[i++] = srcA[aIdx++];
while (bIdx < lenB)
arr->items[i++] = srcB[bIdx++];
arr->len = i;
}
void
CirAttrArray__remove(CirAttrArray *arr, const CirAttr *const *srcA, size_t lenA, const CirAttr *const *removeB, size_t lenB)
{
size_t aIdx = 0, bIdx = 0, i = 0;
CirArray_alloc(arr, lenA);
while (aIdx < lenA && bIdx < lenB) {
CirName nameA = CirAttr_getName(srcA[aIdx]);
CirName nameB = CirAttr_getName(removeB[bIdx]);
if (nameA < nameB) {
// Can add nameA
arr->items[i++] = srcA[aIdx];
aIdx++;
} else if (nameA == nameB) {
// Don't add nameA
aIdx++;
} else { // nameA > nameB
bIdx++;
}
}
// Add the rest
while (aIdx < lenA)
arr->items[i++] = srcA[aIdx++];
assert(i <= arr->alloc);
arr->len = i;
}
static bool
isNameAttr(CirName name)
{
assert(name);
const char *n = CirName_cstr(name);
return !strcmp(n, "section") ||
!strcmp(n, "constructor") ||
!strcmp(n, "destructor") ||
!strcmp(n, "unused") ||
!strcmp(n, "used") ||
!strcmp(n, "weak") ||
!strcmp(n, "no_instrument_function") ||
!strcmp(n, "alias") ||
!strcmp(n, "no_check_memory_usage") ||
!strcmp(n, "exception") ||
!strcmp(n, "__asm__");
}
static bool
isFunAttr(CirName name)
{
assert(name);
const char *n = CirName_cstr(name);
return !strcmp(n, "format") ||
!strcmp(n, "regparm") ||
!strcmp(n, "longcall") ||
!strcmp(n, "noinline") ||
!strcmp(n, "always_inline") ||
!strcmp(n, "gnu_inline") ||
!strcmp(n, "leaf") ||
!strcmp(n, "artificial") ||
!strcmp(n, "warn_unused_result") ||
!strcmp(n, "nonnull");
}
static bool
isTypeAttr(CirName name)
{
assert(name);
const char *n = CirName_cstr(name);
return !strcmp(n, "const") ||
!strcmp(n, "volatile") ||
!strcmp(n, "restrict") ||
!strcmp(n, "mode");
}
static bool
isPrintOutsideAttr(CirName name)
{
assert(name);
const char *n = CirName_cstr(name);
return !strcmp(n, "const") ||
!strcmp(n, "volatile") ||
!strcmp(n, "__asm__") ||
!strcmp(n, "restrict");
}
void
CirAttr__partition(const CirAttr * const *attrs, size_t numAttrs, CirAttrArray *outName, CirAttrArray *outFun, CirAttrArray *outType, int _default)
{
for (size_t i = 0; i < numAttrs; i++) {
CirName name = CirAttr_getName(attrs[i]);
if (isNameAttr(name)) {
CirArray_push(outName, &attrs[i]);
} else if (isFunAttr(name)) {
CirArray_push(outFun, &attrs[i]);
} else if (isTypeAttr(name)) {
CirArray_push(outType, &attrs[i]);
} else if (_default == CIRATTR_PARTITION_DEFAULT_NAME) {
CirArray_push(outName, &attrs[i]);
} else if (_default == CIRATTR_PARTITION_DEFAULT_FUN) {
CirArray_push(outFun, &attrs[i]);
} else if (_default == CIRATTR_PARTITION_DEFAULT_TYPE) {
CirArray_push(outType, &attrs[i]);
} else {
cir_bug("CirAttr__partition: invalid default");
}
}
}
static const char *
mapName(CirName name)
{
assert(name);
const char *n = CirName_cstr(name);
if (!strcmp(n, "restrict")) {
return "__restrict";
} else {
return n;
}
}
static void
CirAttr_print(CirFmt printer, const CirAttr *attr)
{
switch (data1ToType(attr->data1)) {
case CIRATTR_INT:
CirFmt_printI32(printer, ((const CirAttrInt *)attr)->i);
break;
case CIRATTR_STR:
CirFmt_printString(printer, ((const CirAttrStr *)attr)->s);
break;
case CIRATTR_NAME:
CirFmt_printString(printer, mapName(((const CirAttrName *)attr)->name));
break;
case CIRATTR_CONS:
CirFmt_printString(printer, mapName(((const CirAttrCons *)attr)->name));
CirFmt_printString(printer, "(");
size_t numArgs = CirAttr_getNumArgs(attr);
const CirAttr * const *args = CirAttr_getArgs(attr);
for (size_t i = 0; i < numArgs; i++) {
if (i)
CirFmt_printString(printer, ", ");
CirAttr_print(printer, args[i]);
}
CirFmt_printString(printer, ")");
break;
default:
cir_bug("CirAttr_log: unhandled attr type");
}
}
void
CirAttr_log(const CirAttr *attr)
{
CirAttr_print(CirLog_printb, attr);
}
void
CirAttr_printArray(CirFmt printer, const CirAttr * const *attrs, size_t numAttrs)
{
// First pass: attributes which should not be in the attrs block
bool printSpace = false;
for (size_t i = 0; i < numAttrs; i++) {
CirName name = CirAttr_getName(attrs[i]);
if (isPrintOutsideAttr(name)) {
if (printSpace)
CirFmt_printString(printer, " ");
CirAttr_print(printer, attrs[i]);
printSpace = true;
}
}
// Second pass: attributes which should be in the attrs block
bool printComma = false;
for (size_t i = 0; i < numAttrs; i++) {
CirName name = CirAttr_getName(attrs[i]);
if (isPrintOutsideAttr(name)) {
// do nothing
} else {
if (printComma) {
CirFmt_printString(printer, ", ");
} else {
if (printSpace)
CirFmt_printString(printer, " ");
CirFmt_printString(printer, "__attribute__((");
}
printComma = true;
CirAttr_print(printer, attrs[i]);
}
}
if (printComma)
CirFmt_printString(printer, "))");
}
void
CirAttr__logArray(const CirAttr *const *attrs, size_t numAttrs)
{
CirAttr_printArray(CirLog_printb, attrs, numAttrs);
}