From a25a6e20ee9fb6d3d1fd33d012b41ac3e45e8cd0 Mon Sep 17 00:00:00 2001 From: Ken Thompson Date: Thu, 21 May 2009 15:44:06 -0700 Subject: some array init bugs renamed 6g/(gen|align|obj).c R=r OCL=29205 CL=29205 --- src/cmd/6g/Makefile | 6 +- src/cmd/6g/align.c | 38 --- src/cmd/6g/galign.c | 38 +++ src/cmd/6g/gen.c | 863 ---------------------------------------------------- src/cmd/6g/ggen.c | 863 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cmd/6g/gobj.c | 665 ++++++++++++++++++++++++++++++++++++++++ src/cmd/6g/obj.c | 665 ---------------------------------------- src/cmd/gc/walk.c | 9 +- 8 files changed, 1575 insertions(+), 1572 deletions(-) delete mode 100644 src/cmd/6g/align.c create mode 100644 src/cmd/6g/galign.c delete mode 100644 src/cmd/6g/gen.c create mode 100644 src/cmd/6g/ggen.c create mode 100644 src/cmd/6g/gobj.c delete mode 100644 src/cmd/6g/obj.c (limited to 'src') diff --git a/src/cmd/6g/Makefile b/src/cmd/6g/Makefile index 4d2b6ab97..d4bb7c4b4 100644 --- a/src/cmd/6g/Makefile +++ b/src/cmd/6g/Makefile @@ -15,9 +15,9 @@ HFILES=\ OFILES=\ list.$O\ - obj.$O\ - align.$O\ - gen.$O\ + gobj.$O\ + galign.$O\ + ggen.$O\ cgen.$O\ gsubr.$O\ peep.$O\ diff --git a/src/cmd/6g/align.c b/src/cmd/6g/align.c deleted file mode 100644 index fa7315057..000000000 --- a/src/cmd/6g/align.c +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -#include "gg.h" - -int thechar = '6'; -char* thestring = "amd64"; - - -/* - * go declares several platform-specific type aliases: - * int, uint, float, and uintptr - */ -Typedef typedefs[] = -{ - "int", TINT, TINT32, - "uint", TUINT, TUINT32, - "uintptr", TUINTPTR, TUINT64, - "float", TFLOAT, TFLOAT32, - 0 -}; - -void -betypeinit(void) -{ - maxround = 8; - widthptr = 8; - - zprog.link = P; - zprog.as = AGOK; - zprog.from.type = D_NONE; - zprog.from.index = D_NONE; - zprog.from.scale = 0; - zprog.to = zprog.from; - - listinit(); -} diff --git a/src/cmd/6g/galign.c b/src/cmd/6g/galign.c new file mode 100644 index 000000000..fa7315057 --- /dev/null +++ b/src/cmd/6g/galign.c @@ -0,0 +1,38 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +#include "gg.h" + +int thechar = '6'; +char* thestring = "amd64"; + + +/* + * go declares several platform-specific type aliases: + * int, uint, float, and uintptr + */ +Typedef typedefs[] = +{ + "int", TINT, TINT32, + "uint", TUINT, TUINT32, + "uintptr", TUINTPTR, TUINT64, + "float", TFLOAT, TFLOAT32, + 0 +}; + +void +betypeinit(void) +{ + maxround = 8; + widthptr = 8; + + zprog.link = P; + zprog.as = AGOK; + zprog.from.type = D_NONE; + zprog.from.index = D_NONE; + zprog.from.scale = 0; + zprog.to = zprog.from; + + listinit(); +} diff --git a/src/cmd/6g/gen.c b/src/cmd/6g/gen.c deleted file mode 100644 index 1b7505e61..000000000 --- a/src/cmd/6g/gen.c +++ /dev/null @@ -1,863 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - - -#undef EXTERN -#define EXTERN -#include "gg.h" -#include "opt.h" - -void -compile(Node *fn) -{ - Plist *pl; - Node nod1; - Prog *ptxt; - int32 lno; - Type *t; - Iter save; - - if(newproc == N) { - newproc = sysfunc("newproc"); - deferproc = sysfunc("deferproc"); - deferreturn = sysfunc("deferreturn"); - throwindex = sysfunc("throwindex"); - throwreturn = sysfunc("throwreturn"); - } - - if(fn->nbody == N) - return; - - // set up domain for labels - labellist = L; - - lno = setlineno(fn); - - curfn = fn; - dowidth(curfn->type); - - if(curfn->type->outnamed) { - // add clearing of the output parameters - t = structfirst(&save, getoutarg(curfn->type)); - while(t != T) { - if(t->nname != N) - curfn->nbody = list(nod(OAS, t->nname, N), curfn->nbody); - t = structnext(&save); - } - } - - hasdefer = 0; - walk(curfn); - if(nerrors != 0) - goto ret; - - allocparams(); - - continpc = P; - breakpc = P; - - pl = newplist(); - pl->name = curfn->nname; - pl->locals = autodcl; - - nodconst(&nod1, types[TINT32], 0); - ptxt = gins(ATEXT, curfn->nname, &nod1); - afunclit(&ptxt->from); - - ginit(); - gen(curfn->enter); - gen(curfn->nbody); - gclean(); - checklabels(); - - if(curfn->type->outtuple != 0) - ginscall(throwreturn, 0); - - if(hasdefer) - ginscall(deferreturn, 0); - pc->as = ARET; // overwrite AEND - pc->lineno = lineno; - - if(!debug['N'] || debug['R'] || debug['P']) - regopt(ptxt); - - // fill in argument size - ptxt->to.offset = rnd(curfn->type->argwid, maxround); - - // fill in final stack size - ptxt->to.offset <<= 32; - ptxt->to.offset |= rnd(stksize+maxarg, maxround); - - if(debug['f']) - frame(0); - -ret: - lineno = lno; -} - -/* - * generate: - * call f - * proc=0 normal call - * proc=1 goroutine run in new proc - * proc=2 defer call save away stack - */ -void -ginscall(Node *f, int proc) -{ - Prog *p; - Node reg, con; - - switch(proc) { - default: - fatal("ginscall: bad proc %d", proc); - break; - - case 0: // normal call - p = gins(ACALL, N, f); - afunclit(&p->to); - break; - - case 1: // call in new proc (go) - case 2: // defered call (defer) - nodreg(®, types[TINT64], D_AX); - gins(APUSHQ, f, N); - nodconst(&con, types[TINT32], argsize(f->type)); - gins(APUSHQ, &con, N); - if(proc == 1) - ginscall(newproc, 0); - else - ginscall(deferproc, 0); - gins(APOPQ, N, ®); - gins(APOPQ, N, ®); - break; - } -} - -/* - * n is call to interface method. - * generate res = n. - */ -void -cgen_callinter(Node *n, Node *res, int proc) -{ - Node *i, *f; - Node tmpi, nodo, nodr, nodsp; - - i = n->left; - if(i->op != ODOTINTER) - fatal("cgen_callinter: not ODOTINTER %O", i->op); - - f = i->right; // field - if(f->op != ONAME) - fatal("cgen_callinter: not ONAME %O", f->op); - - i = i->left; // interface - - if(!i->addable) { - tempname(&tmpi, i->type); - cgen(i, &tmpi); - i = &tmpi; - } - - gen(n->right); // args - - regalloc(&nodr, types[tptr], res); - regalloc(&nodo, types[tptr], &nodr); - nodo.op = OINDREG; - - agen(i, &nodr); // REG = &inter - - nodindreg(&nodsp, types[tptr], D_SP); - nodo.xoffset += widthptr; - cgen(&nodo, &nodsp); // 0(SP) = 8(REG) -- i.s - - nodo.xoffset -= widthptr; - cgen(&nodo, &nodr); // REG = 0(REG) -- i.m - - nodo.xoffset = n->left->xoffset + 4*widthptr; - cgen(&nodo, &nodr); // REG = 32+offset(REG) -- i.m->fun[f] - - // BOTCH nodr.type = fntype; - nodr.type = n->left->type; - ginscall(&nodr, proc); - - regfree(&nodr); - regfree(&nodo); - - setmaxarg(n->left->type); -} - -/* - * generate function call; - * proc=0 normal call - * proc=1 goroutine run in new proc - * proc=2 defer call save away stack - */ -void -cgen_call(Node *n, int proc) -{ - Type *t; - Node nod, afun; - - if(n == N) - return; - - if(n->left->ullman >= UINF) { - // if name involves a fn call - // precompute the address of the fn - tempname(&afun, types[tptr]); - cgen(n->left, &afun); - } - - gen(n->right); // assign the args - t = n->left->type; - - setmaxarg(t); - - // call tempname pointer - if(n->left->ullman >= UINF) { - regalloc(&nod, types[tptr], N); - cgen_as(&nod, &afun); - nod.type = t; - ginscall(&nod, proc); - regfree(&nod); - goto ret; - } - - // call pointer - if(n->left->op != ONAME || n->left->class != PFUNC) { - regalloc(&nod, types[tptr], N); - cgen_as(&nod, n->left); - nod.type = t; - ginscall(&nod, proc); - regfree(&nod); - goto ret; - } - - // call direct - n->left->method = 1; - ginscall(n->left, proc); - - -ret: - ; -} - -/* - * call to n has already been generated. - * generate: - * res = return value from call. - */ -void -cgen_callret(Node *n, Node *res) -{ - Node nod; - Type *fp, *t; - Iter flist; - - t = n->left->type; - if(t->etype == TPTR32 || t->etype == TPTR64) - t = t->type; - - fp = structfirst(&flist, getoutarg(t)); - if(fp == T) - fatal("cgen_callret: nil"); - - memset(&nod, 0, sizeof(nod)); - nod.op = OINDREG; - nod.val.u.reg = D_SP; - nod.addable = 1; - - nod.xoffset = fp->width; - nod.type = fp->type; - cgen_as(res, &nod); -} - -/* - * call to n has already been generated. - * generate: - * res = &return value from call. - */ -void -cgen_aret(Node *n, Node *res) -{ - Node nod1, nod2; - Type *fp, *t; - Iter flist; - - t = n->left->type; - if(isptr[t->etype]) - t = t->type; - - fp = structfirst(&flist, getoutarg(t)); - if(fp == T) - fatal("cgen_aret: nil"); - - memset(&nod1, 0, sizeof(nod1)); - nod1.op = OINDREG; - nod1.val.u.reg = D_SP; - nod1.addable = 1; - - nod1.xoffset = fp->width; - nod1.type = fp->type; - - if(res->op != OREGISTER) { - regalloc(&nod2, types[tptr], res); - gins(ALEAQ, &nod1, &nod2); - gins(AMOVQ, &nod2, res); - regfree(&nod2); - } else - gins(ALEAQ, &nod1, res); -} - -/* - * generate return. - * n->left is assignments to return values. - */ -void -cgen_ret(Node *n) -{ - gen(n->left); // copy out args - if(hasdefer) - ginscall(deferreturn, 0); - gins(ARET, N, N); -} - -/* - * generate += *= etc. - */ -void -cgen_asop(Node *n) -{ - Node n1, n2, n3, n4; - Node *nl, *nr; - Prog *p1; - Addr addr; - int a; - - nl = n->left; - nr = n->right; - - if(nr->ullman >= UINF && nl->ullman >= UINF) { - tempname(&n1, nr->type); - cgen(nr, &n1); - n2 = *n; - n2.right = &n1; - cgen_asop(&n2); - goto ret; - } - - if(!isint[nl->type->etype]) - goto hard; - if(!isint[nr->type->etype]) - goto hard; - - switch(n->etype) { - case OADD: - if(smallintconst(nr)) - if(mpgetfix(nr->val.u.xval) == 1) { - a = optoas(OINC, nl->type); - if(nl->addable) { - gins(a, N, nl); - goto ret; - } - if(sudoaddable(a, nl, &addr)) { - p1 = gins(a, N, N); - p1->to = addr; - sudoclean(); - goto ret; - } - } - break; - - case OSUB: - if(smallintconst(nr)) - if(mpgetfix(nr->val.u.xval) == 1) { - a = optoas(ODEC, nl->type); - if(nl->addable) { - gins(a, N, nl); - goto ret; - } - if(sudoaddable(a, nl, &addr)) { - p1 = gins(a, N, N); - p1->to = addr; - sudoclean(); - goto ret; - } - } - break; - } - - switch(n->etype) { - case OADD: - case OSUB: - case OXOR: - case OAND: - case OOR: - a = optoas(n->etype, nl->type); - if(nl->addable) { - if(smallintconst(nr)) { - gins(a, nr, nl); - goto ret; - } - regalloc(&n2, nr->type, N); - cgen(nr, &n2); - gins(a, &n2, nl); - regfree(&n2); - goto ret; - } - if(nr->ullman < UINF) - if(sudoaddable(a, nl, &addr)) { - if(smallintconst(nr)) { - p1 = gins(a, nr, N); - p1->to = addr; - sudoclean(); - goto ret; - } - regalloc(&n2, nr->type, N); - cgen(nr, &n2); - p1 = gins(a, &n2, N); - p1->to = addr; - regfree(&n2); - sudoclean(); - goto ret; - } - } - -hard: - if(nr->ullman > nl->ullman) { - regalloc(&n2, nr->type, N); - cgen(nr, &n2); - igen(nl, &n1, N); - } else { - igen(nl, &n1, N); - regalloc(&n2, nr->type, N); - cgen(nr, &n2); - } - - n3 = *n; - n3.left = &n1; - n3.right = &n2; - n3.op = n->etype; - - regalloc(&n4, nl->type, N); - cgen(&n3, &n4); - gmove(&n4, &n1); - - regfree(&n1); - regfree(&n2); - regfree(&n4); - -ret: - ; -} - -int -samereg(Node *a, Node *b) -{ - if(a->op != OREGISTER) - return 0; - if(b->op != OREGISTER) - return 0; - if(a->val.u.reg != b->val.u.reg) - return 0; - return 1; -} - -/* - * generate division. - * caller must set: - * ax = allocated AX register - * dx = allocated DX register - * generates one of: - * res = nl / nr - * res = nl % nr - * according to op. - */ -void -dodiv(int op, Node *nl, Node *nr, Node *res, Node *ax, Node *dx) -{ - int a; - Node n3, n4; - Type *t; - - t = nl->type; - if(t->width == 1) { - if(issigned[t->etype]) - t = types[TINT32]; - else - t = types[TUINT32]; - } - a = optoas(op, t); - - regalloc(&n3, nr->type, N); - if(nl->ullman >= nr->ullman) { - cgen(nl, ax); - if(!issigned[t->etype]) { - nodconst(&n4, t, 0); - gmove(&n4, dx); - } else - gins(optoas(OEXTEND, t), N, N); - cgen(nr, &n3); - } else { - cgen(nr, &n3); - cgen(nl, ax); - if(!issigned[t->etype]) { - nodconst(&n4, t, 0); - gmove(&n4, dx); - } else - gins(optoas(OEXTEND, t), N, N); - } - gins(a, &n3, N); - regfree(&n3); - - if(op == ODIV) - gmove(ax, res); - else - gmove(dx, res); -} - -/* - * generate division according to op, one of: - * res = nl / nr - * res = nl % nr - */ -void -cgen_div(int op, Node *nl, Node *nr, Node *res) -{ - Node ax, dx; - int rax, rdx; - - rax = reg[D_AX]; - rdx = reg[D_DX]; - - nodreg(&ax, types[TINT64], D_AX); - nodreg(&dx, types[TINT64], D_DX); - regalloc(&ax, nl->type, &ax); - regalloc(&dx, nl->type, &dx); - - dodiv(op, nl, nr, res, &ax, &dx); - - regfree(&ax); - regfree(&dx); -} - -/* - * generate shift according to op, one of: - * res = nl << nr - * res = nl >> nr - */ -void -cgen_shift(int op, Node *nl, Node *nr, Node *res) -{ - Node n1, n2, n3; - int a; - Prog *p1; - - a = optoas(op, nl->type); - - if(nr->op == OLITERAL) { - regalloc(&n1, nl->type, res); - cgen(nl, &n1); - if(mpgetfix(nr->val.u.xval) >= nl->type->width*8) { - // large shift gets 2 shifts by width - nodconst(&n3, types[TUINT32], nl->type->width*8-1); - gins(a, &n3, &n1); - gins(a, &n3, &n1); - } else - gins(a, nr, &n1); - gmove(&n1, res); - regfree(&n1); - goto ret; - } - - nodreg(&n1, types[TUINT32], D_CX); - regalloc(&n1, nr->type, &n1); // to hold the shift type in CX - regalloc(&n3, types[TUINT64], &n1); // to clear high bits of CX - - regalloc(&n2, nl->type, res); - if(nl->ullman >= nr->ullman) { - cgen(nl, &n2); - cgen(nr, &n1); - gmove(&n1, &n3); - } else { - cgen(nr, &n1); - gmove(&n1, &n3); - cgen(nl, &n2); - } - regfree(&n3); - - // test and fix up large shifts - nodconst(&n3, types[TUINT64], nl->type->width*8); - gins(optoas(OCMP, types[TUINT64]), &n1, &n3); - p1 = gbranch(optoas(OLT, types[TUINT64]), T); - if(op == ORSH && issigned[nl->type->etype]) { - nodconst(&n3, types[TUINT32], nl->type->width*8-1); - gins(a, &n3, &n2); - } else { - nodconst(&n3, nl->type, 0); - gmove(&n3, &n2); - } - patch(p1, pc); - gins(a, &n1, &n2); - - gmove(&n2, res); - - regfree(&n1); - regfree(&n2); - -ret: - ; -} - -/* - * generate byte multiply: - * res = nl * nr - * no byte multiply instruction so have to do - * 16-bit multiply and take bottom half. - */ -void -cgen_bmul(int op, Node *nl, Node *nr, Node *res) -{ - Node n1b, n2b, n1w, n2w; - Type *t; - int a; - - if(nl->ullman >= nr->ullman) { - regalloc(&n1b, nl->type, res); - cgen(nl, &n1b); - regalloc(&n2b, nr->type, N); - cgen(nr, &n2b); - } else { - regalloc(&n2b, nr->type, N); - cgen(nr, &n2b); - regalloc(&n1b, nl->type, res); - cgen(nl, &n1b); - } - - // copy from byte to short registers - t = types[TUINT16]; - if(issigned[nl->type->etype]) - t = types[TINT16]; - - regalloc(&n2w, t, &n2b); - cgen(&n2b, &n2w); - - regalloc(&n1w, t, &n1b); - cgen(&n1b, &n1w); - - a = optoas(op, t); - gins(a, &n2w, &n1w); - cgen(&n1w, &n1b); - cgen(&n1b, res); - - regfree(&n1w); - regfree(&n2w); - regfree(&n1b); - regfree(&n2b); -} - -void -clearfat(Node *nl) -{ - uint32 w, c, q; - Node n1; - - /* clear a fat object */ - if(debug['g']) - dump("\nclearfat", nl); - - w = nl->type->width; - c = w % 8; // bytes - q = w / 8; // quads - - gconreg(AMOVQ, 0, D_AX); - nodreg(&n1, types[tptr], D_DI); - agen(nl, &n1); - - if(q >= 4) { - gconreg(AMOVQ, q, D_CX); - gins(AREP, N, N); // repeat - gins(ASTOSQ, N, N); // STOQ AL,*(DI)+ - } else - while(q > 0) { - gins(ASTOSQ, N, N); // STOQ AL,*(DI)+ - q--; - } - - if(c >= 4) { - gconreg(AMOVQ, c, D_CX); - gins(AREP, N, N); // repeat - gins(ASTOSB, N, N); // STOB AL,*(DI)+ - } else - while(c > 0) { - gins(ASTOSB, N, N); // STOB AL,*(DI)+ - c--; - } -} - -int -getlit(Node *lit) -{ - if(smallintconst(lit)) - return mpgetfix(lit->val.u.xval); - return -1; -} - -int -stataddr(Node *nam, Node *n) -{ - int l; - - if(n == N) - goto no; - - switch(n->op) { - case ONAME: - *nam = *n; - return 1; - - case ODOT: - if(!stataddr(nam, n->left)) - break; - nam->xoffset += n->xoffset; - nam->type = n->type; - return 1; - - case OINDEX: - if(n->left->type->bound < 0) - break; - if(!stataddr(nam, n->left)) - break; - l = getlit(n->right); - if(l < 0) - break; - nam->xoffset += l*n->type->width; - nam->type = n->type; - return 1; - } - -no: - return 0; -} - -int -gen_as_init(Node *nr, Node *nl) -{ - Node nam, nod1; - Prog *p; - - if(!initflag) - goto no; - - if(nr == N) { - if(!stataddr(&nam, nl)) - goto no; - if(nam.class != PEXTERN) - goto no; - return 1; - } - - if(nr->op == OCOMPSLICE) { - // create a slice pointing to an array - if(!stataddr(&nam, nl)) { - dump("stataddr", nl); - goto no; - } - - p = gins(ADATA, &nam, nr->left); - p->from.scale = types[tptr]->width; - p->to.index = p->to.type; - p->to.type = D_ADDR; -//print("%P\n", p); - - nodconst(&nod1, types[TINT32], nr->left->type->bound); - p = gins(ADATA, &nam, &nod1); - p->from.scale = types[TINT32]->width; - p->from.offset += types[tptr]->width; -//print("%P\n", p); - - p = gins(ADATA, &nam, &nod1); - p->from.scale = types[TINT32]->width; - p->from.offset += types[tptr]->width+types[TINT32]->width; - - goto yes; - } - - if(nr->op == OCOMPMAP) { - goto yes; - } - - if(nr->type == T || - !eqtype(nl->type, nr->type)) - goto no; - - if(!stataddr(&nam, nl)) - goto no; - if(nam.class != PEXTERN) - goto no; - - switch(nr->op) { - default: - goto no; - - case OLITERAL: - goto lit; - } - -no: - return 0; - -lit: - switch(nr->type->etype) { - default: - goto no; - - case TBOOL: - if(memcmp(nam.sym->name, "initdone·", 9) == 0) - goto no; - case TINT8: - case TUINT8: - case TINT16: - case TUINT16: - case TINT32: - case TUINT32: - case TINT64: - case TUINT64: - case TINT: - case TUINT: - case TFLOAT32: - case TFLOAT64: - case TFLOAT: - p = gins(ADATA, &nam, nr); - p->from.scale = nr->type->width; - break; - - case TSTRING: - p = gins(ADATA, &nam, N); - datastring(nr->val.u.sval->s, nr->val.u.sval->len, &p->to); - p->from.scale = types[tptr]->width; - p->to.index = p->to.type; - p->to.type = D_ADDR; -//print("%P\n", p); - - nodconst(&nod1, types[TINT32], nr->val.u.sval->len); - p = gins(ADATA, &nam, &nod1); - p->from.scale = types[TINT32]->width; - p->from.offset += types[tptr]->width; -//print("%P\n", p); - - p = gins(ADATA, &nam, &nod1); - p->from.scale = types[TINT32]->width; - p->from.offset += types[tptr]->width+types[TINT32]->width; - break; - } - -yes: -//dump("\ngen_as_init", nl); -//dump("", nr); -//print("%P\n", p); - return 1; -} diff --git a/src/cmd/6g/ggen.c b/src/cmd/6g/ggen.c new file mode 100644 index 000000000..1b7505e61 --- /dev/null +++ b/src/cmd/6g/ggen.c @@ -0,0 +1,863 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + + +#undef EXTERN +#define EXTERN +#include "gg.h" +#include "opt.h" + +void +compile(Node *fn) +{ + Plist *pl; + Node nod1; + Prog *ptxt; + int32 lno; + Type *t; + Iter save; + + if(newproc == N) { + newproc = sysfunc("newproc"); + deferproc = sysfunc("deferproc"); + deferreturn = sysfunc("deferreturn"); + throwindex = sysfunc("throwindex"); + throwreturn = sysfunc("throwreturn"); + } + + if(fn->nbody == N) + return; + + // set up domain for labels + labellist = L; + + lno = setlineno(fn); + + curfn = fn; + dowidth(curfn->type); + + if(curfn->type->outnamed) { + // add clearing of the output parameters + t = structfirst(&save, getoutarg(curfn->type)); + while(t != T) { + if(t->nname != N) + curfn->nbody = list(nod(OAS, t->nname, N), curfn->nbody); + t = structnext(&save); + } + } + + hasdefer = 0; + walk(curfn); + if(nerrors != 0) + goto ret; + + allocparams(); + + continpc = P; + breakpc = P; + + pl = newplist(); + pl->name = curfn->nname; + pl->locals = autodcl; + + nodconst(&nod1, types[TINT32], 0); + ptxt = gins(ATEXT, curfn->nname, &nod1); + afunclit(&ptxt->from); + + ginit(); + gen(curfn->enter); + gen(curfn->nbody); + gclean(); + checklabels(); + + if(curfn->type->outtuple != 0) + ginscall(throwreturn, 0); + + if(hasdefer) + ginscall(deferreturn, 0); + pc->as = ARET; // overwrite AEND + pc->lineno = lineno; + + if(!debug['N'] || debug['R'] || debug['P']) + regopt(ptxt); + + // fill in argument size + ptxt->to.offset = rnd(curfn->type->argwid, maxround); + + // fill in final stack size + ptxt->to.offset <<= 32; + ptxt->to.offset |= rnd(stksize+maxarg, maxround); + + if(debug['f']) + frame(0); + +ret: + lineno = lno; +} + +/* + * generate: + * call f + * proc=0 normal call + * proc=1 goroutine run in new proc + * proc=2 defer call save away stack + */ +void +ginscall(Node *f, int proc) +{ + Prog *p; + Node reg, con; + + switch(proc) { + default: + fatal("ginscall: bad proc %d", proc); + break; + + case 0: // normal call + p = gins(ACALL, N, f); + afunclit(&p->to); + break; + + case 1: // call in new proc (go) + case 2: // defered call (defer) + nodreg(®, types[TINT64], D_AX); + gins(APUSHQ, f, N); + nodconst(&con, types[TINT32], argsize(f->type)); + gins(APUSHQ, &con, N); + if(proc == 1) + ginscall(newproc, 0); + else + ginscall(deferproc, 0); + gins(APOPQ, N, ®); + gins(APOPQ, N, ®); + break; + } +} + +/* + * n is call to interface method. + * generate res = n. + */ +void +cgen_callinter(Node *n, Node *res, int proc) +{ + Node *i, *f; + Node tmpi, nodo, nodr, nodsp; + + i = n->left; + if(i->op != ODOTINTER) + fatal("cgen_callinter: not ODOTINTER %O", i->op); + + f = i->right; // field + if(f->op != ONAME) + fatal("cgen_callinter: not ONAME %O", f->op); + + i = i->left; // interface + + if(!i->addable) { + tempname(&tmpi, i->type); + cgen(i, &tmpi); + i = &tmpi; + } + + gen(n->right); // args + + regalloc(&nodr, types[tptr], res); + regalloc(&nodo, types[tptr], &nodr); + nodo.op = OINDREG; + + agen(i, &nodr); // REG = &inter + + nodindreg(&nodsp, types[tptr], D_SP); + nodo.xoffset += widthptr; + cgen(&nodo, &nodsp); // 0(SP) = 8(REG) -- i.s + + nodo.xoffset -= widthptr; + cgen(&nodo, &nodr); // REG = 0(REG) -- i.m + + nodo.xoffset = n->left->xoffset + 4*widthptr; + cgen(&nodo, &nodr); // REG = 32+offset(REG) -- i.m->fun[f] + + // BOTCH nodr.type = fntype; + nodr.type = n->left->type; + ginscall(&nodr, proc); + + regfree(&nodr); + regfree(&nodo); + + setmaxarg(n->left->type); +} + +/* + * generate function call; + * proc=0 normal call + * proc=1 goroutine run in new proc + * proc=2 defer call save away stack + */ +void +cgen_call(Node *n, int proc) +{ + Type *t; + Node nod, afun; + + if(n == N) + return; + + if(n->left->ullman >= UINF) { + // if name involves a fn call + // precompute the address of the fn + tempname(&afun, types[tptr]); + cgen(n->left, &afun); + } + + gen(n->right); // assign the args + t = n->left->type; + + setmaxarg(t); + + // call tempname pointer + if(n->left->ullman >= UINF) { + regalloc(&nod, types[tptr], N); + cgen_as(&nod, &afun); + nod.type = t; + ginscall(&nod, proc); + regfree(&nod); + goto ret; + } + + // call pointer + if(n->left->op != ONAME || n->left->class != PFUNC) { + regalloc(&nod, types[tptr], N); + cgen_as(&nod, n->left); + nod.type = t; + ginscall(&nod, proc); + regfree(&nod); + goto ret; + } + + // call direct + n->left->method = 1; + ginscall(n->left, proc); + + +ret: + ; +} + +/* + * call to n has already been generated. + * generate: + * res = return value from call. + */ +void +cgen_callret(Node *n, Node *res) +{ + Node nod; + Type *fp, *t; + Iter flist; + + t = n->left->type; + if(t->etype == TPTR32 || t->etype == TPTR64) + t = t->type; + + fp = structfirst(&flist, getoutarg(t)); + if(fp == T) + fatal("cgen_callret: nil"); + + memset(&nod, 0, sizeof(nod)); + nod.op = OINDREG; + nod.val.u.reg = D_SP; + nod.addable = 1; + + nod.xoffset = fp->width; + nod.type = fp->type; + cgen_as(res, &nod); +} + +/* + * call to n has already been generated. + * generate: + * res = &return value from call. + */ +void +cgen_aret(Node *n, Node *res) +{ + Node nod1, nod2; + Type *fp, *t; + Iter flist; + + t = n->left->type; + if(isptr[t->etype]) + t = t->type; + + fp = structfirst(&flist, getoutarg(t)); + if(fp == T) + fatal("cgen_aret: nil"); + + memset(&nod1, 0, sizeof(nod1)); + nod1.op = OINDREG; + nod1.val.u.reg = D_SP; + nod1.addable = 1; + + nod1.xoffset = fp->width; + nod1.type = fp->type; + + if(res->op != OREGISTER) { + regalloc(&nod2, types[tptr], res); + gins(ALEAQ, &nod1, &nod2); + gins(AMOVQ, &nod2, res); + regfree(&nod2); + } else + gins(ALEAQ, &nod1, res); +} + +/* + * generate return. + * n->left is assignments to return values. + */ +void +cgen_ret(Node *n) +{ + gen(n->left); // copy out args + if(hasdefer) + ginscall(deferreturn, 0); + gins(ARET, N, N); +} + +/* + * generate += *= etc. + */ +void +cgen_asop(Node *n) +{ + Node n1, n2, n3, n4; + Node *nl, *nr; + Prog *p1; + Addr addr; + int a; + + nl = n->left; + nr = n->right; + + if(nr->ullman >= UINF && nl->ullman >= UINF) { + tempname(&n1, nr->type); + cgen(nr, &n1); + n2 = *n; + n2.right = &n1; + cgen_asop(&n2); + goto ret; + } + + if(!isint[nl->type->etype]) + goto hard; + if(!isint[nr->type->etype]) + goto hard; + + switch(n->etype) { + case OADD: + if(smallintconst(nr)) + if(mpgetfix(nr->val.u.xval) == 1) { + a = optoas(OINC, nl->type); + if(nl->addable) { + gins(a, N, nl); + goto ret; + } + if(sudoaddable(a, nl, &addr)) { + p1 = gins(a, N, N); + p1->to = addr; + sudoclean(); + goto ret; + } + } + break; + + case OSUB: + if(smallintconst(nr)) + if(mpgetfix(nr->val.u.xval) == 1) { + a = optoas(ODEC, nl->type); + if(nl->addable) { + gins(a, N, nl); + goto ret; + } + if(sudoaddable(a, nl, &addr)) { + p1 = gins(a, N, N); + p1->to = addr; + sudoclean(); + goto ret; + } + } + break; + } + + switch(n->etype) { + case OADD: + case OSUB: + case OXOR: + case OAND: + case OOR: + a = optoas(n->etype, nl->type); + if(nl->addable) { + if(smallintconst(nr)) { + gins(a, nr, nl); + goto ret; + } + regalloc(&n2, nr->type, N); + cgen(nr, &n2); + gins(a, &n2, nl); + regfree(&n2); + goto ret; + } + if(nr->ullman < UINF) + if(sudoaddable(a, nl, &addr)) { + if(smallintconst(nr)) { + p1 = gins(a, nr, N); + p1->to = addr; + sudoclean(); + goto ret; + } + regalloc(&n2, nr->type, N); + cgen(nr, &n2); + p1 = gins(a, &n2, N); + p1->to = addr; + regfree(&n2); + sudoclean(); + goto ret; + } + } + +hard: + if(nr->ullman > nl->ullman) { + regalloc(&n2, nr->type, N); + cgen(nr, &n2); + igen(nl, &n1, N); + } else { + igen(nl, &n1, N); + regalloc(&n2, nr->type, N); + cgen(nr, &n2); + } + + n3 = *n; + n3.left = &n1; + n3.right = &n2; + n3.op = n->etype; + + regalloc(&n4, nl->type, N); + cgen(&n3, &n4); + gmove(&n4, &n1); + + regfree(&n1); + regfree(&n2); + regfree(&n4); + +ret: + ; +} + +int +samereg(Node *a, Node *b) +{ + if(a->op != OREGISTER) + return 0; + if(b->op != OREGISTER) + return 0; + if(a->val.u.reg != b->val.u.reg) + return 0; + return 1; +} + +/* + * generate division. + * caller must set: + * ax = allocated AX register + * dx = allocated DX register + * generates one of: + * res = nl / nr + * res = nl % nr + * according to op. + */ +void +dodiv(int op, Node *nl, Node *nr, Node *res, Node *ax, Node *dx) +{ + int a; + Node n3, n4; + Type *t; + + t = nl->type; + if(t->width == 1) { + if(issigned[t->etype]) + t = types[TINT32]; + else + t = types[TUINT32]; + } + a = optoas(op, t); + + regalloc(&n3, nr->type, N); + if(nl->ullman >= nr->ullman) { + cgen(nl, ax); + if(!issigned[t->etype]) { + nodconst(&n4, t, 0); + gmove(&n4, dx); + } else + gins(optoas(OEXTEND, t), N, N); + cgen(nr, &n3); + } else { + cgen(nr, &n3); + cgen(nl, ax); + if(!issigned[t->etype]) { + nodconst(&n4, t, 0); + gmove(&n4, dx); + } else + gins(optoas(OEXTEND, t), N, N); + } + gins(a, &n3, N); + regfree(&n3); + + if(op == ODIV) + gmove(ax, res); + else + gmove(dx, res); +} + +/* + * generate division according to op, one of: + * res = nl / nr + * res = nl % nr + */ +void +cgen_div(int op, Node *nl, Node *nr, Node *res) +{ + Node ax, dx; + int rax, rdx; + + rax = reg[D_AX]; + rdx = reg[D_DX]; + + nodreg(&ax, types[TINT64], D_AX); + nodreg(&dx, types[TINT64], D_DX); + regalloc(&ax, nl->type, &ax); + regalloc(&dx, nl->type, &dx); + + dodiv(op, nl, nr, res, &ax, &dx); + + regfree(&ax); + regfree(&dx); +} + +/* + * generate shift according to op, one of: + * res = nl << nr + * res = nl >> nr + */ +void +cgen_shift(int op, Node *nl, Node *nr, Node *res) +{ + Node n1, n2, n3; + int a; + Prog *p1; + + a = optoas(op, nl->type); + + if(nr->op == OLITERAL) { + regalloc(&n1, nl->type, res); + cgen(nl, &n1); + if(mpgetfix(nr->val.u.xval) >= nl->type->width*8) { + // large shift gets 2 shifts by width + nodconst(&n3, types[TUINT32], nl->type->width*8-1); + gins(a, &n3, &n1); + gins(a, &n3, &n1); + } else + gins(a, nr, &n1); + gmove(&n1, res); + regfree(&n1); + goto ret; + } + + nodreg(&n1, types[TUINT32], D_CX); + regalloc(&n1, nr->type, &n1); // to hold the shift type in CX + regalloc(&n3, types[TUINT64], &n1); // to clear high bits of CX + + regalloc(&n2, nl->type, res); + if(nl->ullman >= nr->ullman) { + cgen(nl, &n2); + cgen(nr, &n1); + gmove(&n1, &n3); + } else { + cgen(nr, &n1); + gmove(&n1, &n3); + cgen(nl, &n2); + } + regfree(&n3); + + // test and fix up large shifts + nodconst(&n3, types[TUINT64], nl->type->width*8); + gins(optoas(OCMP, types[TUINT64]), &n1, &n3); + p1 = gbranch(optoas(OLT, types[TUINT64]), T); + if(op == ORSH && issigned[nl->type->etype]) { + nodconst(&n3, types[TUINT32], nl->type->width*8-1); + gins(a, &n3, &n2); + } else { + nodconst(&n3, nl->type, 0); + gmove(&n3, &n2); + } + patch(p1, pc); + gins(a, &n1, &n2); + + gmove(&n2, res); + + regfree(&n1); + regfree(&n2); + +ret: + ; +} + +/* + * generate byte multiply: + * res = nl * nr + * no byte multiply instruction so have to do + * 16-bit multiply and take bottom half. + */ +void +cgen_bmul(int op, Node *nl, Node *nr, Node *res) +{ + Node n1b, n2b, n1w, n2w; + Type *t; + int a; + + if(nl->ullman >= nr->ullman) { + regalloc(&n1b, nl->type, res); + cgen(nl, &n1b); + regalloc(&n2b, nr->type, N); + cgen(nr, &n2b); + } else { + regalloc(&n2b, nr->type, N); + cgen(nr, &n2b); + regalloc(&n1b, nl->type, res); + cgen(nl, &n1b); + } + + // copy from byte to short registers + t = types[TUINT16]; + if(issigned[nl->type->etype]) + t = types[TINT16]; + + regalloc(&n2w, t, &n2b); + cgen(&n2b, &n2w); + + regalloc(&n1w, t, &n1b); + cgen(&n1b, &n1w); + + a = optoas(op, t); + gins(a, &n2w, &n1w); + cgen(&n1w, &n1b); + cgen(&n1b, res); + + regfree(&n1w); + regfree(&n2w); + regfree(&n1b); + regfree(&n2b); +} + +void +clearfat(Node *nl) +{ + uint32 w, c, q; + Node n1; + + /* clear a fat object */ + if(debug['g']) + dump("\nclearfat", nl); + + w = nl->type->width; + c = w % 8; // bytes + q = w / 8; // quads + + gconreg(AMOVQ, 0, D_AX); + nodreg(&n1, types[tptr], D_DI); + agen(nl, &n1); + + if(q >= 4) { + gconreg(AMOVQ, q, D_CX); + gins(AREP, N, N); // repeat + gins(ASTOSQ, N, N); // STOQ AL,*(DI)+ + } else + while(q > 0) { + gins(ASTOSQ, N, N); // STOQ AL,*(DI)+ + q--; + } + + if(c >= 4) { + gconreg(AMOVQ, c, D_CX); + gins(AREP, N, N); // repeat + gins(ASTOSB, N, N); // STOB AL,*(DI)+ + } else + while(c > 0) { + gins(ASTOSB, N, N); // STOB AL,*(DI)+ + c--; + } +} + +int +getlit(Node *lit) +{ + if(smallintconst(lit)) + return mpgetfix(lit->val.u.xval); + return -1; +} + +int +stataddr(Node *nam, Node *n) +{ + int l; + + if(n == N) + goto no; + + switch(n->op) { + case ONAME: + *nam = *n; + return 1; + + case ODOT: + if(!stataddr(nam, n->left)) + break; + nam->xoffset += n->xoffset; + nam->type = n->type; + return 1; + + case OINDEX: + if(n->left->type->bound < 0) + break; + if(!stataddr(nam, n->left)) + break; + l = getlit(n->right); + if(l < 0) + break; + nam->xoffset += l*n->type->width; + nam->type = n->type; + return 1; + } + +no: + return 0; +} + +int +gen_as_init(Node *nr, Node *nl) +{ + Node nam, nod1; + Prog *p; + + if(!initflag) + goto no; + + if(nr == N) { + if(!stataddr(&nam, nl)) + goto no; + if(nam.class != PEXTERN) + goto no; + return 1; + } + + if(nr->op == OCOMPSLICE) { + // create a slice pointing to an array + if(!stataddr(&nam, nl)) { + dump("stataddr", nl); + goto no; + } + + p = gins(ADATA, &nam, nr->left); + p->from.scale = types[tptr]->width; + p->to.index = p->to.type; + p->to.type = D_ADDR; +//print("%P\n", p); + + nodconst(&nod1, types[TINT32], nr->left->type->bound); + p = gins(ADATA, &nam, &nod1); + p->from.scale = types[TINT32]->width; + p->from.offset += types[tptr]->width; +//print("%P\n", p); + + p = gins(ADATA, &nam, &nod1); + p->from.scale = types[TINT32]->width; + p->from.offset += types[tptr]->width+types[TINT32]->width; + + goto yes; + } + + if(nr->op == OCOMPMAP) { + goto yes; + } + + if(nr->type == T || + !eqtype(nl->type, nr->type)) + goto no; + + if(!stataddr(&nam, nl)) + goto no; + if(nam.class != PEXTERN) + goto no; + + switch(nr->op) { + default: + goto no; + + case OLITERAL: + goto lit; + } + +no: + return 0; + +lit: + switch(nr->type->etype) { + default: + goto no; + + case TBOOL: + if(memcmp(nam.sym->name, "initdone·", 9) == 0) + goto no; + case TINT8: + case TUINT8: + case TINT16: + case TUINT16: + case TINT32: + case TUINT32: + case TINT64: + case TUINT64: + case TINT: + case TUINT: + case TFLOAT32: + case TFLOAT64: + case TFLOAT: + p = gins(ADATA, &nam, nr); + p->from.scale = nr->type->width; + break; + + case TSTRING: + p = gins(ADATA, &nam, N); + datastring(nr->val.u.sval->s, nr->val.u.sval->len, &p->to); + p->from.scale = types[tptr]->width; + p->to.index = p->to.type; + p->to.type = D_ADDR; +//print("%P\n", p); + + nodconst(&nod1, types[TINT32], nr->val.u.sval->len); + p = gins(ADATA, &nam, &nod1); + p->from.scale = types[TINT32]->width; + p->from.offset += types[tptr]->width; +//print("%P\n", p); + + p = gins(ADATA, &nam, &nod1); + p->from.scale = types[TINT32]->width; + p->from.offset += types[tptr]->width+types[TINT32]->width; + break; + } + +yes: +//dump("\ngen_as_init", nl); +//dump("", nr); +//print("%P\n", p); + return 1; +} diff --git a/src/cmd/6g/gobj.c b/src/cmd/6g/gobj.c new file mode 100644 index 000000000..e990d661d --- /dev/null +++ b/src/cmd/6g/gobj.c @@ -0,0 +1,665 @@ +// Derived from Inferno utils/6c/swt.c +// http://code.google.com/p/inferno-os/source/browse/utils/6c/swt.c +// +// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. +// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) +// Portions Copyright © 1997-1999 Vita Nuova Limited +// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) +// Portions Copyright © 2004,2006 Bruce Ellis +// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) +// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others +// Portions Copyright © 2009 The Go Authors. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "gg.h" + +void +zname(Biobuf *b, Sym *s, int t) +{ + char *n; + + Bputc(b, ANAME); /* as */ + Bputc(b, ANAME>>8); /* as */ + Bputc(b, t); /* type */ + Bputc(b, s->sym); /* sym */ + + for(n=s->package; *n; n++) + Bputc(b, *n); + Bputdot(b); + for(n=s->name; *n; n++) + Bputc(b, *n); + Bputc(b, 0); +} + +void +zfile(Biobuf *b, char *p, int n) +{ + Bputc(b, ANAME); + Bputc(b, ANAME>>8); + Bputc(b, D_FILE); + Bputc(b, 1); + Bputc(b, '<'); + Bwrite(b, p, n); + Bputc(b, 0); +} + +void +zhist(Biobuf *b, int line, vlong offset) +{ + Addr a; + + Bputc(b, AHISTORY); + Bputc(b, AHISTORY>>8); + Bputc(b, line); + Bputc(b, line>>8); + Bputc(b, line>>16); + Bputc(b, line>>24); + zaddr(b, &zprog.from, 0); + a = zprog.to; + if(offset != 0) { + a.offset = offset; + a.type = D_CONST; + } + zaddr(b, &a, 0); +} + +void +zaddr(Biobuf *b, Addr *a, int s) +{ + int32 l; + uint64 e; + int i, t; + char *n; + + t = 0; + if(a->index != D_NONE || a->scale != 0) + t |= T_INDEX; + if(s != 0) + t |= T_SYM; + + switch(a->type) { + + case D_BRANCH: + a->offset = a->branch->loc; + + default: + t |= T_TYPE; + + case D_NONE: + if(a->offset != 0) { + t |= T_OFFSET; + l = a->offset; + if((vlong)l != a->offset) + t |= T_64; + } + break; + case D_FCONST: + t |= T_FCONST; + break; + case D_SCONST: + t |= T_SCONST; + break; + } + Bputc(b, t); + + if(t & T_INDEX) { /* implies index, scale */ + Bputc(b, a->index); + Bputc(b, a->scale); + } + if(t & T_OFFSET) { /* implies offset */ + l = a->offset; + Bputc(b, l); + Bputc(b, l>>8); + Bputc(b, l>>16); + Bputc(b, l>>24); + if(t & T_64) { + l = a->offset>>32; + Bputc(b, l); + Bputc(b, l>>8); + Bputc(b, l>>16); + Bputc(b, l>>24); + } + } + if(t & T_SYM) /* implies sym */ + Bputc(b, s); + if(t & T_FCONST) { + ieeedtod(&e, a->dval); + l = e; + Bputc(b, l); + Bputc(b, l>>8); + Bputc(b, l>>16); + Bputc(b, l>>24); + l = e >> 32; + Bputc(b, l); + Bputc(b, l>>8); + Bputc(b, l>>16); + Bputc(b, l>>24); + return; + } + if(t & T_SCONST) { + n = a->sval; + for(i=0; itype); +} + +void +dumpfuncs(void) +{ + Plist *pl; + int sf, st, t, sym; + struct { Sym *sym; short type; } h[NSYM]; + Sym *s; + Prog *p; + + for(sym=0; symlink) { + for(p=pl->firstpc; p!=P; p=p->link) { + p->loc = pcloc; + if(p->as != ADATA && p->as != AGLOBL) + pcloc++; + } + } + + // put out functions + for(pl=plist; pl!=nil; pl=pl->link) { + + if(debug['S']) { + s = S; + if(pl->name != N) + s = pl->name->sym; + print("\n--- prog list \"%S\" ---\n", s); + for(p=pl->firstpc; p!=P; p=p->link) + print("%P\n", p); + } + + for(p=pl->firstpc; p!=P; p=p->link) { + jackpot: + sf = 0; + s = p->from.sym; + while(s != S) { + sf = s->sym; + if(sf < 0 || sf >= NSYM) + sf = 0; + t = p->from.type; + if(t == D_ADDR) + t = p->from.index; + if(h[sf].type == t) + if(h[sf].sym == s) + break; + s->sym = sym; + zname(bout, s, t); + h[sym].sym = s; + h[sym].type = t; + sf = sym; + sym++; + if(sym >= NSYM) + sym = 1; + break; + } + st = 0; + s = p->to.sym; + while(s != S) { + st = s->sym; + if(st < 0 || st >= NSYM) + st = 0; + t = p->to.type; + if(t == D_ADDR) + t = p->to.index; + if(h[st].type == t) + if(h[st].sym == s) + break; + s->sym = sym; + zname(bout, s, t); + h[sym].sym = s; + h[sym].type = t; + st = sym; + sym++; + if(sym >= NSYM) + sym = 1; + if(st == sf) + goto jackpot; + break; + } + Bputc(bout, p->as); + Bputc(bout, p->as>>8); + Bputc(bout, p->lineno); + Bputc(bout, p->lineno>>8); + Bputc(bout, p->lineno>>16); + Bputc(bout, p->lineno>>24); + zaddr(bout, &p->from, sf); + zaddr(bout, &p->to, st); + } + } +} + +/* deferred DATA output */ +static Prog *strdat; +static Prog *estrdat; +static int gflag; +static Prog *savepc; + +static void +data(void) +{ + gflag = debug['g']; + debug['g'] = 0; + + if(estrdat == nil) { + strdat = mal(sizeof(*pc)); + clearp(strdat); + estrdat = strdat; + } + if(savepc) + fatal("data phase error"); + savepc = pc; + pc = estrdat; +} + +static void +text(void) +{ + if(!savepc) + fatal("text phase error"); + debug['g'] = gflag; + estrdat = pc; + pc = savepc; + savepc = nil; +} + +void +dumpdata(void) +{ + Prog *p; + + if(estrdat == nil) + return; + *pc = *strdat; + if(gflag) + for(p=pc; p!=estrdat; p=p->link) + print("%P\n", p); + pc = estrdat; +} + +/* + * make a refer to the data s, s+len + * emitting DATA if needed. + */ +void +datastring(char *s, int len, Addr *a) +{ + int w; + Prog *p; + Addr ac, ao; + static int gen; + struct { + Strlit lit; + char buf[100]; + } tmp; + + // string + memset(&ao, 0, sizeof(ao)); + ao.type = D_STATIC; + ao.index = D_NONE; + ao.etype = TINT32; + ao.offset = 0; // fill in + + // constant + memset(&ac, 0, sizeof(ac)); + ac.type = D_CONST; + ac.index = D_NONE; + ac.offset = 0; // fill in + + // huge strings are made static to avoid long names. + if(len > 100) { + snprint(namebuf, sizeof(namebuf), ".string.%d", gen++); + ao.sym = lookup(namebuf); + ao.type = D_STATIC; + } else { + if(len > 0 && s[len-1] == '\0') + len--; + tmp.lit.len = len; + memmove(tmp.lit.s, s, len); + tmp.lit.s[len] = '\0'; + len++; + snprint(namebuf, sizeof(namebuf), "\"%Z\"", &tmp.lit); + ao.sym = pkglookup(namebuf, "string"); + ao.type = D_EXTERN; + } + *a = ao; + + // only generate data the first time. + if(ao.sym->uniq) + return; + ao.sym->uniq = 1; + + data(); + for(w=0; wfrom = ao; + p->from.offset = w; + + p->from.scale = NSNAME; + if(w+8 > len) + p->from.scale = len-w; + + p->to = ac; + p->to.type = D_SCONST; + p->to.offset = len; + memmove(p->to.sval, s+w, p->from.scale); + } + p = pc; + ggloblsym(ao.sym, len, ao.type == D_EXTERN); + if(ao.type == D_STATIC) + p->from.type = D_STATIC; + text(); +} + +/* + * make a refer to the string sval, + * emitting DATA if needed. + */ +void +datagostring(Strlit *sval, Addr *a) +{ + Prog *p; + Addr ac, ao, ap; + int32 wi, wp; + static int gen; + + memset(&ac, 0, sizeof(ac)); + memset(&ao, 0, sizeof(ao)); + memset(&ap, 0, sizeof(ap)); + + // constant + ac.type = D_CONST; + ac.index = D_NONE; + ac.offset = 0; // fill in + + // string len+ptr + ao.type = D_STATIC; // fill in + ao.index = D_NONE; + ao.etype = TINT32; + ao.sym = nil; // fill in + + // $string len+ptr + datastring(sval->s, sval->len, &ap); + ap.index = ap.type; + ap.type = D_ADDR; + ap.etype = TINT32; + + wi = types[TUINT32]->width; + wp = types[tptr]->width; + + if(ap.index == D_STATIC) { + // huge strings are made static to avoid long names + snprint(namebuf, sizeof(namebuf), ".gostring.%d", ++gen); + ao.sym = lookup(namebuf); + ao.type = D_STATIC; + } else { + // small strings get named by their contents, + // so that multiple modules using the same string + // can share it. + snprint(namebuf, sizeof(namebuf), "\"%Z\"", sval); + ao.sym = pkglookup(namebuf, "go.string"); + ao.type = D_EXTERN; + } + + *a = ao; + if(ao.sym->uniq) + return; + ao.sym->uniq = 1; + + data(); + // DATA gostring, wp, $cstring + p = pc; + gins(ADATA, N, N); + p->from = ao; + p->from.scale = wp; + p->to = ap; + + // DATA gostring+wp, wi, $len + p = pc; + gins(ADATA, N, N); + p->from = ao; + p->from.offset = wp; + p->from.scale = wi; + p->to = ac; + p->to.offset = sval->len; + + p = pc; + ggloblsym(ao.sym, types[TSTRING]->width, ao.type == D_EXTERN); + if(ao.type == D_STATIC) + p->from.type = D_STATIC; + text(); +} + +int +dstringptr(Sym *s, int off, char *str) +{ + Prog *p; + + off = rnd(off, widthptr); + p = gins(ADATA, N, N); + p->from.type = D_EXTERN; + p->from.index = D_NONE; + p->from.sym = s; + p->from.offset = off; + p->from.scale = widthptr; + + datastring(str, strlen(str)+1, &p->to); + p->to.index = p->to.type; + p->to.type = D_ADDR; + p->to.etype = TINT32; + off += widthptr; + + return off; +} + +int +duintxx(Sym *s, int off, uint64 v, int wid) +{ + Prog *p; + + off = rnd(off, wid); + + p = gins(ADATA, N, N); + p->from.type = D_EXTERN; + p->from.index = D_NONE; + p->from.sym = s; + p->from.offset = off; + p->from.scale = wid; + p->to.type = D_CONST; + p->to.index = D_NONE; + p->to.offset = v; + off += wid; + + return off; +} + +int +duint32(Sym *s, int off, uint32 v) +{ + return duintxx(s, off, v, 4); +} + +int +duint16(Sym *s, int off, uint32 v) +{ + return duintxx(s, off, v, 2); +} + +int +duintptr(Sym *s, int off, uint32 v) +{ + return duintxx(s, off, v, 8); +} + +int +dsymptr(Sym *s, int off, Sym *x) +{ + Prog *p; + + off = rnd(off, widthptr); + + p = gins(ADATA, N, N); + p->from.type = D_EXTERN; + p->from.index = D_NONE; + p->from.sym = s; + p->from.offset = off; + p->from.scale = widthptr; + p->to.type = D_ADDR; + p->to.index = D_EXTERN; + p->to.sym = x; + p->to.offset = 0; + off += widthptr; + + return off; +} + + +void +genembedtramp(Type *t, Sig *b) +{ + Sym *e; + int c, d, o, loaded; + Prog *p; + Type *f; + + e = lookup(b->name); + for(d=0; dname); + +out: +// print("genembedtramp %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); + + newplist()->name = newname(b->sym); + + //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); + + loaded = 0; + o = 0; + for(c=d-1; c>=0; c--) { + f = dotlist[c].field; + o += f->width; + if(!isptr[f->type->etype]) + continue; + if(!loaded) { + loaded = 1; + //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); + } + + //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; + if(loaded) + p->to.type = D_AX; + else { + p->to.type = D_INDIR+D_SP; + p->to.offset = 8; + } +//print("4. %P\n", p); + } + + //MOVQ AX, 8(SP) + if(loaded) { + 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); + } else { + // TODO(rsc): obviously this is unnecessary, + // but 6l has a bug, and it can't handle + // JMP instructions too close to the top of + // a new function. + p = pc; + gins(ANOP, N, N); + } + + f = dotlist[0].field; + //JMP main·*Sub_test2(SB) + if(isptr[f->type->etype]) + f = f->type; + p = pc; + gins(AJMP, N, N); + p->to.type = D_EXTERN; + p->to.sym = methodsym(lookup(b->name), ptrto(f->type)); +//print("6. %P\n", p); + + pc->as = ARET; // overwrite AEND +} + +void +nopout(Prog *p) +{ + p->as = ANOP; +} + diff --git a/src/cmd/6g/obj.c b/src/cmd/6g/obj.c deleted file mode 100644 index e990d661d..000000000 --- a/src/cmd/6g/obj.c +++ /dev/null @@ -1,665 +0,0 @@ -// Derived from Inferno utils/6c/swt.c -// http://code.google.com/p/inferno-os/source/browse/utils/6c/swt.c -// -// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. -// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) -// Portions Copyright © 1997-1999 Vita Nuova Limited -// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) -// Portions Copyright © 2004,2006 Bruce Ellis -// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) -// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others -// Portions Copyright © 2009 The Go Authors. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include "gg.h" - -void -zname(Biobuf *b, Sym *s, int t) -{ - char *n; - - Bputc(b, ANAME); /* as */ - Bputc(b, ANAME>>8); /* as */ - Bputc(b, t); /* type */ - Bputc(b, s->sym); /* sym */ - - for(n=s->package; *n; n++) - Bputc(b, *n); - Bputdot(b); - for(n=s->name; *n; n++) - Bputc(b, *n); - Bputc(b, 0); -} - -void -zfile(Biobuf *b, char *p, int n) -{ - Bputc(b, ANAME); - Bputc(b, ANAME>>8); - Bputc(b, D_FILE); - Bputc(b, 1); - Bputc(b, '<'); - Bwrite(b, p, n); - Bputc(b, 0); -} - -void -zhist(Biobuf *b, int line, vlong offset) -{ - Addr a; - - Bputc(b, AHISTORY); - Bputc(b, AHISTORY>>8); - Bputc(b, line); - Bputc(b, line>>8); - Bputc(b, line>>16); - Bputc(b, line>>24); - zaddr(b, &zprog.from, 0); - a = zprog.to; - if(offset != 0) { - a.offset = offset; - a.type = D_CONST; - } - zaddr(b, &a, 0); -} - -void -zaddr(Biobuf *b, Addr *a, int s) -{ - int32 l; - uint64 e; - int i, t; - char *n; - - t = 0; - if(a->index != D_NONE || a->scale != 0) - t |= T_INDEX; - if(s != 0) - t |= T_SYM; - - switch(a->type) { - - case D_BRANCH: - a->offset = a->branch->loc; - - default: - t |= T_TYPE; - - case D_NONE: - if(a->offset != 0) { - t |= T_OFFSET; - l = a->offset; - if((vlong)l != a->offset) - t |= T_64; - } - break; - case D_FCONST: - t |= T_FCONST; - break; - case D_SCONST: - t |= T_SCONST; - break; - } - Bputc(b, t); - - if(t & T_INDEX) { /* implies index, scale */ - Bputc(b, a->index); - Bputc(b, a->scale); - } - if(t & T_OFFSET) { /* implies offset */ - l = a->offset; - Bputc(b, l); - Bputc(b, l>>8); - Bputc(b, l>>16); - Bputc(b, l>>24); - if(t & T_64) { - l = a->offset>>32; - Bputc(b, l); - Bputc(b, l>>8); - Bputc(b, l>>16); - Bputc(b, l>>24); - } - } - if(t & T_SYM) /* implies sym */ - Bputc(b, s); - if(t & T_FCONST) { - ieeedtod(&e, a->dval); - l = e; - Bputc(b, l); - Bputc(b, l>>8); - Bputc(b, l>>16); - Bputc(b, l>>24); - l = e >> 32; - Bputc(b, l); - Bputc(b, l>>8); - Bputc(b, l>>16); - Bputc(b, l>>24); - return; - } - if(t & T_SCONST) { - n = a->sval; - for(i=0; itype); -} - -void -dumpfuncs(void) -{ - Plist *pl; - int sf, st, t, sym; - struct { Sym *sym; short type; } h[NSYM]; - Sym *s; - Prog *p; - - for(sym=0; symlink) { - for(p=pl->firstpc; p!=P; p=p->link) { - p->loc = pcloc; - if(p->as != ADATA && p->as != AGLOBL) - pcloc++; - } - } - - // put out functions - for(pl=plist; pl!=nil; pl=pl->link) { - - if(debug['S']) { - s = S; - if(pl->name != N) - s = pl->name->sym; - print("\n--- prog list \"%S\" ---\n", s); - for(p=pl->firstpc; p!=P; p=p->link) - print("%P\n", p); - } - - for(p=pl->firstpc; p!=P; p=p->link) { - jackpot: - sf = 0; - s = p->from.sym; - while(s != S) { - sf = s->sym; - if(sf < 0 || sf >= NSYM) - sf = 0; - t = p->from.type; - if(t == D_ADDR) - t = p->from.index; - if(h[sf].type == t) - if(h[sf].sym == s) - break; - s->sym = sym; - zname(bout, s, t); - h[sym].sym = s; - h[sym].type = t; - sf = sym; - sym++; - if(sym >= NSYM) - sym = 1; - break; - } - st = 0; - s = p->to.sym; - while(s != S) { - st = s->sym; - if(st < 0 || st >= NSYM) - st = 0; - t = p->to.type; - if(t == D_ADDR) - t = p->to.index; - if(h[st].type == t) - if(h[st].sym == s) - break; - s->sym = sym; - zname(bout, s, t); - h[sym].sym = s; - h[sym].type = t; - st = sym; - sym++; - if(sym >= NSYM) - sym = 1; - if(st == sf) - goto jackpot; - break; - } - Bputc(bout, p->as); - Bputc(bout, p->as>>8); - Bputc(bout, p->lineno); - Bputc(bout, p->lineno>>8); - Bputc(bout, p->lineno>>16); - Bputc(bout, p->lineno>>24); - zaddr(bout, &p->from, sf); - zaddr(bout, &p->to, st); - } - } -} - -/* deferred DATA output */ -static Prog *strdat; -static Prog *estrdat; -static int gflag; -static Prog *savepc; - -static void -data(void) -{ - gflag = debug['g']; - debug['g'] = 0; - - if(estrdat == nil) { - strdat = mal(sizeof(*pc)); - clearp(strdat); - estrdat = strdat; - } - if(savepc) - fatal("data phase error"); - savepc = pc; - pc = estrdat; -} - -static void -text(void) -{ - if(!savepc) - fatal("text phase error"); - debug['g'] = gflag; - estrdat = pc; - pc = savepc; - savepc = nil; -} - -void -dumpdata(void) -{ - Prog *p; - - if(estrdat == nil) - return; - *pc = *strdat; - if(gflag) - for(p=pc; p!=estrdat; p=p->link) - print("%P\n", p); - pc = estrdat; -} - -/* - * make a refer to the data s, s+len - * emitting DATA if needed. - */ -void -datastring(char *s, int len, Addr *a) -{ - int w; - Prog *p; - Addr ac, ao; - static int gen; - struct { - Strlit lit; - char buf[100]; - } tmp; - - // string - memset(&ao, 0, sizeof(ao)); - ao.type = D_STATIC; - ao.index = D_NONE; - ao.etype = TINT32; - ao.offset = 0; // fill in - - // constant - memset(&ac, 0, sizeof(ac)); - ac.type = D_CONST; - ac.index = D_NONE; - ac.offset = 0; // fill in - - // huge strings are made static to avoid long names. - if(len > 100) { - snprint(namebuf, sizeof(namebuf), ".string.%d", gen++); - ao.sym = lookup(namebuf); - ao.type = D_STATIC; - } else { - if(len > 0 && s[len-1] == '\0') - len--; - tmp.lit.len = len; - memmove(tmp.lit.s, s, len); - tmp.lit.s[len] = '\0'; - len++; - snprint(namebuf, sizeof(namebuf), "\"%Z\"", &tmp.lit); - ao.sym = pkglookup(namebuf, "string"); - ao.type = D_EXTERN; - } - *a = ao; - - // only generate data the first time. - if(ao.sym->uniq) - return; - ao.sym->uniq = 1; - - data(); - for(w=0; wfrom = ao; - p->from.offset = w; - - p->from.scale = NSNAME; - if(w+8 > len) - p->from.scale = len-w; - - p->to = ac; - p->to.type = D_SCONST; - p->to.offset = len; - memmove(p->to.sval, s+w, p->from.scale); - } - p = pc; - ggloblsym(ao.sym, len, ao.type == D_EXTERN); - if(ao.type == D_STATIC) - p->from.type = D_STATIC; - text(); -} - -/* - * make a refer to the string sval, - * emitting DATA if needed. - */ -void -datagostring(Strlit *sval, Addr *a) -{ - Prog *p; - Addr ac, ao, ap; - int32 wi, wp; - static int gen; - - memset(&ac, 0, sizeof(ac)); - memset(&ao, 0, sizeof(ao)); - memset(&ap, 0, sizeof(ap)); - - // constant - ac.type = D_CONST; - ac.index = D_NONE; - ac.offset = 0; // fill in - - // string len+ptr - ao.type = D_STATIC; // fill in - ao.index = D_NONE; - ao.etype = TINT32; - ao.sym = nil; // fill in - - // $string len+ptr - datastring(sval->s, sval->len, &ap); - ap.index = ap.type; - ap.type = D_ADDR; - ap.etype = TINT32; - - wi = types[TUINT32]->width; - wp = types[tptr]->width; - - if(ap.index == D_STATIC) { - // huge strings are made static to avoid long names - snprint(namebuf, sizeof(namebuf), ".gostring.%d", ++gen); - ao.sym = lookup(namebuf); - ao.type = D_STATIC; - } else { - // small strings get named by their contents, - // so that multiple modules using the same string - // can share it. - snprint(namebuf, sizeof(namebuf), "\"%Z\"", sval); - ao.sym = pkglookup(namebuf, "go.string"); - ao.type = D_EXTERN; - } - - *a = ao; - if(ao.sym->uniq) - return; - ao.sym->uniq = 1; - - data(); - // DATA gostring, wp, $cstring - p = pc; - gins(ADATA, N, N); - p->from = ao; - p->from.scale = wp; - p->to = ap; - - // DATA gostring+wp, wi, $len - p = pc; - gins(ADATA, N, N); - p->from = ao; - p->from.offset = wp; - p->from.scale = wi; - p->to = ac; - p->to.offset = sval->len; - - p = pc; - ggloblsym(ao.sym, types[TSTRING]->width, ao.type == D_EXTERN); - if(ao.type == D_STATIC) - p->from.type = D_STATIC; - text(); -} - -int -dstringptr(Sym *s, int off, char *str) -{ - Prog *p; - - off = rnd(off, widthptr); - p = gins(ADATA, N, N); - p->from.type = D_EXTERN; - p->from.index = D_NONE; - p->from.sym = s; - p->from.offset = off; - p->from.scale = widthptr; - - datastring(str, strlen(str)+1, &p->to); - p->to.index = p->to.type; - p->to.type = D_ADDR; - p->to.etype = TINT32; - off += widthptr; - - return off; -} - -int -duintxx(Sym *s, int off, uint64 v, int wid) -{ - Prog *p; - - off = rnd(off, wid); - - p = gins(ADATA, N, N); - p->from.type = D_EXTERN; - p->from.index = D_NONE; - p->from.sym = s; - p->from.offset = off; - p->from.scale = wid; - p->to.type = D_CONST; - p->to.index = D_NONE; - p->to.offset = v; - off += wid; - - return off; -} - -int -duint32(Sym *s, int off, uint32 v) -{ - return duintxx(s, off, v, 4); -} - -int -duint16(Sym *s, int off, uint32 v) -{ - return duintxx(s, off, v, 2); -} - -int -duintptr(Sym *s, int off, uint32 v) -{ - return duintxx(s, off, v, 8); -} - -int -dsymptr(Sym *s, int off, Sym *x) -{ - Prog *p; - - off = rnd(off, widthptr); - - p = gins(ADATA, N, N); - p->from.type = D_EXTERN; - p->from.index = D_NONE; - p->from.sym = s; - p->from.offset = off; - p->from.scale = widthptr; - p->to.type = D_ADDR; - p->to.index = D_EXTERN; - p->to.sym = x; - p->to.offset = 0; - off += widthptr; - - return off; -} - - -void -genembedtramp(Type *t, Sig *b) -{ - Sym *e; - int c, d, o, loaded; - Prog *p; - Type *f; - - e = lookup(b->name); - for(d=0; dname); - -out: -// print("genembedtramp %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); - - newplist()->name = newname(b->sym); - - //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); - - loaded = 0; - o = 0; - for(c=d-1; c>=0; c--) { - f = dotlist[c].field; - o += f->width; - if(!isptr[f->type->etype]) - continue; - if(!loaded) { - loaded = 1; - //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); - } - - //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; - if(loaded) - p->to.type = D_AX; - else { - p->to.type = D_INDIR+D_SP; - p->to.offset = 8; - } -//print("4. %P\n", p); - } - - //MOVQ AX, 8(SP) - if(loaded) { - 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); - } else { - // TODO(rsc): obviously this is unnecessary, - // but 6l has a bug, and it can't handle - // JMP instructions too close to the top of - // a new function. - p = pc; - gins(ANOP, N, N); - } - - f = dotlist[0].field; - //JMP main·*Sub_test2(SB) - if(isptr[f->type->etype]) - f = f->type; - p = pc; - gins(AJMP, N, N); - p->to.type = D_EXTERN; - p->to.sym = methodsym(lookup(b->name), ptrto(f->type)); -//print("6. %P\n", p); - - pc->as = ARET; // overwrite AEND -} - -void -nopout(Prog *p) -{ - p->as = ANOP; -} - diff --git a/src/cmd/gc/walk.c b/src/cmd/gc/walk.c index 8b4fc40f2..e20f0c75c 100644 --- a/src/cmd/gc/walk.c +++ b/src/cmd/gc/walk.c @@ -3970,9 +3970,11 @@ structlit(Node *n, Node *var) r = listfirst(&saver, &n->left); if(r != N && r->op == OEMPTY) r = N; + if(r == N) + return var; mixflag = 0; - if(r != N && r->op == OKEY) { + if(r->op == OKEY) { a = nod(OAS, var, N); addtop = list(addtop, a); goto loop2; @@ -4050,11 +4052,11 @@ arraylit(Node *n, Node *var) r = N; while(r != N) { - b++; if(r->op == OKEY) { evconst(r->left); b = nonnegconst(r->left); } + b++; if(b > ninit) ninit = b; r = listnext(&saver); @@ -4064,7 +4066,8 @@ arraylit(Node *n, Node *var) if(b == -100) { // flag for [...] b = ninit; - t = shallow(t); + if(var == N) + t = shallow(t); t->bound = b; } -- cgit v1.2.3