Commit 88b5c5f0 authored by Ken Thompson's avatar Ken Thompson

make for slice/map/chan

new for pointers

R=r
OCL=22158
CL=22158
parent 1da03aae
...@@ -292,7 +292,7 @@ enum ...@@ -292,7 +292,7 @@ enum
OLIST, OCMP, OPTR, OARRAY, ORANGE, OLIST, OCMP, OPTR, OARRAY, ORANGE,
ORETURN, OFOR, OIF, OSWITCH, ORETURN, OFOR, OIF, OSWITCH,
OAS, OASOP, OCASE, OXCASE, OFALL, OXFALL, OAS, OASOP, OCASE, OXCASE, OFALL, OXFALL,
OGOTO, OPROC, ONEW, OEMPTY, OSELECT, OGOTO, OPROC, OMAKE, ONEW, OEMPTY, OSELECT,
OLEN, OCAP, OPANIC, OPANICN, OPRINT, OPRINTN, OTYPEOF, OLEN, OCAP, OPANIC, OPANICN, OPRINT, OPRINTN, OTYPEOF,
OOROR, OOROR,
...@@ -789,6 +789,7 @@ int ascompat(Type*, Type*); ...@@ -789,6 +789,7 @@ int ascompat(Type*, Type*);
Node* prcompat(Node*, int); Node* prcompat(Node*, int);
Node* nodpanic(int32); Node* nodpanic(int32);
Node* newcompat(Node*); Node* newcompat(Node*);
Node* makecompat(Node*);
Node* stringop(Node*, int); Node* stringop(Node*, int);
Type* fixmap(Type*); Type* fixmap(Type*);
Node* mapop(Node*, int); Node* mapop(Node*, int);
......
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
%token <sym> LPACKAGE LIMPORT LEXPORT %token <sym> LPACKAGE LIMPORT LEXPORT
%token <sym> LMAP LCHAN LINTERFACE LFUNC LSTRUCT %token <sym> LMAP LCHAN LINTERFACE LFUNC LSTRUCT
%token <sym> LCOLAS LFALL LRETURN LDDD %token <sym> LCOLAS LFALL LRETURN LDDD
%token <sym> LNEW LLEN LCAP LTYPEOF LPANIC LPANICN LPRINT LPRINTN %token <sym> LLEN LCAP LTYPEOF LPANIC LPANICN LPRINT LPRINTN
%token <sym> LVAR LTYPE LCONST LCONVERT LSELECT %token <sym> LVAR LTYPE LCONST LCONVERT LSELECT LMAKE LNEW
%token <sym> LFOR LIF LELSE LSWITCH LCASE LDEFAULT %token <sym> LFOR LIF LELSE LSWITCH LCASE LDEFAULT
%token <sym> LBREAK LCONTINUE LGO LGOTO LRANGE %token <sym> LBREAK LCONTINUE LGO LGOTO LRANGE
%token <sym> LNIL LTRUE LFALSE LIOTA %token <sym> LNIL LTRUE LFALSE LIOTA
...@@ -864,6 +864,16 @@ pexpr: ...@@ -864,6 +864,16 @@ pexpr:
$$ = nod(ONEW, $5, N); $$ = nod(ONEW, $5, N);
$$->type = $3; $$->type = $3;
} }
| LMAKE '(' type ')'
{
$$ = nod(OMAKE, N, N);
$$->type = $3;
}
| LMAKE '(' type ',' expr_list ')'
{
$$ = nod(OMAKE, $5, N);
$$->type = $3;
}
| latype '(' expr ')' | latype '(' expr ')'
{ {
$$ = nod(OCONV, $3, N); $$ = nod(OCONV, $3, N);
...@@ -980,6 +990,7 @@ sym3: ...@@ -980,6 +990,7 @@ sym3:
| LPRINT | LPRINT
| LPRINTN | LPRINTN
| LNEW | LNEW
| LMAKE
| LBASETYPE | LBASETYPE
| LTYPEOF | LTYPEOF
......
...@@ -1064,6 +1064,7 @@ static struct ...@@ -1064,6 +1064,7 @@ static struct
"import", LIMPORT, Txxx, "import", LIMPORT, Txxx,
"interface", LINTERFACE, Txxx, "interface", LINTERFACE, Txxx,
"iota", LIOTA, Txxx, "iota", LIOTA, Txxx,
"make", LMAKE, Txxx,
"map", LMAP, Txxx, "map", LMAP, Txxx,
"new", LNEW, Txxx, "new", LNEW, Txxx,
"len", LLEN, Txxx, "len", LLEN, Txxx,
......
...@@ -708,6 +708,7 @@ opnames[] = ...@@ -708,6 +708,7 @@ opnames[] =
[OVAR] = "VAR", [OVAR] = "VAR",
[OIMPORT] = "IMPORT", [OIMPORT] = "IMPORT",
[OXOR] = "XOR", [OXOR] = "XOR",
[OMAKE] = "MAKE",
[ONEW] = "NEW", [ONEW] = "NEW",
[OFALL] = "FALL", [OFALL] = "FALL",
[OXFALL] = "XFALL", [OXFALL] = "XFALL",
......
...@@ -590,7 +590,7 @@ loop: ...@@ -590,7 +590,7 @@ loop:
} }
} }
// convert dynamic to static generated by ONEW // convert dynamic to static generated by ONEW/OMAKE
if(issarray(t) && isdarray(l->type)) if(issarray(t) && isdarray(l->type))
goto ret; goto ret;
...@@ -979,7 +979,7 @@ loop: ...@@ -979,7 +979,7 @@ loop:
nvar = nod(0, N, N); nvar = nod(0, N, N);
tempname(nvar, t); tempname(nvar, t);
nnew = nod(ONEW, N, N); nnew = nod(OMAKE, N, N);
nnew->type = t; nnew->type = t;
nnew = newcompat(nnew); nnew = newcompat(nnew);
...@@ -1017,6 +1017,12 @@ loop: ...@@ -1017,6 +1017,12 @@ loop:
n->type = t->type; n->type = t->type;
goto ret; goto ret;
case OMAKE:
if(top != Erv)
goto nottop;
indir(n, makecompat(n));
goto ret;
case ONEW: case ONEW:
if(top != Erv) if(top != Erv)
goto nottop; goto nottop;
...@@ -2014,7 +2020,7 @@ nodpanic(int32 lineno) ...@@ -2014,7 +2020,7 @@ nodpanic(int32 lineno)
} }
Node* Node*
newcompat(Node *n) makecompat(Node *n)
{ {
Node *r, *on; Node *r, *on;
Type *t, *t0; Type *t, *t0;
...@@ -2026,21 +2032,24 @@ newcompat(Node *n) ...@@ -2026,21 +2032,24 @@ newcompat(Node *n)
if(t0->etype == TARRAY) if(t0->etype == TARRAY)
return arrayop(n, Erv); return arrayop(n, Erv);
if(!isptr[t0->etype] || t0->type == T) if(!isptr[t0->etype])
goto bad; goto bad;
t = t0->type; t = t0->type;
if(t == T)
goto bad;
switch(t->etype) { switch(t->etype) {
case TSTRING: case TSTRING:
goto bad; goto bad;
// the call looks like new(map[int]int) // the call looks like new(MAP[int]int)
// but internally we see new(*MAP[int]int) // but internally we see new(*MAP[int]int)
case TMAP: case TMAP:
r = mapop(n, Erv); r = mapop(n, Erv);
break; break;
// the call looks like new(chan int) // the call looks like new(CHAN int)
// but internally we see new(*CHAN int) // but internally we see new(*CHAN int)
case TCHAN: case TCHAN:
r = chanop(n, Erv); r = chanop(n, Erv);
...@@ -2048,7 +2057,42 @@ newcompat(Node *n) ...@@ -2048,7 +2057,42 @@ newcompat(Node *n)
default: default:
if(n->left != N) if(n->left != N)
yyerror("cannot new(%T, expr)", t0); yyerror("cannot make(%T, expr)", t0);
dowidth(t);
on = syslook("mal", 1);
argtype(on, t);
r = nodintconst(t->width);
r = nod(OCALL, on, r);
walktype(r, Erv);
break;
}
return r;
bad:
yyerror("cannot make(%T)", t0);
return n;
}
Node*
newcompat(Node *n)
{
Node *r, *on;
Type *t, *t0;
t = n->type;
if(t == T)
goto bad;
switch(t->etype) {
case TSTRING:
case TMAP:
case TCHAN:
goto bad;
default:
if(n->left != N)
yyerror("cannot new(%T, expr)", t);
dowidth(t); dowidth(t);
on = syslook("mal", 1); on = syslook("mal", 1);
argtype(on, t); argtype(on, t);
...@@ -2061,7 +2105,7 @@ newcompat(Node *n) ...@@ -2061,7 +2105,7 @@ newcompat(Node *n)
return r; return r;
bad: bad:
yyerror("cannot new(%T)", t0); yyerror("cannot new(%T)", t);
return n; return n;
} }
...@@ -2224,7 +2268,7 @@ mapop(Node *n, int top) ...@@ -2224,7 +2268,7 @@ mapop(Node *n, int top)
default: default:
fatal("mapop: unknown op %O", n->op); fatal("mapop: unknown op %O", n->op);
case ONEW: case OMAKE:
if(top != Erv) if(top != Erv)
goto nottop; goto nottop;
...@@ -2436,7 +2480,7 @@ chanop(Node *n, int top) ...@@ -2436,7 +2480,7 @@ chanop(Node *n, int top)
default: default:
fatal("chanop: unknown op %O", n->op); fatal("chanop: unknown op %O", n->op);
case ONEW: case OMAKE:
// newchan(elemsize int, elemalg int, // newchan(elemsize int, elemalg int,
// hint int) (hmap *chan[any-1]); // hint int) (hmap *chan[any-1]);
...@@ -2642,7 +2686,7 @@ arrayop(Node *n, int top) ...@@ -2642,7 +2686,7 @@ arrayop(Node *n, int top)
n->right = r; n->right = r;
return n; return n;
case ONEW: case OMAKE:
// newarray(nel int, max int, width int) (ary []any) // newarray(nel int, max int, width int) (ary []any)
t = fixarray(n->type); t = fixarray(n->type);
if(t == T) if(t == T)
...@@ -3523,7 +3567,7 @@ arraylit(Node *n) ...@@ -3523,7 +3567,7 @@ arraylit(Node *n)
var = nod(OXXX, N, N); var = nod(OXXX, N, N);
tempname(var, t); tempname(var, t);
nnew = nod(ONEW, N, N); nnew = nod(OMAKE, N, N);
nnew->type = t; nnew->type = t;
nas = nod(OAS, var, nnew); nas = nod(OAS, var, nnew);
...@@ -3560,7 +3604,7 @@ maplit(Node *n) ...@@ -3560,7 +3604,7 @@ maplit(Node *n)
var = nod(OXXX, N, N); var = nod(OXXX, N, N);
tempname(var, t); tempname(var, t);
a = nod(ONEW, N, N); a = nod(OMAKE, N, N);
a->type = t; a->type = t;
a = nod(OAS, var, a); a = nod(OAS, var, a);
addtop = list(addtop, a); addtop = list(addtop, a);
......
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