git subrepo pull --force deps/lightrec
[pcsx_rearmed.git] / deps / lightning / check / nodata.c
CommitLineData
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
24jit_uint8_t *data;
25jit_state_t *_jit;
26jit_word_t data_length;
27jit_word_t note_length;
28#if defined(__sgi)
29int mmap_fd;
30#endif
31void (*function)(void);
32
33void
34gencode(jit_word_t flags)
35{
36 jit_word_t offset;
37 jit_word_t length;
38
39 _jit = jit_new_state();
40
41 jit_name("main");
42 jit_prolog();
43 jit_prepare();
44 jit_pushargi((jit_word_t)"%f\n");
45 jit_ellipsis();
46 jit_pushargi_d(1.5);
47 jit_finishi(printf);
48 jit_note("nodata.c", __LINE__);
49
50 /* call to jit_realize() is only required when using an alternate
51 * code buffer. Note that not using mmap'ed memory may not work
52 * on several ports and/or operating system versions */
53 jit_realize();
54
55 if (jit_get_data(&data_length, &note_length) != NULL)
56 abort();
57
58 length = 0;
59 if (!(flags & JIT_DISABLE_DATA))
60 length += data_length;
61 if (!(flags & JIT_DISABLE_NOTE))
62 length += note_length;
63
64 /* check that a too small buffer fails */
65 if (flags)
66 jit_set_data(length ? data : NULL, length, flags);
67
68 /* and calling again with enough space works */
69 offset = (length + 7) & -8;
70 function = jit_emit();
71 if (function == NULL)
72 abort();
73
74 jit_clear_state();
75 (*function)();
76 jit_destroy_state();
77}
78
79int
80main(int argc, char *argv[])
81{
82#if defined(__sgi)
83 mmap_fd = open("/dev/zero", O_RDWR);
84#endif
85
86 data = mmap(NULL, 4096,
87 PROT_READ | PROT_WRITE,
88 MAP_PRIVATE | MAP_ANON, mmap_fd, 0);
89 assert(data != MAP_FAILED);
90#if defined(__sgi)
91 close(mmap_fd);
92#endif
93
94 init_jit(argv[0]);
95
96 gencode(0);
97 gencode(JIT_DISABLE_DATA);
98 gencode(JIT_DISABLE_NOTE);
99 gencode(JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
100
101 finish_jit();
102
103 munmap(data, 4096);
104
105 return (0);
106}