shithub: riscv

Download patch

ref: 17f7f6be4e1a316c0f5f26ff70e047aece4de2bc
parent: 01140dbe1733050a8da45645eac5c4279f0629b4
author: Jacob Moody <moody@posixcafe.org>
date: Sun May 19 16:57:04 EDT 2024

/sys/src: cleanup power64 assembly

--- a/sys/src/ape/lib/9/power64/getcallerpc.s
+++ b/sys/src/ape/lib/9/power64/getcallerpc.s
@@ -1,3 +1,3 @@
-TEXT	getcallerpc(SB), $-4
-	MOVD	0(R1), R3
+TEXT	getcallerpc(SB), $-8
+	MOVD	0(RSP), RARG
 	RETURN
--- a/sys/src/ape/lib/9/power64/getfcr.s
+++ b/sys/src/ape/lib/9/power64/getfcr.s
@@ -1,18 +1,18 @@
 TEXT	getfcr(SB), $8
 	MOVFL	FPSCR, F3
 	FMOVD	F3, f-8(SP)
-	MOVW	-4(SP), R3
+	MOVW	-4(SP), RARG
 	RETURN
 
 TEXT	getfsr(SB), $8
 	MOVFL	FPSCR, F3
 	FMOVD	F3, f-8(SP)
-	MOVW	-4(SP), R3
+	MOVW	-4(SP), RARG
 	RETURN
 
 TEXT	setfcr(SB), $8
 	SYNC
-	MOVW	R3, -4(SP)
+	MOVW	RARG, -4(SP)
 	FMOVD	-8(SP), F3
 	MOVFL	F3, FPSCR
 	ISYNC
@@ -20,7 +20,7 @@
 
 TEXT	setfsr(SB), $8
 	SYNC
-	MOVW	R3, -4(SP)
+	MOVW	RARG, -4(SP)
 	FMOVD	-8(SP), F3
 	MOVFL	F3, FPSCR
 	ISYNC
--- a/sys/src/ape/lib/ap/power64/atom.s
+++ /dev/null
@@ -1,34 +1,0 @@
-TEXT ainc(SB), 1, $-4				/* long ainc(long *); */
-	BR	_trap
-	RET
-
-TEXT adec(SB), 1, $-4				/* long adec(long*); */
-	BR	_trap
-	RET
-
-TEXT _xinc(SB), 1, $-4				/* void _xinc(long *); */
-	BR	_trap
-	RET
-
-TEXT _xdec(SB), 1, $-4				/* long _xdec(long *); */
-	BR	_trap
-	RET
-
-/*
- * int cas(uint* p, int ov, int nv);
- */
-TEXT cas(SB), 1, $-4
-	BR	_trap
-	RET
-
-/*
- * int casv(u64int* p, u64int ov, u64int nv);
- */
-TEXT casv(SB), 1, $-4
-	BR	_trap
-	RET
-
-_trap:
-	MOVD	$0, R0
-	MOVD	0(R0), R0
-	RET
--- a/sys/src/ape/lib/ap/power64/cycles.s
+++ b/sys/src/ape/lib/ap/power64/cycles.s
@@ -12,6 +12,6 @@
 	MOVW	SPR(TBRU),R5
 	CMP	R5,R7
 	BNE	loop
-	MOVW	R7,0(R3)
-	MOVW	R8,4(R3)
+	MOVW	R7,0(RARG)
+	MOVW	R8,4(RARG)
 	RETURN
--- a/sys/src/ape/lib/ap/power64/main9.s
+++ b/sys/src/ape/lib/ap/power64/main9.s
@@ -4,9 +4,9 @@
 	ADD	$-16, R1
 	MOVD	$0, R0
 	MOVD	$setSB(SB), R2
-	MOVD	R3, _tos(SB)
+	MOVD	RARG, _tos(SB)
 
-	MOVD	$_apemain(SB), R3
+	MOVD	$_apemain(SB), RARG
 	MOVD	R0, LR
 	MOVD	$_callmain(SB), R4
 	MOVD	R4, CTR
