asmc.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef __ASMC_H
  2. #define __ASMC_H
  3. #include "asmc_types.h"
  4. struct __handles_t {
  5. void (*write)(char c);
  6. int (*platform_setjmp)(void *env);
  7. void (*platform_longjmp)(void *env, int status);
  8. void *(*malloc)(size_t size);
  9. void *(*calloc)(size_t num, size_t size);
  10. void (*free)(void *ptr);
  11. void *(*realloc)(void *ptr, size_t new_size);
  12. char *(*itoa)(unsigned int x);
  13. void (*dump_stacktrace)();
  14. int (*vfs_open)(const char *name);
  15. void (*vfs_close)(int fd);
  16. int (*vfs_read)(int fd);
  17. void (*vfs_write)(int c, int fd);
  18. void (*vfs_truncate)(int fd);
  19. int (*vfs_seek)(int whence, int offset, int fd);
  20. int (*input_getc)();
  21. };
  22. struct __handles_t *__handles;
  23. FILE __stdin = {0, 0, 0};
  24. FILE *stdin;
  25. FILE __stdout = {1, 0, 0};
  26. FILE *stdout;
  27. FILE __stderr = {2, 0, 0};
  28. FILE *stderr;
  29. int main(int, char *[]);
  30. #include "setjmp.h"
  31. int __return_code;
  32. int __aborted;
  33. jmp_buf __return_jump_buf;
  34. unsigned int __get_handles() {
  35. return (unsigned int) __handles;
  36. }
  37. void __init_stdlib() {
  38. #ifdef __HANDLES
  39. __handles = (struct __handles_t*) __HANDLES;
  40. #else
  41. __handles = &__builtin_handles;
  42. #endif
  43. __aborted = 0;
  44. stdin = &__stdin;
  45. stdout = &__stdout;
  46. stderr = &__stderr;
  47. }
  48. // Nothing to do, for the moment
  49. void __cleanup_stdlib() {
  50. }
  51. int fputs(const char *s, FILE *stream);
  52. int fprintf(FILE* stream, const char * format, ...);
  53. void __dump_stacktrace() {
  54. __handles->dump_stacktrace();
  55. }
  56. #ifdef __TINYC__
  57. #define __unimplemented() fprintf(stderr, "Unimplemented call %s at %s:%d\n", __func__, __FILE__, __LINE__)
  58. #else
  59. void __unimplemented() {
  60. fputs("UNIMPLEMENTED CALL\n", stderr);
  61. __dump_stacktrace();
  62. }
  63. #endif
  64. #include "stdio.h"
  65. int _start(int argc, char *argv[]) {
  66. __init_stdlib();
  67. if (setjmp(__return_jump_buf) == 0) {
  68. __return_code = main(argc, argv);
  69. }
  70. if (__aborted) {
  71. fputs("ABORT\n", stderr);
  72. } else {
  73. __cleanup_stdlib();
  74. }
  75. return __return_code;
  76. }
  77. #endif