bootstrap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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("LOAD_EFFECTIVE_ADDRESS_rdi %8"
  27. "LOAD_INTEGER_rdi"
  28. "LOAD_IMMEDIATE_rax %0"
  29. "PUSH_RAX"
  30. "LOAD_EFFECTIVE_ADDRESS_rsi %0"
  31. "LOAD_IMMEDIATE_rdx %1"
  32. "SYSCALL"
  33. "LOAD_IMMEDIATE_rbx %0"
  34. "CMP"
  35. "POP_RAX"
  36. "JUMP_NE %FUNCTION_fgetc_Done"
  37. "LOAD_IMMEDIATE_rax %-1"
  38. ":FUNCTION_fgetc_Done");
  39. }
  40. void fputc(char s, FILE* f)
  41. {
  42. asm("LOAD_IMMEDIATE_rax %1"
  43. "LOAD_EFFECTIVE_ADDRESS_rdi %8"
  44. "LOAD_INTEGER_rdi"
  45. "LOAD_EFFECTIVE_ADDRESS_rsi %16"
  46. "LOAD_IMMEDIATE_rdx %1"
  47. "SYSCALL");
  48. }
  49. FILE* open(char* name, int flag, int mode)
  50. {
  51. asm("LOAD_EFFECTIVE_ADDRESS_rdi %24"
  52. "LOAD_INTEGER_rdi"
  53. "LOAD_EFFECTIVE_ADDRESS_rsi %16"
  54. "LOAD_INTEGER_rsi"
  55. "LOAD_EFFECTIVE_ADDRESS_rdx %8"
  56. "LOAD_INTEGER_rdx"
  57. "LOAD_IMMEDIATE_rax %2"
  58. "SYSCALL");
  59. }
  60. FILE* fopen(char* filename, char* mode)
  61. {
  62. FILE* f;
  63. if('w' == mode[0])
  64. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  65. f = open(filename, 577 , 384);
  66. }
  67. else
  68. { /* Everything else is a read */
  69. f = open(filename, 0, 0);
  70. }
  71. /* Negative numbers are error codes */
  72. if(0 > f)
  73. {
  74. return 0;
  75. }
  76. return f;
  77. }
  78. int close(int fd)
  79. {
  80. asm("LOAD_EFFECTIVE_ADDRESS_rdi %8"
  81. "LOAD_INTEGER_rdi"
  82. "LOAD_IMMEDIATE_rax %3"
  83. "SYSCALL");
  84. }
  85. int fclose(FILE* stream)
  86. {
  87. int error = close(stream);
  88. return error;
  89. }
  90. int brk(void *addr)
  91. {
  92. asm("LOAD_RSP_IMMEDIATE_into_rax %8"
  93. "PUSH_RAX"
  94. "LOAD_IMMEDIATE_rax %12"
  95. "POP_RBX"
  96. "COPY_rbx_to_rdi"
  97. "SYSCALL");
  98. }
  99. long _malloc_ptr;
  100. long _brk_ptr;
  101. void* malloc(int size)
  102. {
  103. if(NULL == _brk_ptr)
  104. {
  105. _brk_ptr = brk(0);
  106. _malloc_ptr = _brk_ptr;
  107. }
  108. if(_brk_ptr < _malloc_ptr + size)
  109. {
  110. _brk_ptr = brk(_malloc_ptr + size);
  111. if(-1 == _brk_ptr) return 0;
  112. }
  113. long old_malloc = _malloc_ptr;
  114. _malloc_ptr = _malloc_ptr + size;
  115. return old_malloc;
  116. }
  117. void* memset(void* ptr, int value, int num)
  118. {
  119. char* s;
  120. for(s = ptr; 0 < num; num = num - 1)
  121. {
  122. s[0] = value;
  123. s = s + 1;
  124. }
  125. }
  126. void* calloc(int count, int size)
  127. {
  128. void* ret = malloc(count * size);
  129. if(NULL == ret) return NULL;
  130. memset(ret, 0, (count * size));
  131. return ret;
  132. }
  133. void free(void* l)
  134. {
  135. return;
  136. }
  137. void exit(int value)
  138. {
  139. asm("POP_RBX"
  140. "POP_RDI"
  141. "LOAD_IMMEDIATE_rax %0x3C"
  142. "SYSCALL");
  143. }