| 1 |
/* |
| 2 |
* core.c - core kernel interface |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#define LIBGAUCHE_BODY |
| 35 |
#include "gauche.h" |
| 36 |
#include "gauche/priv/configP.h" |
| 37 |
#include "gauche/priv/builtin-syms.h" |
| 38 |
|
| 39 |
/* GC_print_static_roots() is declared in private/gc_priv.h. It is too much |
| 40 |
hassle to include it with other GC internal baggages, so we just declare |
| 41 |
it. It's a private function of GC, so watch out the changes in GC. */ |
| 42 |
extern void GC_print_static_roots(void); |
| 43 |
|
| 44 |
/* |
| 45 |
* out-of-memory handler. this will be called by GC. |
| 46 |
*/ |
| 47 |
|
| 48 |
static void *oom_handler(size_t bytes) |
| 49 |
{ |
| 50 |
Scm_Panic("out of memory (%lu). aborting...", bytes); |
| 51 |
return NULL; /* dummy */ |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* Features list used by cond-expand macro |
| 56 |
*/ |
| 57 |
static struct { |
| 58 |
ScmObj alist; /* list of (feature) or (feature module) */ |
| 59 |
ScmObj dlist; /* list of disabled features */ |
| 60 |
ScmInternalMutex mutex; |
| 61 |
} cond_features = { SCM_NIL, SCM_NIL, SCM_INTERNAL_MUTEX_INITIALIZER }; |
| 62 |
|
| 63 |
/*============================================================= |
| 64 |
* Program initialization |
| 65 |
*/ |
| 66 |
|
| 67 |
extern void Scm__InitModule(void); |
| 68 |
extern void Scm__InitHash(void); |
| 69 |
extern void Scm__InitSymbol(void); |
| 70 |
extern void Scm__InitNumber(void); |
| 71 |
extern void Scm__InitChar(void); |
| 72 |
extern void Scm__InitClass(void); |
| 73 |
extern void Scm__InitMemoTable(void); |
| 74 |
extern void Scm__InitList(void); |
| 75 |
extern void Scm__InitExceptions(void); |
| 76 |
extern void Scm__InitPort(void); |
| 77 |
extern void Scm__InitWrite(void); |
| 78 |
extern void Scm__InitCompaux(void); |
| 79 |
extern void Scm__InitMacro(void); |
| 80 |
extern void Scm__InitLoad(void); |
| 81 |
extern void Scm__InitThreadLocal(void); |
| 82 |
extern void Scm__InitParameter(void); |
| 83 |
extern void Scm__InitProc(void); |
| 84 |
extern void Scm__InitRegexp(void); |
| 85 |
extern void Scm__InitRead(void); |
| 86 |
extern void Scm__InitSignal(void); |
| 87 |
extern void Scm__InitSystem(void); |
| 88 |
extern void Scm__InitVM(void); |
| 89 |
extern void Scm__InitAutoloads(void); |
| 90 |
extern void Scm__InitCollection(void); |
| 91 |
extern void Scm__InitComparator(void); |
| 92 |
extern void Scm__InitExecenv(void); |
| 93 |
extern void Scm__InitLazy(void); |
| 94 |
extern void Scm__InitNative(void); |
| 95 |
extern void Scm__InitNet(void); |
| 96 |
extern void Scm__InitNetAddr(void); |
| 97 |
extern void Scm__InitNetDb(void); |
| 98 |
extern void Scm__InitMutex(void); |
| 99 |
extern void Scm__InitThreads(void); |
| 100 |
|
| 101 |
extern void Scm_Init_libalpha(void); |
| 102 |
extern void Scm_Init_libbool(void); |
| 103 |
extern void Scm_Init_libbox(void); |
| 104 |
extern void Scm_Init_libchar(void); |
| 105 |
extern void Scm_Init_libcode(void); |
| 106 |
extern void Scm_Init_libcmp(void); |
| 107 |
extern void Scm_Init_libdict(void); |
| 108 |
extern void Scm_Init_libeval(void); |
| 109 |
extern void Scm_Init_libexc(void); |
| 110 |
extern void Scm_Init_libfmt(void); |
| 111 |
extern void Scm_Init_libhash(void); |
| 112 |
extern void Scm_Init_libio(void); |
| 113 |
extern void Scm_Init_liblazy(void); |
| 114 |
extern void Scm_Init_liblist(void); |
| 115 |
extern void Scm_Init_libmemo(void); |
| 116 |
extern void Scm_Init_libmisc(void); |
| 117 |
extern void Scm_Init_libmod(void); |
| 118 |
extern void Scm_Init_libnative(void); |
| 119 |
extern void Scm_Init_libnet(void); |
| 120 |
extern void Scm_Init_libnum(void); |
| 121 |
extern void Scm_Init_libobj(void); |
| 122 |
extern void Scm_Init_libproc(void); |
| 123 |
extern void Scm_Init_librx(void); |
| 124 |
extern void Scm_Init_libsrfis(void); |
| 125 |
extern void Scm_Init_libstr(void); |
| 126 |
extern void Scm_Init_libsym(void); |
| 127 |
extern void Scm_Init_libsys(void); |
| 128 |
extern void Scm_Init_libthr(void); |
| 129 |
extern void Scm_Init_libtype(void); |
| 130 |
extern void Scm_Init_libvec(void); |
| 131 |
extern void Scm_Init_libmacbase(void); |
| 132 |
extern void Scm_Init_compile(void); |
| 133 |
extern void Scm_Init_libmacro(void); |
| 134 |
extern void Scm_Init_libparam(void); |
| 135 |
extern void Scm_Init_libomega(void); |
| 136 |
|
| 137 |
extern void Scm__FinishModuleInitialization(void); |
| 138 |
|
| 139 |
static void finalizable(void); |
| 140 |
static void init_cond_features(void); |
| 141 |
|
| 142 |
#ifdef GAUCHE_USE_PTHREADS |
| 143 |
/* a trick to make sure the gc thread object is linked */ |
| 144 |
static int (*ptr_pthread_create)(pthread_t *, |
| 145 |
GC_PTHREAD_CREATE_CONST pthread_attr_t *, |
| 146 |
void *(*)(void *), void * /* arg */) = NULL; |
| 147 |
#endif |
| 148 |
|
| 149 |
/* To track how far we've gone through booting stages. See gauche.h |
| 150 |
This variable is changed before any other threads are created, so no mutex |
| 151 |
is needed. */ |
| 152 |
static ScmRuntimeState runtime_state = SCM_RUNTIME_INITIALIZING; |
| 153 |
|
| 154 |
/* |
| 155 |
* Entry point of initlalizing Gauche runtime |
| 156 |
*/ |
| 157 |
void Scm_Init(const char *signature) |
| 158 |
{ |
| 159 |
if (runtime_state > SCM_RUNTIME_INITIALIZING) return; |
| 160 |
|
| 161 |
int debug_init = FALSE; |
| 162 |
if (Scm_GetEnv("GAUCHE_DEBUG_INITIALIZATION") != NULL) { |
| 163 |
debug_init = TRUE; |
| 164 |
} |
| 165 |
#define CALL_INIT(f) \ |
| 166 |
do { \ |
| 167 |
if (debug_init) fprintf(stderr, "Calling %s...\n", #f); \ |
| 168 |
f(); \ |
| 169 |
} while (0) |
| 170 |
|
| 171 |
/* make sure the main program links the same version of libgauche */ |
| 172 |
if (strcmp(signature, GAUCHE_SIGNATURE) != 0) { |
| 173 |
Scm_Panic("libgauche ABI version mismatch: libgauche %s, expected %s", |
| 174 |
GAUCHE_SIGNATURE, signature); |
| 175 |
} |
| 176 |
|
| 177 |
/* Some platforms require this. It is harmless if GC is |
| 178 |
already initialized, so we call it here just in case. */ |
| 179 |
GC_init(); |
| 180 |
|
| 181 |
/* Set up GC parameters. We need to call finalizers at the safe |
| 182 |
point of VM loop, so we disable auto finalizer invocation, and |
| 183 |
ask GC to call us back when finalizers are queued. */ |
| 184 |
GC_set_oom_fn(oom_handler); |
| 185 |
GC_set_finalize_on_demand(TRUE); |
| 186 |
GC_set_finalizer_notifier(finalizable); |
| 187 |
|
| 188 |
/* Newer bdwgc delays spawning marker threads until the client creates |
| 189 |
first thread. We can take advantage of parallel markers even with |
| 190 |
single-threaded program, so we ask them to go parallel now. */ |
| 191 |
GC_start_mark_threads(); |
| 192 |
|
| 193 |
(void)SCM_INTERNAL_MUTEX_INIT(cond_features.mutex); |
| 194 |
|
| 195 |
/* Initialize components. The order is important, for some components |
| 196 |
rely on the other components to be initialized. */ |
| 197 |
CALL_INIT(Scm__InitThreadLocal); |
| 198 |
CALL_INIT(Scm__InitParameter); |
| 199 |
CALL_INIT(Scm__InitVM); |
| 200 |
CALL_INIT(Scm__InitHash); |
| 201 |
CALL_INIT(Scm__InitSymbol); |
| 202 |
CALL_INIT(Scm__InitModule); |
| 203 |
CALL_INIT(Scm__InitNumber); |
| 204 |
CALL_INIT(Scm__InitChar); |
| 205 |
CALL_INIT(Scm__InitClass); |
| 206 |
CALL_INIT(Scm__InitMemoTable); |
| 207 |
CALL_INIT(Scm__InitList); |
| 208 |
CALL_INIT(Scm__InitCollection); |
| 209 |
CALL_INIT(Scm__InitExceptions); |
| 210 |
CALL_INIT(Scm__InitProc); |
| 211 |
CALL_INIT(Scm__InitPort); |
| 212 |
CALL_INIT(Scm__InitWrite); |
| 213 |
CALL_INIT(Scm__InitMacro); |
| 214 |
CALL_INIT(Scm__InitLoad); |
| 215 |
CALL_INIT(Scm__InitRegexp); |
| 216 |
CALL_INIT(Scm__InitRead); |
| 217 |
CALL_INIT(Scm__InitSignal); |
| 218 |
CALL_INIT(Scm__InitSystem); |
| 219 |
CALL_INIT(Scm__InitComparator); |
| 220 |
CALL_INIT(Scm__InitExecenv); |
| 221 |
CALL_INIT(Scm__InitNative); |
| 222 |
CALL_INIT(Scm__InitNet); |
| 223 |
CALL_INIT(Scm__InitNetAddr); |
| 224 |
CALL_INIT(Scm__InitNetDb); |
| 225 |
CALL_INIT(Scm__InitMutex); |
| 226 |
CALL_INIT(Scm__InitThreads); |
| 227 |
|
| 228 |
CALL_INIT(Scm_Init_libalpha); |
| 229 |
CALL_INIT(Scm_Init_libbool); |
| 230 |
CALL_INIT(Scm_Init_libbox); |
| 231 |
CALL_INIT(Scm_Init_libchar); |
| 232 |
CALL_INIT(Scm_Init_libcode); |
| 233 |
CALL_INIT(Scm_Init_libcmp); |
| 234 |
CALL_INIT(Scm_Init_libeval); |
| 235 |
CALL_INIT(Scm_Init_libexc); |
| 236 |
CALL_INIT(Scm_Init_libfmt); |
| 237 |
CALL_INIT(Scm_Init_libhash); |
| 238 |
CALL_INIT(Scm_Init_libio); |
| 239 |
CALL_INIT(Scm_Init_liblazy); |
| 240 |
CALL_INIT(Scm_Init_liblist); |
| 241 |
CALL_INIT(Scm_Init_libmemo); |
| 242 |
CALL_INIT(Scm_Init_libmisc); |
| 243 |
CALL_INIT(Scm_Init_libmod); |
| 244 |
CALL_INIT(Scm_Init_libnative); |
| 245 |
CALL_INIT(Scm_Init_libnet); |
| 246 |
CALL_INIT(Scm_Init_libnum); |
| 247 |
CALL_INIT(Scm_Init_libobj); |
| 248 |
CALL_INIT(Scm_Init_libproc); |
| 249 |
CALL_INIT(Scm_Init_librx); |
| 250 |
CALL_INIT(Scm_Init_libsrfis); |
| 251 |
CALL_INIT(Scm_Init_libstr); |
| 252 |
CALL_INIT(Scm_Init_libsym); |
| 253 |
CALL_INIT(Scm_Init_libsys); |
| 254 |
CALL_INIT(Scm_Init_libvec); |
| 255 |
CALL_INIT(Scm_Init_libmacbase); |
| 256 |
CALL_INIT(Scm_Init_compile); |
| 257 |
/* lib* that uses er-macro-transformer must be initialized |
| 258 |
after libmacro */ |
| 259 |
CALL_INIT(Scm_Init_libmacro); |
| 260 |
CALL_INIT(Scm_Init_libparam); |
| 261 |
CALL_INIT(Scm_Init_libtype); |
| 262 |
CALL_INIT(Scm_Init_libthr); |
| 263 |
CALL_INIT(Scm_Init_libdict); |
| 264 |
CALL_INIT(Scm_Init_libomega); |
| 265 |
|
| 266 |
CALL_INIT(Scm__InitCompaux); |
| 267 |
|
| 268 |
Scm_SelectModule(Scm_GaucheModule()); |
| 269 |
CALL_INIT(Scm__InitAutoloads); |
| 270 |
|
| 271 |
Scm_SelectModule(Scm_UserModule()); |
| 272 |
|
| 273 |
/* Final setup of cond-features alist. */ |
| 274 |
CALL_INIT(init_cond_features); |
| 275 |
|
| 276 |
#ifdef GAUCHE_USE_PTHREADS |
| 277 |
/* a trick to make sure the gc thread object is linked */ |
| 278 |
ptr_pthread_create = GC_pthread_create; |
| 279 |
#endif |
| 280 |
|
| 281 |
Scm__FinishModuleInitialization(); |
| 282 |
runtime_state = SCM_RUNTIME_INITIALIZED; |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* Runtime state |
| 287 |
*/ |
| 288 |
|
| 289 |
ScmRuntimeState Scm_RuntimeState() |
| 290 |
{ |
| 291 |
return runtime_state; |
| 292 |
} |
| 293 |
|
| 294 |
/* This should only be called from main.c or equivalent. */ |
| 295 |
void Scm_SetRuntimeReplState(int full) |
| 296 |
{ |
| 297 |
runtime_state = full? SCM_RUNTIME_FULL_REPL : SCM_RUNTIME_MINI_REPL; |
| 298 |
} |
| 299 |
|
| 300 |
/*============================================================= |
| 301 |
* Memory utilities |
| 302 |
*/ |
| 303 |
|
| 304 |
#if SCM_ATOMIC_NEED_HELPER |
| 305 |
/* This is only needed if we fall back to libatomic_ops and not GNUC */ |
| 306 |
#include "gauche/priv/atomicP.h" |
| 307 |
|
| 308 |
int Scm__AtomicCompareExchange(ScmAtomicVar *loc, |
| 309 |
ScmAtomicWord *expectedloc, |
| 310 |
ScmAtomicWord newval) |
| 311 |
{ |
| 312 |
ScmAtomicWord expected = *expectedloc; |
| 313 |
ScmAtomicWord oldval = AO_fetch_compare_and_swap_full(loc, |
| 314 |
expected, |
| 315 |
newval); |
| 316 |
*expectedloc = expected; |
| 317 |
return oldval == expected; |
| 318 |
} |
| 319 |
|
| 320 |
ScmAtomicWord Scm__AtomicExchange(ScmAtomicVar *loc, |
| 321 |
ScmAtomicWord newval) |
| 322 |
{ |
| 323 |
ScmAtomicWord current; |
| 324 |
do { |
| 325 |
current = *loc; |
| 326 |
} while (AO_compare_and_swap_full(loc, current, newval)); |
| 327 |
return current; |
| 328 |
} |
| 329 |
#endif /*SCM_ATOMIC_NEED_HELPER*/ |
| 330 |
|
| 331 |
|
| 332 |
/*============================================================= |
| 333 |
* GC utilities |
| 334 |
*/ |
| 335 |
|
| 336 |
void Scm_GC() |
| 337 |
{ |
| 338 |
GC_gcollect(); |
| 339 |
} |
| 340 |
|
| 341 |
void Scm_PrintStaticRoots() |
| 342 |
{ |
| 343 |
GC_print_static_roots(); |
| 344 |
} |
| 345 |
|
| 346 |
/* |
| 347 |
* External API to register root set in dynamically loaded library. |
| 348 |
* Boehm GC doesn't do this automatically on some platforms. |
| 349 |
* |
| 350 |
* NB: The scheme we're using to find bss area (by Scm__bss{start|end}) |
| 351 |
* is getting less effective, since more platforms are adopting the |
| 352 |
* linker that rearranges bss variables. The extensions should not |
| 353 |
* keep GC_MALLOCED pointer into the bss variable. |
| 354 |
*/ |
| 355 |
void Scm_RegisterDL(void *data_start, void *data_end, |
| 356 |
void *bss_start, void *bss_end) |
| 357 |
{ |
| 358 |
if (data_start < data_end) { |
| 359 |
GC_add_roots(data_start, data_end); |
| 360 |
} |
| 361 |
if (bss_start < bss_end) { |
| 362 |
GC_add_roots(bss_start, bss_end); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
* Useful routine for debugging, to check if an object is inadvertently |
| 368 |
* collected. |
| 369 |
*/ |
| 370 |
static void gc_sentinel(ScmObj obj, void *data) |
| 371 |
{ |
| 372 |
Scm_Printf(SCM_CURERR, "WARNING: object %s(%p) is inadvertently collected\n", (char *)data, obj); |
| 373 |
} |
| 374 |
|
| 375 |
void Scm_GCSentinel(void *obj, const char *name) |
| 376 |
{ |
| 377 |
Scm_RegisterFinalizer(SCM_OBJ(obj), gc_sentinel, (void*)name); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
/*============================================================= |
| 382 |
* Finalization. Scheme finalizers are added as NO_ORDER. |
| 383 |
*/ |
| 384 |
void Scm_RegisterFinalizer(ScmObj z, ScmFinalizerProc finalizer, void *data) |
| 385 |
{ |
| 386 |
GC_finalization_proc ofn; void *ocd; |
| 387 |
GC_REGISTER_FINALIZER_NO_ORDER(z, (GC_finalization_proc)finalizer, |
| 388 |
data, &ofn, &ocd); |
| 389 |
} |
| 390 |
|
| 391 |
void Scm_UnregisterFinalizer(ScmObj z) |
| 392 |
{ |
| 393 |
GC_finalization_proc ofn; void *ocd; |
| 394 |
GC_REGISTER_FINALIZER_NO_ORDER(z, (GC_finalization_proc)NULL, NULL, |
| 395 |
&ofn, &ocd); |
| 396 |
} |
| 397 |
|
| 398 |
/* GC calls this back when finalizers are queued. */ |
| 399 |
void finalizable(void) |
| 400 |
{ |
| 401 |
ScmVM *vm = Scm_VM(); |
| 402 |
if (vm != NULL) { |
| 403 |
vm->finalizerPending = TRUE; |
| 404 |
vm->attentionRequest = TRUE; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/* Called from VM loop. Queue is not empty. */ |
| 409 |
ScmObj Scm_VMFinalizerRun(ScmVM *vm) |
| 410 |
{ |
| 411 |
GC_invoke_finalizers(); |
| 412 |
vm->finalizerPending = FALSE; |
| 413 |
return SCM_UNDEFINED; |
| 414 |
} |
| 415 |
|
| 416 |
/*============================================================= |
| 417 |
* Program cleanup & termination |
| 418 |
*/ |
| 419 |
|
| 420 |
struct cleanup_handler_rec { |
| 421 |
void (*handler)(void *data); |
| 422 |
void *data; |
| 423 |
struct cleanup_handler_rec *next; |
| 424 |
}; |
| 425 |
|
| 426 |
static struct { |
| 427 |
int dirty; /* Flag to avoid cleaning up more than once. */ |
| 428 |
struct cleanup_handler_rec *handlers; |
| 429 |
} cleanup = { TRUE, NULL }; |
| 430 |
|
| 431 |
/* Add cleanup handler. Returns an opaque handle, which can be |
| 432 |
passed to DeleteCleanupHandler. */ |
| 433 |
void *Scm_AddCleanupHandler(void (*h)(void *d), void *d) |
| 434 |
{ |
| 435 |
struct cleanup_handler_rec *r = SCM_NEW(struct cleanup_handler_rec); |
| 436 |
r->handler = h; |
| 437 |
r->data = d; |
| 438 |
r->next = cleanup.handlers; |
| 439 |
cleanup.handlers = r; |
| 440 |
return r; |
| 441 |
} |
| 442 |
|
| 443 |
/* Delete cleanup handler. HANDLE should be an opaque pointer |
| 444 |
returned from Scm_AddCleanupHandler, but it won't complain if |
| 445 |
other pointer is given. */ |
| 446 |
void Scm_DeleteCleanupHandler(void *handle) |
| 447 |
{ |
| 448 |
struct cleanup_handler_rec *x = NULL, *y = cleanup.handlers; |
| 449 |
while (y) { |
| 450 |
if (y == handle) { |
| 451 |
if (x == NULL) { |
| 452 |
cleanup.handlers = y->next; |
| 453 |
} else { |
| 454 |
x->next = y->next; |
| 455 |
} |
| 456 |
break; |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
void Scm__MutexCleanup(void *mutex_) |
| 462 |
{ |
| 463 |
ScmInternalMutex *mutex = mutex_; |
| 464 |
(void)SCM_INTERNAL_MUTEX_UNLOCK(*mutex); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
/* Scm_Cleanup and Scm_Exit |
| 469 |
Usually calling Scm_Exit is the easiest way to terminate Gauche |
| 470 |
application safely. If the application wants to continue operation |
| 471 |
after shutting down the Scheme part, however, it can call Scm_Cleanup(). |
| 472 |
*/ |
| 473 |
|
| 474 |
/* To avoid complication in supporting different platforms */ |
| 475 |
#define EXIT_CODE(code) ((code)&0xff) |
| 476 |
|
| 477 |
void Scm_Exit(int code) |
| 478 |
{ |
| 479 |
Scm_Cleanup(); |
| 480 |
exit(EXIT_CODE(code)); |
| 481 |
} |
| 482 |
|
| 483 |
void Scm_Cleanup(void) |
| 484 |
{ |
| 485 |
if (!cleanup.dirty) return; |
| 486 |
cleanup.dirty = FALSE; |
| 487 |
|
| 488 |
Scm_VMFlushDynamicHandlers(); |
| 489 |
|
| 490 |
/* Call the C-registered cleanup handlers. */ |
| 491 |
for (struct cleanup_handler_rec *ch = cleanup.handlers; ch; ch = ch->next) { |
| 492 |
ch->handler(ch->data); |
| 493 |
} |
| 494 |
|
| 495 |
/* Flush Scheme ports. */ |
| 496 |
Scm_FlushAllPorts(TRUE); |
| 497 |
} |
| 498 |
|
| 499 |
int Scm_ObjToExitCode(ScmObj obj) |
| 500 |
{ |
| 501 |
if (SCM_EQ(obj, SCM_TRUE)) { |
| 502 |
return 0; |
| 503 |
} else if (SCM_EQ(obj, SCM_FALSE)) { |
| 504 |
return 1; |
| 505 |
} else if (SCM_INTP(obj)) { |
| 506 |
return SCM_INT_VALUE(obj) & 0xff; |
| 507 |
} else if (SCM_INTEGERP(obj)) { |
| 508 |
return Scm_GetInteger(Scm_LogAnd(obj, SCM_MAKE_INT(0xff))); |
| 509 |
} else { |
| 510 |
return 70; /* EX_SOFTWARE */ |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
void Scm_Panic(const char *msg, ...) |
| 515 |
{ |
| 516 |
va_list args; |
| 517 |
va_start(args, msg); |
| 518 |
vfprintf(stderr, msg, args); |
| 519 |
va_end(args); |
| 520 |
fputc('\n', stderr); |
| 521 |
fflush(stderr); |
| 522 |
_exit(EXIT_CODE(1)); |
| 523 |
} |
| 524 |
|
| 525 |
/* Use this for absolute emergency. Newline is not attached to msg. */ |
| 526 |
void Scm_Abort(const char *msg) |
| 527 |
{ |
| 528 |
int size = (int)strlen(msg); |
| 529 |
/* this may return an error, but we don't care, since we exit anyway. */ |
| 530 |
SCM_IGNORE_RESULT(write(2, msg, size)); |
| 531 |
_exit(EXIT_CODE(1)); |
| 532 |
} |
| 533 |
|
| 534 |
/*============================================================= |
| 535 |
* Inspect the configuration |
| 536 |
*/ |
| 537 |
|
| 538 |
/* Returns the cond-features alist. */ |
| 539 |
ScmObj |
| 540 |
Scm_GetFeatures() |
| 541 |
{ |
| 542 |
return cond_features.alist; |
| 543 |
} |
| 544 |
|
| 545 |
/* |
| 546 |
* Enables FEATURE. Once added, the symbol FEATURE will be recognized |
| 547 |
* by cond-expand. |
| 548 |
* |
| 549 |
* (cond-expand (FEATURE body ...)) |
| 550 |
* |
| 551 |
* If loading a module is required in order to make FEATURE available, |
| 552 |
* such module name can be specified in MODULE argument. It can be NULL |
| 553 |
* if the feature is built-in. |
| 554 |
* |
| 555 |
* If FEATURE is already in the disabled list, it is not added. |
| 556 |
* |
| 557 |
* This API is meant to expose configuration information to a Scheme |
| 558 |
* level. |
| 559 |
*/ |
| 560 |
void |
| 561 |
Scm_AddFeature(const char *feature, const char *module) |
| 562 |
{ |
| 563 |
ScmObj f = SCM_INTERN(feature); |
| 564 |
ScmObj cell = module? SCM_LIST2(f, SCM_INTERN(module)): SCM_LIST1(f); |
| 565 |
|
| 566 |
(void)SCM_INTERNAL_MUTEX_LOCK(cond_features.mutex); |
| 567 |
if (SCM_FALSEP(Scm_Memq(f, cond_features.dlist))) { |
| 568 |
cond_features.alist = Scm_Cons(cell, cond_features.alist); |
| 569 |
} |
| 570 |
(void)SCM_INTERNAL_MUTEX_UNLOCK(cond_features.mutex); |
| 571 |
} |
| 572 |
|
| 573 |
/* |
| 574 |
* Disables FEATURE. If FEATURE is already enabled, it is removed |
| 575 |
* from the features alist. |
| 576 |
* Mainly used by gosh's -F-feature command-line option. |
| 577 |
*/ |
| 578 |
void |
| 579 |
Scm_DisableFeature(const char *feature) |
| 580 |
{ |
| 581 |
ScmObj f = SCM_INTERN(feature); |
| 582 |
(void)SCM_INTERNAL_MUTEX_LOCK(cond_features.mutex); |
| 583 |
cond_features.dlist = Scm_Cons(f, cond_features.dlist); |
| 584 |
cond_features.alist = Scm_AssocDelete(f, cond_features.alist, SCM_CMP_EQ); |
| 585 |
(void)SCM_INTERNAL_MUTEX_UNLOCK(cond_features.mutex); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
static void |
| 590 |
init_cond_features() |
| 591 |
{ |
| 592 |
/* The initial cond-features list. */ |
| 593 |
static struct { |
| 594 |
const char *feature; |
| 595 |
const char *module; |
| 596 |
} init_features[] = { |
| 597 |
{ "gauche", NULL }, |
| 598 |
{ "gauche-" GAUCHE_VERSION , NULL }, |
| 599 |
|
| 600 |
/* Platform */ |
| 601 |
#if defined(GAUCHE_WINDOWS) |
| 602 |
{ "gauche.os.windows", NULL }, |
| 603 |
{ "gauche-windows", NULL }, /* for backward compatibility */ |
| 604 |
#elif defined(__CYGWIN__) /* cygwin is different enough to deserve this */ |
| 605 |
{ "gauche.os.cygwin", NULL }, |
| 606 |
#endif |
| 607 |
|
| 608 |
/* R7RS */ |
| 609 |
/* NB: We should probably make checking 'r7rs' trigger loading |
| 610 |
r7rs module and setting up the environment. */ |
| 611 |
{ "r7rs", NULL }, |
| 612 |
|
| 613 |
/* R7RS Appendix B */ |
| 614 |
{ "exact-closed", NULL }, |
| 615 |
// { "exact-complex", NULL }, /* not yet */ |
| 616 |
{ "ieee-float", NULL }, |
| 617 |
{ "full-unicode", NULL }, |
| 618 |
{ "ratios", NULL }, |
| 619 |
#if defined(GAUCHE_WINDOWS) |
| 620 |
{ "windows", NULL }, |
| 621 |
#else |
| 622 |
{ "posix", NULL }, |
| 623 |
#endif |
| 624 |
/* OS/CPU arch flags are generated to features.c by gen-features.sh */ |
| 625 |
/* TODO: C memory model flags */ |
| 626 |
#if defined(WORDS_BIGENDIAN) |
| 627 |
{ "big-endian", NULL }, |
| 628 |
#else |
| 629 |
{ "little-endian", NULL }, /* NB: r7rs say nothing on mixed endian */ |
| 630 |
#endif |
| 631 |
|
| 632 |
/* SRFIs that are not libraries */ |
| 633 |
{ "srfi-22", NULL }, /* Scheme scripts */ |
| 634 |
{ "srfi-138", NULL }, /* compile-r7rs script */ |
| 635 |
|
| 636 |
/* Threads */ |
| 637 |
#if defined(GAUCHE_USE_PTHREADS) |
| 638 |
{ "gauche.sys.threads", "gauche.threads" }, |
| 639 |
{ "gauche.sys.pthreads", "gauche.threads" }, |
| 640 |
#elif defined(GAUCHE_USE_WTHREADS) |
| 641 |
{ "gauche.sys.threads", "gauche.threads" }, |
| 642 |
{ "gauche.sys.wthreads", "gauche.threads" }, |
| 643 |
#endif |
| 644 |
|
| 645 |
/* TLS/SSL. This nees to be in the core in order to switch |
| 646 |
code _before_ loading rfc.tls */ |
| 647 |
#if defined(GAUCHE_USE_MBEDTLS) |
| 648 |
{ "gauche.net.tls", "rfc.tls" }, |
| 649 |
#endif |
| 650 |
#if defined(GAUCHE_USE_MBEDTLS) |
| 651 |
/* NB: Kludge - mbedTLS is implemented in a separate module rfc.tls.mbed, |
| 652 |
but we don't autoload it with cond-expand gauche.net.tls.mbedtls. |
| 653 |
If the gauche is compiled with mbedTLS support but the target system |
| 654 |
lacks mbed DSO, using rfc.tls.mbed causes runtime error. We don't |
| 655 |
want that merely by checking feature identifier. We set autoload |
| 656 |
in rfc.tls, so the runtime error only occur when the program actually |
| 657 |
try to use <mbed-tls>. */ |
| 658 |
{ "gauche.net.tls.mbedtls", NULL }, |
| 659 |
#endif |
| 660 |
|
| 661 |
/* zlib */ |
| 662 |
#if defined(USE_ZLIB) |
| 663 |
{ "gauche.sys.zlib", "rfc.zlib" }, |
| 664 |
#endif |
| 665 |
|
| 666 |
{ "regexp-non-greedy", "srfi.115" }, |
| 667 |
{ "regexp-look-around", "srfi.115" }, |
| 668 |
{ "regexp-backrefs", "srfi.115" }, |
| 669 |
{ "regexp-unicode", "srfi.115" }, |
| 670 |
|
| 671 |
#include "features.c" |
| 672 |
|
| 673 |
{ NULL, NULL } |
| 674 |
}; |
| 675 |
|
| 676 |
for (int i=0; init_features[i].feature; i++) { |
| 677 |
Scm_AddFeature(init_features[i].feature, init_features[i].module); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
#include "buildinfo.c" |
| 682 |
|
| 683 |
ScmObj Scm_BuildGoshVersion() |
| 684 |
{ |
| 685 |
return SCM_MAKE_STR(build_gosh_version); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
/*============================================================= |
| 690 |
* 'Main' |
| 691 |
*/ |
| 692 |
|
| 693 |
/* |
| 694 |
* When creating DLL under Cygwin, we need the following dummy main() |
| 695 |
* or we get "undefined reference _WinMain@16" error. |
| 696 |
* (See cygwin FAQ, http://cygwin.com/faq/) |
| 697 |
*/ |
| 698 |
#ifdef __CYGWIN__ |
| 699 |
int main(void) |
| 700 |
{ |
| 701 |
return 0; |
| 702 |
} |
| 703 |
#endif /*__CYGWIN__*/ |
| 704 |
|
| 705 |
/* |
| 706 |
* A simple main routine useful to build a binary executable. |
| 707 |
*/ |
| 708 |
|
| 709 |
void Scm_SimpleMain(int argc, const char *argv[], |
| 710 |
const char *script, u_long flags SCM_UNUSED) |
| 711 |
{ |
| 712 |
SCM_ASSERT(argc > 0); |
| 713 |
ScmObj args = Scm_InitCommandLine2(argc, argv, SCM_COMMAND_LINE_BOTH); |
| 714 |
|
| 715 |
if (script) { |
| 716 |
ScmObj s = SCM_MAKE_STR(script); |
| 717 |
ScmObj p = Scm_MakeInputStringPort(SCM_STRING(s), TRUE); |
| 718 |
Scm_LoadFromPort(SCM_PORT(p), SCM_LOAD_PROPAGATE_ERROR, NULL); |
| 719 |
} |
| 720 |
|
| 721 |
ScmModule *user = Scm_UserModule(); |
| 722 |
ScmObj mainproc = Scm_GlobalVariableRef(user, SCM_SYMBOL(SCM_INTERN("main")), 0); |
| 723 |
if (SCM_PROCEDUREP(mainproc)) { |
| 724 |
static ScmObj run_main_proc = SCM_UNDEFINED; |
| 725 |
SCM_BIND_PROC(run_main_proc, "run-main", Scm_GaucheInternalModule()); |
| 726 |
SCM_ASSERT(SCM_PROCEDUREP(run_main_proc)); |
| 727 |
|
| 728 |
ScmEvalPacket epak; |
| 729 |
int r = Scm_Apply(run_main_proc, SCM_LIST2(mainproc, args), &epak); |
| 730 |
if (r >= 0 && SCM_INTP(epak.results[0])) { |
| 731 |
Scm_Exit(SCM_INT_VALUE(epak.results[0])); |
| 732 |
} else { |
| 733 |
Scm_Exit(70); /* EX_SOFTWARE */ |
| 734 |
} |
| 735 |
} else { |
| 736 |
Scm_Exit(0); |
| 737 |
} |
| 738 |
} |