--- a/sys/src/ape/lib/ap/power64/main9p.s
+++ b/sys/src/ape/lib/ap/power64/main9p.s
@@ -4,9 +4,9 @@
 	ADD	$-16, R1
 	MOVD	$0, R0
 	MOVD	$setSB(SB), R2
-	MOVD	R3, _tos(SB)
+	MOVD	RARG, _tos(SB)
 
-	MOVD	$_profmain(SB), R3
+	MOVD	$_profmain(SB), RARG
 	MOVD	R0, LR
 	MOVD	$_callmain(SB), R4
 	MOVD	R4, CTR
@@ -13,7 +13,7 @@
 	BR	(CTR)
 
 TEXT	_callpc(SB), 1, $-8
-	MOVD	argp-8(FP), R3
+	MOVD	argp-8(FP), RARG
 TEXT	_saveret(SB), 1, $-8
 TEXT	_savearg(SB), 1, $-8
 	RETURN
--- a/sys/src/ape/lib/ap/power64/setjmp.s
+++ b/sys/src/ape/lib/ap/power64/setjmp.s
@@ -1,29 +1,29 @@
 TEXT	setjmp(SB), 1, $-8
 	MOVD	LR, R4
-	MOVD	R1, (R3)
-	MOVD	R4, 4(R3)
-	MOVW	$0, R3
+	MOVD	RSP, (RARG)
+	MOVD	R4, 8(RARG)
+	MOVW	$0, RARG
 	RETURN
 
-TEXT	sigsetjmp(SB), 1, $-4
+TEXT	sigsetjmp(SB), 1, $-8
 	MOVW	savemask+4(FP), R4
-	MOVW	R4, 0(R3)
+	MOVW	R4, 0(RARG)
 	MOVW	$_psigblocked(SB), R4
-	MOVW	R4, 4(R3)
-	MOVW	LR, R4
-	MOVW	R1, 8(R3)
-	MOVW	R4, 12(R3)
-	MOVW	$0, R3
+	MOVW	R4, 4(RARG)
+	MOVD	LR, R4
+	MOVD	R1, 8(RARG)
+	MOVD	R4, 16(RARG)
+	MOVW	$0, RARG
 	RETURN
 
 TEXT	longjmp(SB), 1, $-8
-	MOVD	R3, R4
-	MOVW	r+12(FP), R3
-	CMP	R3, $0
+	MOVD	RARG, R4
+	MOVW	r+12(FP), RARG
+	CMP	RARG, $0
 	BNE	ok		/* ansi: "longjmp(0) => longjmp(1)" */
-	MOVW	$1, R3		/* bless their pointed heads */
-ok:	MOVD	(R4), R1
-	MOVD	4(R4), R4
+	MOVW	$1, RARG		/* bless their pointed heads */
+ok:	MOVD	(R4), RSP
+	MOVD	8(R4), R4
 	MOVD	R4, LR
 	BR	(LR)
 
@@ -32,6 +32,6 @@
  * in the uregs given to notejmp
  */
 TEXT	__noterestore(SB), 1, $-8
-	MOVD	R4, R3
+	MOVD	R4, RARG
 	MOVD	R5, LR
 	BR	(LR)
--- a/sys/src/ape/lib/ap/power64/tas.s
+++ b/sys/src/ape/lib/ap/power64/tas.s
@@ -1,6 +1,6 @@
 TEXT	tas(SB), $0
 	SYNC
-	MOVD	R3, R4
+	MOVD	RARG, R4
 	MOVWZ	$0xdeaddead,R5
 tas1:
 /* taken out for the 755.  dcbf and L2 caching do not seem to get on
@@ -8,8 +8,8 @@
     (also see l.s) */
 //	DCBF	(R4)	
 	SYNC
-	LWAR	(R4), R3
-	CMP	R3, $0
+	LWAR	(R4), RARG
+	CMP	RARG, $0
 	BNE	tas0
 	STWCCC	R5, (R4)
 	BNE	tas1
--- a/sys/src/cmd/9a/lex.c
+++ b/sys/src/cmd/9a/lex.c
@@ -215,6 +215,9 @@
 	"R30",		LREG,	30,
 	"R31",		LREG,	31,
 
