1
0
mirror of https://github.com/golang/go synced 2024-11-25 02:17:57 -07:00

gc: make constant arith errors a little more friendly

Fixes #2804.

R=ken2
CC=golang-dev
https://golang.org/cl/5652067
This commit is contained in:
Russ Cox 2012-02-11 00:50:56 -05:00
parent 2233942e3c
commit 337547d1c9
9 changed files with 55 additions and 33 deletions

View File

@ -589,7 +589,7 @@ evconst(Node *n)
case TUP(OADD, CTINT): case TUP(OADD, CTINT):
case TUP(OADD, CTRUNE): case TUP(OADD, CTRUNE):
mpaddfixfix(v.u.xval, rv.u.xval); mpaddfixfix(v.u.xval, rv.u.xval, 0);
break; break;
case TUP(OSUB, CTINT): case TUP(OSUB, CTINT):
case TUP(OSUB, CTRUNE): case TUP(OSUB, CTRUNE):

View File

@ -1049,7 +1049,7 @@ void mpsubfltflt(Mpflt *a, Mpflt *b);
/* /*
* mparith2.c * mparith2.c
*/ */
void mpaddfixfix(Mpint *a, Mpint *b); void mpaddfixfix(Mpint *a, Mpint *b, int);
void mpandfixfix(Mpint *a, Mpint *b); void mpandfixfix(Mpint *a, Mpint *b);
void mpandnotfixfix(Mpint *a, Mpint *b); void mpandnotfixfix(Mpint *a, Mpint *b);
void mpdivfract(Mpint *a, Mpint *b); void mpdivfract(Mpint *a, Mpint *b);

View File

@ -2027,7 +2027,7 @@ hidden_constant:
{ {
if($2->val.ctype == CTRUNE && $4->val.ctype == CTINT) { if($2->val.ctype == CTRUNE && $4->val.ctype == CTINT) {
$$ = $2; $$ = $2;
mpaddfixfix($2->val.u.xval, $4->val.u.xval); mpaddfixfix($2->val.u.xval, $4->val.u.xval, 0);
break; break;
} }
$$ = nodcplxlit($2->val, $4->val); $$ = nodcplxlit($2->val, $4->val);

View File

@ -72,7 +72,7 @@ void
mpsubfixfix(Mpint *a, Mpint *b) mpsubfixfix(Mpint *a, Mpint *b)
{ {
mpnegfix(a); mpnegfix(a);
mpaddfixfix(a, b); mpaddfixfix(a, b, 0);
mpnegfix(a); mpnegfix(a);
} }
@ -90,7 +90,7 @@ mpaddcfix(Mpint *a, vlong c)
Mpint b; Mpint b;
mpmovecfix(&b, c); mpmovecfix(&b, c);
mpaddfixfix(a, &b); mpaddfixfix(a, &b, 0);
} }
void void
@ -302,7 +302,7 @@ mpatoflt(Mpflt *a, char *as)
if(c >= '0' && c <= '9') { if(c >= '0' && c <= '9') {
ex = ex*10 + (c-'0'); ex = ex*10 + (c-'0');
if(ex > 1e8) { if(ex > 1e8) {
yyerror("exponent out of range"); yyerror("constant exponent out of range: %s", as);
errorexit(); errorexit();
} }
continue; continue;
@ -343,7 +343,7 @@ out:
return; return;
bad: bad:
yyerror("set ovf in mpatof"); yyerror("constant too large: %s", as);
mpmovecflt(a, 0.0); mpmovecflt(a, 0.0);
} }
@ -431,7 +431,7 @@ out:
return; return;
bad: bad:
yyerror("set ovf in mpatov: %s", as); yyerror("constant too large: %s", as);
mpmovecfix(a, 0); mpmovecfix(a, 0);
} }

View File

