diff options
author | Rob Pike <r@golang.org> | 2008-06-26 14:09:26 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2008-06-26 14:09:26 -0700 |
commit | c3f6369528a29406434aff2f9c611f2c23f988f9 (patch) | |
tree | e446a18d52b4311b7fabada1de0e78b8e72fd773 | |
parent | 619af0f48a6c2bc671b3bc86e2f6e6b94e1f7b9d (diff) | |
download | golang-c3f6369528a29406434aff2f9c611f2c23f988f9.tar.gz |
add sys.readfile()
add args to linux runtime
SVN=124961
-rw-r--r-- | src/cmd/gc/sys.go | 5 | ||||
-rw-r--r-- | src/cmd/gc/sysimport.c | 155 | ||||
-rw-r--r-- | src/runtime/Makefile | 3 | ||||
-rw-r--r-- | src/runtime/rt0_amd64_darwin.s | 32 | ||||
-rw-r--r-- | src/runtime/rt0_amd64_linux.s | 39 | ||||
-rw-r--r-- | src/runtime/runtime.h | 5 | ||||
-rw-r--r-- | src/runtime/sys_file.c | 80 |
7 files changed, 238 insertions, 81 deletions
diff --git a/src/cmd/gc/sys.go b/src/cmd/gc/sys.go index 613a0dcbc..4ea54b88d 100644 --- a/src/cmd/gc/sys.go +++ b/src/cmd/gc/sys.go @@ -40,6 +40,8 @@ func mapaccess2(hmap *map[any]any, key any) (val any, pres bool); func mapassign1(hmap *map[any]any, key any, val any); func mapassign2(hmap *map[any]any, key any, val any, pres bool); +func readfile(string) (string, bool); // read file into string; boolean status + export mal breakpoint @@ -79,4 +81,7 @@ export mapassign1 mapassign2 + // files + readfile + ; diff --git a/src/cmd/gc/sysimport.c b/src/cmd/gc/sysimport.c index 501708337..284da748e 100644 --- a/src/cmd/gc/sysimport.c +++ b/src/cmd/gc/sysimport.c @@ -3,10 +3,10 @@ char* sysimport = "type sys._e002 {}\n" "type sys.any 24\n" "type sys._e003 *sys.any\n" - "type sys._o181 {_e179 sys._e003}\n" + "type sys._o207 {_e205 sys._e003}\n" "type sys.uint32 6\n" - "type sys._i183 {_e180 sys.uint32}\n" - "type sys._e001 (sys._e002 sys._o181 sys._i183)\n" + "type sys._i209 {_e206 sys.uint32}\n" + "type sys._e001 (sys._e002 sys._o207 sys._i209)\n" "var !sys.mal sys._e001\n" "type sys._e005 {}\n" "type sys._e006 {}\n" @@ -16,151 +16,156 @@ char* sysimport = "type sys._e009 {}\n" "type sys._e010 {}\n" "type sys.int32 5\n" - "type sys._i189 {_e188 sys.int32}\n" - "type sys._e008 (sys._e009 sys._e010 sys._i189)\n" + "type sys._i215 {_e214 sys.int32}\n" + "type sys._e008 (sys._e009 sys._e010 sys._i215)\n" "var !sys.panicl sys._e008\n" "type sys._e012 {}\n" "type sys._e013 {}\n" "type sys.bool 12\n" - "type sys._i194 {_e193 sys.bool}\n" - "type sys._e011 (sys._e012 sys._e013 sys._i194)\n" + "type sys._i220 {_e219 sys.bool}\n" + "type sys._e011 (sys._e012 sys._e013 sys._i220)\n" "var !sys.printbool sys._e011\n" "type sys._e015 {}\n" "type sys._e016 {}\n" "type sys.float64 10\n" - "type sys._i199 {_e198 sys.float64}\n" - "type sys._e014 (sys._e015 sys._e016 sys._i199)\n" + "type sys._i225 {_e224 sys.float64}\n" + "type sys._e014 (sys._e015 sys._e016 sys._i225)\n" "var !sys.printfloat sys._e014\n" "type sys._e018 {}\n" "type sys._e019 {}\n" "type sys.int64 7\n" - "type sys._i204 {_e203 sys.int64}\n" - "type sys._e017 (sys._e018 sys._e019 sys._i204)\n" + "type sys._i230 {_e229 sys.int64}\n" + "type sys._e017 (sys._e018 sys._e019 sys._i230)\n" "var !sys.printint sys._e017\n" "type sys._e021 {}\n" "type sys._e022 {}\n" "type sys._e023 25\n" "type sys.string *sys._e023\n" - "type sys._i209 {_e208 sys.string}\n" - "type sys._e020 (sys._e021 sys._e022 sys._i209)\n" + "type sys._i235 {_e234 sys.string}\n" + "type sys._e020 (sys._e021 sys._e022 sys._i235)\n" "var !sys.printstring sys._e020\n" "type sys._e025 {}\n" "type sys._e026 {}\n" "type sys.uint8 2\n" "type sys._e027 *sys.uint8\n" - "type sys._i214 {_e213 sys._e027}\n" - "type sys._e024 (sys._e025 sys._e026 sys._i214)\n" + "type sys._i240 {_e239 sys._e027}\n" + "type sys._e024 (sys._e025 sys._e026 sys._i240)\n" "var !sys.printpointer sys._e024\n" "type sys._e029 {}\n" - "type sys._o221 {_e218 sys.string}\n" - "type sys._i223 {_e219 sys.string _e220 sys.string}\n" - "type sys._e028 (sys._e029 sys._o221 sys._i223)\n" + "type sys._o247 {_e244 sys.string}\n" + "type sys._i249 {_e245 sys.string _e246 sys.string}\n" + "type sys._e028 (sys._e029 sys._o247 sys._i249)\n" "var !sys.catstring sys._e028\n" "type sys._e031 {}\n" - "type sys._o231 {_e228 sys.int32}\n" - "type sys._i233 {_e229 sys.string _e230 sys.string}\n" - "type sys._e030 (sys._e031 sys._o231 sys._i233)\n" + "type sys._o257 {_e254 sys.int32}\n" + "type sys._i259 {_e255 sys.string _e256 sys.string}\n" + "type sys._e030 (sys._e031 sys._o257 sys._i259)\n" "var !sys.cmpstring sys._e030\n" "type sys._e033 {}\n" - "type sys._o242 {_e238 sys.string}\n" - "type sys._i244 {_e239 sys.string _e240 sys.int32 _e241 sys.int32}\n" - "type sys._e032 (sys._e033 sys._o242 sys._i244)\n" + "type sys._o268 {_e264 sys.string}\n" + "type sys._i270 {_e265 sys.string _e266 sys.int32 _e267 sys.int32}\n" + "type sys._e032 (sys._e033 sys._o268 sys._i270)\n" "var !sys.slicestring sys._e032\n" "type sys._e035 {}\n" - "type sys._o253 {_e250 sys.uint8}\n" - "type sys._i255 {_e251 sys.string _e252 sys.int32}\n" - "type sys._e034 (sys._e035 sys._o253 sys._i255)\n" + "type sys._o279 {_e276 sys.uint8}\n" + "type sys._i281 {_e277 sys.string _e278 sys.int32}\n" + "type sys._e034 (sys._e035 sys._o279 sys._i281)\n" "var !sys.indexstring sys._e034\n" "type sys._e037 {}\n" - "type sys._o262 {_e260 sys.string}\n" - "type sys._i264 {_e261 sys.int64}\n" - "type sys._e036 (sys._e037 sys._o262 sys._i264)\n" + "type sys._o288 {_e286 sys.string}\n" + "type sys._i290 {_e287 sys.int64}\n" + "type sys._e036 (sys._e037 sys._o288 sys._i290)\n" "var !sys.intstring sys._e036\n" "type sys._e039 {}\n" - "type sys._o271 {_e268 sys.string}\n" + "type sys._o297 {_e294 sys.string}\n" "type sys._e040 *sys.uint8\n" - "type sys._i273 {_e269 sys._e040 _e270 sys.int32}\n" - "type sys._e038 (sys._e039 sys._o271 sys._i273)\n" + "type sys._i299 {_e295 sys._e040 _e296 sys.int32}\n" + "type sys._e038 (sys._e039 sys._o297 sys._i299)\n" "var !sys.byteastring sys._e038\n" "type sys._e042 {}\n" "type sys._e043 <>\n" - "type sys._o282 {_e278 sys._e043}\n" + "type sys._o308 {_e304 sys._e043}\n" "type sys._e044 *sys.uint8\n" "type sys._e045 *sys.uint8\n" - "type sys._s289 {}\n" - "type sys._e046 *sys._s289\n" - "type sys._i284 {_e279 sys._e044 _e280 sys._e045 _e281 sys._e046}\n" - "type sys._e041 (sys._e042 sys._o282 sys._i284)\n" + "type sys._s315 {}\n" + "type sys._e046 *sys._s315\n" + "type sys._i310 {_e305 sys._e044 _e306 sys._e045 _e307 sys._e046}\n" + "type sys._e041 (sys._e042 sys._o308 sys._i310)\n" "var !sys.mkiface sys._e041\n" "type sys._e048 {}\n" - "type sys._o293 {_e292 sys.int32}\n" + "type sys._o319 {_e318 sys.int32}\n" "type sys._e049 {}\n" - "type sys._e047 (sys._e048 sys._o293 sys._e049)\n" + "type sys._e047 (sys._e048 sys._o319 sys._e049)\n" "var !sys.argc sys._e047\n" "type sys._e051 {}\n" - "type sys._o298 {_e296 sys.string}\n" - "type sys._i300 {_e297 sys.int32}\n" - "type sys._e050 (sys._e051 sys._o298 sys._i300)\n" - "var !sys.argv sys._e050\n" - "type sys._e053 {}\n" - "type sys._o306 {_e304 sys.string}\n" - "type sys._i308 {_e305 sys.int32}\n" - "type sys._e052 (sys._e053 sys._o306 sys._i308)\n" - "var !sys.envv sys._e052\n" - "type sys._e055 {}\n" - "type sys._o313 {_e312 sys.int32}\n" + "type sys._o323 {_e322 sys.int32}\n" + "type sys._e052 {}\n" + "type sys._e050 (sys._e051 sys._o323 sys._e052)\n" + "var !sys.envc sys._e050\n" + "type sys._e054 {}\n" + "type sys._o328 {_e326 sys.string}\n" + "type sys._i330 {_e327 sys.int32}\n" + "type sys._e053 (sys._e054 sys._o328 sys._i330)\n" + "var !sys.argv sys._e053\n" "type sys._e056 {}\n" - "type sys._e054 (sys._e055 sys._o313 sys._e056)\n" - "var !sys.envc sys._e054\n" + "type sys._o336 {_e334 sys.string}\n" + "type sys._i338 {_e335 sys.int32}\n" + "type sys._e055 (sys._e056 sys._o336 sys._i338)\n" + "var !sys.envv sys._e055\n" "type sys._e058 {}\n" - "type sys._o319 {_e316 sys.int32 _e317 sys.float64}\n" - "type sys._i321 {_e318 sys.float64}\n" - "type sys._e057 (sys._e058 sys._o319 sys._i321)\n" + "type sys._o345 {_e342 sys.int32 _e343 sys.float64}\n" + "type sys._i347 {_e344 sys.float64}\n" + "type sys._e057 (sys._e058 sys._o345 sys._i347)\n" "var !sys.frexp sys._e057\n" "type sys._e060 {}\n" - "type sys._o328 {_e325 sys.float64}\n" - "type sys._i330 {_e326 sys.int32 _e327 sys.float64}\n" - "type sys._e059 (sys._e060 sys._o328 sys._i330)\n" + "type sys._o354 {_e351 sys.float64}\n" + "type sys._i356 {_e352 sys.int32 _e353 sys.float64}\n" + "type sys._e059 (sys._e060 sys._o354 sys._i356)\n" "var !sys.ldexp sys._e059\n" "type sys._e062 {}\n" - "type sys._o338 {_e335 sys.float64 _e336 sys.float64}\n" - "type sys._i340 {_e337 sys.float64}\n" - "type sys._e061 (sys._e062 sys._o338 sys._i340)\n" + "type sys._o364 {_e361 sys.float64 _e362 sys.float64}\n" + "type sys._i366 {_e363 sys.float64}\n" + "type sys._e061 (sys._e062 sys._o364 sys._i366)\n" "var !sys.modf sys._e061\n" "type sys._e064 {}\n" "type sys._e066 [sys.any] sys.any\n" "type sys._e065 *sys._e066\n" - "type sys._o344 {hmap sys._e065}\n" - "type sys._i346 {keysize sys.uint32 valsize sys.uint32 keyalg sys.uint32 valalg sys.uint32 hint sys.uint32}\n" - "type sys._e063 (sys._e064 sys._o344 sys._i346)\n" + "type sys._o370 {hmap sys._e065}\n" + "type sys._i372 {keysize sys.uint32 valsize sys.uint32 keyalg sys.uint32 valalg sys.uint32 hint sys.uint32}\n" + "type sys._e063 (sys._e064 sys._o370 sys._i372)\n" "var !sys.newmap sys._e063\n" "type sys._e068 {}\n" - "type sys._o354 {val sys.any}\n" + "type sys._o380 {val sys.any}\n" "type sys._e070 [sys.any] sys.any\n" "type sys._e069 *sys._e070\n" - "type sys._i356 {hmap sys._e069 key sys.any}\n" - "type sys._e067 (sys._e068 sys._o354 sys._i356)\n" + "type sys._i382 {hmap sys._e069 key sys.any}\n" + "type sys._e067 (sys._e068 sys._o380 sys._i382)\n" "var !sys.mapaccess1 sys._e067\n" "type sys._e072 {}\n" - "type sys._o361 {val sys.any pres sys.bool}\n" + "type sys._o387 {val sys.any pres sys.bool}\n" "type sys._e074 [sys.any] sys.any\n" "type sys._e073 *sys._e074\n" - "type sys._i363 {hmap sys._e073 key sys.any}\n" - "type sys._e071 (sys._e072 sys._o361 sys._i363)\n" + "type sys._i389 {hmap sys._e073 key sys.any}\n" + "type sys._e071 (sys._e072 sys._o387 sys._i389)\n" "var !sys.mapaccess2 sys._e071\n" "type sys._e076 {}\n" "type sys._e077 {}\n" "type sys._e079 [sys.any] sys.any\n" "type sys._e078 *sys._e079\n" - "type sys._i368 {hmap sys._e078 key sys.any val sys.any}\n" - "type sys._e075 (sys._e076 sys._e077 sys._i368)\n" + "type sys._i394 {hmap sys._e078 key sys.any val sys.any}\n" + "type sys._e075 (sys._e076 sys._e077 sys._i394)\n" "var !sys.mapassign1 sys._e075\n" "type sys._e081 {}\n" "type sys._e082 {}\n" "type sys._e084 [sys.any] sys.any\n" "type sys._e083 *sys._e084\n" - "type sys._i374 {hmap sys._e083 key sys.any val sys.any pres sys.bool}\n" - "type sys._e080 (sys._e081 sys._e082 sys._i374)\n" + "type sys._i400 {hmap sys._e083 key sys.any val sys.any pres sys.bool}\n" + "type sys._e080 (sys._e081 sys._e082 sys._i400)\n" "var !sys.mapassign2 sys._e080\n" + "type sys._e086 {}\n" + "type sys._o410 {_e407 sys.string _e408 sys.bool}\n" + "type sys._i412 {_e409 sys.string}\n" + "type sys._e085 (sys._e086 sys._o410 sys._i412)\n" + "var !sys.readfile sys._e085\n" "))\n" ; diff --git a/src/runtime/Makefile b/src/runtime/Makefile index 11e39cc6c..cc1e8c2ea 100644 --- a/src/runtime/Makefile +++ b/src/runtime/Makefile @@ -16,9 +16,10 @@ LIBOFILES=\ rt1_$(GOARCH)_$(GOOS).$O\ rt2_$(GOARCH).$O\ runtime.$O\ + sys_file.$O\ OFILES=$(RT0OFILES) $(LIBOFILES) -HFILES= +HFILES=runtime.h install: rt0 $(LIB) cp $(RT0OFILES) $(GOROOT)/lib diff --git a/src/runtime/rt0_amd64_darwin.s b/src/runtime/rt0_amd64_darwin.s index 031cc059d..ad2e70843 100644 --- a/src/runtime/rt0_amd64_darwin.s +++ b/src/runtime/rt0_amd64_darwin.s @@ -7,8 +7,8 @@ TEXT _rt0_amd64_darwin(SB),1,$-8 PUSHQ $0 MOVQ SP, BP ANDQ $~15, SP - MOVQ 8(BP), DI - LEAQ 16(BP), SI + MOVQ 8(BP), DI // argc + LEAQ 16(BP), SI // argv MOVL DI, DX ADDL $1, DX SHLL $3, DX @@ -57,6 +57,34 @@ TEXT sys·write(SB),1,$-8 CALL notok(SB) RET +TEXT open(SB),1,$-8 + MOVQ 8(SP), DI + MOVL 16(SP), SI + MOVL $5, AX // syscall entry + SYSCALL + RET + +TEXT close(SB),1,$-8 + MOVL 8(SP), DI + MOVL $6, AX // syscall entry + SYSCALL + RET + +TEXT fstat(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL $189, AX // syscall entry + SYSCALL + RET + +TEXT read(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL 24(SP), DX + MOVL $3, AX // syscall entry + SYSCALL + RET + TEXT sys·sigaction(SB),1,$-8 MOVL 8(SP), DI // arg 1 sig MOVQ 16(SP), SI // arg 2 act diff --git a/src/runtime/rt0_amd64_linux.s b/src/runtime/rt0_amd64_linux.s index 30f72728a..d89dc927e 100644 --- a/src/runtime/rt0_amd64_linux.s +++ b/src/runtime/rt0_amd64_linux.s @@ -7,8 +7,8 @@ TEXT _rt0_amd64_linux(SB),1,$-8 PUSHQ $0 MOVQ SP, BP ANDQ $~15, SP - MOVQ 8(BP), DI - LEAQ 16(BP), SI + MOVQ 8(BP), DI // argc + LEAQ 16(BP), SI // argv MOVL DI, DX ADDL $1, DX SHLL $3, DX @@ -24,6 +24,11 @@ loop: done: ADDQ $8, CX + SUBQ $16, SP + MOVL DI, 0(SP) + MOVQ SI, 8(SP) + CALL args(SB) + ADDQ $16, SP CALL check(SB) CALL main·main(SB) CALL sys·exit(SB) @@ -52,6 +57,34 @@ TEXT sys·write(SB),1,$-8 CALL notok(SB) RET +TEXT open(SB),1,$-8 + MOVQ 8(SP), DI + MOVL 16(SP), SI + MOVL $2, AX // syscall entry + SYSCALL + RET + +TEXT close(SB),1,$-8 + MOVL 8(SP), DI + MOVL $3, AX // syscall entry + SYSCALL + RET + +TEXT fstat(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL $5, AX // syscall entry + SYSCALL + RET + +TEXT read(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL 24(SP), DX + MOVL $0, AX // syscall entry + SYSCALL + RET + TEXT sys·rt_sigaction(SB),1,$-8 MOVL 8(SP), DI MOVQ 16(SP), SI @@ -94,7 +127,7 @@ TEXT sys·mmap(SB),1,$-8 MOVL $9, AX // syscall entry SYSCALL CMPQ AX, $0xfffffffffffff001 - JNE 2(PC) + JLS 2(PC) CALL notok(SB) RET diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 4d185a5dc..c645992d8 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -90,6 +90,10 @@ void* mal(uint32); uint32 cmpstring(string, string); void initsig(void); void traceback(uint8 *pc, uint8 *sp); +int32 open(byte*, int32); +int32 read(int32, void*, int32); +void close(int32); +int32 fstat(int32, void*); struct SigTab { int32 catch; @@ -124,3 +128,4 @@ void sys·intstring(int64, string); void sys·ifaces2i(Sigi*, Sigs*, Map*, void*); void sys·ifacei2i(Sigi*, Map*, void*); void sys·ifacei2s(Sigs*, Map*, void*); +void sys·readfile(string, string, bool); diff --git a/src/runtime/sys_file.c b/src/runtime/sys_file.c new file mode 100644 index 000000000..80dec3da8 --- /dev/null +++ b/src/runtime/sys_file.c @@ -0,0 +1,80 @@ +// 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. + +#include "runtime.h" + +typedef uint64 dev_t; +typedef uint64 ino_t; +typedef uint32 mode_t; +typedef uint64 nlink_t; +typedef uint32 uid_t; +typedef uint32 gid_t; +typedef int64 off_t; +typedef int64 blksize_t; +typedef int64 blkcnt_t; +typedef int64 time_t; + +struct timespec { + time_t tv_sec; + int64 tv_nsec; +}; + +struct stat { + dev_t st_dev; /* ID of device containing file */ + ino_t st_ino; /* inode number */ + nlink_t st_nlink; /* number of hard links */ + mode_t st_mode; /* protection */ + uid_t st_uid; /* user ID of owner */ + gid_t st_gid; /* group ID of owner */ + int32 pad0; + dev_t st_rdev; /* device ID (if special file) */ + off_t st_size; /* total size, in bytes */ + blksize_t st_blksize; /* blocksize for filesystem I/O */ + blkcnt_t st_blocks; /* number of blocks allocated */ + struct timespec st_atime; /* time of last access */ + struct timespec st_mtime; /* time of last modification */ + struct timespec st_ctime; /* time of last status change */ +}; + +void +sys·readfile(string filein, string fileout, bool okout) +{ + int32 fd; + byte namebuf[256]; + struct stat statbuf; + + fileout = nil; + okout = false; + + if(filein == nil || filein->len >= sizeof(namebuf)) + goto out; + + mcpy(namebuf, filein->str, filein->len); + namebuf[filein->len] = '\0'; + fd = open(namebuf, 0); + if(fd < 0) + goto out; + + if (fstat(fd, &statbuf) < 0) + goto close_out; + + if (statbuf.st_size <= 0) + goto close_out; + + fileout = mal(sizeof(fileout->len)+statbuf.st_size + 1); + fileout->len = statbuf.st_size; + + if (read(fd, fileout->str, statbuf.st_size) != statbuf.st_size) { + fileout = nil; + goto close_out; + } + okout = 1; + +close_out: + close(fd); +out: + FLUSH(&fileout); + FLUSH(&okout); + return; +} |