1#include <picolibc.h>
2
3! Entry: arg0: destination
4!        arg1: source
5! Exit:  result: destination
6!
7/* SH5 code Copyright 2002 SuperH Ltd. */
8
9#include "asm.h"
10
11ENTRY(strcpy)
12
13#if __SHMEDIA__
14
15	pta/l shortstring,tr1
16	ldlo.q r3,0,r4
17	ptabs r18,tr4
18	shlli r3,3,r7
19	addi r2, 8, r0
20	mcmpeq.b r4,r63,r6
21	SHHI r6,r7,r6
22	bnei/u r6,0,tr1 // shortstring
23	pta/l no_lddst, tr2
24	ori r3,-8,r23
25	sub r2, r23, r0
26	sub r3, r2, r21
27	addi r21, 8, r20
28	ldx.q r0, r21, r5
29	pta/l loop, tr0
30	ori r2,-8,r22
31	mcmpeq.b r5, r63, r6
32	bgt/u r22, r23, tr2 // no_lddst
33
34	// r22 < r23 :  Need to do a load from the destination.
35	// r22 == r23 : Doesn't actually need to load from destination,
36	//              but still can be handled here.
37	ldlo.q r2, 0, r9
38	movi -1, r8
39	SHLO r8, r7, r8
40	mcmv r4, r8, r9
41	stlo.q r2, 0, r9
42	beqi/l r6, 0, tr0 // loop
43
44	add r5, r63, r4
45	addi r0, 8, r0
46	blink tr1, r63 // shortstring
47no_lddst:
48	// r22 > r23: note that for r22 == r23 the sthi.q would clobber
49	//            bytes before the destination region.
50	stlo.q r2, 0, r4
51	SHHI r4, r7, r4
52	sthi.q r0, -1, r4
53	beqi/l r6, 0, tr0 // loop
54
55	add r5, r63, r4
56	addi r0, 8, r0
57shortstring:
58#ifndef __LITTLE_ENDIAN__
59	pta/l shortstring2,tr1
60	byterev r4,r4
61#endif
62shortstring2:
63	st.b r0,-8,r4
64	andi r4,0xff,r5
65	shlri r4,8,r4
66	addi r0,1,r0
67	bnei/l r5,0,tr1
68	blink tr4,r63 // return
69
70	.balign 8
71loop:
72	stlo.q r0, 0, r5
73	ldx.q r0, r20, r4
74	addi r0, 16, r0
75	sthi.q r0, -9, r5
76	mcmpeq.b r4, r63, r6
77	bnei/u r6, 0, tr1 // shortstring
78	ldx.q r0, r21, r5
79	stlo.q r0, -8, r4
80	sthi.q r0, -1, r4
81	mcmpeq.b r5, r63, r6
82	beqi/l r6, 0, tr0 // loop
83
84	add r5, r63, r4
85	addi r0, 8, r0
86	blink tr1, r63 // shortstring
87
88#else /* ! __SHMEDIA__, i.e. SH 1..4 / SHcompact */
89
90#ifdef __SH5__
91#define DST r2
92#define SRC r3
93#define TMP r4
94#define RESULT R2
95!        r0,r1,r3,r4: clobbered
96#else
97#define DST r4
98#define SRC r5
99#define TMP r2
100#define RESULT r0
101!        r1-r2,r5: clobbered
102#endif
103	mov     DST,r0
104	or      SRC,r0
105	tst	#3,r0
106	SL(bf, L_setup_char_loop, mov DST,r0)
107	mov.l   @SRC+,r1
108	mov     #0,TMP
109	cmp/str TMP,r1
110	SL(bt, Longword_loop_end, sub SRC,r0)
111	.align  2
112Longword_loop:
113	mov.l   r1,@(r0,SRC)
114	mov.l   @SRC+,r1
115	cmp/str TMP,r1
116	bt      Longword_loop_end
117	mov.l   r1,@(r0,SRC)
118	mov.l   @SRC+,r1
119	cmp/str TMP,r1
120	bf      Longword_loop
121Longword_loop_end:
122	add	#-4,SRC
123	add	#3,r0
124	.align  2
125L_char_loop:
126	mov.b	@SRC+,r1
127L_char_loop_start:
128	tst	r1,r1
129	SL(bf, L_char_loop, mov.b r1,@(r0,SRC))
130	rts
131	mov DST,RESULT
132L_setup_char_loop:
133	mov.b	@SRC+,r1
134	bra L_char_loop_start
135	sub SRC,r0
136#endif /* ! __SHMEDIA__ */
137