run_tcc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef LEVEL
  18. #define LEVEL 0
  19. #endif
  20. #define RUN_IPXE 1
  21. #define RUN_SINGLE_CREAM 0
  22. #define RUN_MM0_C 0
  23. int recursively_compile() {
  24. #if LEVEL == 10
  25. printf("Let's not get mad, stopping recursion...\n");
  26. return 0;
  27. #endif
  28. const char *includes[7];
  29. includes[0] = ASMC_PREFIX;
  30. includes[1] = ASMC_PREFIX "/stdlib";
  31. includes[2] = ASMC_PREFIX "/tinycc-aux";
  32. includes[3] = ASMC_PREFIX "/tinycc";
  33. includes[4] = ASMC_PREFIX "/tinycc/softfloat";
  34. includes[5] = ASMC_PREFIX "/tinycc/softfloat/include";
  35. includes[6] = ASMC_PREFIX "/tinycc/softfloat/8086";
  36. const char *files[1];
  37. printf("Compiling tinycc recursively... (level %d)\n", LEVEL);
  38. #if LEVEL == 1 && RUN_IPXE
  39. #define ADD_TCC_SYMBOLS
  40. files[0] = ASMC_PREFIX "/run_tcc_ipxe.c";
  41. #elif LEVEL == 1 && RUN_SINGLE_CREAM
  42. #define ADD_TCC_SYMBOLS
  43. files[0] = ASMC_PREFIX "/run_tcc_sc.c";
  44. #elif LEVEL == 1 && RUN_MM0_C
  45. #define ADD_TCC_SYMBOLS
  46. files[0] = ASMC_PREFIX "/run_tcc_mm0_c.c";
  47. #else
  48. files[0] = ASMC_PREFIX "/run_tcc.c";
  49. #endif
  50. TCCState *state = tcc_new();
  51. char buf[1024];
  52. sprintf(buf, "%d", __get_handles());
  53. tcc_define_symbol(state, "__HANDLES", buf);
  54. sprintf(buf, "%d", LEVEL+1);
  55. tcc_define_symbol(state, "LEVEL", buf);
  56. #ifdef ADD_TCC_SYMBOLS
  57. tcc_add_symbol(state, "tcc_new", tcc_new);
  58. tcc_add_symbol(state, "tcc_delete", tcc_delete);
  59. tcc_add_symbol(state, "tcc_set_lib_path", tcc_set_lib_path);
  60. tcc_add_symbol(state, "tcc_set_error_func", tcc_set_error_func);
  61. tcc_add_symbol(state, "tcc_set_options", tcc_set_options);
  62. tcc_add_symbol(state, "tcc_add_include_path", tcc_add_include_path);
  63. tcc_add_symbol(state, "tcc_add_sysinclude_path", tcc_add_sysinclude_path);
  64. tcc_add_symbol(state, "tcc_define_symbol", tcc_define_symbol);
  65. tcc_add_symbol(state, "tcc_undefine_symbol", tcc_undefine_symbol);
  66. tcc_add_symbol(state, "tcc_add_file", tcc_add_file);
  67. tcc_add_symbol(state, "tcc_compile_string", tcc_compile_string);
  68. tcc_add_symbol(state, "tcc_set_output_type", tcc_set_output_type);
  69. tcc_add_symbol(state, "tcc_add_library_path", tcc_add_library_path);
  70. tcc_add_symbol(state, "tcc_add_library", tcc_add_library);
  71. tcc_add_symbol(state, "tcc_add_symbol", tcc_add_symbol);
  72. tcc_add_symbol(state, "tcc_output_file", tcc_output_file);
  73. tcc_add_symbol(state, "tcc_run", tcc_run);
  74. tcc_add_symbol(state, "tcc_relocate", tcc_relocate);
  75. tcc_add_symbol(state, "tcc_get_symbol", tcc_get_symbol);
  76. #endif
  77. if (run_tinycc(state, sizeof(files) / sizeof(files[0]), files,
  78. sizeof(includes) / sizeof(includes[0]), includes)) {
  79. "tinycc invocation failed...\n";
  80. return 1;
  81. }
  82. int (*start)() = tcc_get_symbol(state, "_start");
  83. if (!start) {
  84. printf("tcc_get_symbol() failed\n");
  85. return 1;
  86. }
  87. printf("Entering tinycc...\n");
  88. int ret = start();
  89. printf("Tinycc returned %d!\n", ret);
  90. tcc_delete(state);
  91. return 0;
  92. }
  93. int main(int argc, char *argv[]) {
  94. if (recursively_compile()) {
  95. printf("Recursive compilation failed...\n");
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. // Include the actual tinycc compiler
  101. #define USE_SOFTFLOAT 1
  102. #define ONE_SOURCE 1
  103. #include "libtcc.c"