Commit 1d4daa2d authored by Ken Thompson's avatar Ken Thompson

foundation for import unsafe

R=r
OCL=20794
CL=20794
parent 6478df1c
...@@ -203,7 +203,7 @@ typedefs[] = ...@@ -203,7 +203,7 @@ typedefs[] =
{ {
"int", TINT, TINT32, "int", TINT, TINT32,
"uint", TUINT, TUINT32, "uint", TUINT, TUINT32,
"uptrint", TUINTPTR, TUINT64, "uintptr", TUINTPTR, TUINT64,
"float", TFLOAT, TFLOAT32, "float", TFLOAT, TFLOAT32,
}; };
......
...@@ -39,10 +39,13 @@ y.tab.h: $(YFILES) ...@@ -39,10 +39,13 @@ y.tab.h: $(YFILES)
y.tab.c: y.tab.h y.tab.c: y.tab.h
test -f y.tab.c && touch y.tab.c test -f y.tab.c && touch y.tab.c
sysimport.c: sys.go mksys.c sysimport.c: sys.go unsafe.go mksys.c
gcc -o mksys mksys.c gcc -o mksys mksys.c
6g sys.go 6g sys.go
./mksys sys.6 >_sysimport.c && mv _sysimport.c sysimport.c 6g unsafe.go
./mksys sys >_sysimport.c &&\
./mksys unsafe >>_sysimport.c &&\
mv _sysimport.c sysimport.c
clean: clean:
rm -f $(OFILES) *.6 enam.c 6.out a.out y.tab.h y.tab.c $(LIB) _sysimport.c rm -f $(OFILES) *.6 enam.c 6.out a.out y.tab.h y.tab.c $(LIB) _sysimport.c
......
...@@ -467,6 +467,7 @@ EXTERN Sym* pkgmyname; // my name for package ...@@ -467,6 +467,7 @@ EXTERN Sym* pkgmyname; // my name for package
EXTERN Sym* pkgimportname; // package name from imported package EXTERN Sym* pkgimportname; // package name from imported package
EXTERN int tptr; // either TPTR32 or TPTR64 EXTERN int tptr; // either TPTR32 or TPTR64
extern char* sysimport; extern char* sysimport;
extern char* unsafeimport;
EXTERN char* filename; // name to uniqify names EXTERN char* filename; // name to uniqify names
EXTERN void (*dcladj)(Sym*); // declaration is being exported/packaged EXTERN void (*dcladj)(Sym*); // declaration is being exported/packaged
...@@ -535,7 +536,7 @@ int yyparse(void); ...@@ -535,7 +536,7 @@ int yyparse(void);
int mainlex(int, char*[]); int mainlex(int, char*[]);
void setfilename(char*); void setfilename(char*);
void importfile(Val*); void importfile(Val*);
void cannedimports(void); void cannedimports(char*, char*);
void unimportfile(); void unimportfile();
int32 yylex(void); int32 yylex(void);
void lexinit(void); void lexinit(void);
......
...@@ -104,12 +104,12 @@ package: ...@@ -104,12 +104,12 @@ package:
{ {
yyerror("package statement must be first"); yyerror("package statement must be first");
mkpackage("main"); mkpackage("main");
cannedimports(); cannedimports("sys.6", sysimport);
} }
| LPACKAGE sym | LPACKAGE sym
{ {
mkpackage($2->name); mkpackage($2->name);
cannedimports(); cannedimports("sys.6", sysimport);
} }
imports: imports:
...@@ -1086,6 +1086,8 @@ Bnon_fn_type: ...@@ -1086,6 +1086,8 @@ Bnon_fn_type:
nametype: nametype:
LATYPE LATYPE
{ {
if($1->otype != T && $1->otype->etype == TANY)
yyerror("the any type is restricted");
$$ = oldtype($1); $$ = oldtype($1);
} }
......
...@@ -218,6 +218,11 @@ importfile(Val *f) ...@@ -218,6 +218,11 @@ importfile(Val *f)
return; return;
} }
if(strcmp(f->u.sval->s, "unsafe") == 0) {
cannedimports("unsafe.6", unsafeimport);
return;
}
if(!findpkg(f->u.sval)) if(!findpkg(f->u.sval))
fatal("can't find import: %Z", f->u.sval); fatal("can't find import: %Z", f->u.sval);
imp = Bopen(namebuf, OREAD); imp = Bopen(namebuf, OREAD);
...@@ -277,11 +282,8 @@ unimportfile(void) ...@@ -277,11 +282,8 @@ unimportfile(void)
} }
void void
cannedimports(void) cannedimports(char *file, char *cp)
{ {
char *file;
file = "sys.6";
lineno++; // if sys.6 is included on line 1, lineno++; // if sys.6 is included on line 1,
linehist(file, 0); // the debugger gets confused linehist(file, 0); // the debugger gets confused
...@@ -290,7 +292,7 @@ cannedimports(void) ...@@ -290,7 +292,7 @@ cannedimports(void)
curio.peekc = 0; curio.peekc = 0;
curio.peekc1 = 0; curio.peekc1 = 0;
curio.infile = file; curio.infile = file;
curio.cp = sysimport; curio.cp = cp;
pkgmyname = S; pkgmyname = S;
inimportsys = 1; inimportsys = 1;
......
...@@ -13,15 +13,22 @@ ...@@ -13,15 +13,22 @@
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
char *name;
FILE *fin; FILE *fin;
char buf[1024], *p, *q; char buf[1024], initfunc[1024], *p, *q;
if(argc != 2) { if(argc != 2) {
fprintf(stderr, "usage: mksys sys.6\n"); fprintf(stderr, "usage: sys sys\n");
fprintf(stderr, "in file $1.6 s/PACKAGE/$1/\n");
exit(1); exit(1);
} }
if((fin = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "open %s: %s\n", argv[1], strerror(errno)); name = argv[1];
snprintf(initfunc, sizeof(initfunc), "init_%s_function", name);
snprintf(buf, sizeof(buf), "%s.6", name);
if((fin = fopen(buf, "r")) == NULL) {
fprintf(stderr, "open %s: %s\n", buf, strerror(errno));
exit(1); exit(1);
} }
...@@ -33,7 +40,7 @@ main(int argc, char **argv) ...@@ -33,7 +40,7 @@ main(int argc, char **argv)
exit(1); exit(1);
begin: begin:
printf("char *sysimport = \n"); printf("char *%simport = \n", name);
// process imports, stopping at $$ that closes them // process imports, stopping at $$ that closes them
while(fgets(buf, sizeof buf, fin) != NULL) { while(fgets(buf, sizeof buf, fin) != NULL) {
...@@ -45,17 +52,21 @@ begin: ...@@ -45,17 +52,21 @@ begin:
for(p=buf; *p==' ' || *p == '\t'; p++) for(p=buf; *p==' ' || *p == '\t'; p++)
; ;
// cut out decl of init_sys_function - it doesn't exist // cut out decl of init_$1_function - it doesn't exist
if(strstr(buf, "init_sys_function")) if(strstr(buf, initfunc))
continue; continue;
// sys.go claims to be in package SYS to avoid // sys.go claims to be in package PACKAGE to avoid
// conflicts during "6g sys.go". rename SYS to sys. // conflicts during "6g sys.go". rename PACKAGE to $2.
for(q=p; *q; q++) printf("\t\"");
if(memcmp(q, "SYS", 3) == 0) while(q = strstr(p, "PACKAGE")) {
memmove(q, "sys", 3); *q = 0;
printf("%s", p); // up to the substitution
printf("%s", name); // the sub name
p = q+7; // continue with rest
}
printf("\t\"%s\\n\"\n", p); printf("%s\\n\"\n", p);
} }
fprintf(stderr, "did not find end of imports\n"); fprintf(stderr, "did not find end of imports\n");
exit(1); exit(1);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package SYS // rename to avoid redeclaration errors package PACKAGE
export func mal(int32) *any; export func mal(int32) *any;
export func breakpoint(); export func breakpoint();
......
...@@ -79,3 +79,8 @@ char *sysimport = ...@@ -79,3 +79,8 @@ char *sysimport =
"export func sys.semrelease (sema *int32)\n" "export func sys.semrelease (sema *int32)\n"
"\n" "\n"
"$$\n"; "$$\n";
char *unsafeimport =
"package unsafe\n"
"export type unsafe.pointer *any\n"
"\n"
"$$\n";
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package PACKAGE
export type pointer *any;
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