run_tcc_sc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define ASMC_PREFIX "/disk1"
  25. #define SC_PREFIX ASMC_PREFIX "/sc/src"
  26. #define SC_TEMP "/ram/sc"
  27. const char *includes[] = {
  28. ASMC_PREFIX,
  29. ASMC_PREFIX "/stdlib",
  30. };
  31. const char *sources[][2] = {
  32. {SC_PREFIX "/sch3.c", SC_TEMP "/sch3.o"},
  33. };
  34. #define RUN_SCRIPT
  35. char *sch3_argv[] = {
  36. "sch3",
  37. #ifdef RUN_SCRIPT
  38. "/disk1/sch3.scm",
  39. #endif
  40. };
  41. int main(int argc, char *argv[]) {
  42. restart:
  43. printf("Here is where we compile single_cream!\n");
  44. int res;
  45. TCCState *state;
  46. // First compile all files
  47. for (int j = 0; j < sizeof(sources) / sizeof(sources[0]); j++) {
  48. state = tcc_new();
  49. tcc_set_options(state, "-nostdinc -nostdlib");
  50. tcc_set_output_type(state, TCC_OUTPUT_OBJ);
  51. char buf[1024];
  52. sprintf(buf, "%d", __get_handles());
  53. tcc_define_symbol(state, "__HANDLES", buf);
  54. for (int i = 0; i < sizeof(includes) / sizeof(includes[0]); i++) {
  55. res = tcc_add_include_path(state, includes[i]);
  56. if (res) {
  57. printf("tcc_add_include_path() failed...\n");
  58. return 1;
  59. }
  60. }
  61. res = tcc_add_file(state, sources[j][0]);
  62. if (res) {
  63. printf("tcc_add_file() failed...\n");
  64. return 1;
  65. }
  66. res = tcc_output_file(state, sources[j][1]);
  67. if (res) {
  68. printf("tcc_output_file() failed...\n");
  69. return 1;
  70. }
  71. tcc_delete(state);
  72. }
  73. // Then link everything together
  74. state = tcc_new();
  75. tcc_set_options(state, "-nostdinc -nostdlib");
  76. tcc_set_output_type(state, TCC_OUTPUT_MEMORY);
  77. for (int i = 0; i < sizeof(sources) / sizeof(sources[0]); i++) {
  78. res = tcc_add_file(state, sources[i][1]);
  79. if (res) {
  80. printf("tcc_add_file() failed...\n");
  81. return 1;
  82. }
  83. }
  84. res = tcc_relocate(state, TCC_RELOCATE_AUTO);
  85. if (res) {
  86. printf("tcc_relocate() failed...\n");
  87. return 1;
  88. }
  89. int (*start)(int, char *[]) = tcc_get_symbol(state, "_start");
  90. if (!start) {
  91. printf("tcc_get_symbol() failed...\n");
  92. return 1;
  93. }
  94. printf("Jumping into single_cream!\n");
  95. res = start(sizeof(sch3_argv)/sizeof(sch3_argv[0]), sch3_argv);
  96. printf("single_cream returned %d!\n", res);
  97. tcc_delete(state);
  98. #ifndef RUN_SCRIPT
  99. goto restart;
  100. #endif
  101. return res;
  102. }