bootstrap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet 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. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // CONSTANT stdin 0
  18. // CONSTANT stdout 1
  19. // CONSTANT stderr 2
  20. // CONSTANT EOF 0xFFFFFFFF
  21. // CONSTANT NULL 0
  22. // CONSTANT EXIT_FAILURE 1
  23. // CONSTANT EXIT_SUCCESS 0
  24. int fgetc(FILE* f)
  25. {
  26. asm("SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  27. "PUSH_X0"
  28. "SET_X1_FROM_SP"
  29. "SET_X2_TO_1"
  30. "SET_X8_TO_SYS_READ"
  31. "SYSCALL"
  32. "SET_X1_TO_0"
  33. "CMP_X1_X0"
  34. "POP_X0"
  35. "SKIP_INST_NE"
  36. "SET_X0_TO_MINUS_1");
  37. }
  38. void fputc(char s, FILE* f)
  39. {
  40. asm("SET_X0_FROM_BP" "SUB_X0_8"
  41. "SET_X1_FROM_X0"
  42. "SET_X0_FROM_BP" "SUB_X0_16" "DEREF_X0"
  43. "SET_X2_TO_1"
  44. "SET_X8_TO_SYS_WRITE"
  45. "SYSCALL");
  46. }
  47. FILE* open(char* name, int flag, int mode)
  48. {
  49. asm("SET_X0_FROM_BP" "SUB_X0_24" "DEREF_X0"
  50. "SET_X3_FROM_X0"
  51. "SET_X0_FROM_BP" "SUB_X0_16" "DEREF_X0"
  52. "SET_X2_FROM_X0"
  53. "SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  54. "SET_X1_FROM_X0"
  55. "SET_X0_TO_FCNTL_H_AT_FDCWD"
  56. "SET_X8_TO_SYS_OPENAT"
  57. "SYSCALL");
  58. }
  59. FILE* fopen(char* filename, char* mode)
  60. {
  61. FILE* f;
  62. if('w' == mode[0])
  63. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  64. f = open(filename, 577 , 384);
  65. }
  66. else
  67. { /* Everything else is a read */
  68. f = open(filename, 0, 0);
  69. }
  70. /* Negative numbers are error codes */
  71. if(0 > f)
  72. {
  73. return 0;
  74. }
  75. return f;
  76. }
  77. int close(int fd)
  78. {
  79. asm("SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  80. "SET_X8_TO_SYS_CLOSE"
  81. "SYSCALL");
  82. }
  83. int fclose(FILE* stream)
  84. {
  85. int error = close(stream);
  86. return error;
  87. }
  88. int brk(void *addr)
  89. {
  90. asm("SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  91. "SET_X8_TO_SYS_BRK"
  92. "SYSCALL");
  93. }
  94. long _malloc_ptr;
  95. long _brk_ptr;
  96. void* malloc(int size)
  97. {
  98. if(NULL == _brk_ptr)
  99. {
  100. _brk_ptr = brk(0);
  101. _malloc_ptr = _brk_ptr;
  102. }
  103. if(_brk_ptr < _malloc_ptr + size)
  104. {
  105. _brk_ptr = brk(_malloc_ptr + size);
  106. if(-1 == _brk_ptr) return 0;
  107. }
  108. long old_malloc = _malloc_ptr;
  109. _malloc_ptr = _malloc_ptr + size;
  110. return old_malloc;
  111. }
  112. void* memset(void* ptr, int value, int num)
  113. {
  114. char* s;
  115. for(s = ptr; 0 < num; num = num - 1)
  116. {
  117. s[0] = value;
  118. s = s + 1;
  119. }
  120. }
  121. void* calloc(int count, int size)
  122. {
  123. void* ret = malloc(count * size);
  124. if(NULL == ret) return NULL;
  125. memset(ret, 0, (count * size));
  126. return ret;
  127. }
  128. void free(void* l)
  129. {
  130. return;
  131. }
  132. void exit(int value)
  133. {
  134. asm("SET_X0_FROM_BP" "SUB_X0_8" "DEREF_X0"
  135. "SET_X8_TO_SYS_EXIT"
  136. "SYSCALL");
  137. }