1#if __WORDSIZE == 32
2#  define SIZE	4
3#else
4#  define SIZE	8
5#endif
6.data	8192
7fmt:
8.c	"xfibs(%d) = %d\n"
9/* Simulate a language specific stack */
10.align	SIZE
11top:
12/* Top, or base of the stack */
13.size	SIZE
14stk:
15.size	8160
16
17.code
18	jmpi main
19
20/* Usually a trampoline is created before the code that uses it, but
21 * for testing purposes it is not required.
22 * In this test case, it would mean "main" would be converted in a
23 * different jit_state_t to native code, before xfibs was know.
24 */
25	name xfibs
26xfibs:
27	/* return address is in %r0 */
28	/* argument and return value in %v0 */
29	prolog
30	tramp 64
31	blti_u out %v0 2
32	subi %v1 %v0 1		/* V1 = N-1 */
33	subi %v2 %v0 2		/* V1 = N-2 */
34
35	/* save return address */
36	ldi %r1 top
37	stxi $(SIZE * 0) %r1 %r0
38	/* save operands */
39	stxi $(SIZE * 1) %r1 %v0
40	stxi $(SIZE * 2) %r1 %v1
41	stxi $(SIZE * 3) %r1 %v2
42	/* adjust "language" stack */
43	addi %r1 %r1 $(SIZE * 4)
44	sti top %r1
45
46	/* return address */
47	movi %r0 ret1
48	/* argument */
49	movr %v0 %v1
50	/* indirect goto */
51	jmpi xfibs
52ret1:
53	movr %v1 %v0		/* V1 = rfibs(N-1) */
54	/* save V1 */
55	ldi %r1 top
56	stxi $(-SIZE * 2) %r1 %v1
57
58	/* reload V2 */
59	ldxi %v2 %r1 $(-SIZE * 1)
60
61	/* return address */
62	movi %r0 ret2
63	/* argument */
64	movr %v0 %v2
65	/* indirect goto */
66	jmpi xfibs
67ret2:
68	movr %v2 %v0		/* V2 = rfibs(N-2) */
69
70	/* reload return address */
71	ldi %r1 top
72	subi %r1 %r1 $(SIZE * 4)
73	ldxi %r0 %r1 $(SIZE * 0)
74	/* reload operands */
75	ldxi %v0 %r1 $(SIZE * 1)
76	ldxi %v1 %r1 $(SIZE * 2)
77	/* V2 already loaded */
78	/* update "language" stack */
79	sti top %r1
80
81	addi %v1 %v1 1
82	addr %v0 %v1 %v2
83	jmpr %r0
84out:
85	movi %v0 1
86	jmpr %r0
87	epilog
88
89	name main
90main:
91	prolog
92	frame 64
93
94	/* Initialize language stack */
95	movi %r0 stk
96	sti top %r0
97
98	/* return address */
99	movi %r0 done
100	/* argument */
101	movi %v0 32
102	jmpi xfibs
103done:
104	prepare
105		pushargi fmt
106		ellipsis
107		pushargi 32
108		pushargr %v0
109	finishi @printf
110	ret
111	epilog
112