init.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Part of Scheme 48 1.9. See file COPYING for notices and license.
  3. *
  4. * Authors: Richard Kelsey, Jonathan Rees, Martin Gasbichler, Mike Sperber,
  5. * David Frese, Marcus Crestani
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "scheme48vm.h"
  10. #include "scheme48heap.h"
  11. #include "scheme48image.h"
  12. #include "scheme48ffi.h"
  13. extern long s48_get_file_size(unsigned char *);
  14. #if !defined(DEFAULT_HEAP_SIZE)
  15. /* 4 megacells = 16 megabytes on 32-bit systems */
  16. #define DEFAULT_HEAP_SIZE 4000000L
  17. #endif
  18. #if !defined(DEFAULT_STACK_SIZE)
  19. /* 2500 cells = 10000 bytes */
  20. #define DEFAULT_STACK_SIZE 2500L
  21. #endif
  22. #if defined(STATIC_AREAS) && defined(S48_GC_BIBOP)
  23. #error "The BIBOP GC doesn't support the STATIC_AREAS feature yet."
  24. #endif
  25. #if defined(STATIC_AREAS)
  26. #define DEFAULT_IMAGE_NAME NULL
  27. #else
  28. /* DEFAULT_IMAGE_NAME should be defined using the -D switch to cc. */
  29. #if !defined(DEFAULT_IMAGE_NAME)
  30. #define DEFAULT_IMAGE_NAME "scheme48.image"
  31. #endif
  32. #endif /* STATIC_AREAS */
  33. extern void s48_sysdep_init(void);
  34. extern void s48_initialize_external_modules(void);
  35. long
  36. s48_initialize(int *argcp, char ***argv)
  37. {
  38. char *image_name = DEFAULT_IMAGE_NAME;
  39. long heap_size = DEFAULT_HEAP_SIZE; /* in numbers of cells */
  40. long stack_size = DEFAULT_STACK_SIZE; /* in numbers of cells */
  41. int errors = 0;
  42. char *stack;
  43. #if defined(STATIC_AREAS)
  44. extern long static_entry;
  45. extern long static_symbol_table;
  46. extern long static_imported_binding_table, static_exported_binding_table;
  47. extern long p_count, *p_areas[], p_sizes[];
  48. extern long i_count, *i_areas[], i_sizes[];
  49. #endif
  50. int argc = *argcp;
  51. int vm_argc = 0;
  52. char *me = *(*argv); /* Save program name. */
  53. {
  54. /* initialize floating-point printer */
  55. extern void s48_free_init(void);
  56. s48_free_init();
  57. }
  58. (*argv)++; argc--; /* Skip program name. */
  59. for (; argc > 0; argc--, (*argv)++)
  60. if ((*argv)[0][0] == '-')
  61. switch ((*argv)[0][1]) {
  62. case 'h':
  63. argc--; (*argv)++;
  64. if (argc == 0) { errors++; break; }
  65. heap_size = atol(*(*argv));
  66. if (heap_size < 0) errors++; /* 0 means now no limit */
  67. break;
  68. case 's':
  69. argc--; (*argv)++;
  70. if (argc == 0) { errors++; break; }
  71. stack_size = atoi(*(*argv));
  72. if (stack_size <= 0) errors++;
  73. break;
  74. case 'i':
  75. argc--; (*argv)++;
  76. if (argc == 0) { errors++; break; }
  77. image_name = *(*argv);
  78. break;
  79. case 'a':
  80. argc--;
  81. vm_argc = argc; /* remaining args are passed to the VM */
  82. argc = 0;
  83. break;
  84. case 'I':
  85. #ifdef S48_GC_BIBOP
  86. heap_size = 0; /* unlimited heap size (BIBOP GC only) */
  87. #endif
  88. /* code for -i */
  89. argc--; (*argv)++;
  90. if (argc == 0) { errors++; break; }
  91. image_name = *(*argv);
  92. /* code for -a */
  93. argc--;
  94. vm_argc = argc; /* remaining args are passed to the VM */
  95. argc = 0;
  96. break;
  97. default:
  98. fprintf(stderr, "Invalid argument: %s\n", *(*argv));
  99. errors++;
  100. }
  101. else
  102. if ((*argv)[0][0] != '\0') {
  103. fprintf(stderr, "Invalid argument: %s\n", *(*argv));
  104. errors++; }
  105. if (errors != 0) {
  106. fprintf(stderr,
  107. "Usage: %s [options] [-a arguments]\n\
  108. Options: -h <heap-size> %s heap size in words (default %ld).%s\n\
  109. -s <stack-size> Stack buffer size in words.\n\
  110. -i <file> Load image from file (default \"%s\")\n",
  111. me,
  112. #if S48_GC_BIBOP
  113. "Maximum",
  114. (long)DEFAULT_HEAP_SIZE,
  115. "\n A heap size of 0 means the heap can grow\n\
  116. unboundedly. This is dangerous because it can\n\
  117. cause your system to run out of memory.",
  118. #else
  119. "Total",
  120. (long)DEFAULT_HEAP_SIZE,
  121. "",
  122. #endif
  123. DEFAULT_IMAGE_NAME
  124. );
  125. return 1;
  126. }
  127. /* Disable GC to read the image and initialize the VM-stack */
  128. s48_forbid_gcB();
  129. s48_sysdep_init();
  130. s48_heap_init();
  131. s48_init();
  132. if (image_name == NULL) {
  133. #if defined(STATIC_AREAS)
  134. s48_register_static_areas(p_count, p_areas, p_sizes,
  135. i_count, i_areas, i_sizes);
  136. s48_set_image_valuesB(static_entry,
  137. static_symbol_table,
  138. static_imported_binding_table,
  139. static_exported_binding_table);
  140. if (s48_initialize_heap(heap_size) == NULL) {
  141. fprintf(stderr, "system is out of memory\n");
  142. return 1; }
  143. #else
  144. fprintf(stderr, "No image file.\n");
  145. return 1;
  146. #endif
  147. } else if (s48_read_image(image_name, heap_size) == -1) {
  148. fprintf(stderr, "Image file \"%s\" is unusable.\n", image_name);
  149. return 1; }
  150. stack = (char *) malloc(stack_size * sizeof(long));
  151. if (!stack) {
  152. fprintf(stderr, "system is out of memory\n");
  153. return 1; }
  154. s48_initialize_ffi();
  155. s48_initialize_vm(stack, stack_size);
  156. s48_initialize_external_modules();
  157. /* Heap und stack are ok. Enable the GC. */
  158. s48_allow_gcB();
  159. *argcp = vm_argc;
  160. return 0;
  161. }