Script.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 void sPointerPrinter(Pointer* b) {
  24. printf("(%d, %d)\n", b->array, b->offset);
  25. }
  26. static IntPrinter intPrinter = sIntPrinter;
  27. static FloatPrinter floatPrinter = sFloatPrinter;
  28. static BoolPrinter boolPrinter = sBoolPrinter;
  29. static PointerPrinter pointerPrinter = sPointerPrinter;
  30. static bool sRead(Script* sc, void* buffer, int length) {
  31. if(sc->readIndex + length > sc->code->length) {
  32. sError(sc, "cannot read expected %d bytes of data from bytecode");
  33. return false;
  34. }
  35. memcpy(buffer, sc->code->code + sc->readIndex, length);
  36. sc->readIndex += length;
  37. return true;
  38. }
  39. static Operation sReadOperation(Script* sc) {
  40. unsigned char c;
  41. if(sRead(sc, &c, 1)) {
  42. return c;
  43. }
  44. return OP_NOTHING;
  45. }
  46. static void* sReserve(Script* sc, int length) {
  47. if(sc->stackIndex + length > SCRIPT_STACK_SIZE) {
  48. sError(sc, "stack overflow");
  49. return NULL;
  50. }
  51. void* p = sc->stack + sc->stackIndex;
  52. sc->stackIndex += length;
  53. return p;
  54. }
  55. static bool sPush(Script* sc, const void* data, int length) {
  56. void* p = sReserve(sc, length);
  57. if(p != NULL) {
  58. memcpy(p, data, length);
  59. return true;
  60. }
  61. return false;
  62. }
  63. static const void* sFree(Script* sc, int length) {
  64. if(sc->stackIndex < length) {
  65. sError(sc, "stack underflow");
  66. return NULL;
  67. }
  68. sc->stackIndex -= length;
  69. return sc->stack + sc->stackIndex;
  70. }
  71. static bool sPop(Script* sc, void* data, int length) {
  72. const void* p = sFree(sc, length);
  73. if(p != NULL) {
  74. memcpy(data, p, length);
  75. return true;
  76. }
  77. return false;
  78. }
  79. static bool sPeek(Script* sc, void* data, int length) {
  80. if(sc->stackIndex < length) {
  81. sError(sc, "stack underflow");
  82. return false;
  83. }
  84. memcpy(data, sc->stack + (sc->stackIndex - length), length);
  85. return true;
  86. }
  87. #define POP_PUSH(type, Type) \
  88. static bool sPop##Type(Script* sc, type* value) { \
  89. return sPop(sc, value, sizeof(type)); \
  90. } \
  91. static bool sPush##Type(Script* sc, type value) { \
  92. return sPush(sc, &value, sizeof(type)); \
  93. }
  94. #define READ_POP_PUSH(type, Type) \
  95. static bool sRead##Type(Script* sc, type* i) { \
  96. return sRead(sc, i, sizeof(type)); \
  97. } \
  98. POP_PUSH(type, Type)
  99. #define PUSH_CONSTANT(type, Type) \
  100. { \
  101. type value; \
  102. if(sRead##Type(sc, &value)) { \
  103. sPush##Type(sc, value); \
  104. } \
  105. }
  106. #define ZERO_CHECK(name) \
  107. if(values[0] == 0) { \
  108. sError(sc, name " by 0"); \
  109. }
  110. #define OP_BASE(type, Type, RType, op, check) \
  111. { \
  112. type values[2]; \
  113. if(sPop##Type(sc, values) && sPop##Type(sc, values + 1)) { \
  114. check; \
  115. sPush##RType(sc, values[1] op values[0]); \
  116. } \
  117. }
  118. #define CHECKED_NUMBER_OP(type, Type, op, check) \
  119. OP_BASE(type, Type, Type, op, check)
  120. #define NUMBER_OP(type, Type, op) CHECKED_NUMBER_OP(type, Type, op, )
  121. #define BOOL_OP(type, Type, op) OP_BASE(type, Type, Bool, op, )
  122. #define DIVISION(type, Type) \
  123. CHECKED_NUMBER_OP(type, Type, /, ZERO_CHECK("division"));
  124. #define MODULE(type, Type) \
  125. CHECKED_NUMBER_OP(type, Type, %, ZERO_CHECK("module"));
  126. READ_POP_PUSH(int, Int)
  127. READ_POP_PUSH(float, Float)
  128. POP_PUSH(bool, Bool)
  129. static bool sPopPointer(Script* sc, Pointer* value) {
  130. return sPop(sc, value, sizeof(Pointer));
  131. }
  132. static bool sPushPointer(Script* sc, Pointer* value) {
  133. return sPush(sc, value, sizeof(Pointer));
  134. }
  135. static void sPrintPointer(Script* sc) {
  136. Pointer p;
  137. if(sPopPointer(sc, &p)) {
  138. pointerPrinter(&p);
  139. }
  140. }
  141. #define PRINT(type, Type, printer) \
  142. { \
  143. type value; \
  144. if(sPop##Type(sc, &value)) { \
  145. printer(value); \
  146. } \
  147. }
  148. #define INVERT_SIGN(type, Type) \
  149. { \
  150. type value = 0; \
  151. if(sPop##Type(sc, &value)) { \
  152. sPush##Type(sc, -value); \
  153. } \
  154. }
  155. static void sReserveBytes(Script* sc) {
  156. int bytes = 0;
  157. int offset = 0;
  158. if(sReadInt(sc, &bytes) && sReadInt(sc, &offset)) {
  159. int oldIndex = sc->stackVarIndex;
  160. sc->stackVarIndex = sc->stackIndex - offset;
  161. sReserve(sc, bytes - offset);
  162. sPushInt(sc, oldIndex);
  163. }
  164. }
  165. static void* sCheckAddress(Script* sc, Pointer* p, int length) {
  166. if(p->array >= 0) {
  167. Array* a = asGet(&sc->arrays, p->array);
  168. if(a == NULL) {
  169. sError(sc, "invalid heap pointer");
  170. return NULL;
  171. } else if(p->offset < 0 || p->offset >= a->size) {
  172. sError(sc, "address %d is out of array bounds", p->offset);
  173. return NULL;
  174. }
  175. return ((char*)a->data) + p->offset;
  176. }
  177. if(p->offset < 0 || p->offset + length > sc->stackIndex) {
  178. sError(sc, "address %d is out of stack bounds", p->offset);
  179. return NULL;
  180. }
  181. return sc->stack + p->offset;
  182. }
  183. static void sNot(Script* sc) {
  184. bool value = false;
  185. if(sPopBool(sc, &value)) {
  186. sPushBool(sc, !value);
  187. }
  188. }
  189. static void sBitNot(Script* sc) {
  190. int value = 0;
  191. if(sPopInt(sc, &value)) {
  192. sPushInt(sc, ~value);
  193. }
  194. }
  195. static void sLine(Script* sc) {
  196. sRead(sc, &sc->line, 2);
  197. }
  198. static void sGoTo(Script* sc) {
  199. int gotoIndex;
  200. if(sReadInt(sc, &gotoIndex)) {
  201. sc->readIndex = gotoIndex;
  202. }
  203. }
  204. static void sGoSub(Script* sc) {
  205. int gotoIndex;
  206. int offset;
  207. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &offset)) {
  208. Pointer p = {.array = -1,
  209. .offset = sc->stackIndex - offset - sizeof(int)};
  210. void* dest = sCheckAddress(sc, &p, sizeof(int));
  211. if(dest != NULL) {
  212. memcpy(dest, &sc->readIndex, sizeof(int));
  213. sc->readIndex = gotoIndex;
  214. }
  215. }
  216. }
  217. static void sReturn(Script* sc) {
  218. int bytes = 0;
  219. int varIndex = 0;
  220. if(sReadInt(sc, &bytes) && sPopInt(sc, &varIndex)) {
  221. sc->stackVarIndex = varIndex;
  222. sFree(sc, bytes);
  223. if(!sPopInt(sc, &sc->readIndex) || sc->readIndex < 0) {
  224. sError(sc, "read index is corrupt");
  225. }
  226. }
  227. }
  228. static void sReturnPointer(Script* sc) {
  229. Pointer p;
  230. if(sPopPointer(sc, &p)) {
  231. sReturn(sc);
  232. sPushPointer(sc, &p);
  233. }
  234. }
  235. #define RETURN(type, Type) \
  236. { \
  237. type value; \
  238. if(sPop##Type(sc, &value)) { \
  239. sReturn(sc); \
  240. sPush##Type(sc, value); \
  241. } \
  242. }
  243. static void sIfGoTo(Script* sc) {
  244. int gotoIndex = 0;
  245. bool value = false;
  246. if(sReadInt(sc, &gotoIndex) && sPopBool(sc, &value) && !value) {
  247. sc->readIndex = gotoIndex;
  248. }
  249. }
  250. static void sPeekFalseGoTo(Script* sc) {
  251. int gotoIndex = 0;
  252. bool value = false;
  253. if(sReadInt(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && !value) {
  254. sc->readIndex = gotoIndex;
  255. }
  256. }
  257. static void sPeekTrueGoTo(Script* sc) {
  258. int gotoIndex = 0;
  259. bool value = false;
  260. if(sReadInt(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && value) {
  261. sc->readIndex = gotoIndex;
  262. }
  263. }
  264. static void sNewArray(Script* sc) {
  265. int length = 0;
  266. int size = 0;
  267. if(sReadInt(sc, &size) && sPopInt(sc, &length)) {
  268. Pointer p = {.array = asAllocate(&sc->arrays, size, length),
  269. .offset = 0};
  270. if(p.array == -1) {
  271. sError(sc, "out of memory");
  272. } else if(p.array == -2) {
  273. sError(sc, "bad allocation");
  274. } else {
  275. sPushPointer(sc, &p);
  276. }
  277. }
  278. }
  279. static void sDeleteArray(Script* sc) {
  280. Pointer p;
  281. if(sPopPointer(sc, &p)) {
  282. if(p.offset != 0) {
  283. sError(sc, "delete of array with offset: %d", p.offset);
  284. return;
  285. }
  286. Array* a = asGet(&sc->arrays, p.array);
  287. if(a == NULL) {
  288. sError(sc, "delete of invalid array");
  289. return;
  290. }
  291. asDeleteArray(&sc->arrays, a, p.array);
  292. }
  293. }
  294. static void sLength(Script* sc) {
  295. Pointer p;
  296. if(sPopPointer(sc, &p)) {
  297. if(p.array == -1) {
  298. sPushInt(sc, 1);
  299. return;
  300. }
  301. Array* a = asGet(&sc->arrays, p.array);
  302. if(a == NULL) {
  303. sError(sc, "invalid heap pointer");
  304. return;
  305. }
  306. sPushInt(sc, a->length);
  307. }
  308. }
  309. static void sDereference(Script* sc) {
  310. int address = 0;
  311. if(sReadInt(sc, &address)) {
  312. Pointer p = {.array = -1, .offset = address + sc->stackVarIndex};
  313. sPushPointer(sc, &p);
  314. }
  315. }
  316. static void sLoad(Script* sc, int length) {
  317. Pointer p;
  318. if(sPopPointer(sc, &p)) {
  319. void* src = sCheckAddress(sc, &p, length);
  320. if(src != NULL) {
  321. sPush(sc, src, length);
  322. }
  323. }
  324. }
  325. static void sDuplicateReference(Script* sc) {
  326. Pointer p;
  327. if(sPeek(sc, &p, sizeof(Pointer))) {
  328. sPushPointer(sc, &p);
  329. }
  330. }
  331. static void sAddReference(Script* sc) {
  332. int size = 0;
  333. int add = 0;
  334. Pointer p;
  335. if(sReadInt(sc, &size) && sPopInt(sc, &add) && sPopPointer(sc, &p)) {
  336. p.offset += add * size;
  337. sPushPointer(sc, &p);
  338. }
  339. }
  340. static void sLoadSize(Script* sc) {
  341. int size = 0;
  342. Pointer p;
  343. if(sReadInt(sc, &size) && sPopPointer(sc, &p)) {
  344. void* src = sCheckAddress(sc, &p, size);
  345. if(src != NULL) {
  346. sPush(sc, src, size);
  347. }
  348. }
  349. }
  350. static void sStore(Script* sc, int length) {
  351. int index = sc->stackIndex - sizeof(Pointer) - length;
  352. if(index < 0) {
  353. sError(sc, "stack underflow");
  354. return;
  355. }
  356. Pointer p;
  357. memcpy(&p, sc->stack + index, sizeof(Pointer));
  358. void* dest = sCheckAddress(sc, &p, length);
  359. if(dest != NULL) {
  360. sPop(sc, dest, length);
  361. sc->stackIndex -= sizeof(Pointer);
  362. }
  363. }
  364. static void sEqualPointer(Script* sc) {
  365. Pointer a;
  366. Pointer b;
  367. if(sPopPointer(sc, &a) && sPopPointer(sc, &b)) {
  368. sPushBool(sc, a.array == b.array && a.offset == b.offset);
  369. }
  370. }
  371. static void sPushPreIntChange(Script* sc) {
  372. char c = 0;
  373. Pointer p;
  374. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) {
  375. void* data = sCheckAddress(sc, &p, sizeof(int));
  376. if(data != NULL) {
  377. int current = 0;
  378. memcpy(&current, data, sizeof(int));
  379. current += c;
  380. sPushInt(sc, current);
  381. memcpy(data, &current, sizeof(int));
  382. }
  383. }
  384. }
  385. static void sPushPostIntChange(Script* sc) {
  386. char c = 0;
  387. Pointer p;
  388. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) {
  389. void* data = sCheckAddress(sc, &p, sizeof(int));
  390. if(data != NULL) {
  391. int current = 0;
  392. memcpy(&current, data, sizeof(int));
  393. sPushInt(sc, current);
  394. current += c;
  395. memcpy(data, &current, sizeof(int));
  396. }
  397. }
  398. }
  399. static void sIntChange(Script* sc) {
  400. char c = 0;
  401. Pointer p;
  402. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) {
  403. void* data = sCheckAddress(sc, &p, sizeof(int));
  404. if(data != NULL) {
  405. int current = 0;
  406. memcpy(&current, data, sizeof(int));
  407. current += c;
  408. memcpy(data, &current, sizeof(int));
  409. }
  410. }
  411. }
  412. #define CASE_NUMBER_OP(name, op) \
  413. case OP_##name##_INT: NUMBER_OP(int, Int, op); break; \
  414. case OP_##name##_FLOAT: \
  415. NUMBER_OP(float, Float, op); \
  416. break;
  417. #define CASE_BOOL_OP(name, op) \
  418. case OP_##name##_INT: BOOL_OP(int, Int, op); break; \
  419. case OP_##name##_FLOAT: \
  420. BOOL_OP(float, Float, op); \
  421. break;
  422. #define CASE_TYPE(TYPE, Type, type) \
  423. case OP_STORE_##TYPE: sStore(sc, sizeof(type)); break; \
  424. case OP_RETURN_##TYPE: RETURN(type, Type); break; \
  425. case OP_PRINT_##TYPE: PRINT(type, Type, type##Printer); break; \
  426. case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); break; \
  427. case OP_LOAD_##TYPE: sLoad(sc, sizeof(type)); break;
  428. static void sConsumeInstruction(Script* sc) {
  429. switch(sReadOperation(sc)) {
  430. CASE_NUMBER_OP(ADD, +);
  431. CASE_NUMBER_OP(SUB, -);
  432. CASE_NUMBER_OP(MUL, *);
  433. CASE_BOOL_OP(LESS, <);
  434. CASE_BOOL_OP(GREATER, >);
  435. CASE_TYPE(INT, Int, int);
  436. CASE_TYPE(BOOL, Bool, bool);
  437. CASE_TYPE(FLOAT, Float, float);
  438. case OP_NOTHING: break;
  439. case OP_PUSH_INT: PUSH_CONSTANT(int, Int); break;
  440. case OP_PUSH_FLOAT: PUSH_CONSTANT(float, Float); break;
  441. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  442. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  443. case OP_DIV_INT: DIVISION(int, Int); break;
  444. case OP_DIV_FLOAT: DIVISION(float, Float); break;
  445. case OP_MOD_INT: MODULE(int, Int); break;
  446. case OP_INVERT_SIGN_INT: INVERT_SIGN(int, Int); break;
  447. case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); break;
  448. case OP_NOT: sNot(sc); break;
  449. case OP_AND: BOOL_OP(bool, Bool, &&); break;
  450. case OP_OR: BOOL_OP(bool, Bool, ||); break;
  451. case OP_BIT_NOT: sBitNot(sc); break;
  452. case OP_BIT_AND: NUMBER_OP(int, Int, &); break;
  453. case OP_BIT_OR: NUMBER_OP(int, Int, |); break;
  454. case OP_BIT_XOR: NUMBER_OP(int, Int, ^); break;
  455. case OP_LEFT_SHIFT: NUMBER_OP(int, Int, <<); break;
  456. case OP_RIGHT_SHIFT: NUMBER_OP(int, Int, >>); break;
  457. case OP_LINE: sLine(sc); break;
  458. case OP_GOTO: sGoTo(sc); break;
  459. case OP_IF_GOTO: sIfGoTo(sc); break;
  460. case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); break;
  461. case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); break;
  462. case OP_GOSUB: sGoSub(sc); break;
  463. case OP_RETURN: sReturn(sc); break;
  464. case OP_RETURN_POINTER: sReturnPointer(sc); break;
  465. case OP_RESERVE: sReserveBytes(sc); break;
  466. case OP_DEREFERENCE_VAR: sDereference(sc); break;
  467. case OP_REFERENCE: sLoad(sc, sizeof(Pointer)); break;
  468. case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); break;
  469. case OP_ADD_REFERENCE: sAddReference(sc); break;
  470. case OP_LOAD: sLoadSize(sc); break;
  471. case OP_NEW: sNewArray(sc); break;
  472. case OP_DELETE: sDeleteArray(sc); break;
  473. case OP_LENGTH: sLength(sc); break;
  474. case OP_STORE_POINTER: sStore(sc, sizeof(Pointer)); break;
  475. case OP_PRINT_POINTER: sPrintPointer(sc); break;
  476. case OP_EQUAL_POINTER: sEqualPointer(sc); break;
  477. case OP_PUSH_PRE_INT_CHANGE: sPushPreIntChange(sc); break;
  478. case OP_PUSH_POST_INT_CHANGE: sPushPostIntChange(sc); break;
  479. case OP_INT_CHANGE: sIntChange(sc); break;
  480. }
  481. }
  482. static bool sHasData(Script* sc) {
  483. return sc->readIndex < sc->code->length;
  484. }
  485. Script* sInit(ByteCode* code) {
  486. Script* sc = malloc(sizeof(Script));
  487. sc->error[0] = '\0';
  488. sc->code = code;
  489. sc->readIndex = 0;
  490. sc->stackIndex = 0;
  491. sc->stackVarIndex = 0;
  492. sc->line = 0;
  493. asInit(&sc->arrays);
  494. return sc;
  495. }
  496. void sDelete(Script* sc) {
  497. bcDelete(sc->code);
  498. asDelete(&sc->arrays);
  499. free(sc);
  500. }
  501. void sRun(Script* sc) {
  502. while(sHasData(sc)) {
  503. sConsumeInstruction(sc);
  504. if(sc->error[0] != '\0') {
  505. puts("error:");
  506. printf(" - info: %s\n", sc->error);
  507. printf(" - line: %d\n", sc->line);
  508. return;
  509. }
  510. }
  511. }
  512. void sSetIntPrinter(IntPrinter p) {
  513. intPrinter = p;
  514. }
  515. void sSetFloatPrinter(FloatPrinter p) {
  516. floatPrinter = p;
  517. }
  518. void sSetBoolPrinter(BoolPrinter p) {
  519. boolPrinter = p;
  520. }
  521. void sSetPointerPrinter(PointerPrinter p) {
  522. pointerPrinter = p;
  523. }