@ -121,7 +121,8 @@ mpcmp(Mpint *a, Mpint *b)
int i; int i;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in cmp"); if(nsavederrors+nerrors == 0)
yyerror("ovf in cmp");
return 0; return 0;
} }
@ -190,13 +191,14 @@ mpshiftfix(Mpint *a, int s)
/// implements fix arihmetic /// implements fix arihmetic
void void
mpaddfixfix(Mpint *a, Mpint *b) mpaddfixfix(Mpint *a, Mpint *b, int quiet)
{ {
int i, c; int i, c;
long x, *a1, *b1; long x, *a1, *b1;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mpaddxx"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mpaddxx");
a->ovf = 1; a->ovf = 1;
return; return;
} }
@ -218,8 +220,8 @@ mpaddfixfix(Mpint *a, Mpint *b)
*a1++ = x; *a1++ = x;
} }
a->ovf = c; a->ovf = c;
if(a->ovf) if(a->ovf && !quiet)
yyerror("set ovf in mpaddxx"); yyerror("constant addition overflow");
return; return;
@ -266,7 +268,8 @@ mpmulfixfix(Mpint *a, Mpint *b)
Mpint s, q; Mpint s, q;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mpmulfixfix"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mpmulfixfix");
a->ovf = 1; a->ovf = 1;
return; return;
} }
@ -290,7 +293,7 @@ mpmulfixfix(Mpint *a, Mpint *b)
x = *a1++; x = *a1++;
for(j=0; j<Mpscale; j++) { for(j=0; j<Mpscale; j++) {
if(x & 1) if(x & 1)
mpaddfixfix(&q, &s); mpaddfixfix(&q, &s, 1);
mplsh(&s); mplsh(&s);
x >>= 1; x >>= 1;
} }
@ -299,7 +302,7 @@ mpmulfixfix(Mpint *a, Mpint *b)
q.neg = a->neg ^ b->neg; q.neg = a->neg ^ b->neg;
mpmovefixfix(a, &q); mpmovefixfix(a, &q);
if(a->ovf) if(a->ovf)
yyerror("set ovf in mpmulfixfix"); yyerror("constant multiplication overflow");
} }
void void
@ -311,7 +314,8 @@ mpmulfract(Mpint *a, Mpint *b)
Mpint s, q; Mpint s, q;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mpmulflt"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mpmulflt");
a->ovf = 1; a->ovf = 1;
return; return;
} }
@ -334,7 +338,7 @@ mpmulfract(Mpint *a, Mpint *b)
for(j=0; j<Mpscale; j++) { for(j=0; j<Mpscale; j++) {
x <<= 1; x <<= 1;
if(x & Mpbase) if(x & Mpbase)
mpaddfixfix(&q, &s); mpaddfixfix(&q, &s, 1);
mprsh(&s); mprsh(&s);
} }
} }
@ -342,7 +346,7 @@ mpmulfract(Mpint *a, Mpint *b)
q.neg = a->neg ^ b->neg; q.neg = a->neg ^ b->neg;
mpmovefixfix(a, &q); mpmovefixfix(a, &q);
if(a->ovf) if(a->ovf)
yyerror("set ovf in mpmulflt"); yyerror("constant multiplication overflow");
} }
void void
@ -353,7 +357,8 @@ mporfixfix(Mpint *a, Mpint *b)
x = 0; x = 0;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mporfixfix"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mporfixfix");
mpmovecfix(a, 0); mpmovecfix(a, 0);
a->ovf = 1; a->ovf = 1;
return; return;
@ -388,7 +393,8 @@ mpandfixfix(Mpint *a, Mpint *b)
x = 0; x = 0;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mpandfixfix"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mpandfixfix");
mpmovecfix(a, 0); mpmovecfix(a, 0);
a->ovf = 1; a->ovf = 1;
return; return;
@ -423,7 +429,8 @@ mpandnotfixfix(Mpint *a, Mpint *b)
x = 0; x = 0;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mpandnotfixfix"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mpandnotfixfix");
mpmovecfix(a, 0); mpmovecfix(a, 0);
a->ovf = 1; a->ovf = 1;
return; return;
@ -458,7 +465,8 @@ mpxorfixfix(Mpint *a, Mpint *b)
x = 0; x = 0;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mporfixfix"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mporfixfix");
mpmovecfix(a, 0); mpmovecfix(a, 0);
a->ovf = 1; a->ovf = 1;
return; return;
@ -491,7 +499,8 @@ mplshfixfix(Mpint *a, Mpint *b)
vlong s; vlong s;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mporfixfix"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mporfixfix");
mpmovecfix(a, 0); mpmovecfix(a, 0);
a->ovf = 1; a->ovf = 1;
return; return;
@ -512,7 +521,8 @@ mprshfixfix(Mpint *a, Mpint *b)
vlong s; vlong s;
if(a->ovf || b->ovf) { if(a->ovf || b->ovf) {
yyerror("ovf in mprshfixfix"); if(nsavederrors+nerrors == 0)
yyerror("ovf in mprshfixfix");
mpmovecfix(a, 0); mpmovecfix(a, 0);
a->ovf = 1; a->ovf = 1;
return; return;
@ -542,7 +552,8 @@ mpgetfix(Mpint *a)
vlong v; vlong v;
if(a->ovf) { if(a->ovf) {
yyerror("constant overflow"); if(nsavederrors+nerrors == 0)
yyerror("constant overflow");
return 0; return 0;
} }
@ -605,7 +616,7 @@ mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d)
r->ovf = 1; r->ovf = 1;
n->neg = ns; n->neg = ns;
d->neg = ds; d->neg = ds;
yyerror("set ovf in mpdivmodfixfix"); yyerror("constant division overflow");
return; return;
} }

