vm.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 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. * stage0 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 stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include "vm_types.h"
  20. /* Prototypes for functions in vm_instructions.c*/
  21. extern void vm_HALT(struct lilith* vm, uint64_t performance_counter);
  22. extern void vm_SYS_EXIT(struct lilith* vm, uint64_t performance_counter);
  23. extern void vm_SYS_CHMOD(struct lilith* vm);
  24. extern void vm_SYS_UNAME(struct lilith* vm);
  25. extern void vm_SYS_GETCWD(struct lilith* vm);
  26. extern void vm_SYS_CHDIR(struct lilith* vm);
  27. extern void vm_SYS_FCHDIR(struct lilith* vm);
  28. extern void vm_SYS_ACCESS(struct lilith* vm);
  29. extern void vm_SYS_FOPEN(struct lilith* vm);
  30. extern void vm_FOPEN_READ(struct lilith* vm);
  31. extern void vm_FOPEN_WRITE(struct lilith* vm);
  32. extern void vm_SYS_FCLOSE(struct lilith* vm);
  33. extern void vm_FCLOSE(struct lilith* vm);
  34. extern void vm_SYS_FSEEK(struct lilith* vm);
  35. extern void vm_REWIND(struct lilith* vm);
  36. extern void vm_FGETC(struct lilith* vm);
  37. extern void vm_SYS_READ(struct lilith* vm);
  38. extern void vm_FPUTC(struct lilith* vm);
  39. extern void vm_SYS_WRITE(struct lilith* vm);
  40. extern void vm_HAL_MEM(struct lilith* vm);
  41. extern void ADD_CI(struct lilith* vm, struct Instruction* c);
  42. extern void ADD_CO(struct lilith* vm, struct Instruction* c);
  43. extern void ADD_CIO(struct lilith* vm, struct Instruction* c);
  44. extern void ADDU_CI(struct lilith* vm, struct Instruction* c);
  45. extern void ADDU_CO(struct lilith* vm, struct Instruction* c);
  46. extern void ADDU_CIO(struct lilith* vm, struct Instruction* c);
  47. extern void SUB_BI(struct lilith* vm, struct Instruction* c);
  48. extern void SUB_BO(struct lilith* vm, struct Instruction* c);
  49. extern void SUB_BIO(struct lilith* vm, struct Instruction* c);
  50. extern void SUBU_BI(struct lilith* vm, struct Instruction* c);
  51. extern void SUBU_BO(struct lilith* vm, struct Instruction* c);
  52. extern void SUBU_BIO(struct lilith* vm, struct Instruction* c);
  53. extern void MULTIPLY(struct lilith* vm, struct Instruction* c);
  54. extern void MULTIPLYU(struct lilith* vm, struct Instruction* c);
  55. extern void DIVIDE(struct lilith* vm, struct Instruction* c);
  56. extern void DIVIDEU(struct lilith* vm, struct Instruction* c);
  57. extern void MUX(struct lilith* vm, struct Instruction* c);
  58. extern void NMUX(struct lilith* vm, struct Instruction* c);
  59. extern void SORT(struct lilith* vm, struct Instruction* c);
  60. extern void SORTU(struct lilith* vm, struct Instruction* c);
  61. extern void ADD(struct lilith* vm, struct Instruction* c);
  62. extern void ADDU(struct lilith* vm, struct Instruction* c);
  63. extern void SUB(struct lilith* vm, struct Instruction* c);
  64. extern void SUBU(struct lilith* vm, struct Instruction* c);
  65. extern void CMP(struct lilith* vm, struct Instruction* c);
  66. extern void CMPU(struct lilith* vm, struct Instruction* c);
  67. extern void MUL(struct lilith* vm, struct Instruction* c);
  68. extern void MULH(struct lilith* vm, struct Instruction* c);
  69. extern void MULU(struct lilith* vm, struct Instruction* c);
  70. extern void MULUH(struct lilith* vm, struct Instruction* c);
  71. extern void DIV(struct lilith* vm, struct Instruction* c);
  72. extern void MOD(struct lilith* vm, struct Instruction* c);
  73. extern void DIVU(struct lilith* vm, struct Instruction* c);
  74. extern void MODU(struct lilith* vm, struct Instruction* c);
  75. extern void MAX(struct lilith* vm, struct Instruction* c);
  76. extern void MAXU(struct lilith* vm, struct Instruction* c);
  77. extern void MIN(struct lilith* vm, struct Instruction* c);
  78. extern void MINU(struct lilith* vm, struct Instruction* c);
  79. extern void AND(struct lilith* vm, struct Instruction* c);
  80. extern void OR(struct lilith* vm, struct Instruction* c);
  81. extern void XOR(struct lilith* vm, struct Instruction* c);
  82. extern void NAND(struct lilith* vm, struct Instruction* c);
  83. extern void NOR(struct lilith* vm, struct Instruction* c);
  84. extern void XNOR(struct lilith* vm, struct Instruction* c);
  85. extern void MPQ(struct lilith* vm, struct Instruction* c);
  86. extern void LPQ(struct lilith* vm, struct Instruction* c);
  87. extern void CPQ(struct lilith* vm, struct Instruction* c);
  88. extern void BPQ(struct lilith* vm, struct Instruction* c);
  89. extern void SAL(struct lilith* vm, struct Instruction* c);
  90. extern void SAR(struct lilith* vm, struct Instruction* c);
  91. extern void SL0(struct lilith* vm, struct Instruction* c);
  92. extern void SR0(struct lilith* vm, struct Instruction* c);
  93. extern void SL1(struct lilith* vm, struct Instruction* c);
  94. extern void SR1(struct lilith* vm, struct Instruction* c);
  95. extern void ROL(struct lilith* vm, struct Instruction* c);
  96. extern void ROR(struct lilith* vm, struct Instruction* c);
  97. extern void LOADX(struct lilith* vm, struct Instruction* c);
  98. extern void LOADX8(struct lilith* vm, struct Instruction* c);
  99. extern void LOADXU8(struct lilith* vm, struct Instruction* c);
  100. extern void LOADX16(struct lilith* vm, struct Instruction* c);
  101. extern void LOADXU16(struct lilith* vm, struct Instruction* c);
  102. extern void LOADX32(struct lilith* vm, struct Instruction* c);
  103. extern void LOADXU32(struct lilith* vm, struct Instruction* c);
  104. extern void STOREX(struct lilith* vm, struct Instruction* c);
  105. extern void STOREX8(struct lilith* vm, struct Instruction* c);
  106. extern void STOREX16(struct lilith* vm, struct Instruction* c);
  107. extern void STOREX32(struct lilith* vm, struct Instruction* c);
  108. extern void NEG(struct lilith* vm, struct Instruction* c);
  109. extern void ABS(struct lilith* vm, struct Instruction* c);
  110. extern void NABS(struct lilith* vm, struct Instruction* c);
  111. extern void SWAP(struct lilith* vm, struct Instruction* c);
  112. extern void COPY(struct lilith* vm, struct Instruction* c);
  113. extern void MOVE(struct lilith* vm, struct Instruction* c);
  114. extern void BRANCH(struct lilith* vm, struct Instruction* c);
  115. extern void CALL(struct lilith* vm, struct Instruction* c);
  116. extern void READPC(struct lilith* vm, struct Instruction* c);
  117. extern void READSCID(struct lilith* vm, struct Instruction* c);
  118. extern void FALSE(struct lilith* vm, struct Instruction* c);
  119. extern void TRUE(struct lilith* vm, struct Instruction* c);
  120. extern void JSR_COROUTINE(struct lilith* vm, struct Instruction* c);
  121. extern void RET(struct lilith* vm, struct Instruction* c);
  122. extern void PUSHPC(struct lilith* vm, struct Instruction* c);
  123. extern void POPPC(struct lilith* vm, struct Instruction* c);
  124. extern void ADDI(struct lilith* vm, struct Instruction* c);
  125. extern void ADDUI(struct lilith* vm, struct Instruction* c);
  126. extern void SUBI(struct lilith* vm, struct Instruction* c);
  127. extern void SUBUI(struct lilith* vm, struct Instruction* c);
  128. extern void CMPI(struct lilith* vm, struct Instruction* c);
  129. extern void LOAD(struct lilith* vm, struct Instruction* c);
  130. extern void LOAD8(struct lilith* vm, struct Instruction* c);
  131. extern void LOADU8(struct lilith* vm, struct Instruction* c);
  132. extern void LOAD16(struct lilith* vm, struct Instruction* c);
  133. extern void LOADU16(struct lilith* vm, struct Instruction* c);
  134. extern void LOAD32(struct lilith* vm, struct Instruction* c);
  135. extern void LOADU32(struct lilith* vm, struct Instruction* c);
  136. extern void CMPUI(struct lilith* vm, struct Instruction* c);
  137. extern void STORE(struct lilith* vm, struct Instruction* c);
  138. extern void STORE8(struct lilith* vm, struct Instruction* c);
  139. extern void STORE16(struct lilith* vm, struct Instruction* c);
  140. extern void STORE32(struct lilith* vm, struct Instruction* c);
  141. extern void JUMP_C(struct lilith* vm, struct Instruction* c);
  142. extern void JUMP_B(struct lilith* vm, struct Instruction* c);
  143. extern void JUMP_O(struct lilith* vm, struct Instruction* c);
  144. extern void JUMP_G(struct lilith* vm, struct Instruction* c);
  145. extern void JUMP_GE(struct lilith* vm, struct Instruction* c);
  146. extern void JUMP_E(struct lilith* vm, struct Instruction* c);
  147. extern void JUMP_NE(struct lilith* vm, struct Instruction* c);
  148. extern void JUMP_LE(struct lilith* vm, struct Instruction* c);
  149. extern void JUMP_L(struct lilith* vm, struct Instruction* c);
  150. extern void JUMP_Z(struct lilith* vm, struct Instruction* c);
  151. extern void JUMP_NZ(struct lilith* vm, struct Instruction* c);
  152. extern void CALLI(struct lilith* vm, struct Instruction* c);
  153. extern void LOADI(struct lilith* vm, struct Instruction* c);
  154. extern void LOADUI(struct lilith* vm, struct Instruction* c);
  155. extern void SALI(struct lilith* vm, struct Instruction* c);
  156. extern void SARI(struct lilith* vm, struct Instruction* c);
  157. extern void SL0I(struct lilith* vm, struct Instruction* c);
  158. extern void SR0I(struct lilith* vm, struct Instruction* c);
  159. extern void SL1I(struct lilith* vm, struct Instruction* c);
  160. extern void SR1I(struct lilith* vm, struct Instruction* c);
  161. extern void LOADR(struct lilith* vm, struct Instruction* c);
  162. extern void LOADR8(struct lilith* vm, struct Instruction* c);
  163. extern void LOADRU8(struct lilith* vm, struct Instruction* c);
  164. extern void LOADR16(struct lilith* vm, struct Instruction* c);
  165. extern void LOADRU16(struct lilith* vm, struct Instruction* c);
  166. extern void LOADR32(struct lilith* vm, struct Instruction* c);
  167. extern void LOADRU32(struct lilith* vm, struct Instruction* c);
  168. extern void STORER(struct lilith* vm, struct Instruction* c);
  169. extern void STORER8(struct lilith* vm, struct Instruction* c);
  170. extern void STORER16(struct lilith* vm, struct Instruction* c);
  171. extern void STORER32(struct lilith* vm, struct Instruction* c);
  172. extern void JUMP(struct lilith* vm, struct Instruction* c);
  173. extern void JUMP_P(struct lilith* vm, struct Instruction* c);
  174. extern void JUMP_NP(struct lilith* vm, struct Instruction* c);
  175. extern void CMPJUMPI_G(struct lilith* vm, struct Instruction* c);
  176. extern void CMPJUMPI_GE(struct lilith* vm, struct Instruction* c);
  177. extern void CMPJUMPI_E(struct lilith* vm, struct Instruction* c);
  178. extern void CMPJUMPI_NE(struct lilith* vm, struct Instruction* c);
  179. extern void CMPJUMPI_LE(struct lilith* vm, struct Instruction* c);
  180. extern void CMPJUMPI_L(struct lilith* vm, struct Instruction* c);
  181. extern void CMPJUMPUI_G(struct lilith* vm, struct Instruction* c);
  182. extern void CMPJUMPUI_GE(struct lilith* vm, struct Instruction* c);
  183. extern void CMPJUMPUI_LE(struct lilith* vm, struct Instruction* c);
  184. extern void CMPJUMPUI_L(struct lilith* vm, struct Instruction* c);
  185. extern void CMPSKIPI_G(struct lilith* vm, struct Instruction* c);
  186. extern void CMPSKIPI_GE(struct lilith* vm, struct Instruction* c);
  187. extern void CMPSKIPI_E(struct lilith* vm, struct Instruction* c);
  188. extern void CMPSKIPI_NE(struct lilith* vm, struct Instruction* c);
  189. extern void CMPSKIPI_LE(struct lilith* vm, struct Instruction* c);
  190. extern void CMPSKIPI_L(struct lilith* vm, struct Instruction* c);
  191. extern void CMPSKIPUI_G(struct lilith* vm, struct Instruction* c);
  192. extern void CMPSKIPUI_GE(struct lilith* vm, struct Instruction* c);
  193. extern void CMPSKIPUI_LE(struct lilith* vm, struct Instruction* c);
  194. extern void CMPSKIPUI_L(struct lilith* vm, struct Instruction* c);
  195. extern void PUSHR(struct lilith* vm, struct Instruction* c);
  196. extern void PUSH8(struct lilith* vm, struct Instruction* c);
  197. extern void PUSH16(struct lilith* vm, struct Instruction* c);
  198. extern void PUSH32(struct lilith* vm, struct Instruction* c);
  199. extern void POPR(struct lilith* vm, struct Instruction* c);
  200. extern void POP8(struct lilith* vm, struct Instruction* c);
  201. extern void POPU8(struct lilith* vm, struct Instruction* c);
  202. extern void POP16(struct lilith* vm, struct Instruction* c);
  203. extern void POPU16(struct lilith* vm, struct Instruction* c);
  204. extern void POP32(struct lilith* vm, struct Instruction* c);
  205. extern void POPU32(struct lilith* vm, struct Instruction* c);
  206. extern void ANDI(struct lilith* vm, struct Instruction* c);
  207. extern void ORI(struct lilith* vm, struct Instruction* c);
  208. extern void XORI(struct lilith* vm, struct Instruction* c);
  209. extern void NANDI(struct lilith* vm, struct Instruction* c);
  210. extern void NORI(struct lilith* vm, struct Instruction* c);
  211. extern void XNORI(struct lilith* vm, struct Instruction* c);
  212. extern void NOT(struct lilith* vm, struct Instruction* c);
  213. extern void CMPSKIP_G(struct lilith* vm, struct Instruction* c);
  214. extern void CMPSKIP_GE(struct lilith* vm, struct Instruction* c);
  215. extern void CMPSKIP_E(struct lilith* vm, struct Instruction* c);
  216. extern void CMPSKIP_NE(struct lilith* vm, struct Instruction* c);
  217. extern void CMPSKIP_LE(struct lilith* vm, struct Instruction* c);
  218. extern void CMPSKIP_L(struct lilith* vm, struct Instruction* c);
  219. extern void CMPSKIPU_G(struct lilith* vm, struct Instruction* c);
  220. extern void CMPSKIPU_GE(struct lilith* vm, struct Instruction* c);
  221. extern void CMPSKIPU_LE(struct lilith* vm, struct Instruction* c);
  222. extern void CMPSKIPU_L(struct lilith* vm, struct Instruction* c);
  223. extern void CMPJUMP_G(struct lilith* vm, struct Instruction* c);
  224. extern void CMPJUMP_GE(struct lilith* vm, struct Instruction* c);
  225. extern void CMPJUMP_E(struct lilith* vm, struct Instruction* c);
  226. extern void CMPJUMP_NE(struct lilith* vm, struct Instruction* c);
  227. extern void CMPJUMP_LE(struct lilith* vm, struct Instruction* c);
  228. extern void CMPJUMP_L(struct lilith* vm, struct Instruction* c);
  229. extern void CMPJUMPU_G(struct lilith* vm, struct Instruction* c);
  230. extern void CMPJUMPU_GE(struct lilith* vm, struct Instruction* c);
  231. extern void CMPJUMPU_LE(struct lilith* vm, struct Instruction* c);
  232. extern void CMPJUMPU_L(struct lilith* vm, struct Instruction* c);
  233. extern void SET_G(struct lilith* vm, struct Instruction* c);
  234. extern void SET_GE(struct lilith* vm, struct Instruction* c);
  235. extern void SET_E(struct lilith* vm, struct Instruction* c);
  236. extern void SET_NE(struct lilith* vm, struct Instruction* c);
  237. extern void SET_LE(struct lilith* vm, struct Instruction* c);
  238. extern void SET_L(struct lilith* vm, struct Instruction* c);
  239. /* Prototypes for functions in vm_decode.c*/
  240. extern struct lilith* create_vm(size_t size);
  241. extern void destroy_vm(struct lilith* vm);
  242. extern void read_instruction(struct lilith* vm, struct Instruction *current);
  243. extern void eval_instruction(struct lilith* vm, struct Instruction* current);
  244. extern void outside_of_world(struct lilith* vm, unsigned_vm_register place, char* message);
  245. /* Allow tape names to be effectively changed */
  246. extern char* tape_01_name;
  247. extern char* tape_02_name;
  248. extern FILE* TTY_out;
  249. extern FILE* TTY_in;
  250. /* Enable POSIX Mode */
  251. extern bool POSIX_MODE;
  252. extern bool FUZZING;
  253. extern int Memory_Size;
  254. /* Commonly useful functions */
  255. extern void require(int boolean, char* error);
  256. extern int match(char* a, char* b);