Commit | Line | Data |
---|---|---|
79bfeef6 PC |
1 | /* |
2 | * Simple test for x86_64 rip relative access that can also be useful | |
3 | * on other ports when data is close to instruction pointer. | |
4 | */ | |
5 | ||
6 | #include <lightning.h> | |
7 | #include <stdio.h> | |
8 | #include <assert.h> | |
9 | #include <sys/mman.h> | |
10 | #if defined(__sgi) | |
11 | # include <fcntl.h> | |
12 | #endif | |
13 | #include <unistd.h> | |
14 | ||
15 | #ifndef MAP_ANON | |
16 | # define MAP_ANON MAP_ANONYMOUS | |
17 | # ifndef MAP_ANONYMOUS | |
18 | # define MAP_ANONYMOUS 0 | |
19 | # endif | |
20 | #endif | |
21 | ||
22 | #if !defined(__sgi) | |
23 | #define mmap_fd -1 | |
24 | #endif | |
25 | ||
26 | int | |
27 | main(int argc, char *argv[]) | |
28 | { | |
29 | jit_uint8_t *ptr; | |
30 | jit_state_t *_jit; | |
31 | jit_word_t length; | |
32 | #if defined(__sgi) | |
33 | int mmap_fd; | |
34 | #endif | |
35 | void (*function)(void); | |
36 | int mmap_prot, mmap_flags, result, pagesize; | |
37 | int mult; | |
38 | ||
39 | #if defined(__ia64__) | |
40 | mult = 8; | |
41 | #else | |
42 | mult = 2; | |
43 | #endif | |
44 | pagesize = sysconf(_SC_PAGESIZE); | |
45 | if (pagesize < 4096) | |
46 | pagesize = 4096; | |
47 | ||
48 | #if defined(__sgi) | |
49 | mmap_fd = open("/dev/zero", O_RDWR); | |
50 | #endif | |
51 | ||
52 | mmap_prot = PROT_READ | PROT_WRITE; | |
53 | #if !(__OpenBSD__ || __APPLE__) | |
54 | mmap_prot |= PROT_EXEC; | |
55 | #endif | |
56 | #if __NetBSD__ | |
57 | mmap_prot = PROT_MPROTECT(mmap_prot); | |
58 | mmap_flags = 0; | |
59 | #else | |
60 | mmap_flags = MAP_PRIVATE; | |
61 | #endif | |
62 | mmap_flags |= MAP_ANON; | |
63 | ptr = mmap(NULL, pagesize * mult, mmap_prot, mmap_flags, mmap_fd, 0); | |
64 | assert(ptr != MAP_FAILED); | |
65 | #if defined(__sgi) | |
66 | close(mmap_fd); | |
67 | #endif | |
68 | ||
69 | init_jit(argv[0]); | |
70 | _jit = jit_new_state(); | |
71 | ||
72 | jit_prolog(); | |
73 | jit_movi(JIT_R0, 'c'); | |
74 | jit_sti_c(ptr + 0, JIT_R0); | |
75 | jit_movi(JIT_R0, 'C'); | |
76 | jit_sti_c(ptr + 1, JIT_R0); | |
77 | ||
78 | jit_movi(JIT_R0, 's'); | |
79 | jit_sti_s(ptr + 2, JIT_R0); | |
80 | jit_movi(JIT_R0, 'S'); | |
81 | jit_sti_s(ptr + 4, JIT_R0); | |
82 | ||
83 | jit_movi(JIT_R0, 'i'); | |
84 | jit_sti_i(ptr + 8, JIT_R0); | |
85 | #if __WORDSIZE == 64 | |
86 | jit_movi(JIT_R0, 'I'); | |
87 | jit_sti_i(ptr + 12, JIT_R0); | |
88 | ||
89 | jit_movi(JIT_R0, 'l'); | |
90 | jit_sti_l(ptr + 16, JIT_R0); | |
91 | #endif | |
92 | jit_movi_f(JIT_F0, 1.0); | |
93 | jit_sti_f(ptr + 24, JIT_F0); | |
94 | jit_movi_d(JIT_F0, 2.0); | |
95 | jit_sti_d(ptr + 32, JIT_F0); | |
96 | ||
97 | jit_ldi_c(JIT_R0, ptr + 0); | |
98 | jit_ldi_s(JIT_R1, ptr + 2); | |
99 | jit_ldi_i(JIT_R2, ptr + 8); | |
100 | #if __WORDSIZE == 64 | |
101 | jit_ldi_l(JIT_V0, ptr + 16); | |
102 | #endif | |
103 | jit_prepare(); | |
104 | #if __WORDSIZE == 64 | |
105 | jit_pushargi((jit_word_t)"%c %c %c %c\n"); | |
106 | #else | |
107 | jit_pushargi((jit_word_t)"%c %c %c l\n"); | |
108 | #endif | |
109 | jit_ellipsis(); | |
110 | jit_pushargr(JIT_R0); | |
111 | jit_pushargr(JIT_R1); | |
112 | jit_pushargr(JIT_R2); | |
113 | #if __WORDSIZE == 64 | |
114 | jit_pushargr(JIT_V0); | |
115 | #endif | |
116 | jit_finishi(printf); | |
117 | ||
118 | jit_ldi_uc(JIT_R0, ptr + 1); | |
119 | jit_ldi_us(JIT_R1, ptr + 4); | |
120 | #if __WORDSIZE == 64 | |
121 | jit_ldi_ui(JIT_R2, ptr + 12); | |
122 | #endif | |
123 | jit_prepare(); | |
124 | #if __WORDSIZE == 64 | |
125 | jit_pushargi((jit_word_t)"%c %c %c\n"); | |
126 | #else | |
127 | jit_pushargi((jit_word_t)"%c %c I\n"); | |
128 | #endif | |
129 | jit_ellipsis(); | |
130 | jit_pushargr(JIT_R0); | |
131 | jit_pushargr(JIT_R1); | |
132 | #if __WORDSIZE == 64 | |
133 | jit_pushargr(JIT_R2); | |
134 | #endif | |
135 | jit_finishi(printf); | |
136 | ||
137 | jit_ldi_f(JIT_F0, ptr + 24); | |
138 | jit_extr_f_d(JIT_F0, JIT_F0); | |
139 | jit_ldi_d(JIT_F1, ptr + 32); | |
140 | ||
141 | jit_prepare(); | |
142 | jit_pushargi((jit_word_t)"%.1f %.1f\n"); | |
143 | jit_ellipsis(); | |
144 | jit_pushargr_d(JIT_F0); | |
145 | jit_pushargr_d(JIT_F1); | |
146 | jit_finishi(printf); | |
147 | ||
148 | jit_realize(); | |
149 | ||
150 | jit_set_code(ptr + pagesize, pagesize * (mult - 1)); | |
151 | ||
152 | #if __NetBSD__ || __OpenBSD__ || __APPLE__ | |
153 | result = mprotect(ptr, pagesize, PROT_READ | PROT_WRITE); | |
154 | assert(result == 0); | |
155 | #endif | |
156 | function = jit_emit(); | |
157 | if (function == NULL) | |
158 | abort(); | |
159 | ||
160 | //jit_disassemble(); | |
161 | jit_clear_state(); | |
162 | #if __NetBSD__ || __OpenBSD__ || __APPLE__ | |
163 | result = mprotect(ptr + pagesize, pagesize, PROT_READ | PROT_EXEC); | |
164 | assert(result == 0); | |
165 | #endif | |
166 | (*function)(); | |
167 | jit_destroy_state(); | |
168 | finish_jit(); | |
169 | ||
170 | munmap(ptr, pagesize * mult); | |
171 | ||
172 | return (0); | |
173 | } |