+	"RARG",		LREG,	REGARG,
+	"RSP",		LREG,	REGSP,
+
 	"F",		LF,	0,
 	"F0",		LFREG,	0,
 	"F1",		LFREG,	1,
--- a/sys/src/cmd/gefs/atomic-power64.s
+++ b/sys/src/cmd/gefs/atomic-power64.s
@@ -2,11 +2,10 @@
 TEXT agetl+0(SB),1,$0
 	SYNC
 	// See ISA 3.0B section B.2.3, "Safe Fetch"
-	MOVWZ	0(R3), R3
-	CMPW	R3, R3, CR7
+	MOVWZ	0(RARG), RARG
+	CMPW	RARG, RARG, CR7
 	BC	4, 30, 1(PC) // bne- cr7,0x4
 	ISYNC
-	MOVW	R3, ret+8(FP)
 	RETURN
 
 TEXT agetv+0(SB),1,$0
@@ -13,11 +12,10 @@
 TEXT agetp+0(SB),1,$0
 	SYNC
 	// See ISA 3.0B section B.2.3, "Safe Fetch"
-	MOVD	0(R3), R3
-	CMP	R3, R3, CR7
+	MOVD	0(RARG), RARG
+	CMP	RARG, RARG, CR7
 	BC	4, 30, 1(PC) // bne- cr7,0x4
 	ISYNC
-	MOVD	R3, ret+8(FP)
 	RETURN
 
 /*  set variants */
@@ -24,7 +22,7 @@
 TEXT asetl+0(SB),1,$0
 	MOVW	val+8(FP), R4
 	SYNC
-	MOVW	R4, 0(R3)
+	MOVW	R4, 0(RARG)
 	RETURN
 
 TEXT asetv+0(SB),1,$0
@@ -31,50 +29,48 @@
 TEXT asetp+0(SB),1,$0
 	MOVD	val+8(FP), R4
 	SYNC
-	MOVD	R4, 0(R3)
+	MOVD	R4, 0(RARG)
 	RETURN
 
 /*  inc variants */
 TEXT aincl+0(SB),1,$0
-	MOVD	R3, R4
+	MOVD	RARG, R4
 	MOVW	delta+8(FP), R5
 	LWSYNC
-	LWAR	(R4), R3
-	ADD	R5, R3
-	STWCCC	R3, (R4)
+	LWAR	(R4), RARG
+	ADD	R5, RARG
+	STWCCC	RARG, (R4)
 	BNE	-3(PC)
-	MOVW	R3, ret+16(FP)
 	RETURN
 
 TEXT aincv+0(SB),1,$0
 TEXT aincp+0(SB),1,$0
+	MOVD	RARG, R4
 	MOVD	delta+8(FP), R5
 	LWSYNC
-	LDAR	(R3), R4
-	ADD	R5, R4
-	STDCCC	R4, (R3)
+	LDAR	(R4), RARG
+	ADD	R5, RARG
+	STDCCC	RARG, (R4)
 	BNE	-3(PC)
-	MOVD	R4, ret+16(FP)
 	RETURN
 
 /*  cas variants */
 TEXT acasl+0(SB),1,$0
 	MOVWZ	old+8(FP), R4
-	MOVWZ	new+12(FP), R5
+	MOVWZ	new+16(FP), R5
 	LWSYNC
 casagain:
-	LWAR	(R3), R6
+	LWAR	(RARG), R6
 	CMPW	R6, R4
 	BNE	casfail
-	STWCCC	R5, (R3)
+	STWCCC	R5, (RARG)
 	BNE	casagain
-	MOVD	$1, R3
+	MOVD	$1, RARG
 	LWSYNC
-	MOVB	R3, ret+16(FP)
 	RETURN
 casfail:
 	LWSYNC
-	MOVB	R0, ret+16(FP)
+	AND	R0, RARG
 	RETURN
 
 TEXT acasv+0(SB),1,$0
@@ -83,18 +79,17 @@
 	MOVD	new+16(FP), R5
 	LWSYNC
 cas64again:
-	LDAR	(R3), R6
+	LDAR	(RARG), R6
 	CMP	R6, R4
 	BNE	cas64fail
