Script.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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->length) {
  172. sError(sc, "address %d is out of array bounds", p->offset);
  173. return NULL;
  174. } else if(length != a->typeSize) {
  175. sError(sc, "sizes do not match: %d != %d", length, a->typeSize);
  176. return NULL;
  177. }
  178. return ((char*)a->data) + p->offset * a->typeSize;
  179. }
  180. if(p->offset < 0 || p->offset + length > sc->stackIndex) {
  181. sError(sc, "address %d is out of stack bounds", p->offset);
  182. return NULL;
  183. }
  184. return sc->stack + p->offset;
  185. }
  186. static void sNot(Script* sc) {
  187. bool value = false;
  188. if(sPopBool(sc, &value)) {
  189. sPushBool(sc, !value);
  190. }
  191. }
  192. static void sBitNot(Script* sc) {
  193. int value = 0;
  194. if(sPopInt(sc, &value)) {
  195. sPushInt(sc, ~value);
  196. }
  197. }
  198. static void sLine(Script* sc) {
  199. sRead(sc, &sc->line, 2);
  200. }
  201. static void sGoTo(Script* sc) {
  202. int gotoIndex;
  203. if(sReadInt(sc, &gotoIndex)) {
  204. sc->readIndex = gotoIndex;
  205. }
  206. }
  207. static void sGoSub(Script* sc) {
  208. int gotoIndex;
  209. int offset;
  210. if(sReadInt(sc, &gotoIndex) && sReadInt(sc, &offset)) {
  211. Pointer p = {.array = -1,
  212. .offset = sc->stackIndex - offset - sizeof(int)};
  213. void* dest = sCheckAddress(sc, &p, sizeof(int));
  214. if(dest != NULL) {
  215. memcpy(dest, &sc->readIndex, sizeof(int));
  216. sc->readIndex = gotoIndex;
  217. }
  218. }
  219. }
  220. static void sReturn(Script* sc) {
  221. int bytes = 0;
  222. int varIndex = 0;
  223. if(sReadInt(sc, &bytes) && sPopInt(sc, &varIndex)) {
  224. sc->stackVarIndex = varIndex;
  225. sFree(sc, bytes);
  226. if(!sPopInt(sc, &sc->readIndex) || sc->readIndex < 0) {
  227. sError(sc, "read index is corrupt");
  228. }
  229. }
  230. }
  231. #define RETURN(type, Type) \
  232. { \
  233. type value; \
  234. if(sPop##Type(sc, &value)) { \
  235. sReturn(sc); \
  236. sPush##Type(sc, value); \
  237. } \
  238. }
  239. static void sIfGoTo(Script* sc) {
  240. int gotoIndex = 0;
  241. bool value = false;
  242. if(sReadInt(sc, &gotoIndex) && sPopBool(sc, &value) && !value) {
  243. sc->readIndex = gotoIndex;
  244. }
  245. }
  246. static void sPeekFalseGoTo(Script* sc) {
  247. int gotoIndex = 0;
  248. bool value = false;
  249. if(sReadInt(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && !value) {
  250. sc->readIndex = gotoIndex;
  251. }
  252. }
  253. static void sPeekTrueGoTo(Script* sc) {
  254. int gotoIndex = 0;
  255. bool value = false;
  256. if(sReadInt(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && value) {
  257. sc->readIndex = gotoIndex;
  258. }
  259. }
  260. static void sNewArray(Script* sc) {
  261. int length = 0;
  262. int size = 0;
  263. if(sReadInt(sc, &size) && sPopInt(sc, &length)) {
  264. Pointer p = {.array = asAllocate(&sc->arrays, size, length),
  265. .offset = 0};
  266. if(p.array == -1) {
  267. sError(sc, "out of memory");
  268. } else if(p.array == -2) {
  269. sError(sc, "bad allocation");
  270. } else {
  271. sPushPointer(sc, &p);
  272. }
  273. }
  274. }
  275. static void sDeleteArray(Script* sc) {
  276. Pointer p;
  277. if(sPopPointer(sc, &p)) {
  278. if(p.offset != 0) {
  279. sError(sc, "delete of array with offset: %d", p.offset);
  280. return;
  281. }
  282. Array* a = asGet(&sc->arrays, p.array);
  283. if(a == NULL) {
  284. sError(sc, "delete of invalid array");
  285. return;
  286. }
  287. asDeleteArray(&sc->arrays, a, p.array);
  288. }
  289. }
  290. static void sLength(Script* sc) {
  291. Pointer p;
  292. if(sPopPointer(sc, &p)) {
  293. if(p.array == -1) {
  294. sPushInt(sc, 1);
  295. return;
  296. }
  297. Array* a = asGet(&sc->arrays, p.array);
  298. if(a == NULL) {
  299. sError(sc, "invalid heap pointer");
  300. return;
  301. }
  302. sPushInt(sc, a->length);
  303. }
  304. }
  305. static void sDereference(Script* sc) {
  306. int address = 0;
  307. if(sReadInt(sc, &address)) {
  308. Pointer p = {.array = -1, .offset = address + sc->stackVarIndex};
  309. sPushPointer(sc, &p);
  310. }
  311. }
  312. static void sLoad(Script* sc, int length) {
  313. Pointer p;
  314. if(sPopPointer(sc, &p)) {
  315. void* src = sCheckAddress(sc, &p, length);
  316. if(src != NULL) {
  317. sPush(sc, src, length);
  318. }
  319. }
  320. }
  321. static void sDuplicateReference(Script* sc) {
  322. Pointer p;
  323. if(sPeek(sc, &p, sizeof(Pointer))) {
  324. sPushPointer(sc, &p);
  325. }
  326. }
  327. static void sAddReference(Script* sc) {
  328. int add = 0;
  329. Pointer p;
  330. if(sPopInt(sc, &add) && sPopPointer(sc, &p)) {
  331. p.offset += add;
  332. sPushPointer(sc, &p);
  333. }
  334. }
  335. static void sLoadSize(Script* sc) {
  336. int size = 0;
  337. Pointer p;
  338. if(sReadInt(sc, &size) && sPopPointer(sc, &p)) {
  339. void* src = sCheckAddress(sc, &p, size);
  340. if(src != NULL) {
  341. sPush(sc, src, size);
  342. }
  343. }
  344. }
  345. static void sStore(Script* sc, int length) {
  346. int index = sc->stackIndex - sizeof(Pointer) - length;
  347. if(index < 0) {
  348. sError(sc, "stack underflow");
  349. return;
  350. }
  351. Pointer p;
  352. memcpy(&p, sc->stack + index, sizeof(Pointer));
  353. void* dest = sCheckAddress(sc, &p, length);
  354. if(dest != NULL) {
  355. sPop(sc, dest, length);
  356. sc->stackIndex -= sizeof(Pointer);
  357. }
  358. }
  359. static void sEqualPointer(Script* sc) {
  360. Pointer a;
  361. Pointer b;
  362. if(sPopPointer(sc, &a) && sPopPointer(sc, &b)) {
  363. sPushBool(sc, a.array == b.array && a.offset == b.offset);
  364. }
  365. }
  366. static void sPushPreIntChange(Script* sc) {
  367. char c = 0;
  368. Pointer p;
  369. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) {
  370. void* data = sCheckAddress(sc, &p, sizeof(int));
  371. if(data != NULL) {
  372. int current = 0;
  373. memcpy(&current, data, sizeof(int));
  374. current += c;
  375. sPushInt(sc, current);
  376. memcpy(data, &current, sizeof(int));
  377. }
  378. }
  379. }
  380. static void sPushPostIntChange(Script* sc) {
  381. char c = 0;
  382. Pointer p;
  383. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) {
  384. void* data = sCheckAddress(sc, &p, sizeof(int));
  385. if(data != NULL) {
  386. int current = 0;
  387. memcpy(&current, data, sizeof(int));
  388. sPushInt(sc, current);
  389. current += c;
  390. memcpy(data, &current, sizeof(int));
  391. }
  392. }
  393. }
  394. static void sIntChange(Script* sc) {
  395. char c = 0;
  396. Pointer p;
  397. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) {
  398. void* data = sCheckAddress(sc, &p, sizeof(int));
  399. if(data != NULL) {
  400. int current = 0;
  401. memcpy(&current, data, sizeof(int));
  402. current += c;
  403. memcpy(data, &current, sizeof(int));
  404. }
  405. }
  406. }
  407. #define CASE_NUMBER_OP(name, op) \
  408. case OP_##name##_INT: NUMBER_OP(int, Int, op); break; \
  409. case OP_##name##_FLOAT: \
  410. NUMBER_OP(float, Float, op); \
  411. break;
  412. #define CASE_BOOL_OP(name, op) \
  413. case OP_##name##_INT: BOOL_OP(int, Int, op); break; \
  414. case OP_##name##_FLOAT: \
  415. BOOL_OP(float, Float, op); \
  416. break;
  417. #define CASE_TYPE(TYPE, Type, type) \
  418. case OP_STORE_##TYPE: sStore(sc, sizeof(type)); break; \
  419. case OP_RETURN_##TYPE: RETURN(type, Type); break; \
  420. case OP_PRINT_##TYPE: PRINT(type, Type, type##Printer); break; \
  421. case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); break; \
  422. case OP_LOAD_##TYPE: sLoad(sc, sizeof(type)); break;
  423. static void sConsumeInstruction(Script* sc) {
  424. switch(sReadOperation(sc)) {
  425. CASE_NUMBER_OP(ADD, +);
  426. CASE_NUMBER_OP(SUB, -);
  427. CASE_NUMBER_OP(MUL, *);
  428. CASE_BOOL_OP(LESS, <);
  429. CASE_BOOL_OP(GREATER, >);
  430. CASE_TYPE(INT, Int, int);
  431. CASE_TYPE(BOOL, Bool, bool);
  432. CASE_TYPE(FLOAT, Float, float);
  433. case OP_NOTHING: break;
  434. case OP_PUSH_INT: PUSH_CONSTANT(int, Int); break;
  435. case OP_PUSH_FLOAT: PUSH_CONSTANT(float, Float); break;
  436. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  437. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  438. case OP_DIV_INT: DIVISION(int, Int); break;
  439. case OP_DIV_FLOAT: DIVISION(float, Float); break;
  440. case OP_MOD_INT: MODULE(int, Int); break;
  441. case OP_INVERT_SIGN_INT: INVERT_SIGN(int, Int); break;
  442. case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); break;
  443. case OP_NOT: sNot(sc); break;
  444. case OP_AND: BOOL_OP(bool, Bool, &&); break;
  445. case OP_OR: BOOL_OP(bool, Bool, ||); break;
  446. case OP_BIT_NOT: sBitNot(sc); break;
  447. case OP_BIT_AND: NUMBER_OP(int, Int, &); break;
  448. case OP_BIT_OR: NUMBER_OP(int, Int, |); break;
  449. case OP_BIT_XOR: NUMBER_OP(int, Int, ^); break;
  450. case OP_LEFT_SHIFT: NUMBER_OP(int, Int, <<); break;
  451. case OP_RIGHT_SHIFT: NUMBER_OP(int, Int, >>); break;
  452. case OP_LINE: sLine(sc); break;
  453. case OP_GOTO: sGoTo(sc); break;
  454. case OP_IF_GOTO: sIfGoTo(sc); break;
  455. case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); break;
  456. case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); break;
  457. case OP_GOSUB: sGoSub(sc); break;
  458. case OP_RETURN: sReturn(sc); break;
  459. case OP_RESERVE: sReserveBytes(sc); break;
  460. case OP_DEREFERENCE_VAR: sDereference(sc); break;
  461. case OP_REFERENCE: sLoad(sc, sizeof(Pointer)); break;
  462. case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); break;
  463. case OP_ADD_REFERENCE: sAddReference(sc); break;
  464. case OP_LOAD: sLoadSize(sc); break;
  465. case OP_NEW: sNewArray(sc); break;
  466. case OP_DELETE: sDeleteArray(sc); break;
  467. case OP_LENGTH: sLength(sc); break;
  468. case OP_STORE_POINTER: sStore(sc, sizeof(Pointer)); break;
  469. case OP_PRINT_POINTER: sPrintPointer(sc); break;
  470. case OP_EQUAL_POINTER: sEqualPointer(sc); break;
  471. case OP_PUSH_PRE_INT_CHANGE: sPushPreIntChange(sc); break;
  472. case OP_PUSH_POST_INT_CHANGE: sPushPostIntChange(sc); break;
  473. case OP_INT_CHANGE: sIntChange(sc); break;
  474. }
  475. }
  476. static bool sHasData(Script* sc) {
  477. return sc->readIndex < sc->code->length;
  478. }
  479. Script* sInit(ByteCode* code) {
  480. Script* sc = malloc(sizeof(Script));
  481. sc->error[0] = '\0';
  482. sc->code = code;
  483. sc->readIndex = 0;
  484. sc->stackIndex = 0;
  485. sc->stackVarIndex = 0;
  486. sc->line = 0;
  487. asInit(&sc->arrays);
  488. return sc;
  489. }
  490. void sDelete(Script* sc) {
  491. bcDelete(sc->code);
  492. asDelete(&sc->arrays);
  493. free(sc);
  494. }
  495. void sRun(Script* sc) {
  496. while(sHasData(sc)) {
  497. sConsumeInstruction(sc);
  498. if(sc->error[0] != '\0') {
  499. puts("error:");
  500. printf(" - info: %s\n", sc->error);
  501. printf(" - line: %d\n", sc->line);
  502. return;
  503. }
  504. }
  505. }
  506. void sSetIntPrinter(IntPrinter p) {
  507. intPrinter = p;
  508. }
  509. void sSetFloatPrinter(FloatPrinter p) {
  510. floatPrinter = p;
  511. }
  512. void sSetBoolPrinter(BoolPrinter p) {
  513. boolPrinter = p;
  514. }
  515. void sSetPointerPrinter(PointerPrinter p) {
  516. pointerPrinter = p;
  517. }