Script.c 20 KB

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