run_tcc.c 3.9 KB

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