run_tcc_sc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2019 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include "run_tcc.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #define TCC_TARGET_I386 1
  20. #define __i386__ 1
  21. #define USE_SOFTFLOAT 1
  22. #define ONE_SOURCE 1
  23. #include "tcc.h"
  24. // Silence some internal tcc warnings
  25. #undef free
  26. #undef malloc
  27. #undef realloc
  28. #undef strdup
  29. #define ASMC_PREFIX "/disk1"
  30. #define SC_PREFIX ASMC_PREFIX "/sc/src"
  31. #define SC_TEMP "/ram/sc"
  32. const char *includes[] = {
  33. ASMC_PREFIX,
  34. ASMC_PREFIX "/stdlib",
  35. };
  36. const char *sources[][2] = {
  37. {SC_PREFIX "/sch3.c", SC_TEMP "/sch3.o"},
  38. };
  39. #define RUN_SCRIPT
  40. char *sch3_argv[] = {
  41. "sch3",
  42. #ifdef RUN_SCRIPT
  43. "/disk1/sch3.scm",
  44. #endif
  45. };
  46. int main(int argc, char *argv[]) {
  47. restart:
  48. printf("Here is where we compile single_cream!\n");
  49. int res;
  50. TCCState *state;
  51. // First compile all files
  52. for (int j = 0; j < sizeof(sources) / sizeof(sources[0]); j++) {
  53. state = tcc_new();
  54. tcc_set_options(state, "-nostdinc -nostdlib");
  55. tcc_set_output_type(state, TCC_OUTPUT_OBJ);
  56. char buf[1024];
  57. sprintf(buf, "%d", __get_handles());
  58. tcc_define_symbol(state, "__HANDLES", buf);
  59. for (int i = 0; i < sizeof(includes) / sizeof(includes[0]); i++) {
  60. res = tcc_add_include_path(state, includes[i]);
  61. if (res) {
  62. printf("tcc_add_include_path() failed...\n");
  63. return 1;
  64. }
  65. }
  66. res = tcc_add_file(state, sources[j][0]);
  67. if (res) {
  68. printf("tcc_add_file() failed...\n");
  69. return 1;
  70. }
  71. res = tcc_output_file(state, sources[j][1]);
  72. if (res) {
  73. printf("tcc_output_file() failed...\n");
  74. return 1;
  75. }
  76. tcc_delete(state);
  77. }
  78. // Then link everything together
  79. state = tcc_new();
  80. tcc_set_options(state, "-nostdinc -nostdlib");
  81. tcc_set_output_type(state, TCC_OUTPUT_MEMORY);
  82. for (int i = 0; i < sizeof(sources) / sizeof(sources[0]); i++) {
  83. res = tcc_add_file(state, sources[i][1]);
  84. if (res) {
  85. printf("tcc_add_file() failed...\n");
  86. return 1;
  87. }
  88. }
  89. res = tcc_relocate(state, TCC_RELOCATE_AUTO);
  90. if (res) {
  91. printf("tcc_relocate() failed...\n");
  92. return 1;
  93. }
  94. int (*start)(int, char *[]) = tcc_get_symbol(state, "_start");
  95. if (!start) {
  96. printf("tcc_get_symbol() failed...\n");
  97. return 1;
  98. }
  99. printf("Jumping into single_cream!\n");
  100. res = start(sizeof(sch3_argv)/sizeof(sch3_argv[0]), sch3_argv);
  101. printf("single_cream returned %d!\n", res);
  102. tcc_delete(state);
  103. #ifndef RUN_SCRIPT
  104. goto restart;
  105. #endif
  106. return res;
  107. }