mirror of https://github.com/golang/go.git
292 lines
5.3 KiB
C
292 lines
5.3 KiB
C
// cmd/9c/sgen.c from Vita Nuova.
|
|
//
|
|
// 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-2008 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-2008 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 "gc.h"
|
|
|
|
Prog*
|
|
gtext(Sym *s, int32 stkoff)
|
|
{
|
|
vlong v;
|
|
|
|
v = ((uvlong)argsize(1) << 32) | (stkoff & 0xffffffff);
|
|
if((textflag & NOSPLIT) && stkoff >= 128)
|
|
yyerror("stack frame too large for NOSPLIT function");
|
|
|
|
gpseudo(ATEXT, s, nodgconst(v, types[TVLONG]));
|
|
return p;
|
|
}
|
|
|
|
|
|
void
|
|
noretval(int n)
|
|
{
|
|
|
|
if(n & 1) {
|
|
gins(ANOP, Z, Z);
|
|
p->to.type = D_REG;
|
|
p->to.reg = REGRET;
|
|
}
|
|
if(n & 2) {
|
|
gins(ANOP, Z, Z);
|
|
p->to.type = D_FREG;
|
|
p->to.reg = FREGRET;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* calculate addressability as follows
|
|
* CONST ==> 20 $value
|
|
* NAME ==> 10 name
|
|
* REGISTER ==> 11 register
|
|
* INDREG ==> 12 *[(reg)+offset]
|
|
* &10 ==> 2 $name
|
|
* ADD(2, 20) ==> 2 $name+offset
|
|
* ADD(3, 20) ==> 3 $(reg)+offset
|
|
* &12 ==> 3 $(reg)+offset
|
|
* *11 ==> 11 ??
|
|
* *2 ==> 10 name
|
|
* *3 ==> 12 *(reg)+offset
|
|
* calculate complexity (number of registers)
|
|
*/
|
|
void
|
|
xcom(Node *n)
|
|
{
|
|
Node *l, *r;
|
|
int v;
|
|
|
|
if(n == Z)
|
|
return;
|
|
l = n->left;
|
|
r = n->right;
|
|
n->addable = 0;
|
|
n->complex = 0;
|
|
switch(n->op) {
|
|
case OCONST:
|
|
n->addable = 20;
|
|
return;
|
|
|
|
case OREGISTER:
|
|
n->addable = 11;
|
|
return;
|
|
|
|
case OINDREG:
|
|
n->addable = 12;
|
|
return;
|
|
|
|
case ONAME:
|
|
n->addable = 10;
|
|
return;
|
|
|
|
case OADDR:
|
|
xcom(l);
|
|
if(l->addable == 10)
|
|
n->addable = 2;
|
|
if(l->addable == 12)
|
|
n->addable = 3;
|
|
break;
|
|
|
|
case OIND:
|
|
xcom(l);
|
|
if(l->addable == 11)
|
|
n->addable = 12;
|
|
if(l->addable == 3)
|
|
n->addable = 12;
|
|
if(l->addable == 2)
|
|
n->addable = 10;
|
|
break;
|
|
|
|
case OADD:
|
|
xcom(l);
|
|
xcom(r);
|
|
if(l->addable == 20) {
|
|
if(r->addable == 2)
|
|
n->addable = 2;
|
|
if(r->addable == 3)
|
|
n->addable = 3;
|
|
}
|
|
if(r->addable == 20) {
|
|
if(l->addable == 2)
|
|
n->addable = 2;
|
|
if(l->addable == 3)
|
|
n->addable = 3;
|
|
}
|
|
break;
|
|
|
|
case OASMUL:
|
|
case OASLMUL:
|
|
xcom(l);
|
|
xcom(r);
|
|
v = vlog(r);
|
|
if(v >= 0) {
|
|
n->op = OASASHL;
|
|
r->vconst = v;
|
|
r->type = types[TINT];
|
|
}
|
|
break;
|
|
|
|
case OMUL:
|
|
case OLMUL:
|
|
xcom(l);
|
|
xcom(r);
|
|
v = vlog(r);
|
|
if(v >= 0) {
|
|
n->op = OASHL;
|
|
r->vconst = v;
|
|
r->type = types[TINT];
|
|
}
|
|
v = vlog(l);
|
|
if(v >= 0) {
|
|
n->op = OASHL;
|
|
n->left = r;
|
|
n->right = l;
|
|
r = l;
|
|
l = n->left;
|
|
r->vconst = v;
|
|
r->type = types[TINT];
|
|
simplifyshift(n);
|
|
}
|
|
break;
|
|
|
|
case OASLDIV:
|
|
xcom(l);
|
|
xcom(r);
|
|
v = vlog(r);
|
|
if(v >= 0) {
|
|
n->op = OASLSHR;
|
|
r->vconst = v;
|
|
r->type = types[TINT];
|
|
}
|
|
break;
|
|
|
|
case OLDIV:
|
|
xcom(l);
|
|
xcom(r);
|
|
v = vlog(r);
|
|
if(v >= 0) {
|
|
n->op = OLSHR;
|
|
r->vconst = v;
|
|
r->type = types[TINT];
|
|
simplifyshift(n);
|
|
}
|
|
break;
|
|
|
|
case OASLMOD:
|
|
xcom(l);
|
|
xcom(r);
|
|
v = vlog(r);
|
|
if(v >= 0) {
|
|
n->op = OASAND;
|
|
r->vconst--;
|
|
}
|
|
break;
|
|
|
|
case OLMOD:
|
|
xcom(l);
|
|
xcom(r);
|
|
v = vlog(r);
|
|
if(v >= 0) {
|
|
n->op = OAND;
|
|
r->vconst--;
|
|
}
|
|
break;
|
|
|
|
case OLSHR:
|
|
case OASHL:
|
|
case OASHR:
|
|
xcom(l);
|
|
xcom(r);
|
|
simplifyshift(n);
|
|
break;
|
|
|
|
default:
|
|
if(l != Z)
|
|
xcom(l);
|
|
if(r != Z)
|
|
xcom(r);
|
|
break;
|
|
}
|
|
if(n->addable >= 10)
|
|
return;
|
|
if(l != Z)
|
|
n->complex = l->complex;
|
|
if(r != Z) {
|
|
if(r->complex == n->complex)
|
|
n->complex = r->complex+1;
|
|
else
|
|
if(r->complex > n->complex)
|
|
n->complex = r->complex;
|
|
}
|
|
if(n->complex == 0)
|
|
n->complex++;
|
|
|
|
// if(com64(n))
|
|
// return;
|
|
|
|
switch(n->op) {
|
|
|
|
case OFUNC:
|
|
n->complex = FNX;
|
|
break;
|
|
|
|
case OEQ:
|
|
case ONE:
|
|
case OLE:
|
|
case OLT:
|
|
case OGE:
|
|
case OGT:
|
|
case OHI:
|
|
case OHS:
|
|
case OLO:
|
|
case OLS:
|
|
/*
|
|
* immediate operators, make const on right
|
|
*/
|
|
if(l->op == OCONST) {
|
|
n->left = r;
|
|
n->right = l;
|
|
n->op = invrel[relindex(n->op)];
|
|
}
|
|
break;
|
|
|
|
case OADD:
|
|
case OXOR:
|
|
case OAND:
|
|
case OOR:
|
|
/*
|
|
* immediate operators, make const on right
|
|
*/
|
|
if(l->op == OCONST) {
|
|
n->left = r;
|
|
n->right = l;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|