Commit c6130e9f authored by Ken Thompson's avatar Ken Thompson

get rid of static/dynamic array distinction

R=r
OCL=14634
CL=14634
parent 66a603c9
...@@ -157,14 +157,13 @@ dowidth(Type *t) ...@@ -157,14 +157,13 @@ dowidth(Type *t)
case TSTRING: // implemented as pointer case TSTRING: // implemented as pointer
w = wptr; w = wptr;
break; break;
case TDARRAY:
fatal("width of a dynamic array");
case TARRAY: case TARRAY:
if(t->bound < 0)
fatal("width of a dynamic array");
if(t->type == T) if(t->type == T)
break; break;
dowidth(t->type); dowidth(t->type);
w = t->bound * t->type->width w = t->bound * t->type->width;
;// + offsetof(Array, b[0]);
break; break;
case TSTRUCT: case TSTRUCT:
......
...@@ -136,7 +136,7 @@ cgen(Node *n, Node *res) ...@@ -136,7 +136,7 @@ cgen(Node *n, Node *res)
} }
regalloc(&n1, nl->type, res); regalloc(&n1, nl->type, res);
cgen(nl, &n1); cgen(nl, &n1);
if(isptrto(n->type, TARRAY) && isptrto(nl->type, TDARRAY)) { if(isptrarray(n->type) && isptrdarray(nl->type)) {
// convert dynamic array to static array // convert dynamic array to static array
n2 = n1; n2 = n1;
n2.op = OINDREG; n2.op = OINDREG;
...@@ -144,7 +144,7 @@ cgen(Node *n, Node *res) ...@@ -144,7 +144,7 @@ cgen(Node *n, Node *res)
n2.type = types[tptr]; n2.type = types[tptr];
gins(AMOVQ, &n2, &n1); gins(AMOVQ, &n2, &n1);
} }
if(isptrto(n->type, TDARRAY) && isptrto(nl->type, TARRAY)) { if(isptrdarray(n->type) && isptrarray(nl->type)) {
// conver static array to dynamic array // conver static array to dynamic array
// it is assumed that the dope is just before the array // it is assumed that the dope is just before the array
nodconst(&n2, types[tptr], offsetof(Array,b)); nodconst(&n2, types[tptr], offsetof(Array,b));
...@@ -187,7 +187,7 @@ cgen(Node *n, Node *res) ...@@ -187,7 +187,7 @@ cgen(Node *n, Node *res)
regfree(&n1); regfree(&n1);
break; break;
} }
if(isptrto(nl->type, TDARRAY)) { if(isptrdarray(nl->type)) {
regalloc(&n1, types[tptr], res); regalloc(&n1, types[tptr], res);
cgen(nl, &n1); cgen(nl, &n1);
n1.op = OINDREG; n1.op = OINDREG;
...@@ -201,7 +201,7 @@ cgen(Node *n, Node *res) ...@@ -201,7 +201,7 @@ cgen(Node *n, Node *res)
break; break;
case OCAP: case OCAP:
if(isptrto(nl->type, TDARRAY)) { if(isptrdarray(nl->type)) {
regalloc(&n1, types[tptr], res); regalloc(&n1, types[tptr], res);
cgen(nl, &n1); cgen(nl, &n1);
n1.op = OINDREG; n1.op = OINDREG;
...@@ -387,7 +387,7 @@ agen(Node *n, Node *res) ...@@ -387,7 +387,7 @@ agen(Node *n, Node *res)
// i is in &n1 // i is in &n1
// w is width // w is width
if(isptrto(nl->type, TDARRAY)) { if(isptrdarray(nl->type)) {
regalloc(&n2, types[tptr], res); regalloc(&n2, types[tptr], res);
gmove(res, &n2); gmove(res, &n2);
...@@ -418,7 +418,7 @@ agen(Node *n, Node *res) ...@@ -418,7 +418,7 @@ agen(Node *n, Node *res)
if(!debug['B']) { if(!debug['B']) {
// check bounds // check bounds
nodconst(&n3, types[TUINT32], nl->type->bound); nodconst(&n3, types[TUINT32], nl->type->bound);
if(isptrto(nl->type, TARRAY)) if(isptrarray(nl->type))
nodconst(&n3, types[TUINT32], nl->type->type->bound); nodconst(&n3, types[TUINT32], nl->type->type->bound);
gins(optoas(OCMP, types[TUINT32]), &n1, &n3); gins(optoas(OCMP, types[TUINT32]), &n1, &n3);
......
...@@ -1604,7 +1604,6 @@ isfat(Type *t) ...@@ -1604,7 +1604,6 @@ isfat(Type *t)
switch(t->etype) { switch(t->etype) {
case TSTRUCT: case TSTRUCT:
case TARRAY: case TARRAY:
case TDARRAY:
case TINTER: // maybe remove later case TINTER: // maybe remove later
return 1; return 1;
} }
......
...@@ -481,7 +481,7 @@ loop: ...@@ -481,7 +481,7 @@ loop:
if(n->op != ODCLFIELD || n->type == T) if(n->op != ODCLFIELD || n->type == T)
fatal("stotype: oops %N\n", n); fatal("stotype: oops %N\n", n);
if(n->type->etype == TDARRAY) if(n->type->etype == TARRAY && n->type->bound < 0)
yyerror("type of a structure field cannot be an open array"); yyerror("type of a structure field cannot be an open array");
f = typ(TFIELD); f = typ(TFIELD);
......
...@@ -147,18 +147,16 @@ dumpexporttype(Sym *s) ...@@ -147,18 +147,16 @@ dumpexporttype(Sym *s)
break; break;
case TARRAY: case TARRAY:
case TDARRAY:
reexport(t->type); reexport(t->type);
/* type 2 */ /* type 2 */
Bprint(bout, "\ttype "); Bprint(bout, "\ttype ");
if(s->export != 0) if(s->export != 0)
Bprint(bout, "!"); Bprint(bout, "!");
if(et == TDARRAY) { if(t->bound >= 0)
Bprint(bout, "%lS [%lud] %lS\n", s, t->bound, t->type->sym);
else
Bprint(bout, "%lS [] %lS\n", s, t->type->sym); Bprint(bout, "%lS [] %lS\n", s, t->type->sym);
break;
}
Bprint(bout, "%lS [%lud] %lS\n", s, t->bound, t->type->sym);
break; break;
case TPTR32: case TPTR32:
...@@ -522,13 +520,10 @@ doimport2(Node *ss, Val *b, Node *st) ...@@ -522,13 +520,10 @@ doimport2(Node *ss, Val *b, Node *st)
Type *t; Type *t;
Sym *s; Sym *s;
if(b == nil) { t = typ(TARRAY);
t = typ(TDARRAY); t->bound = -1;
t->dbound = N; if(b != nil)
} else {
t = typ(TARRAY);
t->bound = mpgetfix(b->u.xval); t->bound = mpgetfix(b->u.xval);
}
s = pkglookup(st->sym->name, st->psym->name); s = pkglookup(st->sym->name, st->psym->name);
t->type = s->otype; t->type = s->otype;
......
...@@ -140,8 +140,7 @@ struct Type ...@@ -140,8 +140,7 @@ struct Type
vlong argwid; vlong argwid;
// TARRAY // TARRAY
int32 bound; int32 bound; // negative is dynamic array
Node* dbound;
}; };
#define T ((Type*)0) #define T ((Type*)0)
...@@ -303,7 +302,7 @@ enum ...@@ -303,7 +302,7 @@ enum
TFUNC, TFUNC,
TARRAY, TARRAY,
TDARRAY, T_old_DARRAY,
TSTRUCT, TSTRUCT,
TCHAN, TCHAN,
TMAP, TMAP,
...@@ -558,6 +557,8 @@ void dump(char*, Node*); ...@@ -558,6 +557,8 @@ void dump(char*, Node*);
Type* aindex(Node*, Type*); Type* aindex(Node*, Type*);
int isnil(Node*); int isnil(Node*);
int isptrto(Type*, int); int isptrto(Type*, int);
int isptrarray(Type*);
int isptrdarray(Type*);
int isinter(Type*); int isinter(Type*);
int isbytearray(Type*); int isbytearray(Type*);
int eqtype(Type*, Type*, int); int eqtype(Type*, Type*, int);
......
...@@ -376,38 +376,28 @@ aindex(Node *b, Type *t) ...@@ -376,38 +376,28 @@ aindex(Node *b, Type *t)
Type *r; Type *r;
int bound; int bound;
bound = -1; // open bound
walktype(b, Erv); walktype(b, Erv);
switch(whatis(b)) { switch(whatis(b)) {
default: // variable bound default: // variable bound
walktype(b, Erv);
if(b->type != T && isint[b->type->etype])
goto dyn;
yyerror("array bound must be an integer expression"); yyerror("array bound must be an integer expression");
bound = 0;
break; break;
case Wnil: // open bound case Wnil: // open bound
goto dyn; break;
case Wlitint: // fixed bound case Wlitint: // fixed bound
bound = mpgetfix(b->val.u.xval); bound = mpgetfix(b->val.u.xval);
if(bound < 0)
yyerror("array bound must be non negative");
break; break;
} }
// fixed array // fixed array
r = typ(TARRAY); r = typ(TARRAY);
r->type = t; r->type = t;
r->dbound = b;
r->bound = bound; r->bound = bound;
return r; return r;
dyn:
// dynamic array
r = typ(TDARRAY);
r->type = t;
r->dbound = b;
r->bound = 0;
return r;
} }
void void
...@@ -806,7 +796,7 @@ etnames[] = ...@@ -806,7 +796,7 @@ etnames[] =
[TPTR64] = "PTR64", [TPTR64] = "PTR64",
[TFUNC] = "FUNC", [TFUNC] = "FUNC",
[TARRAY] = "ARRAY", [TARRAY] = "ARRAY",
[TDARRAY] = "DARRAY", // [TDARRAY] = "DARRAY",
[TSTRUCT] = "STRUCT", [TSTRUCT] = "STRUCT",
[TCHAN] = "CHAN", [TCHAN] = "CHAN",
[TMAP] = "MAP", [TMAP] = "MAP",
...@@ -1008,14 +998,10 @@ Tconv(Fmt *fp) ...@@ -1008,14 +998,10 @@ Tconv(Fmt *fp)
break; break;
case TARRAY: case TARRAY:
snprint(buf1, sizeof(buf1), "[%ld]%T", t->bound, t->type); if(t->bound >= 0)
strncat(buf, buf1, sizeof(buf)); snprint(buf1, sizeof(buf1), "[%ld]%T", t->bound, t->type);
break; else
snprint(buf1, sizeof(buf1), "[]%T", t->type);
case TDARRAY:
snprint(buf1, sizeof(buf1), "[]%T", t->type);
if(t->dbound != N)
snprint(buf1, sizeof(buf1), "[<expr>]%T", t->type);
strncat(buf, buf1, sizeof(buf)); strncat(buf, buf1, sizeof(buf));
break; break;
...@@ -1216,6 +1202,24 @@ isptrto(Type *t, int et) ...@@ -1216,6 +1202,24 @@ isptrto(Type *t, int et)
return 1; return 1;
} }
int
isptrarray(Type *t)
{
if(isptrto(t, TARRAY))
if(t->type->bound >= 0)
return 1;
return 0;
}
int
isptrdarray(Type *t)
{
if(isptrto(t, TARRAY))
if(t->type->bound < 0)
return 1;
return 0;
}
int int
isinter(Type *t) isinter(Type *t)
{ {
...@@ -1324,7 +1328,6 @@ loop: ...@@ -1324,7 +1328,6 @@ loop:
case TPTR64: case TPTR64:
case TCHAN: case TCHAN:
case TARRAY: case TARRAY:
case TDARRAY:
stp = &st->type; stp = &st->type;
goto loop; goto loop;
...@@ -1395,7 +1398,6 @@ deep(Type *t) ...@@ -1395,7 +1398,6 @@ deep(Type *t)
case TPTR64: case TPTR64:
case TCHAN: case TCHAN:
case TARRAY: case TARRAY:
case TDARRAY:
nt = shallow(t); nt = shallow(t);
nt->type = deep(t->type); nt->type = deep(t->type);
break; break;
......
...@@ -430,7 +430,7 @@ loop: ...@@ -430,7 +430,7 @@ loop:
} }
// convert dynamic to static generated by ONEW // convert dynamic to static generated by ONEW
if(isptrto(t, TARRAY) && isptrto(l->type, TDARRAY)) if(isptrarray(t) && isptrdarray(l->type))
goto ret; goto ret;
// if(t->etype == TARRAY) { // if(t->etype == TARRAY) {
...@@ -560,10 +560,10 @@ loop: ...@@ -560,10 +560,10 @@ loop:
goto badt; goto badt;
case TSTRING: case TSTRING:
case TMAP: case TMAP:
case TDARRAY:
break; break;
case TARRAY: case TARRAY:
nodconst(n, types[TINT32], t->bound); if(t->bound >= 0)
nodconst(n, types[TINT32], t->bound);
break; break;
} }
n->type = types[TINT32]; n->type = types[TINT32];
...@@ -582,10 +582,9 @@ loop: ...@@ -582,10 +582,9 @@ loop:
switch(t->etype) { switch(t->etype) {
default: default:
goto badt; goto badt;
case TDARRAY:
break;
case TARRAY: case TARRAY:
nodconst(n, types[TINT32], t->bound); if(t->bound >= 0)
nodconst(n, types[TINT32], t->bound);
break; break;
} }
n->type = types[TINT32]; n->type = types[TINT32];
...@@ -652,7 +651,6 @@ loop: ...@@ -652,7 +651,6 @@ loop:
*n = *mapop(n, top); *n = *mapop(n, top);
break; break;
case TDARRAY:
case TARRAY: case TARRAY:
// right side must be an int // right side must be an int
if(n->right->type == T) { if(n->right->type == T) {
...@@ -704,7 +702,7 @@ loop: ...@@ -704,7 +702,7 @@ loop:
*n = *stringop(n, top); *n = *stringop(n, top);
goto ret; goto ret;
} }
if(t->etype == TDARRAY || t->etype == TARRAY) { if(t->etype == TARRAY) {
*n = *arrayop(n, top); *n = *arrayop(n, top);
goto ret; goto ret;
} }
...@@ -1435,8 +1433,8 @@ ascompat(Type *t1, Type *t2) ...@@ -1435,8 +1433,8 @@ ascompat(Type *t1, Type *t2)
if(isptrto(t1, TSTRUCT)) if(isptrto(t1, TSTRUCT))
return 1; return 1;
if(isptrto(t1, TDARRAY)) if(isptrdarray(t1))
if(isptrto(t2, TARRAY)) if(isptrarray(t2))
return 1; return 1;
return 0; return 0;
} }
...@@ -1542,7 +1540,6 @@ newcompat(Node *n) ...@@ -1542,7 +1540,6 @@ newcompat(Node *n)
r = chanop(n, Erv); r = chanop(n, Erv);
return r; return r;
case TDARRAY:
case TARRAY: case TARRAY:
r = arrayop(n, Erv); r = arrayop(n, Erv);
return r; return r;
...@@ -2134,7 +2131,7 @@ fixarray(Type *tm) ...@@ -2134,7 +2131,7 @@ fixarray(Type *tm)
return T; return T;
} }
if(t->etype != TDARRAY && t->etype != TARRAY) { if(t->etype != TARRAY) {
fatal("fixarray: %lT not array", tm); fatal("fixarray: %lT not array", tm);
return T; return T;
} }
...@@ -2172,15 +2169,19 @@ arrayop(Node *n, int top) ...@@ -2172,15 +2169,19 @@ arrayop(Node *n, int top)
r = a; r = a;
a = listfirst(&save, &n->left); // max a = listfirst(&save, &n->left); // max
a = listnext(&save);
if(a == N) if(a == N)
a = nodintconst(0); a = nodintconst(0);
a = nod(OCONV, a, N); a = nod(OCONV, a, N);
a->type = types[TUINT32]; a->type = types[TUINT32];
r = list(a, r); r = list(a, r);
a = t->dbound; // nel a = listfirst(&save, &n->left); // nel
if(a == N) if(a == N) {
if(t->bound < 0)
yyerror("new open array must have size");
a = nodintconst(t->bound); a = nodintconst(t->bound);
}
a = nod(OCONV, a, N); a = nod(OCONV, a, N);
a->type = types[TUINT32]; a->type = types[TUINT32];
r = list(a, r); r = list(a, r);
...@@ -2221,7 +2222,7 @@ arrayop(Node *n, int top) ...@@ -2221,7 +2222,7 @@ arrayop(Node *n, int top)
return n; return n;
case OSLICE: case OSLICE:
if(isptrto(n->left->type, TARRAY)) if(isptrarray(n->left->type))
goto slicestatic; goto slicestatic;
// arrayslices(old *[]any, lb uint32, hb uint32, width uint32) (ary *[]any) // arrayslices(old *[]any, lb uint32, hb uint32, width uint32) (ary *[]any)
...@@ -2381,7 +2382,7 @@ convas(Node *n) ...@@ -2381,7 +2382,7 @@ convas(Node *n)
return n; return n;
} }
if(isptrto(lt, TDARRAY) && isptrto(rt, TARRAY)) { if(isptrdarray(lt) && isptrarray(rt)) {
if(!eqtype(lt->type->type, rt->type->type, 0)) if(!eqtype(lt->type->type, rt->type->type, 0))
goto bad; goto bad;
*n = *arrayop(n, Etop); *n = *arrayop(n, Etop);
......
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