git subrepo pull --force deps/lightrec
[pcsx_rearmed.git] / deps / lightning / check / callee.c
1 #include <lightning.h>
2 #include <stdio.h>
3
4 //#define DEBUG 1
5
6 jit_state_t     *_jit;
7
8 int
9 main(int argc, char *argv[])
10 {
11     int                   off;
12     void                (*code)(void);
13     jit_node_t           *jmp, *inner, *fail;
14
15     init_jit(argv[0]);
16     _jit = jit_new_state();
17
18     jmp = jit_jmpi();
19
20     /* Create a simple function that changes all available JIT_Vx */
21     inner = jit_label();
22     jit_prolog();
23     for (off = JIT_R_NUM - 1; off >= 0; --off) {
24         if (jit_callee_save_p(JIT_R(off)))
25             jit_movi(JIT_R(off), (off + 1) * 2);
26     }
27     for (off = JIT_V_NUM - 1; off >= 0; --off)
28         jit_movi(JIT_V(off), -(off + 1));
29     /* If fprs are callee save, also test them */
30     for (off = JIT_F_NUM - 1; off >= 0; --off) {
31         if (jit_callee_save_p(JIT_F(off)))
32             jit_movi_d(JIT_F(off), -(off + 1));
33     }
34     /* Add some noise as there might be some error in the stack frame and
35      * a standard C function might clobber registers saved in the stack */
36     jit_prepare();
37     jit_pushargi((jit_word_t)stderr);
38     jit_pushargi((jit_word_t)
39                  "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "
40                  "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n");
41     jit_ellipsis();
42     jit_pushargi(1);
43     jit_pushargi(2);
44     jit_pushargi(3);
45     jit_pushargi(4);
46     jit_pushargi(5);
47     jit_pushargi(6);
48     jit_pushargi(7);
49     jit_pushargi(8);
50     jit_pushargi(9);
51     jit_pushargi(10);
52     jit_pushargi(11);
53     jit_pushargi(12);
54     jit_pushargi(13);
55     jit_pushargi(14);
56     jit_pushargi(15);
57     jit_pushargi(16);
58     jit_pushargi(17);
59     jit_pushargi(18);
60     jit_pushargi(19);
61     jit_pushargi(20);
62     jit_pushargi_d(1);
63     jit_pushargi_d(2);
64     jit_pushargi_d(3);
65     jit_pushargi_d(4);
66     jit_pushargi_d(5);
67     jit_pushargi_d(6);
68     jit_pushargi_d(7);
69     jit_pushargi_d(8);
70     jit_pushargi_d(9);
71     jit_pushargi_d(10);
72     jit_pushargi_d(11);
73     jit_pushargi_d(12);
74     jit_pushargi_d(13);
75     jit_pushargi_d(14);
76     jit_pushargi_d(15);
77     jit_pushargi_d(16);
78     jit_pushargi_d(17);
79     jit_pushargi_d(18);
80     jit_pushargi_d(19);
81     jit_pushargi_d(20);
82     jit_finishi(fprintf);
83     jit_ret();
84     jit_epilog();
85
86     jit_patch(jmp);
87     jit_prolog();
88
89     for (off = JIT_R_NUM - 1; off >= 0; --off) {
90         if (jit_callee_save_p(JIT_R(off)))
91             jit_movi(JIT_R(off), -(off + 1) * 2);
92     }
93     for (off = JIT_V_NUM - 1; off >= 0; --off)
94         jit_movi(JIT_V(off), 0x7fffffff - (off + 1));
95     /* If fprs are callee save, also test them */
96     for (off = JIT_F_NUM - 1; off >= 0; --off) {
97         if (jit_callee_save_p(JIT_F(off)))
98             jit_movi_d(JIT_F(off), 0x7fffffff - (off + 1));
99     }
100     jit_patch_at(jit_calli(NULL), inner);
101
102     /* Now validate no register has been clobbered */
103     fail = jit_forward();
104
105     for (off = JIT_R_NUM - 1; off >= 0; --off) {
106         if (jit_callee_save_p(JIT_R(off))) {
107 #if DEBUG
108             jmp = jit_beqi(JIT_R(off), -(off + 1) * 2);
109             jit_calli(abort);
110             jit_patch(jmp);
111 #else
112             jit_patch_at(jit_bnei(JIT_R(off), -(off + 1) * 2), fail);
113 #endif
114         }
115     }
116     for (off = JIT_V_NUM - 1; off >= 0; --off) {
117 #if DEBUG
118         jmp = jit_beqi(JIT_V(off), 0x7fffffff - (off + 1));
119         jit_calli(abort);
120         jit_patch(jmp);
121 #else
122         jit_patch_at(jit_bnei(JIT_V(off), 0x7fffffff - (off + 1)), fail);
123 #endif
124     }
125     for (off = JIT_F_NUM - 1; off >= 0; --off) {
126         if (jit_callee_save_p(JIT_F(off))) {
127 #if DEBUG
128             jmp = jit_beqi_d(JIT_F(off), 0x7fffffff - (off + 1));
129             jit_calli(abort);
130             jit_patch(jmp);
131 #else
132             jit_patch_at(jit_bnei_d(JIT_F(off), 0x7fffffff - (off + 1)), fail);
133 #endif
134         }
135     }
136 #if !DEBUG
137     /* Done if passed all tests */
138     jmp = jit_jmpi();
139     /* Where to land if there was any register clobber */
140     jit_link(fail);
141     jit_calli(abort);
142     /* done */
143     jit_patch(jmp);
144 #endif
145
146     jit_ret();
147     jit_epilog();
148
149     code = jit_emit();
150     jit_clear_state();
151
152     (*code)();
153
154     jit_destroy_state();
155     finish_jit();
156     return (0);
157 }