View File

@ -89,17 +89,17 @@ mpaddfltflt(Mpflt *a, Mpflt *b)
// a is larger, shift b right // a is larger, shift b right
mpmovefltflt(&c, b); mpmovefltflt(&c, b);
mpshiftfix(&c.val, -s); mpshiftfix(&c.val, -s);
mpaddfixfix(&a->val, &c.val); mpaddfixfix(&a->val, &c.val, 0);
goto out; goto out;
} }
if(s < 0) { if(s < 0) {
// b is larger, shift a right // b is larger, shift a right
mpshiftfix(&a->val, s); mpshiftfix(&a->val, s);
a->exp -= s; a->exp -= s;
mpaddfixfix(&a->val, &b->val); mpaddfixfix(&a->val, &b->val, 0);
goto out; goto out;
} }
mpaddfixfix(&a->val, &b->val); mpaddfixfix(&a->val, &b->val, 0);
out: out:
mpnorm(a); mpnorm(a);
@ -153,7 +153,7 @@ mpdivfltflt(Mpflt *a, Mpflt *b)
a->exp = 0; a->exp = 0;
a->val.neg = 0; a->val.neg = 0;
a->val.ovf = 1; a->val.ovf = 1;
yyerror("mpdivfltflt divide by zero"); yyerror("constant division by zero");
return; return;
} }
@ -185,7 +185,7 @@ mpgetflt(Mpflt *a)
uvlong v, vm; uvlong v, vm;
double f; double f;
if(a->val.ovf) if(a->val.ovf && nsavederrors+nerrors == 0)
yyerror("mpgetflt ovf"); yyerror("mpgetflt ovf");
s = sigfig(a); s = sigfig(a);

View File

@ -5188,7 +5188,7 @@ yyreduce:
{ {
if((yyvsp[(2) - (5)].node)->val.ctype == CTRUNE && (yyvsp[(4) - (5)].node)->val.ctype == CTINT) { if((yyvsp[(2) - (5)].node)->val.ctype == CTRUNE && (yyvsp[(4) - (5)].node)->val.ctype == CTINT) {
(yyval.node) = (yyvsp[(2) - (5)].node); (yyval.node) = (yyvsp[(2) - (5)].node);
mpaddfixfix((yyvsp[(2) - (5)].node)->val.u.xval, (yyvsp[(4) - (5)].node)->val.u.xval); mpaddfixfix((yyvsp[(2) - (5)].node)->val.u.xval, (yyvsp[(4) - (5)].node)->val.u.xval, 0);
break; break;
} }
(yyval.node) = nodcplxlit((yyvsp[(2) - (5)].node)->val, (yyvsp[(4) - (5)].node)->val); (yyval.node) = nodcplxlit((yyvsp[(2) - (5)].node)->val, (yyvsp[(4) - (5)].node)->val);

View File

@ -671,6 +671,13 @@ func (b *builder) install(a *action) error {
} }
} }
// remove object dir to keep the amount of
// garbage down in a large build. On an operating system
// with aggressive buffering, cleaning incrementally like
// this keeps the intermediate objects from hitting the disk.
defer os.RemoveAll(a1.objdir)
defer os.Remove(a1.target)
return b.copyFile(a.target, a1.target, perm) return b.copyFile(a.target, a1.target, perm)
} }

View File

@ -10,3 +10,7 @@ const (
A int = 1 A int = 1
B byte; // ERROR "type without expr|expected .=." B byte; // ERROR "type without expr|expected .=."
) )
const LargeA = 1000000000000000000
const LargeB = LargeA * LargeA * LargeA
const LargeC = LargeB * LargeB * LargeB // ERROR "constant multiplication overflow"