1
0
mirror of https://github.com/golang/go synced 2024-11-23 03:40:02 -07:00

embedded methods

R=r
OCL=17851
CL=17851
This commit is contained in:
Ken Thompson 2008-10-25 13:31:25 -07:00
parent 22a6747999
commit b642cf8c00
3 changed files with 71 additions and 30 deletions

View File

@ -513,7 +513,9 @@ void
gentramp(Type *t, Sig *b)
{
Sym *e;
int c, d;
int c, d, o;
Prog *p;
Type *f;
e = lookup(b->name);
for(d=0; d<nelem(dotlist); d++) {
@ -524,25 +526,74 @@ gentramp(Type *t, Sig *b)
fatal("gentramp");
out:
print("gentramp %d\n", d);
print(" t = %lT\n", t);
print(" name = %s\n", b->name);
print(" sym = %S\n", b->sym);
print(" hash = 0x%ux\n", b->hash);
// print("gentramp %d\n", d);
// print(" t = %lT\n", t);
// print(" name = %s\n", b->name);
// print(" sym = %S\n", b->sym);
// print(" hash = 0x%ux\n", b->hash);
//TEXT main·S_test2(SB),7,$0
p = pc;
gins(ATEXT, N, N);
p->from.type = D_EXTERN;
p->from.sym = b->sym;
p->to.type = D_CONST;
p->to.offset = 0;
p->from.scale = 7;
//print("1. %P\n", p);
//MOVQ 8(SP), AX
p = pc;
gins(AMOVQ, N, N);
p->from.type = D_INDIR+D_SP;
p->from.offset = 8;
p->to.type = D_AX;
//print("2. %P\n", p);
o = 0;
for(c=d-1; c>=0; c--) {
print(" %d %d %S\n",
dotlist[c].ptr,
dotlist[c].offset,
dotlist[c].sym);
f = dotlist[c].field;
o += f->width;
if(!isptr[f->type->etype])
continue;
//MOVQ o(AX), AX
p = pc;
gins(AMOVQ, N, N);
p->from.type = D_INDIR+D_AX;
p->from.offset = o;
p->to.type = D_AX;
//print("3. %P\n", p);
o = 0;
}
if(o != 0) {
//ADDQ $XX, AX
p = pc;
gins(AADDQ, N, N);
p->from.type = D_CONST;
p->from.offset = o;
p->to.type = D_AX;
//print("4. %P\n", p);
}
//TEXT main·S_test2(SB),7,$0
// MOVQ 8(SP), AX
// MOVQ XX(AX), AX
// ADDQ $XX, AX
// MOVQ AX, 8(SP)
// JMP main·Sub_test2(SB)
//MOVQ AX, 8(SP)
p = pc;
gins(AMOVQ, N, N);
p->from.type = D_AX;
p->to.type = D_INDIR+D_SP;
p->to.offset = 8;
//print("5. %P\n", p);
f = dotlist[0].field;
//JMP main·Sub_test2(SB)
snprint(namebuf, sizeof(namebuf), "%s_%s",
f->sym->name, b->name);
if(isptr[f->type->etype])
f = f->type;
p = pc;
gins(AJMP, N, N);
p->to.type = D_EXTERN;
p->to.sym = pkglookup(namebuf, f->type->sym->opackage);
//print("6. %P\n", p);
}
void

View File

@ -402,9 +402,7 @@ struct Io
typedef struct Dlist Dlist;
struct Dlist
{
Sym* sym;
uchar ptr;
int offset;
Type* field;
};
EXTERN Dlist dotlist[10]; // size is max depth of embeddeds

View File

@ -2445,13 +2445,8 @@ adddot1(Sym *s, Type *t, int d)
if(f->sym == S)
continue;
a = adddot1(s, f->type, d);
if(a != 0 && c == 0) {
dotlist[d].sym = f->sym;
dotlist[d].offset = f->width;
dotlist[d].ptr = 0;
if(isptr[f->type->etype])
dotlist[d].ptr = 1;
}
if(a != 0 && c == 0)
dotlist[d].field = f;
c += a;
}
@ -2497,7 +2492,7 @@ out:
// rebuild elided dots
for(c=d-1; c>=0; c--) {
n = nod(ODOT, n, n->right);
n->left->right = newname(dotlist[c].sym);
n->left->right = newname(dotlist[c].field->sym);
}
return n;
}
@ -2609,7 +2604,6 @@ expandmeth(Sym *s, Type *t)
}
}
//print("expand %S: %lT", s, t);
for(sl=slist; sl!=nil; sl=sl->link) {
if(sl->good) {
// add it to the base type method list
@ -2620,8 +2614,6 @@ expandmeth(Sym *s, Type *t)
f->down = t->method;
t->method = f;
//print(" %T", f);
}
}
//print("\n");
}