Commit 8e82a673 authored by Rob Pike's avatar Rob Pike

Break runtime.c into separate pieces for maps, strings, print, etc.

Share common assembler for amd64 runtime

SVN=125317
parent b987f7a7
......@@ -13,9 +13,14 @@ RT0OFILES=\
rt0_$(GOARCH)_$(GOOS).$O\
LIBOFILES=\
rt0_$(GOARCH).$O\
rt1_$(GOARCH)_$(GOOS).$O\
rt2_$(GOARCH).$O\
sys_$(GOARCH)_$(GOOS).$O\
runtime.$O\
runtime_map.$O\
runtime_print.$O\
runtime_string.$O\
sys_file.$O\
OFILES=$(RT0OFILES) $(LIBOFILES)
......
// 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.
TEXT _rt0_amd64(SB),7,$-8
// copy arguments forward on an even stack
MOVQ 0(SP), AX // argc
LEAQ 8(SP), BX // argv
SUBQ $(4*8+7), SP // 2args 2auto
ANDQ $~7, SP
MOVQ AX, 16(SP)
MOVQ BX, 24(SP)
// allocate the per-user block
LEAQ peruser<>(SB), R15 // dedicated u. register
LEAQ (-4096+104+4*8)(SP), AX
MOVQ AX, 0(R15) // 0(R15) is stack limit (w 104b guard)
MOVL $1024, AX
MOVL AX, 0(SP)
CALL mal(SB)
LEAQ 104(AX), BX
MOVQ BX, 16(R15) // 16(R15) is limit of istack (w 104b guard)
ADDQ 0(SP), AX
LEAQ (-4*8)(AX), BX
MOVQ BX, 24(R15) // 24(R15) is base of istack (w auto*4)
CALL check(SB)
// process the arguments
MOVL 16(SP), AX // copy argc
MOVL AX, 0(SP)
MOVQ 24(SP), AX // copy argv
MOVQ AX, 8(SP)
CALL args(SB)
CALL main·main(SB)
MOVQ $0, AX
MOVQ AX, 0(SP) // exit status
CALL sys·exit(SB)
CALL notok(SB) // fault
RET
//
// the calling sequence for a routine that
// needs N bytes stack, A args.
//
// N1 = (N+160 > 4096)? N+160: 0
// A1 = A
//
// if N <= 75
// CMPQ SP, 0(R15)
// JHI 3(PC)
// MOVQ $(N1<<0) | (A1<<32)), AX
// CALL _morestack
//
// if N > 75
// LEAQ (-N-75)(SP), AX
// CMPQ AX, 0(R15)
// JHI 3(PC)
// MOVQ $(N1<<0) | (A1<<32)), AX
// CALL _morestack
//
TEXT _morestack(SB), 7, $0
// save stuff on interrupt stack
MOVQ 24(R15), BX // istack
MOVQ SP, 8(BX) // old SP
MOVQ AX, 16(BX) // magic number
MOVQ 0(R15), AX // old limit
MOVQ AX, 24(BX)
// switch and set up new limit
MOVQ BX, SP
MOVQ 16(R15), AX // istack limit
MOVQ AX, 0(R15)
// allocate a new stack max of request and 4k
MOVL 16(SP), AX // magic number
CMPL AX, $4096
JHI 2(PC)
MOVL $4096, AX
MOVL AX, 0(SP)
CALL mal(SB)
// switch to new stack
MOVQ SP, BX // istack
ADDQ $104, AX // new stack limit
MOVQ AX, 0(R15)
ADDQ 0(SP), AX
LEAQ (-104-4*8)(AX), SP // new SP
MOVQ 8(R15), AX
MOVQ AX, 0(SP) // old base
MOVQ SP, 8(R15) // new base
// copy needed stuff from istack to new stack
MOVQ 16(BX), AX // magic number
MOVQ AX, 16(SP)
MOVQ 24(BX), AX // old limit
MOVQ AX, 24(SP)
MOVQ 8(BX), AX // old SP
MOVQ AX, 8(SP)
// are there parameters
MOVL 20(SP), CX // copy count
CMPL CX, $0
JEQ easy
// copy in
LEAQ 16(AX), SI
SUBQ CX, SP
MOVQ SP, DI
SHRL $3, CX
CLD
REP
MOVSQ
// call the intended
CALL 0(AX)
// copy out
MOVQ SP, SI
MOVQ 8(R15), BX // new base
MOVQ 8(BX), AX // old SP
LEAQ 16(AX), DI
MOVL 20(BX), CX // copy count
SHRL $3, CX
CLD
REP
MOVSQ
// restore old SP and limit
MOVQ 8(R15), SP // new base
MOVQ 24(SP), AX // old limit
MOVQ AX, 0(R15)
MOVQ 0(SP), AX
MOVQ AX, 8(R15) // old base
MOVQ 8(SP), AX // old SP
MOVQ AX, SP
// and return to the call behind mine
ADDQ $8, SP
RET
easy:
CALL 0(AX)
// restore old SP and limit
MOVQ 24(SP), AX // old limit
MOVQ AX, 0(R15)
MOVQ 0(SP), AX
MOVQ AX, 8(R15) // old base
MOVQ 8(SP), AX // old SP
MOVQ AX, SP
// and return to the call behind mine
ADDQ $8, SP
RET
TEXT FLUSH(SB),7,$-8
RET
GLOBL peruser<>(SB),$64
......@@ -2,283 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Darwin and Linux use the same linkage to main
TEXT _rt0_amd64_darwin(SB),7,$-8
// copy arguments forward on an even stack
MOVQ 0(SP), AX // argc
LEAQ 8(SP), BX // argv
SUBQ $(4*8+7), SP // 2args 2auto
ANDQ $~7, SP
MOVQ AX, 16(SP)
MOVQ BX, 24(SP)
// allocate the per-user block
LEAQ peruser<>(SB), R15 // dedicated u. register
LEAQ (-4096+104+4*8)(SP), AX
MOVQ AX, 0(R15) // 0(R15) is stack limit (w 104b guard)
MOVL $1024, AX
MOVL AX, 0(SP)
CALL mal(SB)
LEAQ 104(AX), BX
MOVQ BX, 16(R15) // 16(R15) is limit of istack (w 104b guard)
ADDQ 0(SP), AX
LEAQ (-4*8)(AX), BX
MOVQ BX, 24(R15) // 24(R15) is base of istack (w auto*4)
CALL check(SB)
// process the arguments
MOVL 16(SP), AX // copy argc
MOVL AX, 0(SP)
MOVQ 24(SP), AX // copy argv
MOVQ AX, 8(SP)
CALL args(SB)
CALL main·main(SB)
MOVQ $0, AX
MOVQ AX, 0(SP) // exit status
CALL sys·exit(SB)
CALL notok(SB) // fault
RET
//
// the calling sequence for a routine that
// needs N bytes stack, A args.
//
// N1 = (N+160 > 4096)? N+160: 0
// A1 = A
//
// if N <= 75
// CMPQ SP, 0(R15)
// JHI 3(PC)
// MOVQ $(N1<<0) | (A1<<32)), AX
// CALL _morestack
//
// if N > 75
// LEAQ (-N-75)(SP), AX
// CMPQ AX, 0(R15)
// JHI 3(PC)
// MOVQ $(N1<<0) | (A1<<32)), AX
// CALL _morestack
//
TEXT _morestack(SB), 7, $0
// save stuff on interrupt stack
MOVQ 24(R15), BX // istack
MOVQ SP, 8(BX) // old SP
MOVQ AX, 16(BX) // magic number
MOVQ 0(R15), AX // old limit
MOVQ AX, 24(BX)
// switch and set up new limit
MOVQ BX, SP
MOVQ 16(R15), AX // istack limit
MOVQ AX, 0(R15)
// allocate a new stack max of request and 4k
MOVL 16(SP), AX // magic number
CMPL AX, $4096
JHI 2(PC)
MOVL $4096, AX
MOVL AX, 0(SP)
CALL mal(SB)
// switch to new stack
MOVQ SP, BX // istack
ADDQ $104, AX // new stack limit
MOVQ AX, 0(R15)
ADDQ 0(SP), AX
LEAQ (-104-4*8)(AX), SP // new SP
MOVQ 8(R15), AX
MOVQ AX, 0(SP) // old base
MOVQ SP, 8(R15) // new base
// copy needed stuff from istack to new stack
MOVQ 16(BX), AX // magic number
MOVQ AX, 16(SP)
MOVQ 24(BX), AX // old limit
MOVQ AX, 24(SP)
MOVQ 8(BX), AX // old SP
MOVQ AX, 8(SP)
// are there parameters
MOVL 20(SP), CX // copy count
CMPL CX, $0
JEQ easy
// copy in
LEAQ 16(AX), SI
SUBQ CX, SP
MOVQ SP, DI
SHRL $3, CX
CLD
REP
MOVSQ
// call the intended
CALL 0(AX)
// copy out
MOVQ SP, SI
MOVQ 8(R15), BX // new base
MOVQ 8(BX), AX // old SP
LEAQ 16(AX), DI
MOVL 20(BX), CX // copy count
SHRL $3, CX
CLD
REP
MOVSQ
// restore old SP and limit
MOVQ 8(R15), SP // new base
MOVQ 24(SP), AX // old limit
MOVQ AX, 0(R15)
MOVQ 0(SP), AX
MOVQ AX, 8(R15) // old base
MOVQ 8(SP), AX // old SP
MOVQ AX, SP
// and return to the call behind mine
ADDQ $8, SP
RET
easy:
CALL 0(AX)
// restore old SP and limit
MOVQ 24(SP), AX // old limit
MOVQ AX, 0(R15)
MOVQ 0(SP), AX
MOVQ AX, 8(R15) // old base
MOVQ 8(SP), AX // old SP
MOVQ AX, SP
// and return to the call behind mine
ADDQ $8, SP
RET
TEXT FLUSH(SB),7,$-8
RET
TEXT sys·exit(SB),1,$-8
MOVL 8(SP), DI // arg 1 exit status
MOVL $(0x2000000+1), AX // syscall entry
SYSCALL
CALL notok(SB)
RET
TEXT sys·write(SB),1,$-8
MOVL 8(SP), DI // arg 1 fid
MOVQ 16(SP), SI // arg 2 buf
MOVL 24(SP), DX // arg 3 count
MOVL $(0x2000000+4), AX // syscall entry
SYSCALL
JCC 2(PC)
CALL notok(SB)
RET
TEXT open(SB),1,$-8
MOVQ 8(SP), DI
MOVL 16(SP), SI
MOVQ $0, R10
MOVL $(0x2000000+5), AX // syscall entry
SYSCALL
RET
TEXT close(SB),1,$-8
MOVL 8(SP), DI
MOVL $(0x2000000+6), AX // syscall entry
SYSCALL
RET
TEXT fstat(SB),1,$-8
MOVL 8(SP), DI
MOVQ 16(SP), SI
MOVL $(0x2000000+339), AX // syscall entry; really fstat64
SYSCALL
RET
TEXT read(SB),1,$-8
MOVL 8(SP), DI
MOVQ 16(SP), SI
MOVL 24(SP), DX
MOVL $(0x2000000+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
MOVQ 24(SP), DX // arg 3 oact
MOVQ 24(SP), CX // arg 3 oact
MOVQ 24(SP), R10 // arg 3 oact
MOVL $(0x2000000+46), AX // syscall entry
SYSCALL
JCC 2(PC)
CALL notok(SB)
RET
TEXT sigtramp(SB),1,$24
MOVL DX,0(SP)
MOVQ CX,8(SP)
MOVQ R8,16(SP)
CALL sighandler(SB)
RET
TEXT sys·breakpoint(SB),1,$-8
BYTE $0xcc
RET
TEXT sys·mmap(SB),1,$-8
MOVQ 8(SP), DI // arg 1 addr
MOVL 16(SP), SI // arg 2 len
MOVL 20(SP), DX // arg 3 prot
MOVL 24(SP), R10 // arg 4 flags
MOVL 28(SP), R8 // arg 5 fid
MOVL 32(SP), R9 // arg 6 offset
MOVL $(0x2000000+197), AX // syscall entry
SYSCALL
JCC 2(PC)
CALL notok(SB)
RET
TEXT notok(SB),1,$-8
MOVL $0xf1, BP
MOVQ BP, (BP)
RET
TEXT sys·memclr(SB),1,$-8
MOVQ 8(SP), DI // arg 1 addr
MOVL 16(SP), CX // arg 2 count
ADDL $7, CX
SHRL $3, CX
MOVQ $0, AX
CLD
REP
STOSQ
RET
TEXT sys·getcallerpc+0(SB),1,$0
MOVQ x+0(FP),AX
MOVQ -8(AX),AX
RET
GLOBL peruser<>(SB),$64
MOVQ $_rt0_amd64(SB), AX
JMP AX
......@@ -2,287 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Darwin and Linux use the same linkage to main
TEXT _rt0_amd64_linux(SB),7,$-8
// copy arguments forward on an even stack
MOVQ 0(SP), AX // argc
LEAQ 8(SP), BX // argv
SUBQ $(4*8+7), SP // 2args 2auto
ANDQ $~7, SP
MOVQ AX, 16(SP)
MOVQ BX, 24(SP)
// allocate the per-user block
LEAQ peruser<>(SB), R15 // dedicated u. register
LEAQ (-4096+104+4*8)(SP), AX
MOVQ AX, 0(R15) // 0(R15) is stack limit (w 104b guard)
MOVL $1024, AX
MOVL AX, 0(SP)
CALL mal(SB)
LEAQ 104(AX), BX
MOVQ BX, 16(R15) // 16(R15) is limit of istack (w 104b guard)
ADDQ 0(SP), AX
LEAQ (-4*8)(AX), BX
MOVQ BX, 24(R15) // 24(R15) is base of istack (w auto*4)
CALL check(SB)
// process the arguments
MOVL 16(SP), AX // copy argc
MOVL AX, 0(SP)
MOVQ 24(SP), AX // copy argv
MOVQ AX, 8(SP)
CALL args(SB)
CALL main·main(SB)
MOVQ $0, AX
MOVQ AX, 0(SP) // exit status
CALL sys·exit(SB)
CALL notok(SB) // fault
RET
//
// the calling sequence for a routine that
// needs N bytes stack, A args.
//
// N1 = (N+160 > 4096)? N+160: 0
// A1 = A
//
// if N <= 75
// CMPQ SP, 0(R15)
// JHI 3(PC)
// MOVQ $(N1<<0) | (A1<<32)), AX
// CALL _morestack
//
// if N > 75
// LEAQ (-N-75)(SP), AX
// CMPQ AX, 0(R15)
// JHI 3(PC)
// MOVQ $(N1<<0) | (A1<<32)), AX
// CALL _morestack
//
TEXT _morestack(SB), 7, $0
// save stuff on interrupt stack
MOVQ 24(R15), BX // istack
MOVQ SP, 8(BX) // old SP
MOVQ AX, 16(BX) // magic number
MOVQ 0(R15), AX // old limit
MOVQ AX, 24(BX)
// switch and set up new limit
MOVQ BX, SP
MOVQ 16(R15), AX // istack limit
MOVQ AX, 0(R15)
// allocate a new stack max of request and 4k
MOVL 16(SP), AX // magic number
CMPL AX, $4096
JHI 2(PC)
MOVL $4096, AX
MOVL AX, 0(SP)
CALL mal(SB)
// switch to new stack
MOVQ SP, BX // istack
ADDQ $104, AX // new stack limit
MOVQ AX, 0(R15)
ADDQ 0(SP), AX
LEAQ (-104-4*8)(AX), SP // new SP
MOVQ 8(R15), AX
MOVQ AX, 0(SP) // old base
MOVQ SP, 8(R15) // new base
// copy needed stuff from istack to new stack
MOVQ 16(BX), AX // magic number
MOVQ AX, 16(SP)
MOVQ 24(BX), AX // old limit
MOVQ AX, 24(SP)
MOVQ 8(BX), AX // old SP
MOVQ AX, 8(SP)
// are there parameters
MOVL 20(SP), CX // copy count
CMPL CX, $0
JEQ easy
// copy in
LEAQ 16(AX), SI
SUBQ CX, SP
MOVQ SP, DI
SHRL $3, CX
CLD
REP
MOVSQ
// call the intended
CALL 0(AX)
// copy out
MOVQ SP, SI
MOVQ 8(R15), BX // new base
MOVQ 8(BX), AX // old SP
LEAQ 16(AX), DI
MOVL 20(BX), CX // copy count
SHRL $3, CX
CLD
REP
MOVSQ
// restore old SP and limit
MOVQ 8(R15), SP // new base
MOVQ 24(SP), AX // old limit
MOVQ AX, 0(R15)
MOVQ 0(SP), AX
MOVQ AX, 8(R15) // old base
MOVQ 8(SP), AX // old SP
MOVQ AX, SP
// and return to the call behind mine
ADDQ $8, SP
RET
easy:
CALL 0(AX)
// restore old SP and limit
MOVQ 24(SP), AX // old limit
MOVQ AX, 0(R15)
MOVQ 0(SP), AX
MOVQ AX, 8(R15) // old base
MOVQ 8(SP), AX // old SP
MOVQ AX, SP
// and return to the call behind mine
ADDQ $8, SP
RET
TEXT FLUSH(SB),7,$-8
RET
TEXT sys·exit(SB),1,$-8
MOVL 8(SP), DI
MOVL $60, AX
SYSCALL
RET
TEXT sys·write(SB),1,$-8
MOVL 8(SP), DI
MOVQ 16(SP), SI
MOVL 24(SP), DX
MOVL $1, AX // syscall entry
SYSCALL
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
MOVQ 24(SP), DX
MOVQ 32(SP), CX
MOVL CX, R10
MOVL $13, AX // syscall entry
SYSCALL
RET
TEXT sigtramp(SB),1,$24
MOVQ DI,0(SP)
MOVQ SI,8(SP)
MOVQ DX,16(SP)
CALL sighandler(SB)
RET
TEXT sys·breakpoint(SB),1,$-8
BYTE $0xcc
RET
TEXT sys·mmap(SB),1,$-8
MOVQ 8(SP), DI
MOVL 16(SP), SI
MOVL 20(SP), DX
MOVL 24(SP), CX
MOVL 28(SP), R8
MOVL 32(SP), R9
/* flags arg for ANON is 1000 but sb 20 */
MOVL CX, AX
ANDL $~0x1000, CX
ANDL $0x1000, AX
SHRL $7, AX
ORL AX, CX
MOVL CX, R10
MOVL $9, AX // syscall entry
SYSCALL
CMPQ AX, $0xfffffffffffff001
JLS 2(PC)
CALL notok(SB)
RET
TEXT notok(SB),1,$-8
MOVL $0xf1, BP
MOVQ BP, (BP)
RET
TEXT sys·memclr(SB),1,$-8
MOVQ 8(SP), DI // arg 1 addr
MOVL 16(SP), CX // arg 2 count (cannot be zero)
ADDL $7, CX
SHRL $3, CX
MOVQ $0, AX
CLD
REP
STOSQ
RET
TEXT sys·getcallerpc+0(SB),1,$0
MOVQ x+0(FP),AX
MOVQ -8(AX),AX
RET
GLOBL peruser<>(SB),$64
MOVQ $_rt0_amd64(SB), AX
JMP AX
This diff is collapsed.
......@@ -79,6 +79,16 @@ enum
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
#define nil ((void*)0)
/*
* common functions and data
*/
int32 strcmp(byte*, byte*);
int32 findnull(int8*);
void dump(byte*, int32);
extern string emptystring;
extern int32 debug;
/*
* very low level c-called
*/
......@@ -119,6 +129,7 @@ void sys·printbool(bool);
void sys·printfloat(float64);
void sys·printint(int64);
void sys·printstring(string);
void sys·printpc(void*);
void sys·printpointer(void*);
void sys·catstring(string, string, string);
void sys·cmpstring(string, string, int32);
......
This diff is collapsed.
// 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"
void
dump(byte *p, int32 n)
{
uint32 v;
int32 i;
for(i=0; i<n; i++) {
sys·printpointer((byte*)(p[i]>>4));
sys·printpointer((byte*)(p[i]&0xf));
if((i&15) == 15)
prints("\n");
else
prints(" ");
}
if(n & 15)
prints("\n");
}
void
prints(int8 *s)
{
sys·write(1, s, findnull(s));
}
void
sys·printpc(void *p)
{
prints("PC=0x");
sys·printpointer(sys·getcallerpc(p));
}
void
sys·printbool(bool v)
{
if(v) {
sys·write(1, (byte*)"true", 4);
return;
}
sys·write(1, (byte*)"false", 5);
}
void
sys·printfloat(float64 v)
{
sys·write(1, "printfloat", 10);
}
void
sys·printint(int64 v)
{
byte buf[100];
int32 i, s;
s = 0;
if(v < 0) {
v = -v;
s = 1;
if(v < 0) {
sys·write(1, (byte*)"-oo", 3);
return;
}
}
for(i=nelem(buf)-1; i>0; i--) {
buf[i] = v%10 + '0';
if(v < 10)
break;
v = v/10;
}
if(s) {
i--;
buf[i] = '-';
}
sys·write(1, buf+i, nelem(buf)-i);
}
void
sys·printpointer(void *p)
{
uint64 v;
byte buf[100];
int32 i;
v = (int64)p;
for(i=nelem(buf)-1; i>0; i--) {
buf[i] = v%16 + '0';
if(buf[i] > '9')
buf[i] += 'a'-'0'-10;
if(v < 16)
break;
v = v/16;
}
sys·write(1, buf+i, nelem(buf)-i);
}
void
sys·printstring(string v)
{
if(v != nil)
sys·write(1, v->str, v->len);
}
// 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"
static int32 empty = 0;
string emptystring = (string)&empty;
int32
findnull(int8 *s)
{
int32 l;
for(l=0; s[l]!=0; l++)
;
return l;
}
void
sys·catstring(string s1, string s2, string s3)
{
uint32 l;
if(s1 == nil || s1->len == 0) {
s3 = s2;
goto out;
}
if(s2 == nil || s2->len == 0) {
s3 = s1;
goto out;
}
l = s1->len + s2->len;
s3 = mal(sizeof(s3->len)+l);
s3->len = l;
mcpy(s3->str, s1->str, s1->len);
mcpy(s3->str+s1->len, s2->str, s2->len);
out:
FLUSH(&s3);
}
static void
prbounds(int8* s, int32 a, int32 b, int32 c)
{
int32 i;
prints(s);
prints(" ");
sys·printint(a);
prints("<");
sys·printint(b);
prints(">");
sys·printint(c);
prints("\n");
throw("bounds");
}
uint32
cmpstring(string s1, string s2)
{
uint32 i, l;
byte c1, c2;
if(s1 == nil)
s1 = emptystring;
if(s2 == nil)
s2 = emptystring;
l = s1->len;
if(s2->len < l)
l = s2->len;
for(i=0; i<l; i++) {
c1 = s1->str[i];
c2 = s2->str[i];
if(c1 < c2)
return -1;
if(c1 > c2)
return +1;
}
if(s1->len < s2->len)
return -1;
if(s1->len > s2->len)
return +1;
return 0;
}
void
sys·cmpstring(string s1, string s2, int32 v)
{
v = cmpstring(s1, s2);
FLUSH(&v);
}
int32
strcmp(byte *s1, byte *s2)
{
uint32 i;
byte c1, c2;
for(i=0;; i++) {
c1 = s1[i];
c2 = s2[i];
if(c1 < c2)
return -1;
if(c1 > c2)
return +1;
if(c1 == 0)
return 0;
}
}
void
sys·slicestring(string si, int32 lindex, int32 hindex, string so)
{
string s, str;
int32 l;
if(si == nil)
si = emptystring;
if(lindex < 0 || lindex > si->len ||
hindex < lindex || hindex > si->len) {
sys·printpc(&si);
prints(" ");
prbounds("slice", lindex, si->len, hindex);
}
l = hindex-lindex;
so = mal(sizeof(so->len)+l);
so->len = l;
mcpy(so->str, si->str+lindex, l);
FLUSH(&so);
}
void
sys·indexstring(string s, int32 i, byte b)
{
if(s == nil)
s = emptystring;
if(i < 0 || i >= s->len) {
sys·printpc(&s);
prints(" ");
prbounds("index", 0, i, s->len);
}
b = s->str[i];
FLUSH(&b);
}
/*
* this is the plan9 runetochar
* extended for 36 bits in 7 bytes
* note that it truncates to 32 bits
* through the argument passing.
*/
static int32
runetochar(byte *str, uint32 c)
{
int32 i, n;
uint32 mask, mark;
/*
* one character in 7 bits
*/
if(c <= 0x07FUL) {
str[0] = c;
return 1;
}
/*
* every new character picks up 5 bits
* one less in the first byte and
* six more in an extension byte
*/
mask = 0x7ffUL;
mark = 0xC0UL;
for(n=1;; n++) {
if(c <= mask)
break;
mask = (mask<<5) | 0x1fUL;
mark = (mark>>1) | 0x80UL;
}
/*
* lay down the bytes backwards
* n is the number of extension bytes
* mask is the max codepoint
* mark is the zeroth byte indicator
*/
for(i=n; i>0; i--) {
str[i] = 0x80UL | (c&0x3fUL);
c >>= 6;
}
str[0] = mark|c;
return n+1;
}
void
sys·intstring(int64 v, string s)
{
int32 l;
s = mal(sizeof(s->len)+8);
s->len = runetochar(s->str, v);
FLUSH(&s);
}
void
sys·byteastring(byte *a, int32 l, string s)
{
s = mal(sizeof(s->len)+l);
s->len = l;
mcpy(s->str, a, l);
FLUSH(&s);
}
// 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.
//
// System calls and other sys.stuff for AMD64, Darwin
//
TEXT sys·exit(SB),1,$-8
MOVL 8(SP), DI // arg 1 exit status
MOVL $(0x2000000+1), AX // syscall entry
SYSCALL
CALL notok(SB)
RET
TEXT sys·write(SB),1,$-8
MOVL 8(SP), DI // arg 1 fid
MOVQ 16(SP), SI // arg 2 buf
MOVL 24(SP), DX // arg 3 count
MOVL $(0x2000000+4), AX // syscall entry
SYSCALL
JCC 2(PC)
CALL notok(SB)
RET
TEXT open(SB),1,$-8
MOVQ 8(SP), DI
MOVL 16(SP), SI
MOVQ $0, R10
MOVL $(0x2000000+5), AX // syscall entry
SYSCALL
RET
TEXT close(SB),1,$-8
MOVL 8(SP), DI
MOVL $(0x2000000+6), AX // syscall entry
SYSCALL
RET
TEXT fstat(SB),1,$-8
MOVL 8(SP), DI
MOVQ 16(SP), SI
MOVL $(0x2000000+339), AX // syscall entry; really fstat64
SYSCALL
RET
TEXT read(SB),1,$-8
MOVL 8(SP), DI
MOVQ 16(SP), SI
MOVL 24(SP), DX
MOVL $(0x2000000+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
MOVQ 24(SP), DX // arg 3 oact
MOVQ 24(SP), CX // arg 3 oact
MOVQ 24(SP), R10 // arg 3 oact
MOVL $(0x2000000+46), AX // syscall entry
SYSCALL
JCC 2(PC)
CALL notok(SB)
RET
TEXT sigtramp(SB),1,$24
MOVL DX,0(SP)
MOVQ CX,8(SP)
MOVQ R8,16(SP)
CALL sighandler(SB)
RET
TEXT sys·breakpoint(SB),1,$-8
BYTE $0xcc
RET
TEXT sys·mmap(SB),1,$-8
MOVQ 8(SP), DI // arg 1 addr
MOVL 16(SP), SI // arg 2 len
MOVL 20(SP), DX // arg 3 prot
MOVL 24(SP), R10 // arg 4 flags
MOVL 28(SP), R8 // arg 5 fid
MOVL 32(SP), R9 // arg 6 offset
MOVL $(0x2000000+197), AX // syscall entry
SYSCALL
JCC 2(PC)
CALL notok(SB)
RET
TEXT notok(SB),1,$-8
MOVL $0xf1, BP
MOVQ BP, (BP)
RET
TEXT sys·memclr(SB),1,$-8
MOVQ 8(SP), DI // arg 1 addr
MOVL 16(SP), CX // arg 2 count
ADDL $7, CX
SHRL $3, CX
MOVQ $0, AX
CLD
REP
STOSQ
RET
TEXT sys·getcallerpc+0(SB),1,$0
MOVQ x+0(FP),AX
MOVQ -8(AX),AX
RET
// 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.
//
// System calls and other sys.stuff for AMD64, Linux
//
TEXT sys·exit(SB),1,$-8
MOVL 8(SP), DI
MOVL $60, AX
SYSCALL
RET
TEXT sys·write(SB),1,$-8
MOVL 8(SP), DI
MOVQ 16(SP), SI
MOVL 24(SP), DX
MOVL $1, AX // syscall entry
SYSCALL
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
MOVQ 24(SP), DX
MOVQ 32(SP), CX
MOVL CX, R10
MOVL $13, AX // syscall entry
SYSCALL
RET
TEXT sigtramp(SB),1,$24
MOVQ DI,0(SP)
MOVQ SI,8(SP)
MOVQ DX,16(SP)
CALL sighandler(SB)
RET
TEXT sys·breakpoint(SB),1,$-8
BYTE $0xcc
RET
TEXT sys·mmap(SB),1,$-8
MOVQ 8(SP), DI
MOVL 16(SP), SI
MOVL 20(SP), DX
MOVL 24(SP), CX
MOVL 28(SP), R8
MOVL 32(SP), R9
/* flags arg for ANON is 1000 but sb 20 */
MOVL CX, AX
ANDL $~0x1000, CX
ANDL $0x1000, AX
SHRL $7, AX
ORL AX, CX
MOVL CX, R10
MOVL $9, AX // syscall entry
SYSCALL
CMPQ AX, $0xfffffffffffff001
JLS 2(PC)
CALL notok(SB)
RET
TEXT notok(SB),1,$-8
MOVL $0xf1, BP
MOVQ BP, (BP)
RET
TEXT sys·memclr(SB),1,$-8
MOVQ 8(SP), DI // arg 1 addr
MOVL 16(SP), CX // arg 2 count (cannot be zero)
ADDL $7, CX
SHRL $3, CX
MOVQ $0, AX
CLD
REP
STOSQ
RET
TEXT sys·getcallerpc+0(SB),1,$0
MOVQ x+0(FP),AX
MOVQ -8(AX),AX
RET
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