-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.c
944 lines (791 loc) · 20.9 KB
/
types.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/*
* Copyright (c) 2012 Message Systems, Inc. All rights reserved
* For licensing information, see:
* https://bitbucket.org/wez/gimli/src/tip/LICENSE
*/
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* (Portions derived from ctf_types.c and ctf_decl.c)
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "impl.h"
struct gimli_type_collection {
int refcnt;
/* hash of name => gimli_type_t, allows us
* to both lookup by name and detect collisions */
gimli_hash_t type_by_name;
/* hash of name => function type */
gimli_hash_t func_by_name;
/* list of all type objects allocated against us */
STAILQ_HEAD(typelist, gimli_type) typelist;
};
struct gimli_type {
STAILQ_ENTRY(gimli_type) typelist;
int kind;
const char *name;
char *declname;
char declbuf[24];
struct gimli_type_encoding enc;
gimli_type_t target;
struct gimli_type_arinfo arinfo;
struct {
const char *name;
union {
struct gimli_type_membinfo info;
int value;
} u;
} *members;
int num_members;
int alloc_members;
};
/* Some definitions for canonicalizing type names */
/*
* CTF Declaration Stack
*
* In order to implement ctf_type_name(), we must convert a type graph back
* into a C type declaration. Unfortunately, a type graph represents a storage
* class ordering of the type whereas a type declaration must obey the C rules
* for operator precedence, and the two orderings are frequently in conflict.
* For example, consider these CTF type graphs and their C declarations:
*
* CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER : int (*)()
* CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER : int (*)[]
*
* In each case, parentheses are used to raise operator * to higher lexical
* precedence, so the string form of the C declaration cannot be constructed by
* walking the type graph links and forming the string from left to right.
*
* The functions in this file build a set of stacks from the type graph nodes
* corresponding to the C operator precedence levels in the appropriate order.
* The code in ctf_type_name() can then iterate over the levels and nodes in
* lexical precedence order and construct the final C declaration string.
*/
typedef enum {
PREC_BASE,
PREC_POINTER,
PREC_ARRAY,
PREC_FUNCTION,
PREC_MAX
} decl_prec_t;
typedef struct decl_node {
/* linkage */
TAILQ_ENTRY(decl_node) list;
/* type */
gimli_type_t type;
/* type dimension if array */
unsigned int n;
} *decl_node_t;
typedef struct decl {
/* declaration node stacks */
TAILQ_HEAD(nodes, decl_node) nodes[PREC_MAX];
/* storage order of decls */
int order[PREC_MAX];
/* qualifier precision */
decl_prec_t qualp;
/* ordered precision */
decl_prec_t ordp;
/* buffer for output */
char *buf;
/* buffer location */
char *ptr;
/* buffer limit */
char *end;
/* space required */
size_t len;
} *decl_t;
static void decl_init(decl_t cd, char *buf, size_t len)
{
int i;
memset(cd, 0, sizeof(*cd));
for (i = PREC_BASE; i < PREC_MAX; i++) {
cd->order[i] = PREC_BASE - 1;
TAILQ_INIT(&cd->nodes[i]);
}
cd->qualp = PREC_BASE;
cd->ordp = PREC_BASE;
cd->buf = buf;
cd->ptr = buf;
cd->end = buf + len;
}
static void decl_fini(decl_t cd)
{
decl_node_t cdp, ndp;
int i;
for (i = PREC_BASE; i < PREC_MAX; i++) {
TAILQ_FOREACH_SAFE(cdp, &cd->nodes[i], list, ndp) {
free(cdp);
}
}
}
static void decl_push(decl_t cd, gimli_type_t type)
{
decl_node_t cdp;
decl_prec_t prec;
int n = 1;
int is_qual = 0;
switch (type->kind) {
case GIMLI_K_ARRAY:
decl_push(cd, type->arinfo.contents);
n = type->arinfo.nelems;
prec = PREC_ARRAY;
break;
case GIMLI_K_TYPEDEF:
if (type->name[0] == '\0') {
decl_push(cd, type->target);
return;
}
prec = PREC_BASE;
break;
case GIMLI_K_FUNCTION:
decl_push(cd, type->target);
prec = PREC_FUNCTION;
break;
case GIMLI_K_POINTER:
decl_push(cd, type->target);
prec = PREC_POINTER;
break;
case GIMLI_K_VOLATILE:
case GIMLI_K_CONST:
case GIMLI_K_RESTRICT:
decl_push(cd, type->target);
prec = cd->qualp;
is_qual++;
break;
default:
prec = PREC_BASE;
}
cdp = calloc(1, sizeof(*cdp));
cdp->type = type;
cdp->n = n;
if (TAILQ_FIRST(&cd->nodes[prec]) == NULL) {
cd->order[prec] = cd->ordp++;
}
/* reset qualp to the highest precedence level that we've seen
* so far that can be qualified (BASE or POINTER) */
if (prec > cd->qualp && prec < PREC_ARRAY) {
cd->qualp = prec;
}
/* C array declarators are ordered inside-out, so prepend them.
* Also by convention qualifiers of base types precede the type
* specified (e.g.: const int vs int const) even though the two
* forms are equivalent */
if (type->kind == GIMLI_K_ARRAY || (is_qual && prec == PREC_BASE)) {
TAILQ_INSERT_HEAD(&cd->nodes[prec], cdp, list);
} else {
TAILQ_INSERT_TAIL(&cd->nodes[prec], cdp, list);
}
}
static void decl_sprintf(decl_t cd, const char *fmt, ...)
{
size_t len = (size_t)(cd->end - cd->ptr);
va_list ap;
size_t n;
va_start(ap, fmt);
n = vsnprintf(cd->ptr, len, fmt, ap);
va_end(ap);
cd->ptr += n < len ? n : len;
cd->len += n;
}
/* print a string name for a type into buf.
* Return the actual number of bytes (not including \0) needed to
* format the name.
*/
static ssize_t decl_lname(gimli_type_t type, char *buf, size_t len)
{
struct decl cd;
decl_node_t cdp;
decl_prec_t prec, lp, rp;
int ptr, arr;
int k;
decl_init(&cd, buf, len);
decl_push(&cd, type);
ptr = cd.order[PREC_POINTER] > PREC_POINTER;
arr = cd.order[PREC_ARRAY] > PREC_ARRAY;
rp = arr ? PREC_ARRAY : ptr ? PREC_POINTER : -1;
lp = ptr ? PREC_POINTER : arr ? PREC_ARRAY : -1;
/* avoid leading whitespace (see below) */
k = GIMLI_K_POINTER;
for (prec = PREC_BASE; prec < PREC_MAX; prec++) {
TAILQ_FOREACH(cdp, &cd.nodes[prec], list) {
if (k != GIMLI_K_POINTER && k != GIMLI_K_ARRAY) {
decl_sprintf(&cd, " ");
}
if (lp == prec) {
decl_sprintf(&cd, "(");
lp = -1;
}
switch (cdp->type->kind) {
case GIMLI_K_INTEGER:
case GIMLI_K_FLOAT:
case GIMLI_K_TYPEDEF:
decl_sprintf(&cd, "%s", cdp->type->name);
break;
case GIMLI_K_POINTER:
decl_sprintf(&cd, "*");
break;
case GIMLI_K_ARRAY:
decl_sprintf(&cd, "[%u]", cdp->n);
break;
case GIMLI_K_FUNCTION:
decl_sprintf(&cd, "()");
break;
case GIMLI_K_STRUCT:
// case GIMLI_K_FORWARD:
decl_sprintf(&cd, "struct %s", cdp->type->name);
break;
case GIMLI_K_UNION:
decl_sprintf(&cd, "union %s", cdp->type->name);
break;
case GIMLI_K_ENUM:
decl_sprintf(&cd, "enum %s", cdp->type->name);
break;
case GIMLI_K_VOLATILE:
decl_sprintf(&cd, "volatile");
break;
case GIMLI_K_CONST:
decl_sprintf(&cd, "const");
break;
case GIMLI_K_RESTRICT:
decl_sprintf(&cd, "restrict");
break;
}
k = cdp->type->kind;
}
if (rp == prec) {
decl_sprintf(&cd, ")");
}
}
decl_fini(&cd);
return cd.len;
}
gimli_type_collection_t gimli_type_collection_new(void)
{
gimli_type_collection_t col = calloc(1, sizeof(*col));
if (!col) return NULL;
col->refcnt = 1;
STAILQ_INIT(&col->typelist);
col->type_by_name = gimli_hash_new(NULL);
col->func_by_name = gimli_hash_new(NULL);
return col;
}
void gimli_type_collection_addref(gimli_type_collection_t col)
{
col->refcnt++;
}
static void delete_type(gimli_type_t t)
{
int i;
for (i = 0; i < t->num_members; i++) {
switch (t->kind) {
case GIMLI_K_STRUCT:
case GIMLI_K_UNION:
case GIMLI_K_ENUM:
case GIMLI_K_FUNCTION:
;
//free(t->members[i].name);
}
}
free(t->members);
// free(t->name);
if (t->declname != t->declbuf) {
free(t->declname);
}
free(t);
}
static gimli_iter_status_t type_rvisit(gimli_type_t t,
gimli_type_visit_f func,
void *arg, const char *name, uint64_t offset, int depth)
{
gimli_iter_status_t status;
int i;
status = func(name, t, offset, depth, arg);
if (status != GIMLI_ITER_CONT) {
return status;
}
if (t->kind != GIMLI_K_STRUCT && t->kind != GIMLI_K_UNION) {
return GIMLI_ITER_CONT;
}
for (i = 0; i < t->num_members; i++) {
status = type_rvisit(t->members[i].u.info.type, func, arg,
t->members[i].name, offset + t->members[i].u.info.offset, depth + 1);
if (status != GIMLI_ITER_CONT) {
return status;
}
}
return GIMLI_ITER_CONT;
}
gimli_iter_status_t gimli_type_visit(gimli_type_t t,
gimli_type_visit_f func,
void *arg)
{
return type_rvisit(t, func, arg, "", 0, 0);
}
gimli_iter_status_t gimli_type_member_visit(
gimli_type_t t,
gimli_type_member_visit_f func,
void *arg
)
{
gimli_iter_status_t status = GIMLI_ITER_CONT;
int i;
if (t->kind != GIMLI_K_STRUCT && t->kind != GIMLI_K_UNION) {
return GIMLI_ITER_ERR;
}
for (i = 0; i < t->num_members; i++) {
status = func(t->members[i].name, &t->members[i].u.info, arg);
if (status != GIMLI_ITER_CONT) {
break;
}
}
return status;
}
void gimli_type_collection_delete(gimli_type_collection_t col)
{
if (--col->refcnt) return;
gimli_hash_destroy(col->type_by_name);
gimli_hash_destroy(col->func_by_name);
while (!STAILQ_EMPTY(&col->typelist)) {
gimli_type_t t = STAILQ_FIRST(&col->typelist);
STAILQ_REMOVE_HEAD(&col->typelist, typelist);
delete_type(t);
}
free(col);
}
gimli_iter_status_t gimli_type_collection_visit(gimli_type_collection_t col,
gimli_type_collection_visit_f func, void *arg)
{
gimli_type_t t, tmp;
gimli_iter_status_t status;
STAILQ_FOREACH_SAFE(t, &col->typelist, typelist, tmp) {
status = func(col, t, arg);
if (status != GIMLI_ITER_CONT) {
break;
}
}
return status;
}
gimli_type_t gimli_type_collection_find_type(
gimli_type_collection_t col,
const char *name)
{
gimli_type_t t;
if (gimli_hash_find(col->type_by_name, name,
(void**)&t)) {
return t;
}
return NULL;
}
struct type_lookup_data {
const char *typename;
gimli_type_t result;
};
static gimli_iter_status_t find_type_in_object1(
const char *k, int klen, void *item, void *arg)
{
struct type_lookup_data *data = arg;
gimli_mapped_object_t file = item;
if (file->types) {
data->result = gimli_type_collection_find_type(file->types, data->typename);
if (data->result) {
return GIMLI_ITER_STOP;
}
}
return GIMLI_ITER_CONT;
}
static gimli_iter_status_t find_type_in_object2(
const char *k, int klen, void *item, void *arg)
{
struct type_lookup_data *data = arg;
gimli_mapped_object_t file = item;
gimli_dwarf_load_all_types(file);
if (file->types) {
data->result = gimli_type_collection_find_type(file->types, data->typename);
if (data->result) {
return GIMLI_ITER_STOP;
}
}
return GIMLI_ITER_CONT;
}
gimli_type_t gimli_find_type_by_name(gimli_proc_t proc,
const char *objname,
const char *typename)
{
gimli_mapped_object_t f;
struct type_lookup_data data;
memset(&data, 0, sizeof(data));
data.typename = typename;
/* stage 1: lookup in already processed type data */
gimli_hash_iter(proc->files, find_type_in_object1, &data);
if (data.result) {
return data.result;
}
/* stage 2: more expensive crawl of type data */
gimli_hash_iter(proc->files, find_type_in_object2, &data);
return data.result;
}
gimli_type_t gimli_find_type_by_addr(gimli_proc_t proc,
gimli_addr_t addr)
{
return gimli_dwarf_load_type_for_data(proc, addr);
}
gimli_type_t gimli_type_collection_find_function(
gimli_type_collection_t col,
const char *name)
{
gimli_type_t t;
if (gimli_hash_find(col->func_by_name, name,
(void**)&t)) {
return t;
}
return NULL;
}
const char *gimli_type_name(gimli_type_t t)
{
return t->name;
}
const char *gimli_type_declname(gimli_type_t t)
{
ssize_t size;
if (t->declname) return t->declname;
size = decl_lname(t, t->declbuf, sizeof(t->declbuf));
if (size > sizeof(t->declbuf) - 1) {
t->declname = malloc(size + 1);
decl_lname(t, t->declname, size + 1);
} else {
t->declname = t->declbuf;
}
return t->declname;
}
size_t gimli_type_size(gimli_type_t t)
{
size_t s;
t = gimli_type_resolve(t);
s = t->enc.bits;
#if 0
if (s == 0) {
printf("gimli_type_size: kind=%d name=%s has 0 size!\n",
t->kind, t->name);
}
#endif
return s;
}
int gimli_type_kind(gimli_type_t t)
{
return t->kind;
}
void gimli_type_encoding(gimli_type_t t,
struct gimli_type_encoding *enc)
{
memcpy(enc, &t->enc, sizeof(t->enc));
}
static gimli_type_t new_type(gimli_type_collection_t col,
int kind, const char *name,
const struct gimli_type_encoding *enc)
{
gimli_type_t t;
#if 0
if (name) {
switch (kind) {
case GIMLI_K_FUNCTION:
if (gimli_hash_find(col->func_by_name, name, (void**)&t)) {
return t;
}
break;
default:
if (gimli_hash_find(col->type_by_name, name, (void**)&t)) {
return t;
}
break;
}
}
#endif
t = calloc(1, sizeof(*t));
if (!t) return NULL;
STAILQ_INSERT_TAIL(&col->typelist, t, typelist);
t->kind = kind;
if (enc) {
memcpy(&t->enc, enc, sizeof(t->enc));
}
if (name) {
t->name = name;
if (!t->name) {
return NULL;
}
switch (kind) {
case GIMLI_K_FUNCTION:
gimli_hash_insert(col->func_by_name, name, t);
break;
default:
gimli_hash_insert(col->type_by_name, name, t);
break;
}
} else {
t->name = "<anon>";//strdup("<anon>");
}
#if 0
printf(
"TYPE: %p %s kind=%d format=%" PRIx64
" offset=%" PRIu32 " bits=%" PRIu32 "\n",
t, t->name, t->kind, t->enc.format, t->enc.offset, t->enc.bits);
#endif
return t;
}
gimli_type_t gimli_type_new_array(gimli_type_collection_t col,
const struct gimli_type_arinfo *info)
{
gimli_type_t t = new_type(col, GIMLI_K_ARRAY, NULL, NULL);
memcpy(&t->arinfo, info, sizeof(*info));
t->enc.bits = t->arinfo.nelems * gimli_type_size(t->arinfo.contents);
return t;
}
int gimli_type_arinfo(gimli_type_t t,
struct gimli_type_arinfo *info)
{
if (t->kind != GIMLI_K_ARRAY) return 0;
memcpy(info, &t->arinfo, sizeof(t->arinfo));
return 1;
}
gimli_type_t gimli_type_new_integer(gimli_type_collection_t col,
const char *name, const struct gimli_type_encoding *enc)
{
return new_type(col, GIMLI_K_INTEGER, name, enc);
}
gimli_type_t gimli_type_new_float(gimli_type_collection_t col,
const char *name, const struct gimli_type_encoding *enc)
{
return new_type(col, GIMLI_K_FLOAT, name, enc);
}
gimli_type_t gimli_type_resolve(gimli_type_t t)
{
/* simple cycle detection */
gimli_type_t prev = t, orig = t;
do {
switch (t->kind) {
case GIMLI_K_TYPEDEF:
case GIMLI_K_VOLATILE:
case GIMLI_K_CONST:
case GIMLI_K_RESTRICT:
if (t->target == t || t->target == prev ||
t->target == orig) {
fprintf(stderr, "type cycle detected for %p\n",
t);
return NULL;
}
prev = t;
t = t->target;
break;
default:
return t;
}
} while (t);
return NULL;
}
static gimli_type_t new_alias(gimli_type_collection_t col,
int kind, gimli_type_t target)
{
struct gimli_type_encoding enc;
gimli_type_t t;
memset(&enc, 0, sizeof(enc));
if (kind == GIMLI_K_POINTER) {
enc.bits = 8 * sizeof(void*);
}
t = new_type(col, kind, NULL, &enc);
if (!t) return NULL;
t->target = target;
return t;
}
gimli_type_t gimli_type_new_struct(gimli_type_collection_t col, const char *name)
{
return new_type(col, GIMLI_K_STRUCT, name, NULL);
}
gimli_type_t gimli_type_new_union(gimli_type_collection_t col, const char *name)
{
return new_type(col, GIMLI_K_UNION, name, NULL);
}
gimli_type_t gimli_type_new_typedef(gimli_type_collection_t col,
gimli_type_t target, const char *name)
{
gimli_type_t t = new_type(col, GIMLI_K_TYPEDEF, name, NULL);
if (t) t->target = target;
return t;
}
gimli_type_t gimli_type_new_volatile(gimli_type_collection_t col,
gimli_type_t target)
{
return new_alias(col, GIMLI_K_VOLATILE, target);
}
gimli_type_t gimli_type_new_restrict(gimli_type_collection_t col,
gimli_type_t target)
{
return new_alias(col, GIMLI_K_RESTRICT, target);
}
gimli_type_t gimli_type_new_const(gimli_type_collection_t col,
gimli_type_t target)
{
return new_alias(col, GIMLI_K_CONST, target);
}
gimli_type_t gimli_type_new_pointer(gimli_type_collection_t col,
gimli_type_t target)
{
return new_alias(col, GIMLI_K_POINTER, target);
}
gimli_type_t gimli_type_follow_pointer(gimli_type_t t)
{
return t->target;
}
gimli_type_t gimli_type_new_function(gimli_type_collection_t col,
const char *name,
uint32_t flags,
gimli_type_t rettype)
{
gimli_type_t t;
struct gimli_type_encoding enc;
memset(&enc, 0, sizeof(enc));
enc.bits = 8 * sizeof(void*);
enc.format = flags;
t = new_type(col, GIMLI_K_FUNCTION, name, &enc);
/* for functions, the return type is stored in target */
t->target = rettype;
return t;
}
int gimli_type_funcinfo(gimli_type_t t,
struct gimli_type_funcinfo *info)
{
if (t->kind != GIMLI_K_FUNCTION) return 0;
info->rettype = t->target;
info->nargs = t->num_members;
info->flags = t->enc.format;
return 1;
}
static int grow_members(gimli_type_t t)
{
if (t->num_members + 1 >= t->alloc_members) {
t->alloc_members = t->alloc_members ? t->alloc_members * 2 : 8;
t->members = realloc(t->members, t->alloc_members * sizeof(*t->members));
}
return 1;
}
int gimli_type_function_add_parameter(gimli_type_t t,
const char *name, gimli_type_t arg)
{
int n;
if (t->kind != GIMLI_K_FUNCTION) {
return -1;
}
grow_members(t);
n = t->num_members++;
t->members[n].name = name;// ? strdup(name) : NULL;
t->members[n].u.info.type = arg;
return n;
}
gimli_type_t gimli_type_new_enum(gimli_type_collection_t col,
const char *name, const struct gimli_type_encoding *enc)
{
return new_type(col, GIMLI_K_ENUM, name, enc);
}
int gimli_type_enum_add(gimli_type_t t, const char *name, int value)
{
int n;
if (t->kind != GIMLI_K_ENUM) {
return -1;
}
grow_members(t);
n = t->num_members++;
t->members[n].name = name;//strdup(name);
t->members[n].u.value = value;
return n;
}
const char *gimli_type_enum_resolve(gimli_type_t t, int value)
{
int i;
if (t->kind != GIMLI_K_ENUM) {
return NULL;
}
for (i = 0; i < t->num_members; i++) {
if (t->members[i].u.value == value) {
return t->members[i].name;
}
}
return NULL;
}
int gimli_type_membinfo(gimli_type_t t,
const char *name,
struct gimli_type_membinfo *info)
{
int i;
if (!t->members) return 0;
for (i = 0; i < t->num_members; i++) {
if (!strcmp(t->members[i].name, name)) {
memcpy(info, &t->members[i].u.info, sizeof(*info));
return 1;
}
}
return 0;
}
int gimli_type_add_member(gimli_type_t t,
const char *name,
gimli_type_t membertype,
uint64_t size,
uint64_t offset)
{
int n, i;
uint64_t biggest;
switch (t->kind) {
case GIMLI_K_STRUCT:
case GIMLI_K_UNION:
break;
default:
return -1;
}
grow_members(t);
n = t->num_members++;
if (size) {
t->members[n].u.info.size = size;
t->members[n].u.info.offset = offset;
} else {
/* calculate something reasonable.
* TODO: verify alignment! */
t->members[n].u.info.offset = 0;
if (n > 0) {
t->members[n].u.info.offset =
t->members[n - 1].u.info.offset
+ gimli_type_size(t->members[n - 1].u.info.type);
}
t->members[n].u.info.size = gimli_type_size(membertype);
}
t->members[n].name = name;//strdup(name);
t->members[n].u.info.type = membertype;
/* re-compute overall size */
t->enc.bits = 0;
biggest = 0;
for (i = 0; i < t->num_members; i++) {
size = t->members[i].u.info.offset + gimli_type_size(t->members[i].u.info.type);
if (size > biggest) {
biggest = size;
}
}
t->enc.bits = biggest;
return n;
}
/* vim:ts=2:sw=2:et:
*/