init.c 4.2 KB

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