-	STDCCC	R5, (R3)
+	STDCCC	R5, (RARG)
 	BNE	cas64again
-	MOVD	$1, R3
+	MOVD	$1, RARG
 	LWSYNC
-	MOVB	R3, ret+24(FP)
 	RETURN
 cas64fail:
 	LWSYNC
-	MOVB	R0, ret+24(FP)
+	AND	R0, RARG
 	RETURN
 
 /* barriers */
--- a/sys/src/libc/power64/atom.s
+++ b/sys/src/libc/power64/atom.s
@@ -1,17 +1,55 @@
-/* stubs */
-TEXT ainc(SB), 1, $-4				/* long ainc(long *); */
-TEXT adec(SB), 1, $-4				/* long adec(long*); */
+TEXT ainc(SB), 1, $0				/* long ainc(long *); */
+	MOVD	$1, R5
+	BR	alp
+TEXT adec(SB), 1, $0				/* long adec(long*); */
+	MOVD	$-1, R5
+alp:
+	MOVD	R3, R4
+	LWSYNC
+	LWAR	(R4), R3
+	ADD	R5, R3
+	STWCCC	R3, (R4)
+	BNE	-3(PC)
+	RETURN
+
 /*
  * int cas(uint* p, int ov, int nv);
  */
-TEXT cas(SB), 1, $-4
+TEXT cas(SB), 1, $0
+	MOVWZ	old+8(FP), R4
+	MOVWZ	new+16(FP), R5
+	LWSYNC
+casagain:
+	LWAR	(RARG), R6
+	CMPW	R6, R4
+	BNE	casfail
+	STWCCC	R5, (RARG)
+	BNE	casagain
+	MOVD	$1, RARG
+	LWSYNC
+	RETURN
+casfail:
+	LWSYNC
+	AND	R0, RARG
+	RETURN
+
 /*
  * int casv(u64int* p, u64int ov, u64int nv);
  */
-TEXT casv(SB), 1, $-4
-	BR	_trap
+TEXT casv(SB), 1, $0
+	MOVD	old+8(FP), R4
+	MOVD	new+16(FP), R5
+	LWSYNC
+cas64again:
+	LDAR	(RARG), R6
+	CMP	R6, R4
+	BNE	cas64fail
+	STDCCC	R5, (RARG)
+	BNE	cas64again
+	MOVD	$1, RARG
+	LWSYNC
 	RETURN
-_trap:
-	MOVD	$0, R0
-	MOVD	0(R0), R0
+cas64fail:
+	LWSYNC
+	AND	R0, RARG
 	RETURN
--- a/sys/src/libc/power64/cycles.s
+++ b/sys/src/libc/power64/cycles.s
@@ -12,6 +12,6 @@
 	MOVW	SPR(TBRU),R5
 	CMP	R5,R7
 	BNE	loop
-	MOVW	R7,0(R3)
-	MOVW	R8,4(R3)
+	MOVW	R7,0(RARG)
+	MOVW	R8,4(RARG)
 	RETURN
--- a/sys/src/libc/power64/getcallerpc.s
+++ b/sys/src/libc/power64/getcallerpc.s
@@ -1,4 +1,4 @@
 TEXT	getcallerpc(SB),1,$-8
-	MOVD	0(R1), R3
+	MOVD	0(RSP), RARG
 	RETURN
 
--- a/sys/src/libc/power64/getfcr.s
+++ b/sys/src/libc/power64/getfcr.s
@@ -1,18 +1,18 @@
 TEXT	getfcr(SB), $8
 	MOVFL	FPSCR, F3
 	FMOVD	F3, f-8(SP)
-	MOVW	-4(SP), R3
+	MOVW	-4(SP), RARG
 	RETURN
 
 TEXT	getfsr(SB), $8
 	MOVFL	FPSCR, F3
 	FMOVD	F3, f-8(SP)
-	MOVW	-4(SP), R3
+	MOVW	-4(SP), RARG
 	RETURN
 
 TEXT	setfcr(SB), $8
 	SYNC
