Remove more disassembler bogosity
[sbcl.git] / src / runtime / runtime.h
blob68c1a96478d8e80b83e75f7a77befa22cc65b19a
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
12 /* FIXME: Aren't symbols with underscore prefixes supposed to be
13 * reserved for system libraries? Perhaps rename stuff like this
14 * to names like INCLUDED_SBCL_RUNTIME_H. */
15 #ifndef _SBCL_RUNTIME_H_
16 #define _SBCL_RUNTIME_H_
18 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
19 # include "pthreads_win32.h"
20 #else
21 # include <signal.h>
22 # ifdef LISP_FEATURE_SB_THREAD
23 # include <pthread.h>
24 # endif
25 #endif
27 #include <stdint.h>
29 #if defined(LISP_FEATURE_SB_THREAD)
30 #define thread_self() pthread_self()
31 #define thread_kill pthread_kill
33 #ifdef LISP_FEATURE_WIN32
34 #define thread_sigmask _sbcl_pthread_sigmask
35 #else
36 #define thread_sigmask pthread_sigmask
37 #endif
39 #define thread_mutex_lock(l) pthread_mutex_lock(l)
40 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
41 #else
42 #define thread_self() 0
43 #define thread_kill kill_safely
44 #define thread_sigmask sigprocmask
45 #define thread_mutex_lock(l) 0
46 #define thread_mutex_unlock(l) 0
47 #endif
49 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
50 void os_preinit();
51 #endif
53 void os_link_runtime();
55 #if defined(LISP_FEATURE_SB_SAFEPOINT)
57 typedef enum {
58 GC_NONE=0,
59 GC_FLIGHT,
60 GC_MESSAGE,
61 GC_INVOKED,
62 GC_QUIET,
63 GC_SETTLED,
64 GC_COLLECT,
65 GC_NPHASES
66 } gc_phase_t;
68 void map_gc_page();
69 void unmap_gc_page();
70 int check_pending_interrupts();
71 void gc_state_lock();
72 void gc_state_wait(gc_phase_t);
73 void gc_state_unlock();
75 #endif
78 * The next few defines serve as configuration -- edit them inline if
79 * you are a developer and want to affect FSHOW behaviour.
82 /* Block blockable interrupts for each SHOW, if not 0.
83 * (On Windows, this setting has no effect.)
85 * In principle, this is a "configuration option", but I am not aware of
86 * any reason why or when it would be advantageous to disable it. */
87 #define QSHOW_SIGNAL_SAFE 1
89 /* Enable extra-verbose low-level debugging output for signals? (You
90 * probably don't want this unless you're trying to debug very early
91 * cold boot on a new machine, or one where you've just messed up
92 * signal handling.)
94 * Note: It may be that doing this is fundamentally unsound, since it
95 * causes output from signal handlers, and the i/o libraries aren't
96 * necessarily reentrant. But it can still be very convenient for
97 * figuring out what's going on when you have a signal handling
98 * problem.
100 * Possible values are:
101 * 0 -- Never show signal-related output. There is absolutely no
102 * run-time overhead from FSHOW_SIGNAL in this case.
104 * 1 -- (recommended)
105 * Show signal-related output only if selected at run-time
106 * (otherwise almost no run-time overhead).
108 * 2 -- Unconditionally show signal-related output.
109 * Very significant overhead.
111 * For reasons of tradition, we default to 0 on POSIX and 1 on Windows
112 * through :SB-QSHOW.
114 * With option 1, set up environment variable SBCL_DYNDEBUG to include
115 * "fshow" or "fshow_signal" before starting SBCL to enable output.
117 * There is no particular advantage to option 2 except that you do not
118 * need to set environment variables in this case.
120 #ifdef LISP_FEATURE_SB_QSHOW
121 # define QSHOW_SIGNALS 1
122 #else
123 # define QSHOW_SIGNALS 0
124 #endif
126 /* Enable low-level debugging output, if not zero. Defaults to enabled
127 * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 or 2 if you want
128 * low-level debugging output but not the whole signal mess. */
129 #define QSHOW QSHOW_SIGNALS
132 * Configuration options end here -- the following defines do not
133 * generally need customization.
136 #define odxprint(topic, fmt, ...) \
137 do \
138 if (dyndebug_config.dyndebug_##topic) \
139 odxprint_fun(fmt "\n", ##__VA_ARGS__); \
140 while (0)
142 void odxprint_fun(const char *fmt, ...);
143 void fshow_fun(void *ignored, const char *fmt, ...);
145 /* Flags defined in a structure to avoid code duplication between
146 * declaration and definition. */
147 extern struct dyndebug_config {
148 int dyndebug_fshow;
149 int dyndebug_fshow_signal;
150 int dyndebug_gencgc_verbose;
151 int dyndebug_safepoints;
152 int dyndebug_seh;
153 int dyndebug_misc;
154 int dyndebug_pagefaults;
155 int dyndebug_backtrace_when_lost;
156 int dyndebug_sleep_when_lost;
157 int dyndebug_io;
158 int dyndebug_runtime_link;
159 } dyndebug_config;
161 #ifdef LISP_FEATURE_GENCGC
162 extern int gencgc_verbose;
163 #endif
165 void dyndebug_init(void);
167 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
169 extern sigset_t blockable_sigset;
171 #define QSHOW_BLOCK \
172 sigset_t oldset; \
173 thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset)
174 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0)
175 #else
176 #define QSHOW_BLOCK
177 #define QSHOW_UNBLOCK
178 #endif
180 /* The following macros duplicate the expansion of odxprint, because the
181 * extra level of parentheses around `args' prevents us from
182 * implementing FSHOW in terms of odxprint directly. (They also differ
183 * in a newline.)
186 #if QSHOW
187 # define FSHOW(args) \
188 do if (dyndebug_config.dyndebug_fshow) fshow_fun args; while (0)
189 # define SHOW(string) FSHOW((stderr, "/%s\n", string))
190 #else
191 # define FSHOW(args)
192 # define SHOW(string)
193 #endif
195 #if QSHOW_SIGNALS
196 # define FSHOW_SIGNAL(args) \
197 do if (dyndebug_config.dyndebug_fshow_signal) fshow_fun args; while (0)
198 #else
199 # define FSHOW_SIGNAL(args)
200 #endif
202 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
203 * in practice the "foo int" definitions work for all the machines
204 * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
205 * other non-32-bit machine we'll probably need real machine-dependent
206 * and OS-dependent definitions again. */
207 /* even on alpha, int happens to be 4 bytes. long is longer. */
208 /* FIXME: these names really shouldn't reflect their length and this
209 is not quite right for some of the FFI stuff */
210 #if defined(LISP_FEATURE_WIN32)&&defined(LISP_FEATURE_X86_64)
211 typedef unsigned long long u64;
212 typedef signed long long s64;
213 #else
214 typedef unsigned long u64;
215 typedef signed long s64;
216 #endif
217 typedef unsigned int u32;
218 typedef signed int s32;
220 /* this is an integral type the same length as a machine pointer */
221 typedef uintptr_t pointer_sized_uint_t;
223 #ifdef _WIN64
224 #define AMD64_SYSV_ABI __attribute__((sysv_abi))
225 #else
226 #define AMD64_SYSV_ABI
227 #endif
229 #include <sys/types.h>
231 #if defined(LISP_FEATURE_SB_THREAD)
232 typedef pthread_t os_thread_t;
233 #else
234 typedef pid_t os_thread_t;
235 #endif
237 typedef uintptr_t uword_t;
238 typedef intptr_t sword_t;
240 /* FIXME: we do things this way because of the alpha32 port. once
241 alpha64 has arrived, all this nastiness can go away */
242 #if 64 == N_WORD_BITS
243 #define LOW_WORD(c) ((pointer_sized_uint_t)c)
244 #define OBJ_FMTX "lx"
245 typedef uintptr_t lispobj;
246 #else
247 #define OBJ_FMTX "x"
248 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
249 /* fake it on alpha32 */
250 typedef unsigned int lispobj;
251 #endif
253 static inline int
254 lowtag_of(lispobj obj)
256 return obj & LOWTAG_MASK;
259 static inline int
260 widetag_of(lispobj obj)
262 return obj & WIDETAG_MASK;
265 static inline uword_t
266 HeaderValue(lispobj obj)
268 return obj >> N_WIDETAG_BITS;
271 static inline uword_t instance_length(lispobj header)
273 return (header >> N_WIDETAG_BITS);
275 static inline lispobj instance_layout(lispobj* instance_ptr) // native ptr
277 return instance_ptr[1]; // the word following the header is the layout
280 static inline struct cons *
281 CONS(lispobj obj)
283 return (struct cons *)(obj - LIST_POINTER_LOWTAG);
286 static inline struct symbol *
287 SYMBOL(lispobj obj)
289 return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
292 static inline struct fdefn *
293 FDEFN(lispobj obj)
295 return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
298 /* Is the Lisp object obj something with pointer nature (as opposed to
299 * e.g. a fixnum or character or unbound marker)? */
300 static inline int
301 is_lisp_pointer(lispobj obj)
303 #if N_WORD_BITS == 64
304 return (obj & 3) == 3;
305 #else
306 return obj & 1;
307 #endif
310 #include "fixnump.h"
312 /* Is the Lisp object obj something with immediate nature (e.g. a
313 * fixnum or character or unbound marker)? */
314 static inline int
315 is_lisp_immediate(lispobj obj)
317 return (fixnump(obj)
318 || (widetag_of(obj) == CHARACTER_WIDETAG)
319 #if N_WORD_BITS == 64
320 || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
321 #endif
322 || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
325 /* Convert from a lispobj with type bits to a native (ordinary
326 * C/assembly) pointer to the beginning of the object. */
327 static inline lispobj *
328 native_pointer(lispobj obj)
330 return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
333 /* inverse operation: create a suitably tagged lispobj from a native
334 * pointer or integer.*/
335 static inline lispobj
336 make_lispobj(void *o, int low_tag)
338 return LOW_WORD(o) | low_tag;
341 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
342 static inline lispobj
343 make_fixnum(sword_t n)
345 return MAKE_FIXNUM(n);
348 static inline sword_t
349 fixnum_value(lispobj n)
351 return n >> N_FIXNUM_TAG_BITS;
354 static inline uword_t
355 code_header_words(lispobj header) // given header = code->header
357 return HeaderValue(header);
360 static inline sword_t
361 code_instruction_words(lispobj n) // given n = code->code_size
363 /* Convert bytes into words, double-word aligned. */
364 sword_t x = ((n >> N_FIXNUM_TAG_BITS) + LOWTAG_MASK) & ~LOWTAG_MASK;
366 return x >> WORD_SHIFT;
369 #if defined(LISP_FEATURE_WIN32)
370 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
371 * shlobj.h.
373 * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
374 * or get rid of our own boolean type. If the boolean type is only used in
375 * the runtime, and never passed to Lisp, then it doesn't matter which one
376 * we use.
378 #define boolean rpcndr_boolean
379 #include <shlobj.h>
380 #undef boolean
381 #endif
382 typedef int boolean;
384 static inline boolean
385 other_immediate_lowtag_p(lispobj header)
387 /* These lowtags are spaced 4 apart throughout the lowtag space. */
388 return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
391 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
392 * "this function never returns". This is the way that you do it
393 * in GCC later than version 2.5 or so. */
394 #if defined(__GNUC__)
395 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
396 #define never_returns __attribute__ ((noreturn))
397 #else
398 #define never_returns
399 #endif
400 #else
401 #define never_returns
402 #endif
404 extern void *successful_malloc (size_t size);
405 extern char *copied_string (char *string);
407 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
408 # define THREADS_USING_GCSIGNAL 1
409 #endif
411 /* Now that SPARC has precise GENCGC, several places that used to be
412 * #ifdef PCC need adjustment. Clearly, "PPC or SPARC" is as unhelpful
413 * a test as its reverse, "x86 or x86-64". However, the feature
414 * commonly used to differentiate between those two worlds is
415 * C_STACK_IS_CONTROL_STACK, and clearly (or at least in my humble
416 * opinion), at some point we'd like to have precise GC on x86 while
417 * still sharing the C stack, so stack usage ought not imply GC
418 * conservativeness. So let's have a helper feature that makes the code
419 * a bit more future-proof, even if it is itself currently defined in
420 * the naive way: */
421 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
422 # define GENCGC_IS_PRECISE 1
423 #endif
425 void *os_dlsym_default(char *name);
427 #endif /* _SBCL_RUNTIME_H_ */