Script.c 19 KB

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