Commit 47975235 authored by Ken Thompson's avatar Ken Thompson

fixed bugs in const/case

R=r
DELTA=138  (75 added, 12 deleted, 51 changed)
OCL=14129
CL=14131
parent 9b1e6fdd
...@@ -455,10 +455,9 @@ swgen(Node *n) ...@@ -455,10 +455,9 @@ swgen(Node *n)
} }
// put in the break between cases // put in the break between cases
if(any) { if(any)
patch(gbranch(AJMP, T), breakpc); patch(gbranch(AJMP, T), breakpc);
any = 0; any = 1;
}
// over case expressions // over case expressions
c2 = listfirst(&save2, &c1->left); c2 = listfirst(&save2, &c1->left);
......
...@@ -63,12 +63,16 @@ convlit(Node *n, Type *t) ...@@ -63,12 +63,16 @@ convlit(Node *n, Type *t)
break; break;
} }
if(isfloat[et]) { if(isfloat[et]) {
Mpint *xv;
// int to float // int to float
if(mpcmpfltflt(n->val.u.fval, minfltval[et]) < 0) xv = n->val.u.xval;
if(mpcmpfixflt(xv, minfltval[et]) < 0)
goto bad2; goto bad2;
if(mpcmpfltflt(n->val.u.fval, maxfltval[et]) > 0) if(mpcmpfixflt(xv, maxfltval[et]) > 0)
goto bad2; goto bad2;
mpmovefixflt(n->val.u.fval, n->val.u.xval); n->val.u.fval = mal(sizeof(*n->val.u.fval));
mpmovefixflt(n->val.u.fval, xv);
n->val.ctype = CTFLT; n->val.ctype = CTFLT;
break; break;
} }
...@@ -76,12 +80,16 @@ convlit(Node *n, Type *t) ...@@ -76,12 +80,16 @@ convlit(Node *n, Type *t)
case Wlitfloat: case Wlitfloat:
if(isint[et]) { if(isint[et]) {
Mpflt *fv;
// float to int // float to int
if(mpcmpfixfix(n->val.u.xval, minintval[et]) < 0) fv = n->val.u.fval;
if(mpcmpfltfix(fv, minintval[et]) < 0)
goto bad2; goto bad2;
if(mpcmpfixfix(n->val.u.xval, maxintval[et]) > 0) if(mpcmpfltfix(fv, maxintval[et]) > 0)
goto bad2; goto bad2;
mpmovefltfix(n->val.u.xval, n->val.u.fval); n->val.u.xval = mal(sizeof(*n->val.u.xval));
mpmovefltfix(n->val.u.xval, fv);
n->val.ctype = CTINT; n->val.ctype = CTINT;
break; break;
} }
......
...@@ -471,6 +471,8 @@ void mkpackage(char*); ...@@ -471,6 +471,8 @@ void mkpackage(char*);
/* /*
* mparith1.c * mparith1.c
*/ */
int mpcmpfixflt(Mpint *a, Mpflt *b);
int mpcmpfltfix(Mpflt *a, Mpint *b);
int mpcmpfixfix(Mpint *a, Mpint *b); int mpcmpfixfix(Mpint *a, Mpint *b);
int mpcmpfixc(Mpint *b, vlong c); int mpcmpfixc(Mpint *b, vlong c);
int mpcmpfltflt(Mpflt *a, Mpflt *b); int mpcmpfltflt(Mpflt *a, Mpflt *b);
......
...@@ -708,7 +708,7 @@ pexpr: ...@@ -708,7 +708,7 @@ pexpr:
{ {
$$ = $2; $$ = $2;
} }
| pexpr '.' sym1 | pexpr '.' sym2
{ {
$$ = nod(ODOT, $1, newname($3)); $$ = nod(ODOT, $1, newname($3));
} }
...@@ -812,13 +812,13 @@ name_name: ...@@ -812,13 +812,13 @@ name_name:
} }
new_name: new_name:
sym2 sym1
{ {
$$ = newname($1); $$ = newname($1);
} }
new_type: new_type:
sym2 sym1
{ {
$$ = newtype($1); $$ = newtype($1);
} }
...@@ -832,17 +832,36 @@ sym: ...@@ -832,17 +832,36 @@ sym:
sym1: sym1:
sym sym
| key1 | key1
| key2
sym2: sym2:
sym sym
| key1
| key2 | key2
/* /*
* keywords that we can * keywords that we can
>>>> ORIGINAL go/src/cmd/gc/go.y#8
==== THEIRS go/src/cmd/gc/go.y#9
* use as field names * use as field names
==== YOURS go/src/cmd/gc/go.y
* use a variable/type names
<<<<
*/ */
key1: key1:
LNIL
| LTRUE
| LFALSE
| LIOTA
| LLEN
| LPANIC
| LPRINT
| LNEW
| LBASETYPE
/*
* keywords that we can
*/
key2:
LPACKAGE LPACKAGE
| LIMPORT | LIMPORT
| LEXPORT | LEXPORT
...@@ -871,8 +890,21 @@ key1: ...@@ -871,8 +890,21 @@ key1:
| LRANGE | LRANGE
| LIGNORE | LIGNORE
/* >>>> ORIGINAL go/src/cmd/gc/go.y#8
* keywords that we can * use a variable/type names
*/
key2:
LNIL
| LTRUE
| LFALSE
| LIOTA
| LLEN
| LPANIC
| LPRINT
| LNEW
| LBASETYPE
==== THEIRS go/src/cmd/gc/go.y#9
* use as variable/type names * use as variable/type names
*/ */
key2: key2:
...@@ -886,6 +918,8 @@ key2: ...@@ -886,6 +918,8 @@ key2:
| LNEW | LNEW
| LBASETYPE | LBASETYPE
==== YOURS src/cmd/gc/go.y
<<<<
name: name:
lname lname
{ {
...@@ -1394,7 +1428,7 @@ export: ...@@ -1394,7 +1428,7 @@ export:
{ {
exportsym($1); exportsym($1);
} }
| sym '.' sym1 | sym '.' sym2
{ {
exportsym(pkglookup($3->name, $1->name)); exportsym(pkglookup($3->name, $1->name));
} }
...@@ -1578,7 +1612,7 @@ hidden_import: ...@@ -1578,7 +1612,7 @@ hidden_import:
} }
isym: isym:
sym '.' sym1 sym '.' sym2
{ {
$$ = nod(OIMPORT, N, N); $$ = nod(OIMPORT, N, N);
$$->osym = $1; $$->osym = $1;
...@@ -1586,7 +1620,7 @@ isym: ...@@ -1586,7 +1620,7 @@ isym:
$$->sym = $3; $$->sym = $3;
renamepkg($$); renamepkg($$);
} }
| '(' sym ')' sym '.' sym1 | '(' sym ')' sym '.' sym2
{ {
$$ = nod(OIMPORT, N, N); $$ = nod(OIMPORT, N, N);
$$->osym = $2; $$->osym = $2;
......
...@@ -1022,8 +1022,8 @@ void ...@@ -1022,8 +1022,8 @@ void
lexinit(void) lexinit(void)
{ {
int i, etype, lex; int i, etype, lex;
Sym *s;
Type *t; Type *t;
Sym *s;
besetptr(); besetptr();
......
...@@ -6,6 +6,28 @@ ...@@ -6,6 +6,28 @@
/// uses arihmetic /// uses arihmetic
int
mpcmpfixflt(Mpint *a, Mpflt *b)
{
char buf[500];
Mpflt c;
sprint(buf, "%B", a);
mpatoflt(&c, buf);
return mpcmpfltflt(&c, b);
}
int
mpcmpfltfix(Mpflt *a, Mpint *b)
{
char buf[500];
Mpflt c;
sprint(buf, "%B", b);
mpatoflt(&c, buf);
return mpcmpfltflt(a, &c);
}
int int
mpcmpfixfix(Mpint *a, Mpint *b) mpcmpfixfix(Mpint *a, Mpint *b)
{ {
......
...@@ -1064,7 +1064,7 @@ Nconv(Fmt *fp) ...@@ -1064,7 +1064,7 @@ Nconv(Fmt *fp)
snprint(buf1, sizeof(buf1), "S\"%Z\"", n->val.u.sval); snprint(buf1, sizeof(buf1), "S\"%Z\"", n->val.u.sval);
break; break;
case CTBOOL: case CTBOOL:
snprint(buf1, sizeof(buf1), "B%lld", n->val.u.bval); snprint(buf1, sizeof(buf1), "B%d", n->val.u.bval);
break; break;
case CTNIL: case CTNIL:
snprint(buf1, sizeof(buf1), "N"); snprint(buf1, sizeof(buf1), "N");
......
...@@ -1399,11 +1399,11 @@ Node* ...@@ -1399,11 +1399,11 @@ Node*
prcompat(Node *n) prcompat(Node *n)
{ {
Node *l, *r; Node *l, *r;
Node *on;
Type *t; Type *t;
Iter save; Iter save;
int w; int w;
char *name; char *name;
Node *on;
r = N; r = N;
l = listfirst(&save, &n); l = listfirst(&save, &n);
......
...@@ -162,35 +162,34 @@ func NextToken() ...@@ -162,35 +162,34 @@ func NextToken()
c = Get(); c = Get();
} }
switch c { switch c {
case EOF: case EOF:
token = EOF; token = EOF;
case '(': case '(', ')':
case ')': token = c;
token = c; break;
break; default:
default: for i = 0; i < 100 - 1; { // sizeof tokenbuf - 1
for i = 0; i < 100 - 1; { // sizeof tokenbuf - 1 tokenbuf[i] = convert(byte, c);
tokenbuf[i] = convert(byte, c); i = i + 1;
i = i + 1; c = Get();
c = Get(); if c == EOF {
if c == EOF { break;
break;
}
if WhiteSpace(c) || c == ')' {
peekc = c;
break;
}
}
if i >= 100 - 1 { // sizeof tokenbuf - 1
panic("atom too long\n");
} }
tokenlen = i; if WhiteSpace(c) || c == ')' {
tokenbuf[i] = nilchar; peekc = c;
if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' { break;
token = '0';
} else {
token = 'A';
} }
}
if i >= 100 - 1 { // sizeof tokenbuf - 1
panic("atom too long\n");
}
tokenlen = i;
tokenbuf[i] = nilchar;
if '0' <= tokenbuf[0] && tokenbuf[0] <= '9' {
token = '0';
} else {
token = 'A';
}
} }
} }
...@@ -267,16 +266,15 @@ func Parse() *Slist ...@@ -267,16 +266,15 @@ func Parse() *Slist
} else { } else {
// Atom // Atom
switch token { switch token {
case EOF: case EOF:
return nil; return nil;
case '0': case '0':
slist = atom(atoi()); slist = atom(atoi());
case '"': case '"', 'A':
case 'A': slist = atom(0);
slist = atom(0); default:
default: slist = nil;
slist = nil; print("unknown token: ", token, "\n");
print("unknown token"); // token, tokenbuf);
} }
NextToken(); NextToken();
return slist; return slist;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment