Commit | Line | Data |
---|---|---|
4a71579b PC |
1 | /* |
2 | * Simple test of using an alternate buffer for the code. | |
3 | */ | |
4 | ||
5 | #include <lightning.h> | |
6 | #include <stdio.h> | |
7 | #include <assert.h> | |
8 | #include <sys/mman.h> | |
9 | #if defined(__sgi) | |
10 | # include <fcntl.h> | |
11 | #endif | |
12 | ||
13 | #ifndef MAP_ANON | |
14 | # define MAP_ANON MAP_ANONYMOUS | |
15 | # ifndef MAP_ANONYMOUS | |
16 | # define MAP_ANONYMOUS 0 | |
17 | # endif | |
18 | #endif | |
19 | ||
20 | #if !defined(__sgi) | |
21 | #define mmap_fd -1 | |
22 | #endif | |
23 | ||
24 | int | |
25 | main(int argc, char *argv[]) | |
26 | { | |
27 | jit_uint8_t *ptr; | |
28 | jit_state_t *_jit; | |
29 | jit_word_t length; | |
30 | #if defined(__sgi) | |
31 | int mmap_fd; | |
32 | #endif | |
33 | void (*function)(void); | |
34 | ||
35 | #if defined(__sgi) | |
36 | mmap_fd = open("/dev/zero", O_RDWR); | |
37 | #endif | |
38 | ||
39 | ptr = mmap(NULL, 1024 * 1024, | |
40 | PROT_EXEC | PROT_READ | PROT_WRITE, | |
41 | MAP_PRIVATE | MAP_ANON, mmap_fd, 0); | |
42 | assert(ptr != MAP_FAILED); | |
43 | #if defined(__sgi) | |
44 | close(mmap_fd); | |
45 | #endif | |
46 | ||
47 | init_jit(argv[0]); | |
48 | _jit = jit_new_state(); | |
49 | ||
50 | jit_prolog(); | |
51 | jit_prepare(); | |
52 | jit_pushargi((jit_word_t)"%s\n"); | |
53 | jit_ellipsis(); | |
54 | jit_pushargi((jit_word_t)"ok"); | |
55 | jit_finishi(printf); | |
56 | ||
57 | /* call to jit_realize() is only required when using an alternate | |
58 | * code buffer. Note that not using mmap'ed memory may not work | |
59 | * on several ports and/or operating system versions */ | |
60 | jit_realize(); | |
61 | ||
62 | length = 0; | |
63 | if (jit_get_code(&length) != NULL) | |
64 | abort(); | |
65 | ||
66 | if (length <= 4) | |
67 | abort(); | |
68 | ||
69 | /* check that a too small buffer fails */ | |
70 | jit_set_code(ptr, 4); | |
71 | function = jit_emit(); | |
72 | if (function != NULL) | |
73 | abort(); | |
74 | ||
75 | /* and calling again with enough space works */ | |
76 | jit_set_code(ptr, 1024 * 1024); | |
77 | function = jit_emit(); | |
78 | if (function == NULL) | |
79 | abort(); | |
80 | ||
81 | jit_clear_state(); | |
82 | (*function)(); | |
83 | jit_destroy_state(); | |
84 | finish_jit(); | |
85 | ||
86 | munmap(ptr, 1024 * 1024); | |
87 | ||
88 | return (0); | |
89 | } |