Script.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include <stdarg.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "vm/Operation.h"
  7. #include "vm/Script.h"
  8. static void sError(Script* sc, const char* format, ...) {
  9. va_list args;
  10. va_start(args, format);
  11. vsnprintf(sc->error, SCRIPT_ERROR_SIZE, format, args);
  12. va_end(args);
  13. }
  14. static void sIntPrinter(int i) {
  15. printf("%d\n", i);
  16. }
  17. static void sFloatPrinter(float f) {
  18. printf("%.2f\n", f);
  19. }
  20. static void sBoolPrinter(bool b) {
  21. puts(b ? "true" : "false");
  22. }
  23. static IntPrinter intPrinter = sIntPrinter;
  24. static FloatPrinter floatPrinter = sFloatPrinter;
  25. static BoolPrinter boolPrinter = sBoolPrinter;
  26. static bool sRead(Script* sc, void* buffer, int length) {
  27. if(sc->readIndex + length > sc->code->length) {
  28. sError(sc, "cannot read expected %d bytes of data from bytecode");
  29. return false;
  30. }
  31. memcpy(buffer, sc->code->code + sc->readIndex, length);
  32. sc->readIndex += length;
  33. return true;
  34. }
  35. static Operation sReadOperation(Script* sc) {
  36. unsigned char c;
  37. if(sRead(sc, &c, 1)) {
  38. return c;
  39. }
  40. return OP_NOTHING;
  41. }
  42. static void* sReserve(Script* sc, int length) {
  43. if(sc->stackIndex + length > SCRIPT_STACK_SIZE) {
  44. sError(sc, "stack overflow");
  45. return NULL;
  46. }
  47. void* p = sc->stack + sc->stackIndex;
  48. sc->stackIndex += length;
  49. return p;
  50. }
  51. static bool sPush(Script* sc, const void* data, int length) {
  52. void* p = sReserve(sc, length);
  53. if(p != NULL) {
  54. memcpy(p, data, length);
  55. return true;
  56. }
  57. return false;
  58. }
  59. static const void* sFree(Script* sc, int length) {
  60. if(sc->stackIndex < length) {
  61. sError(sc, "stack underflow");
  62. return NULL;
  63. }
  64. sc->stackIndex -= length;
  65. return sc->stack + sc->stackIndex;
  66. }
  67. static bool sPop(Script* sc, void* data, int length) {
  68. const void* p = sFree(sc, length);
  69. if(p != NULL) {
  70. memcpy(data, p, length);
  71. return true;
  72. }
  73. return false;
  74. }
  75. static bool sPeek(Script* sc, void* data, int length) {
  76. if(sc->stackIndex < length) {
  77. sError(sc, "stack underflow");
  78. return false;
  79. }
  80. memcpy(data, sc->stack + (sc->stackIndex - length), length);
  81. return true;
  82. }
  83. #define POP_PUSH(type, Type) \
  84. static bool sPop##Type(Script* sc, type* value) { \
  85. return sPop(sc, value, sizeof(type)); \
  86. } \
  87. static bool sPush##Type(Script* sc, type value) { \
  88. return sPush(sc, &value, sizeof(type)); \
  89. }
  90. #define READ_POP_PUSH(type, Type) \
  91. static bool sRead##Type(Script* sc, type* i) { \
  92. return sRead(sc, i, sizeof(type)); \
  93. } \
  94. POP_PUSH(type, Type)
  95. #define PUSH_CONSTANT(type, Type) \
  96. { \
  97. type value; \
  98. if(sRead##Type(sc, &value)) { \
  99. sPush##Type(sc, value); \
  100. } \
  101. }
  102. #define ZERO_CHECK(name) \
  103. if(values[0] == 0) { \
  104. sError(sc, name " by 0"); \
  105. }
  106. #define OP_BASE(type, Type, RType, op, check) \
  107. { \
  108. type values[2]; \
  109. if(sPop##Type(sc, values) && sPop##Type(sc, values + 1)) { \
  110. check; \
  111. sPush##RType(sc, values[1] op values[0]); \
  112. } \
  113. }
  114. #define CHECKED_NUMBER_OP(type, Type, op, check) \
  115. OP_BASE(type, Type, Type, op, check)
  116. #define NUMBER_OP(type, Type, op) CHECKED_NUMBER_OP(type, Type, op, )
  117. #define BOOL_OP(type, Type, op) OP_BASE(type, Type, Bool, op, )
  118. #define DIVISION(type, Type) \
  119. CHECKED_NUMBER_OP(type, Type, /, ZERO_CHECK("division"));
  120. #define MODULE(type, Type) \
  121. CHECKED_NUMBER_OP(type, Type, %, ZERO_CHECK("module"));
  122. READ_POP_PUSH(int, Int)
  123. READ_POP_PUSH(float, Float)
  124. POP_PUSH(bool, Bool)
  125. #define PRINT(type, Type, printer) \
  126. { \
  127. type value; \
  128. if(sPop##Type(sc, &value)) { \
  129. printer(value); \
  130. } \
  131. }
  132. #define INVERT_SIGN(type, Type) \
  133. { \
  134. type value = 0; \
  135. if(sPop##Type(sc, &value)) { \
  136. sPush##Type(sc, -value); \
  137. } \
  138. }
  139. static void sReserveBytes(Script* sc) {
  140. int bytes = 0;
  141. int offset = 0;
  142. if(sReadInt(sc, &bytes) && sReadInt(sc, &offset)) {
  143. int oldIndex = sc->stackVarIndex;
  144. sc->stackVarIndex = sc->stackIndex - offset;
  145. sReserve(sc, bytes - offset);
  146. sPushInt(sc, oldIndex);
  147. }
  148. }
  149. static bool sCheckAddress(Script* sc, int address, int length) {
  150. if(address < 0 || address + length > sc->stackIndex) {
  151. sError(sc, "address is out of stack bounds");
  152. return false;
  153. }
  154. return true;
  155. }
  156. static void sLoad(Script* sc, int length) {
  157. int address = -1;
  158. if(sReadInt(sc, &address)) {
  159. address += sc->stackVarIndex;
  160. if(sCheckAddress(sc, address, length)) {
  161. sPush(sc, sc->stack + address, length);
  162. }
  163. }
  164. }
  165. static void sStore(Script* sc, int length) {
  166. int address = -1;
  167. if(sReadInt(sc, &address)) {
  168. address += sc->stackVarIndex;
  169. if(sCheckAddress(sc, address, length)) {
  170. sPop(sc, sc->stack + address, length);
  171. }
  172. }
  173. }
  174. static void sNot(Script* sc) {
  175. bool value = false;
  176. if(sPopBool(sc, &value)) {
  177. sPushBool(sc, !value);
  178. }
  179. }
  180. static void sBitNot(Script* sc) {
  181. int value = 0;
  182. if(sPopInt(sc, &value)) {
  183. sPushInt(sc, ~value);
  184. }
  185. }
  186. static void sLine(Script* sc) {
  187. sRead(sc, &sc->line, 2);
  188. }
  189. static void sGoTo(Script* sc) {
  190. int gotoIndex;
  191. if(sReadInt(sc, &gotoIndex)) {
  192. sc->readIndex = gotoIndex;
  193. }
  194. }
  195. static void sGoSub(Script* sc) {
  196. int gotoIndex;
  197. int offset;
  198. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &offset)) {
  199. int address = sc->stackIndex - offset - sizeof(int);
  200. if(sCheckAddress(sc, address, sizeof(int))) {
  201. memcpy(sc->stack + address, &sc->readIndex, sizeof(int));
  202. sc->readIndex = gotoIndex;
  203. }
  204. }
  205. }
  206. static void sReturn(Script* sc) {
  207. int bytes = 0;
  208. int varIndex = 0;
  209. if(sReadInt(sc, &bytes) && sPopInt(sc, &varIndex)) {
  210. sc->stackVarIndex = varIndex;
  211. sFree(sc, bytes);
  212. int returnIndex;
  213. if(sPopInt(sc, &returnIndex)) {
  214. sc->readIndex = returnIndex;
  215. }
  216. }
  217. }
  218. #define RETURN(type, Type) \
  219. { \
  220. type value; \
  221. if(sPop##Type(sc, &value)) { \
  222. sReturn(sc); \
  223. sPush##Type(sc, value); \
  224. } \
  225. }
  226. static void sIfGoTo(Script* sc) {
  227. int gotoIndex = 0;
  228. bool value = false;
  229. if(sReadInt(sc, &gotoIndex) && sPopBool(sc, &value) && !value) {
  230. sc->readIndex = gotoIndex;
  231. }
  232. }
  233. static void sPeekFalseGoTo(Script* sc) {
  234. int gotoIndex = 0;
  235. bool value = false;
  236. if(sReadInt(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && !value) {
  237. sc->readIndex = gotoIndex;
  238. }
  239. }
  240. static void sPeekTrueGoTo(Script* sc) {
  241. int gotoIndex = 0;
  242. bool value = false;
  243. if(sReadInt(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && value) {
  244. sc->readIndex = gotoIndex;
  245. }
  246. }
  247. static void sIntArray(Script* sc) {
  248. (void)sc;
  249. }
  250. #define CASE_NUMBER_OP(name, op) \
  251. case OP_##name##_INT: NUMBER_OP(int, Int, op); break; \
  252. case OP_##name##_FLOAT: \
  253. NUMBER_OP(float, Float, op); \
  254. break;
  255. #define CASE_BOOL_OP(name, op) \
  256. case OP_##name##_INT: BOOL_OP(int, Int, op); break; \
  257. case OP_##name##_FLOAT: \
  258. BOOL_OP(float, Float, op); \
  259. break;
  260. #define CASE_TYPE(TYPE, Type, type) \
  261. case OP_LOAD_##TYPE: sLoad(sc, sizeof(type)); break; \
  262. case OP_STORE_##TYPE: sStore(sc, sizeof(type)); break; \
  263. case OP_RETURN_##TYPE: RETURN(type, Type); break; \
  264. case OP_PRINT_##TYPE: PRINT(type, Type, type##Printer); break; \
  265. case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); break;
  266. static void sConsumeInstruction(Script* sc) {
  267. switch(sReadOperation(sc)) {
  268. CASE_NUMBER_OP(ADD, +);
  269. CASE_NUMBER_OP(SUB, -);
  270. CASE_NUMBER_OP(MUL, *);
  271. CASE_BOOL_OP(LESS, <);
  272. CASE_BOOL_OP(GREATER, >);
  273. CASE_TYPE(INT, Int, int);
  274. CASE_TYPE(BOOL, Bool, bool);
  275. CASE_TYPE(FLOAT, Float, float);
  276. case OP_NOTHING: break;
  277. case OP_PUSH_INT: PUSH_CONSTANT(int, Int); break;
  278. case OP_PUSH_FLOAT: PUSH_CONSTANT(float, Float); break;
  279. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  280. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  281. case OP_DIV_INT: DIVISION(int, Int); break;
  282. case OP_DIV_FLOAT: DIVISION(float, Float); break;
  283. case OP_MOD_INT: MODULE(int, Int); break;
  284. case OP_INVERT_SIGN_INT: INVERT_SIGN(int, Int); break;
  285. case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); break;
  286. case OP_NOT: sNot(sc); break;
  287. case OP_AND: BOOL_OP(bool, Bool, &&); break;
  288. case OP_OR: BOOL_OP(bool, Bool, ||); break;
  289. case OP_BIT_NOT: sBitNot(sc); break;
  290. case OP_BIT_AND: NUMBER_OP(int, Int, &); break;
  291. case OP_BIT_OR: NUMBER_OP(int, Int, |); break;
  292. case OP_BIT_XOR: NUMBER_OP(int, Int, ^); break;
  293. case OP_LEFT_SHIFT: NUMBER_OP(int, Int, <<); break;
  294. case OP_RIGHT_SHIFT: NUMBER_OP(int, Int, >>); break;
  295. case OP_LINE: sLine(sc); break;
  296. case OP_GOTO: sGoTo(sc); break;
  297. case OP_IF_GOTO: sIfGoTo(sc); break;
  298. case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); break;
  299. case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); break;
  300. case OP_GOSUB: sGoSub(sc); break;
  301. case OP_RETURN: sReturn(sc); break;
  302. case OP_RESERVE: sReserveBytes(sc); break;
  303. case OP_INT_ARRAY: sIntArray(sc); break;
  304. case OP_STORE_ARRAY: sStore(sc, sizeof(int)); break;
  305. }
  306. // sCollectGarbage(sc);
  307. }
  308. static bool sHasData(Script* sc) {
  309. return sc->readIndex < sc->code->length;
  310. }
  311. Script* sInit(ByteCode* code) {
  312. Script* sc = malloc(sizeof(Script));
  313. sc->error[0] = '\0';
  314. sc->code = code;
  315. sc->readIndex = 0;
  316. sc->stackIndex = 0;
  317. sc->stackVarIndex = 0;
  318. sc->line = 0;
  319. // aInit(&sc->allocator);
  320. return sc;
  321. }
  322. void sDelete(Script* sc) {
  323. bcDelete(sc->code);
  324. // aDelete(&sc->allocator);
  325. free(sc);
  326. }
  327. void sRun(Script* sc) {
  328. while(sHasData(sc)) {
  329. sConsumeInstruction(sc);
  330. if(sc->error[0] != '\0') {
  331. puts("error:");
  332. printf(" - info: %s\n", sc->error);
  333. printf(" - line: %d\n", sc->line);
  334. return;
  335. }
  336. }
  337. }
  338. void sSetIntPrinter(IntPrinter p) {
  339. intPrinter = p;
  340. }
  341. void sSetFloatPrinter(FloatPrinter p) {
  342. floatPrinter = p;
  343. }
  344. void sSetBoolPrinter(BoolPrinter p) {
  345. boolPrinter = p;
  346. }
  347. /*static void sMark(Script* sc, Object* o) {
  348. if(o->type == OT_ARRAY) {
  349. Array* a = sc->allocator.data + o->as.intValue;
  350. a->marked = true;
  351. for(int i = 0; i < a->length; i++) {
  352. sMark(sc, a->data + i);
  353. }
  354. }
  355. }
  356. void sCollectGarbage(Script* sc) {
  357. aClearMarker(&sc->allocator);
  358. for(int i = 0; i < sc->stackIndex; i++) {
  359. sMark(sc, sc->stack + i);
  360. }
  361. aRemoveUnmarked(&sc->allocator);
  362. }*/