init.c 4.5 KB

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