-	MOVW	R3, -4(SP)
+	MOVW	RARG, -4(SP)
 	FMOVD	-8(SP), F3
 	MOVFL	F3, FPSCR
 	ISYNC
@@ -20,7 +20,7 @@
 
 TEXT	setfsr(SB), $8
 	SYNC
-	MOVW	R3, -4(SP)
+	MOVW	RARG, -4(SP)
 	FMOVD	-8(SP), F3
 	MOVFL	F3, FPSCR
 	ISYNC
--- a/sys/src/libc/power64/main9.s
+++ b/sys/src/libc/power64/main9.s
@@ -2,9 +2,9 @@
 	ADD	$-16, R1
 	MOVD	$0, R0
 	MOVD	$setSB(SB), R2
-	MOVD	R3, _tos(SB)
+	MOVD	RARG, _tos(SB)
 
-	MOVD	$main(SB), R3
+	MOVD	$main(SB), RARG
 	MOVD	R0, LR
 	MOVD	$_callmain(SB), R4
 	MOVD	R4, CTR
--- a/sys/src/libc/power64/main9p.s
+++ b/sys/src/libc/power64/main9p.s
@@ -2,9 +2,9 @@
 	ADD	$-16, R1
 	MOVD	$0, R0
 	MOVD	$setSB(SB), R2
-	MOVD	R3, _tos(SB)
+	MOVD	RARG, _tos(SB)
 
-	MOVD	$_profmain(SB), R3
+	MOVD	$_profmain(SB), RARG
 	MOVD	R0, LR
 	MOVD	$_callmain(SB), R4
 	MOVD	R4, CTR
@@ -11,7 +11,7 @@
 	BR	(CTR)
 
 TEXT	_callpc(SB), 1, $-8
-	MOVD	argp-8(FP), R3
+	MOVD	argp-8(FP), RARG
 TEXT	_saveret(SB), 1, $-8
 TEXT	_savearg(SB), 1, $-8
 	RETURN
--- a/sys/src/libc/power64/setjmp.s
+++ b/sys/src/libc/power64/setjmp.s
@@ -1,18 +1,18 @@
 TEXT	setjmp(SB), 1, $-8
 	MOVD	LR, R4
-	MOVD	R1, (R3)
-	MOVD	R4, 4(R3)
-	MOVW	$0, R3
+	MOVD	RSP, (RARG)
+	MOVD	R4, 8(RARG)
+	MOVW	$0, RARG
 	RETURN
 
 TEXT	longjmp(SB), 1, $-8
-	MOVD	R3, R4
-	MOVW	r+12(FP), R3
-	CMP	R3, $0
+	MOVD	RARG, R4
+	MOVW	r+12(FP), RARG
+	CMP	RARG, $0
 	BNE	ok		/* ansi: "longjmp(0) => longjmp(1)" */
-	MOVW	$1, R3		/* bless their pointed heads */
-ok:	MOVD	(R4), R1
-	MOVD	4(R4), R4
+	MOVW	$1, RARG		/* bless their pointed heads */
+ok:	MOVD	(R4), RSP
+	MOVD	8(R4), R4
 	MOVD	R4, LR
 	BR	(LR)
 
@@ -21,6 +21,6 @@
  * in the uregs given to notejmp
  */
 TEXT	__noterestore(SB), 1, $-8
-	MOVD	R4, R3
+	MOVD	R4, RARG
 	MOVD	R5, LR
 	BR	(LR)
--- a/sys/src/libc/power64/tas.s
+++ b/sys/src/libc/power64/tas.s
@@ -1,6 +1,6 @@
 TEXT	_tas(SB), $0
 	SYNC
-	MOVD	R3, R4
+	MOVD	RARG, R4
 	MOVWZ	$0xdeaddead,R5
 tas1:
 /* taken out for the 755.  dcbf and L2 caching do not seem to get on
@@ -8,8 +8,8 @@
     (also see l.s) */
 //	DCBF	(R4)	
 	SYNC
-	LWAR	(R4), R3
-	CMP	R3, $0
+	LWAR	(R4), RARG
+	CMP	RARG, $0
 	BNE	tas0
 	STWCCC	R5, (R4)
 	BNE	tas1