-
Notifications
You must be signed in to change notification settings - Fork 0
/
exports.c
105 lines (98 loc) · 1.88 KB
/
exports.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
#include "yo.h"
static FILE *f;
static int
retlen(Type *t)
{
if(t == tnone)
return 0;
if(t->kind != Ttup)
return 1;
return decllen(t->ids);
}
static void
tyexported(Decl **arr, int n)
{
for(int i = 0; i < n; ++i){
Decl *d = arr[i];
if(isexported(d)==0)
continue;
Type *t = d->ty;
int l = decllen(t->ids);
fprintf(f, "type .%s struct %d %d\n",
d->sym->name, t->size,l);
for(Decl *dd=t->ids; dd;dd=dd->next){
Type *tt = dd->ty;
fprintf(f,"\t%s %s %d %d %d\n",
dd->sym->name,
tt->decl->sym->name,
tt->kind,
tt->size,
dd->offset);
}
}
}
static void
fnexported(Decl **arr, int n)
{
for(int i = 0; i < n; ++i){
Decl *d = arr[i];
if(isexported(d)==0)
continue;
Type *t = d->ty;
fprintf(f, "fn .%s %d %d %d %d\n",
d->sym->name, d->offset, d->pc->pc,
decllen(t->ids),
retlen(t->tof));
fprintf(f, "\t%d\n", t->size);
for(Decl *dd=t->ids; dd;dd=dd->next){
Type *tt = dd->ty;
fprintf(f,"\t%s %d %d %d\n",
tt->decl->sym->name,
tt->kind,
tt->size,
dd->offset);
}
t = t->tof;
bindsize(t);
fprintf(f, "\t%d\n", t->size);
if(t->kind==Ttup){
for(Decl *dd=t->ids; dd;dd=dd->next){
Type *tt = dd->ty;
fprintf(f,"\t%s %d %d %d\n",
tt->decl->sym->name,
tt->kind,
tt->size,
dd->offset);
}
}else
fprintf(f,"\t%s %d %d %d\n",
t->decl->sym->name,
t->kind,
t->size,
t->decl->offset);
}
}
void
genexports(char *name)
{
f = fopen(name, "w+");
assert(f != nil);
fprintf(f, "%s\n", pkgname->name);
tyexported(types, ntype);
fnexported(fns, nfn);
fprintf(f, "\n");
fclose(f);
}
void
genbin(char *name)
{
f = fopen(name, "w+");
// fprintf(f, "%s\n", pkgname->name);
// fprintf(f, "%d\n", nimport);
// for(int i = 0; i < nimport; ++i){
// fprintf(f, "%d %s %s\n", i, imports[i].path->name, imports[i].sym->name);
// }
wr4(f, ninst);
disinst(f, firstinst);
fclose(f);
}