1/*
2Copyright (c) 2014, Hesham ALMatary
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
19ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
26
27#include <picolibc.h>
28
29.align  4
30.global setjmp
31.type   setjmp,@function
32setjmp:
33
34	l.sw	0(r3), r1
35	l.sw	4(r3), r2
36	/* Skip r3-r8 as they are not preserved across function calls */
37	l.sw	8(r3), r9
38	/* Skip r10 as it's preserved to be used by TLS */
39	/* Skip r11, setjmp always set it to 0 */
40	/* The following set if registers are preserved across function calls */
41	l.sw	12(r3), r14
42	l.sw	16(r3), r16
43	l.sw	20(r3), r18
44	l.sw	24(r3), r20
45	l.sw	28(r3), r22
46	l.sw	32(r3), r24
47	l.sw	36(r3), r26
48	l.sw	40(r3), r28
49	l.sw	44(r3), r30
50	/* Save Status Register */
51	l.mfspr	r13, r0, 17
52	l.sw	48(r3), r13
53/* Set result register to 0 and jump */
54// Different cases for optional delay slot
55#if defined(__OR1K_NODELAY__)
56	l.addi	r11, r0, 0
57	l.jr	r9
58#elif defined(__OR1K_DELAY__)
59	l.jr	r9
60	l.addi	r11, r0, 0
61#else
62	l.addi	r11, r0, 0
63	l.jr	r9
64	l.nop
65#endif
66
67.align  4
68.global longjmp
69.type   longjmp,@function
70longjmp:
71
72  /* If the second argument to longjmp is zero, set return address to 1,
73  otherwise set it to the value of the second argument */
74	l.addi	r11, r0, 1
75	l.sfeq	r4, r0
76	l.bf	1f
77	l.nop
78	l.addi	r11, r4, 0
79
80  /* Load status register */
811:
82	l.lwz	r15, 48(r3)
83	l.mtspr	r0, r15, 17
84
85	l.lwz	r1, 0(r3)
86	l.lwz	r2, 4(r3)
87	/* Skip r3-r8 as they are not preserved across function calls */
88	l.lwz	r9, 8(r3)
89	/* Skip r11 as it's always set by longjmp */
90	l.lwz	r14, 12(r3)
91	l.lwz	r16, 16(r3)
92	l.lwz	r18, 20(r3)
93	l.lwz	r20, 24(r3)
94	l.lwz	r22, 28(r3)
95	l.lwz	r24, 32(r3)
96	l.lwz	r26, 36(r3)
97	l.lwz	r28, 40(r3)
98
99// Different cases for optional delay slot
100#if defined(__OR1K_NODELAY__)
101	l.lwz	r30, 44(r3)
102	l.jr	r9
103#elif defined(__OR1K_DELAY__)
104	l.jr	r9
105	l.lwz	r30, 44(r3)
106#else
107	l.lwz	r30, 44(r3)
108	l.jr	r9
109	l.nop
110#endif
111