1
0

run_tcc_mm0_c.c 3.3 KB

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