Commit e1a06ccc authored by Ken Thompson's avatar Ken Thompson

now checks for lvalue/rvalue context of

     expressions.
start of generics for calling builtin
     functions
start of map type
'any' demoted from reserved word to type

SVN=122808
parent 073486c3
...@@ -331,15 +331,9 @@ agen(Node *n, Node *res) ...@@ -331,15 +331,9 @@ agen(Node *n, Node *res)
regfree(&n2); regfree(&n2);
break; break;
// case OIND: case OIND:
// nl = n->left; cgen(nl, res);
// if(nl->addable) { break;
// gopcode(P_LOAD, T_ADDR, nl);
// break;
// }
// cgen(nl);
// gconv(T_ADDR, nl->type->etype);
// break;
case ODOT: case ODOT:
t = nl->type; t = nl->type;
......
...@@ -497,7 +497,7 @@ swgen(Node *n) ...@@ -497,7 +497,7 @@ swgen(Node *n)
n1.op = OEQ; n1.op = OEQ;
n1.left = &tmp; n1.left = &tmp;
n1.right = s->scase; n1.right = s->scase;
walktype(&n1, 0); walktype(&n1, Erv);
bgen(&n1, 1, s->sprog); bgen(&n1, 1, s->sprog);
} }
if(dflt != P) { if(dflt != P) {
......
...@@ -10,14 +10,15 @@ convlit(Node *n, Type *t) ...@@ -10,14 +10,15 @@ convlit(Node *n, Type *t)
{ {
int et; int et;
if(n->op != OLITERAL) if(n == N || n->op != OLITERAL || t == T)
return; return;
if(t == T)
if(t->etype == TANY || isptrto(t, TANY)) {
defaultlit(n);
return; return;
}
n->type = t;
et = t->etype; et = t->etype;
switch(whatis(n)) { switch(whatis(n)) {
case Wlitint: case Wlitint:
if(isptrto(t, TSTRING)) { if(isptrto(t, TSTRING)) {
...@@ -72,6 +73,7 @@ convlit(Node *n, Type *t) ...@@ -72,6 +73,7 @@ convlit(Node *n, Type *t)
} }
goto bad1; goto bad1;
} }
n->type = t;
return; return;
bad1: bad1:
...@@ -122,9 +124,17 @@ evconst(Node *n) ...@@ -122,9 +124,17 @@ evconst(Node *n)
case Wlitstr: case Wlitstr:
break; break;
} }
if(wl != wr) { if(wl != wr) {
yyerror("illegal combination of literals %d %d", nl->etype, nr->etype); if(wl == Wlitfloat && wr == Wlitint)
return; convlit(n->right, n->left->type);
else
if(wl == Wlitint && wr == Wlitfloat)
convlit(n->left, n->right->type);
else {
yyerror("illegal combination of literals %d %d", nl->etype, nr->etype);
return;
}
} }
switch(TUP(n->op, wl)) { switch(TUP(n->op, wl)) {
......
...@@ -37,9 +37,17 @@ loop: ...@@ -37,9 +37,17 @@ loop:
void void
dodcltype(Type *n, Type *t) dodcltype(Type *n, Type *t)
{ {
Type *nt;
if(n == T) if(n == T)
return; return;
if(t->sym != S) {
// botch -- should be a complete deep copy
nt = typ(Txxx);
*nt = *t;
t = nt;
t->sym = S;
}
addtyp(n, t, dclcontext); addtyp(n, t, dclcontext);
} }
......
...@@ -217,6 +217,17 @@ dumpexporttype(Sym *s) ...@@ -217,6 +217,17 @@ dumpexporttype(Sym *s)
} }
Bprint(bout, "%c\n", (et==TSTRUCT)? '}': '>'); Bprint(bout, "%c\n", (et==TSTRUCT)? '}': '>');
break; break;
case TMAP:
reexport(t->type);
reexport(t->down);
/* type 6 */
Bprint(bout, "\ttype ");
if(s->export != 0)
Bprint(bout, "!");
Bprint(bout, "%lS [%lS] %lS\n", s, t->down->sym, t->type->sym);
break;
} }
} }
...@@ -481,9 +492,18 @@ doimportv1(Node *ss, Node *st) ...@@ -481,9 +492,18 @@ doimportv1(Node *ss, Node *st)
* array type * array type
*/ */
void void
doimport1(Node *ss, Node *ss1, Node *s) doimport1(Node *ss, Node *si, Node *st)
{ {
fatal("doimport1"); Type *t;
Sym *s;
t = typ(TMAP);
s = pkglookup(si->sym->name, si->psym->name);
t->down = s->otype;
s = pkglookup(st->sym->name, st->psym->name);
t->type = s->otype;
importaddtyp(ss, t);
} }
/* /*
......
...@@ -320,6 +320,15 @@ enum ...@@ -320,6 +320,15 @@ enum
PSTATIC, PSTATIC,
}; };
enum
{
Exxx,
Eyyy,
Etop, // evaluated at statement level
Elv, // evaluated in lvalue context
Erv, // evaluated in rvalue context
};
typedef struct Io Io; typedef struct Io Io;
struct Io struct Io
{ {
...@@ -359,6 +368,7 @@ EXTERN uchar isptr[NTYPE]; ...@@ -359,6 +368,7 @@ EXTERN uchar isptr[NTYPE];
EXTERN uchar isint[NTYPE]; EXTERN uchar isint[NTYPE];
EXTERN uchar isfloat[NTYPE]; EXTERN uchar isfloat[NTYPE];
EXTERN uchar issigned[NTYPE]; EXTERN uchar issigned[NTYPE];
EXTERN uchar issimple[NTYPE];
EXTERN uchar okforeq[NTYPE]; EXTERN uchar okforeq[NTYPE];
EXTERN uchar okforadd[NTYPE]; EXTERN uchar okforadd[NTYPE];
EXTERN uchar okforand[NTYPE]; EXTERN uchar okforand[NTYPE];
...@@ -447,6 +457,7 @@ int isptrto(Type*, int); ...@@ -447,6 +457,7 @@ int isptrto(Type*, int);
int isinter(Type*); int isinter(Type*);
int isbytearray(Type*); int isbytearray(Type*);
int eqtype(Type*, Type*, int); int eqtype(Type*, Type*, int);
void argtype(Node*, Type*);
int eqargs(Type*, Type*); int eqargs(Type*, Type*);
ulong typehash(Type*, int); ulong typehash(Type*, int);
void frame(int); void frame(int);
...@@ -457,6 +468,7 @@ void ullmancalc(Node*); ...@@ -457,6 +468,7 @@ void ullmancalc(Node*);
void badtype(int, Type*, Type*); void badtype(int, Type*, Type*);
Type* ptrto(Type*); Type* ptrto(Type*);
Node* cleanidlist(Node*); Node* cleanidlist(Node*);
Node* syslook(char*, int);
Type** getthis(Type*); Type** getthis(Type*);
Type** getoutarg(Type*); Type** getoutarg(Type*);
...@@ -541,10 +553,10 @@ void doimport7(Node*, Node*); ...@@ -541,10 +553,10 @@ void doimport7(Node*, Node*);
*/ */
void walk(Node*); void walk(Node*);
void walktype(Node*, int); void walktype(Node*, int);
Type* walkswitch(Node*, Node*, Type*(*)(Node*, Type*)); Type* walkswitch(Node*, Type*(*)(Node*, Type*));
int casebody(Node*); int casebody(Node*);
int whatis(Node*); int whatis(Node*);
void walkdot(Node*); void walkdot(Node*, int);
Node* ascompatee(int, Node**, Node**); Node* ascompatee(int, Node**, Node**);
Node* ascompatet(int, Node**, Type**, int); Node* ascompatet(int, Node**, Type**, int);
Node* ascompatte(int, Type**, Node**, int); Node* ascompatte(int, Type**, Node**, int);
...@@ -552,7 +564,8 @@ int ascompat(Type*, Type*); ...@@ -552,7 +564,8 @@ int ascompat(Type*, Type*);
Node* prcompat(Node*); Node* prcompat(Node*);
Node* nodpanic(long); Node* nodpanic(long);
Node* newcompat(Node*); Node* newcompat(Node*);
Node* stringop(Node*); Node* stringop(Node*, int);
Node* mapop(Node*, int);
Node* convas(Node*); Node* convas(Node*);
void arrayconv(Type*, Node*); void arrayconv(Type*, Node*);
Node* reorder1(Node*); Node* reorder1(Node*);
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
struct Val val; struct Val val;
int lint; int lint;
} }
%token <sym> LNAME LBASETYPE LATYPE LANY LPACK LACONST %token <sym> LNAME LBASETYPE LATYPE LPACK LACONST
%token <val> LLITERAL LASOP %token <val> LLITERAL LASOP
%token LPACKAGE LIMPORT LEXPORT %token LPACKAGE LIMPORT LEXPORT
%token LMAP LCHAN LINTERFACE LFUNC LSTRUCT %token LMAP LCHAN LINTERFACE LFUNC LSTRUCT
...@@ -191,7 +191,7 @@ vardcl: ...@@ -191,7 +191,7 @@ vardcl:
} }
| new_name '=' expr | new_name '=' expr
{ {
walktype($3, 0); // this is a little harry walktype($3, Erv); // this is a little harry
defaultlit($3); defaultlit($3);
dodclvar($1, $3->type); dodclvar($1, $3->type);
...@@ -201,13 +201,13 @@ vardcl: ...@@ -201,13 +201,13 @@ vardcl:
constdcl: constdcl:
new_name '=' expr new_name '=' expr
{ {
walktype($3, 0); walktype($3, Erv);
dodclconst($1, $3); dodclconst($1, $3);
iota += 1; iota += 1;
} }
| new_name type '=' expr | new_name type '=' expr
{ {
walktype($4, 0); walktype($4, Erv);
convlit($4, $2); convlit($4, $2);
dodclconst($1, $4); dodclconst($1, $4);
iota += 1; iota += 1;
...@@ -257,7 +257,7 @@ simple_stmt: ...@@ -257,7 +257,7 @@ simple_stmt:
} }
| new_name LCOLAS expr | new_name LCOLAS expr
{ {
walktype($3, 0); // this is a little harry walktype($3, Erv); // this is a little harry
defaultlit($3); defaultlit($3);
dodclvar($1, $3->type); dodclvar($1, $3->type);
...@@ -278,17 +278,23 @@ complex_stmt: ...@@ -278,17 +278,23 @@ complex_stmt:
yyerror("switch statement must have case labels"); yyerror("switch statement must have case labels");
$$ = $2; $$ = $2;
$$->op = OSWITCH; $$->op = OSWITCH;
//if($$->ninit != N && $$->ntest == N)
// yyerror("switch expression should not be missing");
} }
| LIF if_stmt | LIF if_stmt
{ {
popdcl("if/switch"); popdcl("if/switch");
$$ = $2; $$ = $2;
//if($$->ninit != N && $$->ntest == N)
// yyerror("if conditional should not be missing");
} }
| LIF if_stmt LELSE else_stmt1 | LIF if_stmt LELSE else_stmt1
{ {
popdcl("if/switch"); popdcl("if/switch");
$$ = $2; $$ = $2;
$$->nelse = $4; $$->nelse = $4;
//if($$->ninit != N && $$->ntest == N)
// yyerror("if conditional should not be missing");
} }
| LRANGE range_stmt | LRANGE range_stmt
{ {
...@@ -352,6 +358,8 @@ semi_stmt: ...@@ -352,6 +358,8 @@ semi_stmt:
popdcl("if/switch"); popdcl("if/switch");
$$ = $2; $$ = $2;
$$->nelse = $4; $$->nelse = $4;
//if($$->ninit != N && $$->ntest == N)
// yyerror("if conditional should not be missing");
} }
compound_stmt: compound_stmt:
...@@ -756,10 +764,6 @@ typeconv: ...@@ -756,10 +764,6 @@ typeconv:
$$->down = $3; $$->down = $3;
$$->type = $5; $$->type = $5;
} }
| LANY
{
$$ = typ(TANY);
}
type: type:
latype latype
...@@ -799,10 +803,6 @@ type: ...@@ -799,10 +803,6 @@ type:
{ {
$$ = dostruct(N, TINTER); $$ = dostruct(N, TINTER);
} }
| LANY
{
$$ = typ(TANY);
}
| fntypeh | fntypeh
| '*' type | '*' type
{ {
...@@ -1284,8 +1284,10 @@ oarg_type_list: ...@@ -1284,8 +1284,10 @@ oarg_type_list:
* an output package * an output package
*/ */
hidden_import: hidden_import:
/* leftover import ignored */
LPACKAGE sym
/* variables */ /* variables */
LVAR hidden_importsym hidden_importsym | LVAR hidden_importsym hidden_importsym
{ {
// var // var
doimportv1($2, $3); doimportv1($2, $3);
......
...@@ -845,8 +845,10 @@ static struct ...@@ -845,8 +845,10 @@ static struct
"char", LBASETYPE, TUINT8, // temp?? "char", LBASETYPE, TUINT8, // temp??
"string", LBASETYPE, TSTRING, "string", LBASETYPE, TSTRING,
"any", LBASETYPE, TANY,
/* keywords */ /* keywords */
"any", LANY, Txxx, // "any", LANY, Txxx,
"break", LBREAK, Txxx, "break", LBREAK, Txxx,
"case", LCASE, Txxx, "case", LCASE, Txxx,
"chan", LCHAN, Txxx, "chan", LCHAN, Txxx,
...@@ -918,13 +920,17 @@ lexinit(void) ...@@ -918,13 +920,17 @@ lexinit(void)
okforeq[i] = 1; okforeq[i] = 1;
okforadd[i] = 1; okforadd[i] = 1;
okforand[i] = 1; okforand[i] = 1;
issimple[i] = 1;
} }
if(isfloat[i]) { if(isfloat[i]) {
okforeq[i] = 1; okforeq[i] = 1;
okforadd[i] = 1; okforadd[i] = 1;
issimple[i] = 1;
} }
switch(i) { switch(i) {
case TBOOL: case TBOOL:
issimple[i] = 1;
case TPTR32: case TPTR32:
case TPTR64: case TPTR64:
okforeq[i] = 1; okforeq[i] = 1;
......
...@@ -36,7 +36,7 @@ warn(char *fmt, ...) ...@@ -36,7 +36,7 @@ warn(char *fmt, ...)
{ {
va_list arg; va_list arg;
print("%L warning: "); print("%L: ");
va_start(arg, fmt); va_start(arg, fmt);
vfprint(1, fmt, arg); vfprint(1, fmt, arg);
va_end(arg); va_end(arg);
...@@ -50,7 +50,7 @@ fatal(char *fmt, ...) ...@@ -50,7 +50,7 @@ fatal(char *fmt, ...)
{ {
va_list arg; va_list arg;
print("%L fatal error: "); print("%L: fatal error: ");
va_start(arg, fmt); va_start(arg, fmt);
vfprint(1, fmt, arg); vfprint(1, fmt, arg);
va_end(arg); va_end(arg);
...@@ -344,7 +344,7 @@ aindex(Node *b, Type *t) ...@@ -344,7 +344,7 @@ aindex(Node *b, Type *t)
if(t->etype == TDARRAY) if(t->etype == TDARRAY)
yyerror("dynamic array type cannot be a dynamic array"); yyerror("dynamic array type cannot be a dynamic array");
walktype(b, 0); walktype(b, Erv);
switch(whatis(b)) { switch(whatis(b)) {
default: default:
yyerror("array bound must be a constant integer expression"); yyerror("array bound must be a constant integer expression");
...@@ -754,6 +754,7 @@ etnames[] = ...@@ -754,6 +754,7 @@ etnames[] =
[TFIELD] = "FIELD", [TFIELD] = "FIELD",
[TSTRING] = "STRING", [TSTRING] = "STRING",
[TCHAN] = "CHAN", [TCHAN] = "CHAN",
[TANY] = "ANY",
}; };
int int
...@@ -884,6 +885,7 @@ Tconv(Fmt *fp) ...@@ -884,6 +885,7 @@ Tconv(Fmt *fp)
strcpy(buf, ""); strcpy(buf, "");
if(t->sym != S) { if(t->sym != S) {
if(t->sym->name[0] != '_')
snprint(buf, sizeof(buf), "<%S>", t->sym); snprint(buf, sizeof(buf), "<%S>", t->sym);
} }
if(t->trecur > 5) { if(t->trecur > 5) {
...@@ -908,13 +910,13 @@ Tconv(Fmt *fp) ...@@ -908,13 +910,13 @@ Tconv(Fmt *fp)
case TFUNC: case TFUNC:
if(fp->flags & FmtLong) if(fp->flags & FmtLong)
snprint(buf1, sizeof(buf1), "%d%d%d(%lT,%lT,%lT)", snprint(buf1, sizeof(buf1), "%d%d%d(%lT,%lT)%lT",
t->thistuple, t->outtuple, t->intuple, t->thistuple, t->intuple, t->outtuple,
t->type, t->type->down, t->type->down->down); t->type, t->type->down->down, t->type->down);
else else
snprint(buf1, sizeof(buf1), "%d%d%d(%T,%T,%T)", snprint(buf1, sizeof(buf1), "%d%d%d(%T,%T)%T",
t->thistuple, t->outtuple, t->intuple, t->thistuple, t->intuple, t->outtuple,
t->type, t->type->down, t->type->down->down); t->type, t->type->down->down, t->type->down);
strncat(buf, buf1, sizeof(buf)); strncat(buf, buf1, sizeof(buf));
break; break;
...@@ -1205,6 +1207,141 @@ eqtype(Type *t1, Type *t2, int d) ...@@ -1205,6 +1207,141 @@ eqtype(Type *t1, Type *t2, int d)
return eqtype(t1->type, t2->type, d+1); return eqtype(t1->type, t2->type, d+1);
} }
static int
subtype(Type **stp, Type *t)
{
Type *st;
loop:
st = *stp;
if(st == T)
return 0;
switch(st->etype) {
default:
return 0;
case TPTR32:
case TPTR64:
stp = &st->type;
goto loop;
case TANY:
*stp = t;
break;
case TMAP:
if(subtype(&st->down, t))
break;
stp = &st->type;
goto loop;
case TFUNC:
for(;;) {
if(subtype(&st->type, t))
break;
if(subtype(&st->type->down->down, t))
break;
if(subtype(&st->type->down, t))
break;
return 0;
}
break;
case TSTRUCT:
for(st=st->type; st!=T; st=st->down)
if(subtype(&st->type, t))
return 1;
return 0;
}
return 1;
}
void
argtype(Node *on, Type *t)
{
if(!subtype(&on->type, t))
fatal("argtype: failed %N %T\n", on, t);
}
Type*
shallow(Type *t)
{
Type *nt;
if(t == T)
return T;
nt = typ(0);
*nt = *t;
return nt;
}
Type*
deep(Type *t)
{
Type *nt, *xt;
if(t == T)
return T;
switch(t->etype) {
default:
nt = t; // share from here down
break;
case TPTR32:
case TPTR64:
nt = shallow(t);
nt->type = deep(t->type);
break;
case TMAP:
nt = shallow(t);
nt->down = deep(t->down);
nt->type = deep(t->type);
break;
case TFUNC:
nt = shallow(t);
nt->type = deep(t->type);
nt->type->down = deep(t->type->down);
nt->type->down->down = deep(t->type->down->down);
break;
case TSTRUCT:
nt = shallow(t);
nt->type = shallow(t->type);
xt = nt->type;
for(t=t->type; t!=T; t=t->down) {
xt->type = deep(t->type);
xt->down = shallow(t->down);
xt = xt->down;
}
break;
}
return nt;
}
Node*
syslook(char *name, int copy)
{
Sym *s;
Node *n;
s = pkglookup(name, "sys");
if(s == S || s->oname == N)
fatal("looksys: cant find sys.%s", name);
if(!copy)
return s->oname;
n = nod(0, N, N);
*n = *s->oname;
n->type = deep(s->oname->type);
return n;
}
/* /*
* are the arg names of two * are the arg names of two
* functions the same. we know * functions the same. we know
...@@ -1386,13 +1523,27 @@ out: ...@@ -1386,13 +1523,27 @@ out:
void void
badtype(int o, Type *tl, Type *tr) badtype(int o, Type *tl, Type *tr)
{ {
yyerror("illegal types for operand");
loop:
switch(o) {
case OCALL:
if(tl == T || tr == T)
break;
if(isptr[tl->etype] && isptr[tr->etype]) {
tl = tl->type;
tr = tr->type;
goto loop;
}
if(tl->etype != TFUNC || tr->etype != TFUNC)
break;
// if(eqtype(t1, t2, 0))
}
yyerror("illegal types for operand: %O", o);
if(tl != T) if(tl != T)
print(" (%T)", tl); print(" (%lT)\n", tl);
print(" %O ", o);
if(tr != T) if(tr != T)
print("(%T)", tr); print(" (%lT)\n", tr);
print("\n");
} }
/* /*
...@@ -1605,6 +1756,11 @@ listnext(Iter *s) ...@@ -1605,6 +1756,11 @@ listnext(Iter *s)
n = s->n; n = s->n;
r = n->right; r = n->right;
if(r == N) {
s->an = &s->n;
s->n = N;
return N;
}
if(r->op == OLIST) { if(r->op == OLIST) {
s->n = r; s->n = r;
s->an = &r->left; s->an = &r->left;
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package foop // rename to avoid redeclaration package foop // rename to avoid redeclaration
func mal(uint32) *byte; func mal(uint32) *any;
func breakpoint(); func breakpoint();
func panicl(int32); func panicl(int32);
...@@ -27,17 +27,27 @@ func frexp(float64) (int32, float64); // break fp into exp,fract ...@@ -27,17 +27,27 @@ func frexp(float64) (int32, float64); // break fp into exp,fract
func ldexp(int32, float64) float64; // make fp from exp,fract func ldexp(int32, float64) float64; // make fp from exp,fract
func modf(float64) (float64, float64); // break fp into double.double func modf(float64) (float64, float64); // break fp into double.double
func newmap(keysize uint32, valsize uint32,
keyalg uint32, valalg uint32,
hint uint32) (hmap *map[any]any);
func mapaccess1(hmap *map[any]any, key any) (val any);
func mapaccess2(hmap *map[any]any, key any) (val any, pres bool);
func mapdelete(hmap *map[any]any, key any);
func mapassign(hmap *map[any]any, any);
export export
mal mal
breakpoint breakpoint
panicl
// print panic
panicl
printbool printbool
printfloat printfloat
printint printint
printstring printstring
printpointer printpointer
// op string
catstring catstring
cmpstring cmpstring
slicestring slicestring
...@@ -49,4 +59,12 @@ export ...@@ -49,4 +59,12 @@ export
frexp frexp
ldexp ldexp
modf modf
// op map
newmap
mapaccess1
mapaccess2
mapdelete
mapassign
; ;
char* sysimport = char* sysimport =
"package sys\n"
"type sys._e002 {}\n" "type sys._e002 {}\n"
"type sys.uint8 2\n" "type sys.any 24\n"
"type sys._e003 *sys.uint8\n" "type sys._e003 *sys.any\n"
"type sys._o137 {_e135 sys._e003}\n" "type sys._o172 {_e170 sys._e003}\n"
"type sys.uint32 6\n" "type sys.uint32 6\n"
"type sys._i139 {_e136 sys.uint32}\n" "type sys._i174 {_e171 sys.uint32}\n"
"type sys._e001 (sys._e002 sys._o137 sys._i139)\n" "type sys._e001 (sys._e002 sys._o172 sys._i174)\n"
"var !sys.mal sys._e001\n" "var !sys.mal sys._e001\n"
"type sys._e005 {}\n" "type sys._e005 {}\n"
"type sys._e006 {}\n" "type sys._e006 {}\n"
...@@ -15,95 +16,131 @@ char* sysimport = ...@@ -15,95 +16,131 @@ char* sysimport =
"type sys._e009 {}\n" "type sys._e009 {}\n"
"type sys._e010 {}\n" "type sys._e010 {}\n"
"type sys.int32 5\n" "type sys.int32 5\n"
"type sys._i145 {_e144 sys.int32}\n" "type sys._i180 {_e179 sys.int32}\n"
"type sys._e008 (sys._e009 sys._e010 sys._i145)\n" "type sys._e008 (sys._e009 sys._e010 sys._i180)\n"
"var !sys.panicl sys._e008\n" "var !sys.panicl sys._e008\n"
"type sys._e012 {}\n" "type sys._e012 {}\n"
"type sys._e013 {}\n" "type sys._e013 {}\n"
"type sys.bool 12\n" "type sys.bool 12\n"
"type sys._i150 {_e149 sys.bool}\n" "type sys._i185 {_e184 sys.bool}\n"
"type sys._e011 (sys._e012 sys._e013 sys._i150)\n" "type sys._e011 (sys._e012 sys._e013 sys._i185)\n"
"var !sys.printbool sys._e011\n" "var !sys.printbool sys._e011\n"
"type sys._e015 {}\n" "type sys._e015 {}\n"
"type sys._e016 {}\n" "type sys._e016 {}\n"
"type sys.float64 10\n" "type sys.float64 10\n"
"type sys._i155 {_e154 sys.float64}\n" "type sys._i190 {_e189 sys.float64}\n"
"type sys._e014 (sys._e015 sys._e016 sys._i155)\n" "type sys._e014 (sys._e015 sys._e016 sys._i190)\n"
"var !sys.printfloat sys._e014\n" "var !sys.printfloat sys._e014\n"
"type sys._e018 {}\n" "type sys._e018 {}\n"
"type sys._e019 {}\n" "type sys._e019 {}\n"
"type sys.int64 7\n" "type sys.int64 7\n"
"type sys._i160 {_e159 sys.int64}\n" "type sys._i195 {_e194 sys.int64}\n"
"type sys._e017 (sys._e018 sys._e019 sys._i160)\n" "type sys._e017 (sys._e018 sys._e019 sys._i195)\n"
"var !sys.printint sys._e017\n" "var !sys.printint sys._e017\n"
"type sys._e021 {}\n" "type sys._e021 {}\n"
"type sys._e022 {}\n" "type sys._e022 {}\n"
"type sys._e023 25\n" "type sys._e023 25\n"
"type sys.string *sys._e023\n" "type sys.string *sys._e023\n"
"type sys._i165 {_e164 sys.string}\n" "type sys._i200 {_e199 sys.string}\n"
"type sys._e020 (sys._e021 sys._e022 sys._i165)\n" "type sys._e020 (sys._e021 sys._e022 sys._i200)\n"
"var !sys.printstring sys._e020\n" "var !sys.printstring sys._e020\n"
"type sys._e025 {}\n" "type sys._e025 {}\n"
"type sys._e026 {}\n" "type sys._e026 {}\n"
"type sys.uint8 2\n"
"type sys._e027 *sys.uint8\n" "type sys._e027 *sys.uint8\n"
"type sys._i170 {_e169 sys._e027}\n" "type sys._i205 {_e204 sys._e027}\n"
"type sys._e024 (sys._e025 sys._e026 sys._i170)\n" "type sys._e024 (sys._e025 sys._e026 sys._i205)\n"
"var !sys.printpointer sys._e024\n" "var !sys.printpointer sys._e024\n"
"type sys._e029 {}\n" "type sys._e029 {}\n"
"type sys._o177 {_e174 sys.string}\n" "type sys._o212 {_e209 sys.string}\n"
"type sys._i179 {_e175 sys.string _e176 sys.string}\n" "type sys._i214 {_e210 sys.string _e211 sys.string}\n"
"type sys._e028 (sys._e029 sys._o177 sys._i179)\n" "type sys._e028 (sys._e029 sys._o212 sys._i214)\n"
"var !sys.catstring sys._e028\n" "var !sys.catstring sys._e028\n"
"type sys._e031 {}\n" "type sys._e031 {}\n"
"type sys._o187 {_e184 sys.int32}\n" "type sys._o222 {_e219 sys.int32}\n"
"type sys._i189 {_e185 sys.string _e186 sys.string}\n" "type sys._i224 {_e220 sys.string _e221 sys.string}\n"
"type sys._e030 (sys._e031 sys._o187 sys._i189)\n" "type sys._e030 (sys._e031 sys._o222 sys._i224)\n"
"var !sys.cmpstring sys._e030\n" "var !sys.cmpstring sys._e030\n"
"type sys._e033 {}\n" "type sys._e033 {}\n"
"type sys._o198 {_e194 sys.string}\n" "type sys._o233 {_e229 sys.string}\n"
"type sys._i200 {_e195 sys.string _e196 sys.int32 _e197 sys.int32}\n" "type sys._i235 {_e230 sys.string _e231 sys.int32 _e232 sys.int32}\n"
"type sys._e032 (sys._e033 sys._o198 sys._i200)\n" "type sys._e032 (sys._e033 sys._o233 sys._i235)\n"
"var !sys.slicestring sys._e032\n" "var !sys.slicestring sys._e032\n"
"type sys._e035 {}\n" "type sys._e035 {}\n"
"type sys._o209 {_e206 sys.uint8}\n" "type sys._o244 {_e241 sys.uint8}\n"
"type sys._i211 {_e207 sys.string _e208 sys.int32}\n" "type sys._i246 {_e242 sys.string _e243 sys.int32}\n"
"type sys._e034 (sys._e035 sys._o209 sys._i211)\n" "type sys._e034 (sys._e035 sys._o244 sys._i246)\n"
"var !sys.indexstring sys._e034\n" "var !sys.indexstring sys._e034\n"
"type sys._e037 {}\n" "type sys._e037 {}\n"
"type sys._o218 {_e216 sys.string}\n" "type sys._o253 {_e251 sys.string}\n"
"type sys._i220 {_e217 sys.int64}\n" "type sys._i255 {_e252 sys.int64}\n"
"type sys._e036 (sys._e037 sys._o218 sys._i220)\n" "type sys._e036 (sys._e037 sys._o253 sys._i255)\n"
"var !sys.intstring sys._e036\n" "var !sys.intstring sys._e036\n"
"type sys._e039 {}\n" "type sys._e039 {}\n"
"type sys._o227 {_e224 sys.string}\n" "type sys._o262 {_e259 sys.string}\n"
"type sys._e040 *sys.uint8\n" "type sys._e040 *sys.uint8\n"
"type sys._i229 {_e225 sys._e040 _e226 sys.int32}\n" "type sys._i264 {_e260 sys._e040 _e261 sys.int32}\n"
"type sys._e038 (sys._e039 sys._o227 sys._i229)\n" "type sys._e038 (sys._e039 sys._o262 sys._i264)\n"
"var !sys.byteastring sys._e038\n" "var !sys.byteastring sys._e038\n"
"type sys._e042 {}\n" "type sys._e042 {}\n"
"type sys._e043 <>\n" "type sys._e043 <>\n"
"type sys._o238 {_e234 sys._e043}\n" "type sys._o273 {_e269 sys._e043}\n"
"type sys._e044 *sys.uint8\n" "type sys._e044 *sys.uint8\n"
"type sys._e045 *sys.uint8\n" "type sys._e045 *sys.uint8\n"
"type sys._s245 {}\n" "type sys._s280 {}\n"
"type sys._e046 *sys._s245\n" "type sys._e046 *sys._s280\n"
"type sys._i240 {_e235 sys._e044 _e236 sys._e045 _e237 sys._e046}\n" "type sys._i275 {_e270 sys._e044 _e271 sys._e045 _e272 sys._e046}\n"
"type sys._e041 (sys._e042 sys._o238 sys._i240)\n" "type sys._e041 (sys._e042 sys._o273 sys._i275)\n"
"var !sys.mkiface sys._e041\n" "var !sys.mkiface sys._e041\n"
"type sys._e048 {}\n" "type sys._e048 {}\n"
"type sys._o251 {_e248 sys.int32 _e249 sys.float64}\n" "type sys._o286 {_e283 sys.int32 _e284 sys.float64}\n"
"type sys._i253 {_e250 sys.float64}\n" "type sys._i288 {_e285 sys.float64}\n"
"type sys._e047 (sys._e048 sys._o251 sys._i253)\n" "type sys._e047 (sys._e048 sys._o286 sys._i288)\n"
"var !sys.frexp sys._e047\n" "var !sys.frexp sys._e047\n"
"type sys._e050 {}\n" "type sys._e050 {}\n"
"type sys._o260 {_e257 sys.float64}\n" "type sys._o295 {_e292 sys.float64}\n"
"type sys._i262 {_e258 sys.int32 _e259 sys.float64}\n" "type sys._i297 {_e293 sys.int32 _e294 sys.float64}\n"
"type sys._e049 (sys._e050 sys._o260 sys._i262)\n" "type sys._e049 (sys._e050 sys._o295 sys._i297)\n"
"var !sys.ldexp sys._e049\n" "var !sys.ldexp sys._e049\n"
"type sys._e052 {}\n" "type sys._e052 {}\n"
"type sys._o270 {_e267 sys.float64 _e268 sys.float64}\n" "type sys._o305 {_e302 sys.float64 _e303 sys.float64}\n"
"type sys._i272 {_e269 sys.float64}\n" "type sys._i307 {_e304 sys.float64}\n"
"type sys._e051 (sys._e052 sys._o270 sys._i272)\n" "type sys._e051 (sys._e052 sys._o305 sys._i307)\n"
"var !sys.modf sys._e051\n" "var !sys.modf sys._e051\n"
"type sys._e054 {}\n"
"type sys._e056 [sys.any] sys.any\n"
"type sys._e055 *sys._e056\n"
"type sys._o311 {hmap sys._e055}\n"
"type sys._i313 {keysize sys.uint32 valsize sys.uint32 keyalg sys.uint32 valalg sys.uint32 hint sys.uint32}\n"
"type sys._e053 (sys._e054 sys._o311 sys._i313)\n"
"var !sys.newmap sys._e053\n"
"type sys._e058 {}\n"
"type sys._o321 {val sys.any}\n"
"type sys._e060 [sys.any] sys.any\n"
"type sys._e059 *sys._e060\n"
"type sys._i323 {hmap sys._e059 key sys.any}\n"
"type sys._e057 (sys._e058 sys._o321 sys._i323)\n"
"var !sys.mapaccess1 sys._e057\n"
"type sys._e062 {}\n"
"type sys._o328 {val sys.any pres sys.bool}\n"
"type sys._e064 [sys.any] sys.any\n"
"type sys._e063 *sys._e064\n"
"type sys._i330 {hmap sys._e063 key sys.any}\n"
"type sys._e061 (sys._e062 sys._o328 sys._i330)\n"
"var !sys.mapaccess2 sys._e061\n"
"type sys._e066 {}\n"
"type sys._e067 {}\n"
"type sys._e069 [sys.any] sys.any\n"
"type sys._e068 *sys._e069\n"
"type sys._i335 {hmap sys._e068 key sys.any}\n"
"type sys._e065 (sys._e066 sys._e067 sys._i335)\n"
"var !sys.mapdelete sys._e065\n"
"type sys._e071 {}\n"
"type sys._e072 {}\n"
"type sys._e074 [sys.any] sys.any\n"
"type sys._e073 *sys._e074\n"
"type sys._i341 {hmap sys._e073 _e340 sys.any}\n"
"type sys._e070 (sys._e071 sys._e072 sys._i341)\n"
"var !sys.mapassign sys._e070\n"
"))\n" "))\n"
; ;
...@@ -13,7 +13,7 @@ void ...@@ -13,7 +13,7 @@ void
walk(Node *fn) walk(Node *fn)
{ {
curfn = fn; curfn = fn;
walktype(fn->nbody, 1); walktype(fn->nbody, Etop);
} }
void void
...@@ -32,6 +32,10 @@ walktype(Node *n, int top) ...@@ -32,6 +32,10 @@ walktype(Node *n, int top)
*/ */
lno = dynlineno; lno = dynlineno;
if(top == Exxx || top == Eyyy) {
dump("", n);
fatal("walktype: top=%d", top);
}
loop: loop:
if(n == N) if(n == N)
...@@ -39,6 +43,9 @@ loop: ...@@ -39,6 +43,9 @@ loop:
if(n->op != ONAME) if(n->op != ONAME)
dynlineno = n->lineno; // for diagnostics dynlineno = n->lineno; // for diagnostics
if(debug['T'])
print("%L walktype %O %d\n", n->op, top);
t = T; t = T;
et = Txxx; et = Txxx;
...@@ -48,21 +55,29 @@ loop: ...@@ -48,21 +55,29 @@ loop:
goto ret; goto ret;
case OPRINT: case OPRINT:
walktype(n->left, 0); if(top != Etop)
goto nottop;
walktype(n->left, Erv);
*n = *prcompat(n->left); *n = *prcompat(n->left);
goto ret; goto ret;
case OPANIC: case OPANIC:
walktype(n->left, 0); if(top != Etop)
goto nottop;
walktype(n->left, Erv);
*n = *nod(OLIST, prcompat(n->left), nodpanic(n->lineno)); *n = *nod(OLIST, prcompat(n->left), nodpanic(n->lineno));
goto ret; goto ret;
case OLITERAL: case OLITERAL:
if(top != Erv)
goto nottop;
n->addable = 1; n->addable = 1;
ullmancalc(n); ullmancalc(n);
goto ret; goto ret;
case ONAME: case ONAME:
if(top == Etop)
goto nottop;
n->addable = 1; n->addable = 1;
ullmancalc(n); ullmancalc(n);
if(n->type == T) { if(n->type == T) {
...@@ -80,63 +95,65 @@ loop: ...@@ -80,63 +95,65 @@ loop:
goto loop; goto loop;
case OFOR: case OFOR:
if(!top) if(top != Etop)
goto nottop; goto nottop;
walktype(n->ninit, 1); walktype(n->ninit, Etop);
walktype(n->ntest, 1); walktype(n->ntest, Erv);
walktype(n->nincr, 1); walktype(n->nincr, Etop);
n = n->nbody; n = n->nbody;
goto loop; goto loop;
case OSWITCH: case OSWITCH:
if(!top) if(top != Etop)
goto nottop; goto nottop;
if(n->ntest == N) if(n->ntest == N)
n->ntest = booltrue; n->ntest = booltrue;
walktype(n->ninit, 1); walktype(n->ninit, Etop);
walktype(n->ntest, 1); walktype(n->ntest, Erv);
walktype(n->nbody, 1); walktype(n->nbody, Etop);
// find common type // find common type
if(n->ntest->type == T) if(n->ntest->type == T)
n->ntest->type = walkswitch(n->ntest, n->nbody, sw1); n->ntest->type = walkswitch(n, sw1);
// if that fails pick a type // if that fails pick a type
if(n->ntest->type == T) if(n->ntest->type == T)
n->ntest->type = walkswitch(n->ntest, n->nbody, sw2); n->ntest->type = walkswitch(n, sw2);
// set the type on all literals // set the type on all literals
if(n->ntest->type != T) if(n->ntest->type != T)
walkswitch(n->ntest, n->nbody, sw3); walkswitch(n, sw3);
walktype(n->ntest, 1); walktype(n->ntest, Erv);
n = n->nincr; n = n->nincr;
goto loop; goto loop;
case OEMPTY: case OEMPTY:
if(!top) if(top != Etop)
goto nottop; goto nottop;
goto ret; goto ret;
case OIF: case OIF:
if(!top) if(top != Etop)
goto nottop; goto nottop;
walktype(n->ninit, 1); walktype(n->ninit, Etop);
walktype(n->ntest, 1); walktype(n->ntest, Erv);
walktype(n->nelse, 1); walktype(n->nelse, Etop);
n = n->nbody; n = n->nbody;
goto loop; goto loop;
case OCALLMETH: case OCALLMETH:
case OCALLINTER: case OCALLINTER:
case OCALL: case OCALL:
if(top == Elv)
goto nottop;
n->ullman = UINF; n->ullman = UINF;
if(n->type != T) if(n->type != T)
goto ret; goto ret;
walktype(n->left, 0); walktype(n->left, Erv);
if(n->left == N) if(n->left == N)
goto ret; goto ret;
...@@ -162,7 +179,7 @@ loop: ...@@ -162,7 +179,7 @@ loop:
if(t->outtuple == 1) if(t->outtuple == 1)
n->type = n->type->type->type; n->type = n->type->type->type;
walktype(n->right, 0); walktype(n->right, Erv);
switch(n->op) { switch(n->op) {
default: default:
...@@ -190,19 +207,26 @@ loop: ...@@ -190,19 +207,26 @@ loop:
goto ret; goto ret;
case OAS: case OAS:
if(!top) if(top != Etop)
goto nottop; goto nottop;
r = n->right;
if(r == N)
goto ret;
l = n->left; l = n->left;
r = n->right;
if(l == N) if(l == N)
goto ret; goto ret;
walktype(l, Elv);
walktype(r, Erv);
if(l == N || l->type == T)
goto ret;
convlit(r, l->type);
if(r == N || r->type == T)
goto ret;
if(r->op == OCALL && l->op == OLIST) { if(r->op == OCALL && l->op == OLIST) {
walktype(l, 0);
walktype(r, 0);
l = ascompatet(n->op, &n->left, &r->type, 0); l = ascompatet(n->op, &n->left, &r->type, 0);
if(l != N) { if(l != N) {
*n = *nod(OLIST, r, reorder2(l)); *n = *nod(OLIST, r, reorder2(l));
...@@ -210,8 +234,6 @@ loop: ...@@ -210,8 +234,6 @@ loop:
goto ret; goto ret;
} }
walktype(l, 0);
walktype(r, 0);
l = ascompatee(n->op, &n->left, &n->right); l = ascompatee(n->op, &n->left, &n->right);
if(l != N) if(l != N)
*n = *reorder3(l); *n = *reorder3(l);
...@@ -221,17 +243,26 @@ loop: ...@@ -221,17 +243,26 @@ loop:
case OCONTINUE: case OCONTINUE:
case OGOTO: case OGOTO:
case OLABEL: case OLABEL:
if(top != Etop)
goto nottop;
goto ret; goto ret;
case OXCASE: case OXCASE:
if(top != Etop)
goto nottop;
yyerror("case statement out of place"); yyerror("case statement out of place");
n->op = OCASE; n->op = OCASE;
case OCASE: case OCASE:
n = n->left; if(top != Etop)
goto nottop;
walktype(n->left, Erv);
n = n->right;
goto loop; goto loop;
case OXFALL: case OXFALL:
if(top != Etop)
goto nottop;
yyerror("fallthrough statement out of place"); yyerror("fallthrough statement out of place");
n->op = OFALL; n->op = OFALL;
...@@ -242,12 +273,16 @@ loop: ...@@ -242,12 +273,16 @@ loop:
case OS2I: case OS2I:
case OI2S: case OI2S:
case OI2I: case OI2I:
if(top != Erv)
goto nottop;
n->addable = 0; n->addable = 0;
walktype(n->left, 0); walktype(n->left, Erv);
goto ret; goto ret;
case OCONV: case OCONV:
walktype(n->left, 0); if(top != Erv)
goto nottop;
walktype(n->left, Erv);
if(n->left == N) if(n->left == N)
goto ret; goto ret;
...@@ -271,12 +306,12 @@ loop: ...@@ -271,12 +306,12 @@ loop:
// to string // to string
if(isptrto(n->type, TSTRING)) { if(isptrto(n->type, TSTRING)) {
if(isint[n->left->type->etype]) { if(isint[n->left->type->etype]) {
*n = *stringop(n); *n = *stringop(n, top);
goto ret; goto ret;
} }
if(isbytearray(n->left->type) != 0) { if(isbytearray(n->left->type) != 0) {
n->op = OARRAY; n->op = OARRAY;
*n = *stringop(n); *n = *stringop(n, top);
goto ret; goto ret;
} }
} }
...@@ -290,22 +325,28 @@ loop: ...@@ -290,22 +325,28 @@ loop:
goto ret; goto ret;
case ORETURN: case ORETURN:
walktype(n->left, 0); if(top != Etop)
goto nottop;
walktype(n->left, Erv);
l = ascompatte(n->op, getoutarg(curfn->type), &n->left, 1); l = ascompatte(n->op, getoutarg(curfn->type), &n->left, 1);
if(l != N) if(l != N)
n->left = reorder4(l); n->left = reorder4(l);
goto ret; goto ret;
case ONOT: case ONOT:
walktype(n->left, 0); if(top != Erv)
goto nottop;
walktype(n->left, Erv);
if(n->left == N || n->left->type == T) if(n->left == N || n->left->type == T)
goto ret; goto ret;
et = n->left->type->etype; et = n->left->type->etype;
break; break;
case OASOP: case OASOP:
if(!top) if(top != Etop)
goto nottop; goto nottop;
walktype(n->left, Elv);
goto com;
case OLSH: case OLSH:
case ORSH: case ORSH:
...@@ -325,8 +366,12 @@ loop: ...@@ -325,8 +366,12 @@ loop:
case OSUB: case OSUB:
case OMUL: case OMUL:
case ODIV: case ODIV:
walktype(n->left, 0); if(top != Erv)
walktype(n->right, 0); goto nottop;
walktype(n->left, Erv);
com:
walktype(n->right, Erv);
if(n->left == N || n->right == N) if(n->left == N || n->right == N)
goto ret; goto ret;
convlit(n->left, n->right->type); convlit(n->left, n->right->type);
...@@ -349,7 +394,7 @@ loop: ...@@ -349,7 +394,7 @@ loop:
case OADD: case OADD:
case OASOP: case OASOP:
if(isptrto(n->left->type, TSTRING)) { if(isptrto(n->left->type, TSTRING)) {
*n = *stringop(n); *n = *stringop(n, top);
goto ret; goto ret;
} }
} }
...@@ -358,7 +403,9 @@ loop: ...@@ -358,7 +403,9 @@ loop:
case OMINUS: case OMINUS:
case OPLUS: case OPLUS:
case OCOM: case OCOM:
walktype(n->left, 0); if(top != Erv)
goto nottop;
walktype(n->left, Erv);
if(n->left == N) if(n->left == N)
goto ret; goto ret;
evconst(n); evconst(n);
...@@ -368,7 +415,9 @@ loop: ...@@ -368,7 +415,9 @@ loop:
break; break;
case OLEN: case OLEN:
walktype(n->left, 0); if(top != Erv)
goto nottop;
walktype(n->left, Erv);
evconst(n); evconst(n);
ullmancalc(n); ullmancalc(n);
t = n->left->type; t = n->left->type;
...@@ -387,8 +436,12 @@ loop: ...@@ -387,8 +436,12 @@ loop:
case OINDEX: case OINDEX:
case OINDEXPTR: case OINDEXPTR:
walktype(n->left, 0); if(top == Etop)
walktype(n->right, 0); goto nottop;
walktype(n->left, top);
walktype(n->right, Erv);
ullmancalc(n); ullmancalc(n);
if(n->left == N || n->right == N) if(n->left == N || n->right == N)
goto ret; goto ret;
...@@ -398,7 +451,7 @@ loop: ...@@ -398,7 +451,7 @@ loop:
// map // map
if(isptrto(t, TMAP)) { if(isptrto(t, TMAP)) {
fatal("index map"); *n = *mapop(n, top);
goto ret; goto ret;
} }
...@@ -412,7 +465,7 @@ loop: ...@@ -412,7 +465,7 @@ loop:
// left side is string // left side is string
if(isptrto(t, TSTRING)) { if(isptrto(t, TSTRING)) {
*n = *stringop(n); *n = *stringop(n, top);
goto ret; goto ret;
} }
...@@ -427,12 +480,15 @@ loop: ...@@ -427,12 +480,15 @@ loop:
goto ret; goto ret;
case OSLICE: case OSLICE:
walktype(n->left, 0); if(top == Etop)
walktype(n->right, 0); goto nottop;
walktype(n->left, top);
walktype(n->right, Erv);
if(n->left == N || n->right == N) if(n->left == N || n->right == N)
goto ret; goto ret;
if(isptrto(n->left->type, TSTRING)) { if(isptrto(n->left->type, TSTRING)) {
*n = *stringop(n); *n = *stringop(n, top);
goto ret; goto ret;
} }
badtype(OSLICE, n->left->type, T); badtype(OSLICE, n->left->type, T);
...@@ -442,11 +498,15 @@ loop: ...@@ -442,11 +498,15 @@ loop:
case ODOTPTR: case ODOTPTR:
case ODOTMETH: case ODOTMETH:
case ODOTINTER: case ODOTINTER:
walkdot(n); if(top == Etop)
goto nottop;
walkdot(n, top);
goto ret; goto ret;
case OADDR: case OADDR:
walktype(n->left, 0); if(top != Erv)
goto nottop;
walktype(n->left, Elv);
if(n->left == N) if(n->left == N)
goto ret; goto ret;
t = n->left->type; t = n->left->type;
...@@ -456,7 +516,9 @@ loop: ...@@ -456,7 +516,9 @@ loop:
goto ret; goto ret;
case OIND: case OIND:
walktype(n->left, 0); if(top == Etop)
goto nottop;
walktype(n->left, top);
if(n->left == N) if(n->left == N)
goto ret; goto ret;
t = n->left->type; t = n->left->type;
...@@ -468,6 +530,8 @@ loop: ...@@ -468,6 +530,8 @@ loop:
goto ret; goto ret;
case ONEW: case ONEW:
if(top != Erv)
goto nottop;
*n = *newcompat(n); *n = *newcompat(n);
goto ret; goto ret;
} }
...@@ -535,7 +599,7 @@ loop: ...@@ -535,7 +599,7 @@ loop:
l = nod(OSUB, l, n->left); l = nod(OSUB, l, n->left);
*n = *l; *n = *l;
walktype(n, 0); walktype(n, Erv);
goto ret; goto ret;
case OLSH: case OLSH:
...@@ -557,7 +621,8 @@ loop: ...@@ -557,7 +621,8 @@ loop:
goto ret; goto ret;
nottop: nottop:
fatal("walktype: not top %O", n->op); dump("bad top", n);
fatal("walktype: top=%d %O", top, n->op);
goto ret; goto ret;
badt: badt:
...@@ -615,16 +680,18 @@ sw3(Node *c, Type *place) ...@@ -615,16 +680,18 @@ sw3(Node *c, Type *place)
} }
Type* Type*
walkswitch(Node *test, Node *body, Type*(*call)(Node*, Type*)) walkswitch(Node *sw, Type*(*call)(Node*, Type*))
{ {
Node *n, *c; Node *n, *c;
Type *place; Type *place;
place = call(test, T); place = call(sw->ntest, T);
n = body; n = sw->nbody;
if(n->op == OLIST) if(n->op == OLIST)
n = n->left; n = n->left;
if(n->op == OEMPTY)
return;
for(; n!=N; n=n->right) { for(; n!=N; n=n->right) {
if(n->op != OCASE) if(n->op != OCASE)
...@@ -656,14 +723,11 @@ casebody(Node *n) ...@@ -656,14 +723,11 @@ casebody(Node *n)
oc = N; // last case statement oc = N; // last case statement
ot = N; // last statement (look for XFALL) ot = N; // last statement (look for XFALL)
t = listfirst(&save, &n); t = listfirst(&save, &n);
if(t->op != OXCASE)
return 0;
loop: loop:
if(t == N) { if(t == N) {
/* empty switch */
if(oc == N) if(oc == N)
return 0; return 0;
return 1; return 1;
...@@ -680,7 +744,7 @@ loop: ...@@ -680,7 +744,7 @@ loop:
ot->op = OFALL; ot->op = OFALL;
} }
/* if first statement is not case then return 0 */ /* if first statement is not case */
if(oc == N) if(oc == N)
return 0; return 0;
...@@ -740,16 +804,19 @@ deep: ...@@ -740,16 +804,19 @@ deep:
} }
void void
walkdot(Node *n) walkdot(Node *n, int top)
{ {
Node *mn; Node *mn;
Type *t, *f; Type *t, *f;
int i; int i;
if(debug['T'])
print("%L walkdot %O %d\n", n->op, top);
if(n->left == N || n->right == N) if(n->left == N || n->right == N)
return; return;
walktype(n->left, 0); walktype(n->left, Erv);
if(n->right->op != ONAME) { if(n->right->op != ONAME) {
yyerror("rhs of . must be a name"); yyerror("rhs of . must be a name");
return; return;
...@@ -918,7 +985,6 @@ loop: ...@@ -918,7 +985,6 @@ loop:
yyerror("error in shape across assignment"); yyerror("error in shape across assignment");
return rev(nn); return rev(nn);
} }
convlit(r, l->type); convlit(r, l->type);
if(!ascompat(l->type, r->type)) { if(!ascompat(l->type, r->type)) {
badtype(op, l->type, r->type); badtype(op, l->type, r->type);
...@@ -971,14 +1037,14 @@ prcompat(Node *n) ...@@ -971,14 +1037,14 @@ prcompat(Node *n)
Iter save; Iter save;
int w; int w;
char *name; char *name;
Sym *s; Node *on;
r = N; r = N;
l = listfirst(&save, &n); l = listfirst(&save, &n);
loop: loop:
if(l == N) { if(l == N) {
walktype(r, 1); walktype(r, Etop);
return r; return r;
} }
...@@ -1006,11 +1072,8 @@ loop: ...@@ -1006,11 +1072,8 @@ loop:
break; break;
} }
s = pkglookup(name, "sys"); on = syslook(name, 0);
if(s == S || s->oname == N) t = *getinarg(on->type);
fatal("prcompat: cant find sys_%s", name);
t = *getinarg(s->oname->type);
if(t != nil) if(t != nil)
t = t->type; t = t->type;
if(t != nil) if(t != nil)
...@@ -1022,9 +1085,9 @@ loop: ...@@ -1022,9 +1085,9 @@ loop:
} }
if(r == N) if(r == N)
r = nod(OCALL, s->oname, l); r = nod(OCALL, on, l);
else else
r = nod(OLIST, r, nod(OCALL, s->oname, l)); r = nod(OLIST, r, nod(OCALL, on, l));
l = listnext(&save); l = listnext(&save);
goto loop; goto loop;
...@@ -1033,57 +1096,55 @@ loop: ...@@ -1033,57 +1096,55 @@ loop:
Node* Node*
nodpanic(long lineno) nodpanic(long lineno)
{ {
Sym *s; Node *n, *on;
char *name;
Node *n;
name = "panicl";
s = pkglookup(name, "sys");
if(s == S || s->oname == N)
fatal("prcompat: cant find sys_%s", name);
on = syslook("panicl", 0);
n = nodintconst(lineno); n = nodintconst(lineno);
n = nod(OCALL, s->oname, n); n = nod(OCALL, on, n);
walktype(n, 1); walktype(n, Etop);
return n; return n;
} }
Node* Node*
newcompat(Node *n) newcompat(Node *n)
{ {
Node *r; Node *r, *on;
Type *t; Type *t;
Sym *s;
t = n->type;
if(t == T || !isptr[t->etype] || t->type == T)
fatal("newcompat: type should be pointer %lT", t);
t = t->type;
if(t->etype == TMAP) {
r = mapop(n, Erv);
return r;
}
if(n->left != N) if(n->left != N)
yyerror("dont know what new(,e) means"); yyerror("dont know what new(,e) means");
t = n->type;
if(t == T || !isptr[t->etype])
fatal("NEW sb pointer %lT", t);
dowidth(t->type); dowidth(t);
on = syslook("mal", 1);
s = pkglookup("mal", "sys"); argtype(on, t);
if(s == S || s->oname == N)
fatal("newcompat: cant find sys_mal");
r = nodintconst(t->type->width); r = nodintconst(t->width);
r = nod(OCALL, s->oname, r); r = nod(OCALL, on, r);
walktype(r, 0); walktype(r, Erv);
// r = nod(OCONV, r, N); // r = nod(OCONV, r, N);
r->type = t; r->type = n->type;
return r; return r;
} }
Node* Node*
stringop(Node *n) stringop(Node *n, int top)
{ {
Node *r, *c; Node *r, *c, *on;
Sym *s; long lno, l;
long lno;
long l;
lno = dynlineno; lno = dynlineno;
dynlineno = n->lineno; dynlineno = n->lineno;
...@@ -1099,23 +1160,18 @@ stringop(Node *n) ...@@ -1099,23 +1160,18 @@ stringop(Node *n)
case OLE: case OLE:
case OLT: case OLT:
// sys_cmpstring(s1, s2) :: 0 // sys_cmpstring(s1, s2) :: 0
s = pkglookup("cmpstring", "sys"); on = syslook("cmpstring", 0);
if(s == S || s->oname == N)
fatal("stringop: cant find sys_cmpstring");
r = nod(OLIST, n->left, n->right); r = nod(OLIST, n->left, n->right);
r = nod(OCALL, s->oname, r); r = nod(OCALL, on, r);
c = nodintconst(0); c = nodintconst(0);
r = nod(n->op, r, c); r = nod(n->op, r, c);
break; break;
case OADD: case OADD:
// sys_catstring(s1, s2) // sys_catstring(s1, s2)
s = pkglookup("catstring", "sys"); on = syslook("catstring", 0);
if(s == S || s->oname == N)
fatal("stringop: cant find sys_catstring");
r = nod(OLIST, n->left, n->right); r = nod(OLIST, n->left, n->right);
r = nod(OCALL, s->oname, r); r = nod(OCALL, on, r);
break; break;
case OASOP: case OASOP:
...@@ -1126,11 +1182,11 @@ stringop(Node *n) ...@@ -1126,11 +1182,11 @@ stringop(Node *n)
case OADD: case OADD:
// s1 = sys_catstring(s1, s2) // s1 = sys_catstring(s1, s2)
s = pkglookup("catstring", "sys"); if(n->etype != OADD)
if(s == S || s->oname == N || n->etype != OADD) fatal("stringop: not cat");
fatal("stringop: cant find sys_catstring");
r = nod(OLIST, n->left, n->right); r = nod(OLIST, n->left, n->right);
r = nod(OCALL, s->oname, r); on = syslook("catstring", 0);
r = nod(OCALL, on, r);
r = nod(OAS, n->left, r); r = nod(OAS, n->left, r);
break; break;
} }
...@@ -1138,10 +1194,6 @@ stringop(Node *n) ...@@ -1138,10 +1194,6 @@ stringop(Node *n)
case OSLICE: case OSLICE:
// sys_slicestring(s, lb, hb) // sys_slicestring(s, lb, hb)
s = pkglookup("slicestring", "sys");
if(s == S || s->oname == N)
fatal("stringop: cant find sys_slicestring");
r = nod(OCONV, n->right->left, N); r = nod(OCONV, n->right->left, N);
r->type = types[TINT32]; r->type = types[TINT32];
...@@ -1149,43 +1201,30 @@ stringop(Node *n) ...@@ -1149,43 +1201,30 @@ stringop(Node *n)
c->type = types[TINT32]; c->type = types[TINT32];
r = nod(OLIST, r, c); r = nod(OLIST, r, c);
r = nod(OLIST, n->left, r); r = nod(OLIST, n->left, r);
on = syslook("slicestring", 0);
r = nod(OCALL, s->oname, r); r = nod(OCALL, on, r);
break; break;
case OINDEX: case OINDEX:
// sys_indexstring(s, i) // sys_indexstring(s, i)
s = pkglookup("indexstring", "sys");
if(s == S || s->oname == N)
fatal("stringop: cant find sys_indexstring");
r = nod(OCONV, n->right, N); r = nod(OCONV, n->right, N);
r->type = types[TINT32]; r->type = types[TINT32];
r = nod(OLIST, n->left, r); r = nod(OLIST, n->left, r);
r = nod(OCALL, s->oname, r); on = syslook("indexstring", 0);
r = nod(OCALL, on, r);
break; break;
case OCONV: case OCONV:
// sys_intstring(v) // sys_intstring(v)
s = pkglookup("intstring", "sys");
if(s == S || s->oname == N)
fatal("stringop: cant find sys_intstring");
r = nod(OCONV, n->left, N); r = nod(OCONV, n->left, N);
r->type = types[TINT64]; r->type = types[TINT64];
on = syslook("intstring", 0);
r = nod(OCALL, s->oname, r); r = nod(OCALL, on, r);
break; break;
case OARRAY: case OARRAY:
// byteastring(a, l) // byteastring(a, l)
s = pkglookup("byteastring", "sys");
if(s == S || s->oname == N)
fatal("stringop: cant find sys_byteastring");
c = nodintconst(0); c = nodintconst(0);
r = nod(OINDEX, n->left, c); r = nod(OINDEX, n->left, c);
r = nod(OADDR, r, N); r = nod(OADDR, r, N);
...@@ -1194,11 +1233,158 @@ stringop(Node *n) ...@@ -1194,11 +1233,158 @@ stringop(Node *n)
c = nodintconst(l-1); c = nodintconst(l-1);
r = nod(OLIST, r, c); r = nod(OLIST, r, c);
r = nod(OCALL, s->oname, r); on = syslook("byteastring", 0);
r = nod(OCALL, on, r);
break;
}
walktype(r, top);
dynlineno = lno;
return r;
}
Type*
fixmap(Type *tm)
{
Type *t;
t = tm->type;
if(t == T) {
fatal("fixmap: t nil");
return T;
}
if(t->etype != TMAP) {
fatal("fixmap: %O not map");
return T;
}
if(t->down == T || t->type == T) {
fatal("fixmap: map key/value types are nil");
return T;
}
dowidth(t->down);
dowidth(t->type);
return t;
}
static int
algtype(Type *t)
{
int a;
a = 0;
if(issimple[t->etype])
a = 1; // simple mem
else
if(isptrto(t, TSTRING))
a = 2; // string
else
if(isptr[t->etype])
a = 3; // pointer
else
if(isinter(t))
a = 4; // interface
else
fatal("algtype: cant find type %T", t);
return a;
}
Node*
mapop(Node *n, int top)
{
long lno;
Node *r, *a;
Type *t;
Node *on;
int alg1, alg2;
lno = dynlineno;
dynlineno = n->lineno;
print("mapop %O\n", n->op);
r = n;
switch(n->op) {
default:
fatal("stringop: unknown op %E", n->op);
case ONEW:
// newmap(keysize uint32, valsize uint32,
// keyalg uint32, valalg uint32,
// hint uint32) (hmap *map[any]any);
t = fixmap(n->type);
if(t == T)
break;
a = n->left; // hint
if(n->left == N)
a = nodintconst(0);
r = a;
a = nodintconst(algtype(t->type)); // val algorithm
r = nod(OLIST, a, r);
a = nodintconst(algtype(t->down)); // key algorithm
r = nod(OLIST, a, r);
a = nodintconst(t->type->width); // val width
r = nod(OLIST, a, r);
a = nodintconst(t->down->width); // key width
r = nod(OLIST, a, r);
on = syslook("newmap", 1);
print("type1=%lT\n", on->type);
argtype(on, t->down); // any-1
argtype(on, t->type); // any-2
print("type5=%lT\n", on->type);
r = nod(OCALL, on, r);
walktype(r, top);
r->type = n->type;
break;
case OINDEX:
case OINDEXPTR:
// mapaccess1(hmap *map[any]any, key any) (val any);
t = fixmap(n->left->type);
if(t == T)
break;
convlit(n->right, t->down);
if(!eqtype(n->right->type, t->down, 0)) {
badtype(n->op, n->right->type, t->down);
break;
}
a = n->right; // key
if(!isptr[t->down->etype]) {
a = nod(OADDR, a, N);
a->type = ptrto(t);
}
r = a;
a = n->left; // map
r = nod(OLIST, a, r);
on = syslook("mapaccess1", 1);
print("type1=%lT\n", on->type);
argtype(on, t->down); // any-1
argtype(on, t->type); // any-2
argtype(on, t->down); // any-3
argtype(on, t->type); // any-4
print("type5=%lT\n", on->type);
r = nod(OCALL, on, r);
walktype(r, Erv);
r->type = ptrto(t->type);
r = nod(OIND, r, N);
r->type = t->type;
break; break;
} }
walktype(r, 1);
dynlineno = lno; dynlineno = lno;
return r; return r;
} }
...@@ -1265,7 +1451,7 @@ ret: ...@@ -1265,7 +1451,7 @@ ret:
n->right = nod(o, r, N); n->right = nod(o, r, N);
n->right->type = l->type; n->right->type = l->type;
walktype(n, 1); walktype(n, Etop);
return n; return n;
} }
...@@ -1289,7 +1475,7 @@ loop: ...@@ -1289,7 +1475,7 @@ loop:
} }
c++; c++;
walktype(l, 0); walktype(l, Erv);
convlit(l, t->type); convlit(l, t->type);
if(!ascompat(l->type, t->type)) if(!ascompat(l->type, t->type))
badtype(OARRAY, l->type, t->type); badtype(OARRAY, l->type, t->type);
......
...@@ -147,7 +147,7 @@ throw(int8 *s) ...@@ -147,7 +147,7 @@ throw(int8 *s)
sys_exit(1); sys_exit(1);
} }
static void void
mcpy(byte *t, byte *f, uint32 n) mcpy(byte *t, byte *f, uint32 n)
{ {
while(n > 0) { while(n > 0) {
...@@ -169,7 +169,7 @@ brk(uint32 n) ...@@ -169,7 +169,7 @@ brk(uint32 n)
return v; return v;
} }
static void* void*
mal(uint32 n) mal(uint32 n)
{ {
byte* v; byte* v;
......
...@@ -84,6 +84,8 @@ enum ...@@ -84,6 +84,8 @@ enum
*/ */
void FLUSH(void*); void FLUSH(void*);
void prints(int8*); void prints(int8*);
void mcpy(byte*, byte*, uint32);
void* mal(uint32);
void sys_exit(int32); void sys_exit(int32);
void sys_write(int32, void*, int32); void sys_write(int32, void*, int32);
void sys_breakpoint(void); void sys_breakpoint(void);
...@@ -97,6 +99,7 @@ void sys_printbool(bool); ...@@ -97,6 +99,7 @@ void sys_printbool(bool);
void sys_printfloat(float64); void sys_printfloat(float64);
void sys_printint(int64); void sys_printint(int64);
void sys_printstring(string); void sys_printstring(string);
void sys_printpointer(void*);
void sys_catstring(string, string, string); void sys_catstring(string, string, string);
void sys_cmpstring(string, string, int32); void sys_cmpstring(string, string, int32);
void sys_slicestring(string, int32, int32, string); void sys_slicestring(string, int32, int32, string);
......
...@@ -16,6 +16,7 @@ func assert(cond bool, msg string) { ...@@ -16,6 +16,7 @@ func assert(cond bool, msg string) {
func main() { func main() {
i5 := 5; i5 := 5;
i7 := 7; i7 := 7;
hello := "hello";
switch true { switch true {
case i5 < 5: assert(false, "<"); case i5 < 5: assert(false, "<");
...@@ -122,6 +123,13 @@ func main() { ...@@ -122,6 +123,13 @@ func main() {
} }
assert(fired > 0, "fired"); assert(fired > 0, "fired");
switch hello {
case "wowie": assert(false, "wowie");
case "hello": assert(true, "hello");
case "jumpn": assert(false, "jumpn");
default: assert(false, "default");
}
fired = 0; fired = 0;
switch i := i5 + 2; i { switch i := i5 + 2; i {
case i7: fired = 1; case i7: fired = 1;
......
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