1/* libgcc routines for RL78
2   Copyright (C) 2011-2020 Free Software Foundation, Inc.
3   Contributed by Red Hat.
4
5   This file is part of GCC.
6
7   GCC is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published
9   by the Free Software Foundation; either version 3, or (at your
10   option) any later version.
11
12   GCC is distributed in the hope that it will be useful, but WITHOUT
13   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15   License for more details.
16
17   Under Section 7 of GPL version 3, you are granted additional
18   permissions described in the GCC Runtime Library Exception, version
19   3.1, as published by the Free Software Foundation.
20
21   You should have received a copy of the GNU General Public License and
22   a copy of the GCC Runtime Library Exception along with this program;
23   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24   <http://www.gnu.org/licenses/>.  */
25
26/* RL78 Trampoline support
27
28  Since the RL78's RAM is not in the first 64k, we cannot "just" use a
29  function pointer to point to a trampoline on the stack.  So, we
30  create N fixed trampolines that read from an array, and allocate
31  them as needed.
32
33*/
34
35#include "vregs.h"
36
37	.data
38	.p2align	1
39trampoline_array:
40
41	.macro stub n
42
43	.text
44trampoline_\n:
45        .type   trampoline_\n, @function
46	movw	ax, !trampoline_chain_\n
47	movw	r14, ax
48	movw	ax, !trampoline_addr_\n
49	br	ax
50	.size	trampoline_\n, .-trampoline_\n
51
52	.data
53trampoline_frame_\n:
54	.short	0
55trampoline_stub_\n:
56	.short	trampoline_\n
57trampoline_chain_\n:
58	.short	0
59trampoline_addr_\n:
60	.short	0
61
62#define TO_FRAME 0
63#define TO_STUB  2
64#define TO_CHAIN 4
65#define TO_ADDR  6
66#define TO_SIZE  8
67
68	.endm
69
70	stub	0
71	stub	1
72	stub	2
73	stub	3
74	stub	4
75	stub	5
76
77trampoline_array_end:
78
79/* Given the function pointer in R8 and the static chain
80   pointer in R10, allocate a trampoline and return its address in
81   R8. */
82
83START_FUNC ___trampoline_init
84	movw	hl, #trampoline_array
85
861:	movw	ax, [hl + TO_ADDR]
87	cmpw	ax, #0
88	bz	$2f
89
90	movw	ax, hl
91	addw	ax, #TO_SIZE
92	movw	hl, ax
93	cmpw	ax, #trampoline_array_end
94	bnz	$1b
95	brk			; no more slots?
96
972:	movw	ax, r8
98	movw	[hl + TO_ADDR], ax
99	movw	ax, r10
100	movw	[hl + TO_CHAIN], ax
101	movw	ax, sp
102	movw	[hl + TO_FRAME], ax
103
104	movw	ax, [hl + TO_STUB]
105	movw	r8, ax
106	ret
107END_FUNC ___trampoline_init
108
109
110START_FUNC ___trampoline_uninit
111	movw	hl, #trampoline_array
112	movw	ax, sp
113	movw	bc, ax
114
1151:	movw	ax, [hl + TO_FRAME]
116	cmpw	ax, bc
117	bc	$2f
118
119	clrw	ax
120	movw	[hl + TO_ADDR], ax
121
1222:	movw	ax, hl
123	addw	ax, #TO_SIZE
124	movw	hl, ax
125	cmpw	ax, #trampoline_array_end
126	bnz	$1b
127
128	ret
129END_FUNC ___trampoline_uninit
130