Script.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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(length < 0) {
  33. sError(sc, "invalid reserve length %d", length);
  34. return NULL;
  35. } else if(sc->stackIndex + length > SCRIPT_STACK_SIZE) {
  36. sError(sc, "stack overflow");
  37. return NULL;
  38. }
  39. void* p = sc->stack + sc->stackIndex;
  40. sc->stackIndex += length;
  41. return p;
  42. }
  43. static bool sPush(Script* sc, const void* data, int length) {
  44. void* p = sReserve(sc, length);
  45. if(p != NULL) {
  46. memcpy(p, data, length);
  47. return true;
  48. }
  49. return false;
  50. }
  51. static const void* sFree(Script* sc, int length) {
  52. if(sc->stackIndex < length) {
  53. sError(sc, "stack underflow");
  54. return NULL;
  55. }
  56. sc->stackIndex -= length;
  57. return sc->stack + sc->stackIndex;
  58. }
  59. static bool sPop(Script* sc, void* data, int length) {
  60. const void* p = sFree(sc, length);
  61. if(p != NULL) {
  62. memcpy(data, p, length);
  63. return true;
  64. }
  65. return false;
  66. }
  67. static bool sPeek(Script* sc, void* data, int length) {
  68. if(sc->stackIndex < length) {
  69. sError(sc, "stack underflow");
  70. return false;
  71. }
  72. memcpy(data, sc->stack + (sc->stackIndex - length), length);
  73. return true;
  74. }
  75. #define POP_PUSH(type, Type) \
  76. bool sPop##Type(Script* sc, type* value) { \
  77. return sPop(sc, value, sizeof(type)); \
  78. } \
  79. bool sPush##Type(Script* sc, type value) { \
  80. return sPush(sc, &value, sizeof(type)); \
  81. }
  82. #define READ_POP_PUSH(type, Type) \
  83. static bool sRead##Type(Script* sc, type* i) { \
  84. return sRead(sc, i, sizeof(type)); \
  85. } \
  86. POP_PUSH(type, Type)
  87. #define PUSH_CONSTANT(type, Type) \
  88. { \
  89. type value; \
  90. if(sRead##Type(sc, &value)) { \
  91. sPush##Type(sc, value); \
  92. } \
  93. }
  94. #define ZERO_CHECK(name) \
  95. if(values[0] == 0) { \
  96. sError(sc, name " by 0"); \
  97. return; \
  98. }
  99. #define OP_BASE(type, Type, RType, op, check) \
  100. { \
  101. type values[2]; \
  102. if(sPop##Type(sc, values) && sPop##Type(sc, values + 1)) { \
  103. check; \
  104. sPush##RType(sc, values[1] op values[0]); \
  105. } \
  106. }
  107. #define CHECKED_NUMBER_OP(type, Type, op, check) \
  108. OP_BASE(type, Type, Type, op, check)
  109. #define NUMBER_OP(type, Type, op) CHECKED_NUMBER_OP(type, Type, op, )
  110. #define BOOL_OP(type, Type, op) OP_BASE(type, Type, Bool, op, )
  111. #define DIVISION(type, Type) \
  112. CHECKED_NUMBER_OP(type, Type, /, ZERO_CHECK("division"));
  113. #define MODULE(type, Type) \
  114. CHECKED_NUMBER_OP(type, Type, %, ZERO_CHECK("module"));
  115. READ_POP_PUSH(int32, Int32)
  116. READ_POP_PUSH(int64, Int64)
  117. READ_POP_PUSH(float, Float)
  118. POP_PUSH(bool, Bool)
  119. bool sPopPointer(Script* sc, Pointer* value) {
  120. return sPop(sc, value, sizeof(Pointer));
  121. }
  122. bool sPushPointer(Script* sc, Pointer* value) {
  123. return sPush(sc, value, sizeof(Pointer));
  124. }
  125. bool sPushNullPointer(Script* sc) {
  126. Pointer p = {-1, -1};
  127. return sPushPointer(sc, &p);
  128. }
  129. #define INVERT_SIGN(type, Type) \
  130. { \
  131. type value = 0; \
  132. if(sPop##Type(sc, &value)) { \
  133. sPush##Type(sc, -value); \
  134. } \
  135. }
  136. static void sReserveBytes(Script* sc) {
  137. int32 bytes = 0;
  138. int32 offset = 0;
  139. if(sReadInt32(sc, &bytes) && sReadInt32(sc, &offset)) {
  140. int32 oldIndex = sc->stackVarIndex;
  141. sc->stackVarIndex = sc->stackIndex - offset;
  142. sReserve(sc, bytes - offset);
  143. sPushInt32(sc, oldIndex);
  144. }
  145. }
  146. static void sGlobalReserveBytes(Script* sc) {
  147. int32 bytes = 0;
  148. if(sReadInt32(sc, &bytes)) {
  149. sc->stackVarIndex = sc->stackIndex;
  150. if(bytes >= 0) {
  151. sReserve(sc, bytes);
  152. } else {
  153. sc->stackIndex += bytes;
  154. if(sc->stackIndex < 0) {
  155. sError(sc, "invalid global free %d", bytes);
  156. }
  157. }
  158. }
  159. }
  160. static int32 sGetTextLength(Script* sc, Pointer* p) {
  161. const int SIZE = sizeof(int32);
  162. int sizeAddress = -p->array;
  163. if(sizeAddress < 0 || sizeAddress + SIZE >= sc->code->length) {
  164. sError(sc, "invalid code array length address %d", sizeAddress);
  165. return -1;
  166. }
  167. int32 size;
  168. memcpy(&size, sc->code->code + sizeAddress, SIZE);
  169. return size;
  170. }
  171. static void* sCheckTextAddress(Script* sc, Pointer* p, int length) {
  172. const int SIZE = sizeof(int32);
  173. int address = p->offset + SIZE;
  174. if(address + length > sc->code->length) {
  175. sError(sc, "address %d is out of code bounds", address);
  176. return NULL;
  177. }
  178. int32 size = sGetTextLength(sc, p);
  179. if(size == -1) {
  180. return NULL;
  181. }
  182. int startAddress = -p->array + SIZE;
  183. int endAddress = startAddress + size * SIZE;
  184. if(address + length > endAddress || address < startAddress) {
  185. sError(sc, "invalid text address %d", (address - startAddress) / SIZE);
  186. return NULL;
  187. }
  188. return sc->code->code + address;
  189. }
  190. void* sCheckAddress(Script* sc, Pointer* p, int length) {
  191. if(p->array >= 0) {
  192. SnuviArray* a = asGet(&sc->arrays, p->array);
  193. if(a == NULL) {
  194. sError(sc, "invalid heap pointer");
  195. return NULL;
  196. } else if(p->offset < 0 || p->offset >= a->size) {
  197. sError(sc, "address %d is out of array bounds", p->offset);
  198. return NULL;
  199. }
  200. return ((char*)a->data) + p->offset;
  201. } else if(p->array == -1) {
  202. if(p->offset < 0 || p->offset + length > sc->stackIndex) {
  203. sError(sc, "address %d is out of stack bounds", p->offset);
  204. return NULL;
  205. }
  206. return sc->stack + p->offset;
  207. }
  208. return sCheckTextAddress(sc, p, length);
  209. }
  210. static void sNot(Script* sc) {
  211. bool value = false;
  212. if(sPopBool(sc, &value)) {
  213. sPushBool(sc, !value);
  214. }
  215. }
  216. static void sBitNotInt32(Script* sc) {
  217. int32 value = 0;
  218. if(sPopInt32(sc, &value)) {
  219. sPushInt32(sc, ~value);
  220. }
  221. }
  222. static void sBitNotInt64(Script* sc) {
  223. int64 value = 0;
  224. if(sPopInt64(sc, &value)) {
  225. sPushInt64(sc, ~value);
  226. }
  227. }
  228. static void sLine(Script* sc) {
  229. sRead(sc, &sc->line, 2);
  230. }
  231. static void sGoTo(Script* sc) {
  232. int32 gotoIndex;
  233. if(sReadInt32(sc, &gotoIndex)) {
  234. sc->readIndex = gotoIndex;
  235. }
  236. }
  237. static void sGoSub(Script* sc) {
  238. int32 gotoIndex;
  239. int32 offset;
  240. if(sReadInt32(sc, &gotoIndex) && sReadInt32(sc, &offset)) {
  241. Pointer p = {.array = -1,
  242. .offset = sc->stackIndex - offset - sizeof(int32)};
  243. void* dest = sCheckAddress(sc, &p, sizeof(int32));
  244. if(dest != NULL) {
  245. memcpy(dest, &sc->readIndex, sizeof(int32));
  246. sc->readIndex = gotoIndex;
  247. }
  248. }
  249. }
  250. static void sReturn(Script* sc) {
  251. int32 bytes = 0;
  252. int32 varIndex = 0;
  253. if(sReadInt32(sc, &bytes) && sPopInt32(sc, &varIndex)) {
  254. sc->stackVarIndex = varIndex;
  255. sFree(sc, bytes);
  256. if(!sPopInt32(sc, &sc->readIndex) || sc->readIndex < 0) {
  257. sError(sc, "read index is corrupt");
  258. }
  259. }
  260. }
  261. static void sReturnPointer(Script* sc) {
  262. Pointer p;
  263. if(sPopPointer(sc, &p)) {
  264. sReturn(sc);
  265. sPushPointer(sc, &p);
  266. }
  267. }
  268. #define RETURN(type, Type) \
  269. { \
  270. type value; \
  271. if(sPop##Type(sc, &value)) { \
  272. sReturn(sc); \
  273. sPush##Type(sc, value); \
  274. } \
  275. }
  276. static void sIfGoTo(Script* sc) {
  277. int32 gotoIndex = 0;
  278. bool value = false;
  279. if(sReadInt32(sc, &gotoIndex) && sPopBool(sc, &value) && !value) {
  280. sc->readIndex = gotoIndex;
  281. }
  282. }
  283. static void sPeekFalseGoTo(Script* sc) {
  284. int32 gotoIndex = 0;
  285. bool value = false;
  286. if(sReadInt32(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) &&
  287. !value) {
  288. sc->readIndex = gotoIndex;
  289. }
  290. }
  291. static void sPeekTrueGoTo(Script* sc) {
  292. int32 gotoIndex = 0;
  293. bool value = false;
  294. if(sReadInt32(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && value) {
  295. sc->readIndex = gotoIndex;
  296. }
  297. }
  298. static void sNewArray(Script* sc) {
  299. int32 length = 0;
  300. int32 size = 0;
  301. if(sReadInt32(sc, &size) && sPopInt32(sc, &length)) {
  302. Pointer p = {.array = asAllocate(&sc->arrays, size, length),
  303. .offset = 0};
  304. if(p.array == -1) {
  305. sError(sc, "out of memory");
  306. } else if(p.array == -2) {
  307. sError(sc, "bad allocation");
  308. } else {
  309. sPushPointer(sc, &p);
  310. }
  311. }
  312. }
  313. static void sDeleteArray(Script* sc) {
  314. Pointer p;
  315. if(sPopPointer(sc, &p)) {
  316. if(p.offset != 0) {
  317. sError(sc, "delete of array with offset: %d", p.offset);
  318. return;
  319. }
  320. SnuviArray* a = asGet(&sc->arrays, p.array);
  321. if(a == NULL) {
  322. sError(sc, "delete of invalid array");
  323. return;
  324. }
  325. asDeleteArray(&sc->arrays, a, p.array);
  326. }
  327. }
  328. bool sGetPointerLength(Script* sc, Pointer* p, int32* length) {
  329. if(p->array == -1) {
  330. *length = p->offset >= 0;
  331. return false;
  332. } else if(p->array <= -1) {
  333. int32 l = sGetTextLength(sc, p);
  334. if(l != -1) {
  335. *length = l;
  336. return false;
  337. }
  338. return true;
  339. }
  340. SnuviArray* a = asGet(&sc->arrays, p->array);
  341. if(a == NULL) {
  342. sError(sc, "invalid heap pointer %d", p->array);
  343. return true;
  344. }
  345. *length = a->length;
  346. return false;
  347. }
  348. static void sLength(Script* sc) {
  349. Pointer p;
  350. int32 length;
  351. if(sPopPointer(sc, &p) && !sGetPointerLength(sc, &p, &length)) {
  352. sPushInt32(sc, length);
  353. }
  354. }
  355. static void sDereference(Script* sc) {
  356. int32 address = 0;
  357. if(sReadInt32(sc, &address)) {
  358. Pointer p = {.array = -1, .offset = address + sc->stackVarIndex};
  359. sPushPointer(sc, &p);
  360. }
  361. }
  362. static void sGlobalDereference(Script* sc) {
  363. int32 address = 0;
  364. if(sReadInt32(sc, &address)) {
  365. Pointer p = {.array = -1, .offset = address};
  366. sPushPointer(sc, &p);
  367. }
  368. }
  369. static void sLoad(Script* sc, int length) {
  370. Pointer p;
  371. if(sPopPointer(sc, &p)) {
  372. void* src = sCheckAddress(sc, &p, length);
  373. if(src != NULL) {
  374. sPush(sc, src, length);
  375. }
  376. }
  377. }
  378. static void sDuplicateReference(Script* sc) {
  379. Pointer p;
  380. if(sPeek(sc, &p, sizeof(Pointer))) {
  381. sPushPointer(sc, &p);
  382. }
  383. }
  384. static void sAddReference(Script* sc) {
  385. int32 size = 0;
  386. int32 add = 0;
  387. Pointer p;
  388. if(sReadInt32(sc, &size) && sPopInt32(sc, &add) && sPopPointer(sc, &p)) {
  389. p.offset += add * size;
  390. sPushPointer(sc, &p);
  391. }
  392. }
  393. static void sLoadSize(Script* sc) {
  394. int32 size = 0;
  395. Pointer p;
  396. if(sReadInt32(sc, &size) && sPopPointer(sc, &p)) {
  397. void* src = sCheckAddress(sc, &p, size);
  398. if(src != NULL) {
  399. sPush(sc, src, size);
  400. }
  401. }
  402. }
  403. static void sStore(Script* sc, int length) {
  404. int index = sc->stackIndex - sizeof(Pointer) - length;
  405. if(index < 0) {
  406. sError(sc, "stack underflow");
  407. return;
  408. }
  409. Pointer p;
  410. memcpy(&p, sc->stack + index, sizeof(Pointer));
  411. void* dest = sCheckAddress(sc, &p, length);
  412. if(dest != NULL) {
  413. sPop(sc, dest, length);
  414. sc->stackIndex -= sizeof(Pointer);
  415. }
  416. }
  417. static void sEqualPointer(Script* sc, bool wanted) {
  418. Pointer a;
  419. Pointer b;
  420. if(sPopPointer(sc, &a) && sPopPointer(sc, &b)) {
  421. sPushBool(sc, (a.array == b.array && a.offset == b.offset) == wanted);
  422. }
  423. }
  424. static void sCall(Script* sc) {
  425. int32 function = 0;
  426. if(sReadInt32(sc, &function) && gfsCall(sc, function)) {
  427. sError(sc, "invalid function call");
  428. }
  429. }
  430. static void sPushText(Script* sc) {
  431. int address = sc->readIndex;
  432. int32 length;
  433. if(sReadInt32(sc, &length)) {
  434. Pointer p = {.array = -address, .offset = address};
  435. sPushPointer(sc, &p);
  436. sc->readIndex += sizeof(int32) * length;
  437. }
  438. }
  439. #define CHANGE_OP(type, op) \
  440. { \
  441. char c = 0; \
  442. Pointer p; \
  443. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) { \
  444. void* data = sCheckAddress(sc, &p, sizeof(type)); \
  445. if(data != NULL) { \
  446. type current; \
  447. memcpy(&current, data, sizeof(type)); \
  448. op \
  449. } \
  450. } \
  451. }
  452. #define PUSH_PRE_CHANGE(Type, type) \
  453. CHANGE_OP(type, current += c; sPush##Type(sc, current); \
  454. memcpy(data, &current, sizeof(type));)
  455. #define PUSH_POST_CHANGE(Type, type) \
  456. CHANGE_OP(type, sPush##Type(sc, current); current += c; \
  457. memcpy(data, &current, sizeof(type));)
  458. #define CHANGE(type) \
  459. CHANGE_OP(type, current += c; memcpy(data, &current, sizeof(type));)
  460. #define CAST(From, from, To) \
  461. { \
  462. from value; \
  463. if(sPop##From(sc, &value)) { \
  464. sPush##To(sc, value); \
  465. } \
  466. }
  467. #define CASE_CAST(TYPE1, Type1, type1, TYPE2, Type2, type2) \
  468. case OP_##TYPE1##_TO_##TYPE2: CAST(Type1, type1, Type2); break; \
  469. case OP_##TYPE2##_TO_##TYPE1: \
  470. CAST(Type2, type2, Type1); \
  471. break;
  472. #define CASE_CHANGE(TYPE, Type, type) \
  473. case OP_PUSH_PRE_CHANGE_##TYPE: PUSH_PRE_CHANGE(Type, type); break; \
  474. case OP_PUSH_POST_CHANGE_##TYPE: PUSH_POST_CHANGE(Type, type); break; \
  475. case OP_CHANGE_##TYPE: \
  476. CHANGE(type); \
  477. break;
  478. #define CASE_INTEGRAL_OP(name, op) \
  479. case OP_##name##_INT32: NUMBER_OP(int32, Int32, op); break; \
  480. case OP_##name##_INT64: \
  481. NUMBER_OP(int64, Int64, op); \
  482. break;
  483. #define CASE_NUMBER_OP(name, op) \
  484. CASE_INTEGRAL_OP(name, op) \
  485. case OP_##name##_FLOAT: \
  486. NUMBER_OP(float, Float, op); \
  487. break;
  488. #define CASE_BOOL_OP(name, op) \
  489. case OP_##name##_INT32: BOOL_OP(int32, Int32, op); break; \
  490. case OP_##name##_INT64: BOOL_OP(int64, Int64, op); break; \
  491. case OP_##name##_FLOAT: \
  492. BOOL_OP(float, Float, op); \
  493. break;
  494. #define CASE_TYPE(TYPE, Type, type) \
  495. case OP_STORE_##TYPE: sStore(sc, sizeof(type)); break; \
  496. case OP_RETURN_##TYPE: RETURN(type, Type); break; \
  497. case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); break; \
  498. case OP_NOT_EQUAL_##TYPE: BOOL_OP(type, Type, !=); break; \
  499. case OP_LOAD_##TYPE: sLoad(sc, sizeof(type)); break;
  500. static void sConsumeInstruction(Script* sc) {
  501. switch(sReadOperation(sc)) {
  502. CASE_NUMBER_OP(ADD, +);
  503. CASE_NUMBER_OP(SUB, -);
  504. CASE_NUMBER_OP(MUL, *);
  505. CASE_BOOL_OP(LESS, <);
  506. CASE_BOOL_OP(LESS_EQUAL, <=);
  507. CASE_BOOL_OP(GREATER, >);
  508. CASE_BOOL_OP(GREATER_EQUAL, >=);
  509. CASE_TYPE(INT32, Int32, int32);
  510. CASE_TYPE(INT64, Int64, int64);
  511. CASE_TYPE(BOOL, Bool, bool);
  512. CASE_TYPE(FLOAT, Float, float);
  513. CASE_CHANGE(INT32, Int32, int32);
  514. CASE_CHANGE(INT64, Int64, int64);
  515. CASE_INTEGRAL_OP(BIT_AND, &);
  516. CASE_INTEGRAL_OP(BIT_OR, |);
  517. CASE_INTEGRAL_OP(BIT_XOR, ^);
  518. CASE_INTEGRAL_OP(LEFT_SHIFT, <<);
  519. CASE_INTEGRAL_OP(RIGHT_SHIFT, >>);
  520. CASE_CAST(INT32, Int32, int32, FLOAT, Float, float);
  521. CASE_CAST(INT32, Int32, int32, INT64, Int64, int64);
  522. CASE_CAST(INT64, Int64, int64, FLOAT, Float, float);
  523. case OP_NOTHING: break;
  524. case OP_PUSH_INT32: PUSH_CONSTANT(int32, Int32); break;
  525. case OP_PUSH_INT64: PUSH_CONSTANT(int64, Int64); break;
  526. case OP_PUSH_FLOAT: PUSH_CONSTANT(float, Float); break;
  527. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  528. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  529. case OP_PUSH_NULLPTR: sPushNullPointer(sc); break;
  530. case OP_PUSH_TEXT: sPushText(sc); break;
  531. case OP_DIV_INT32: DIVISION(int32, Int32); break;
  532. case OP_DIV_INT64: DIVISION(int64, Int64); break;
  533. case OP_DIV_FLOAT: DIVISION(float, Float); break;
  534. case OP_MOD_INT32: MODULE(int32, Int32); break;
  535. case OP_MOD_INT64: MODULE(int64, Int64); break;
  536. case OP_INVERT_SIGN_INT32: INVERT_SIGN(int32, Int32); break;
  537. case OP_INVERT_SIGN_INT64: INVERT_SIGN(int64, Int64); break;
  538. case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); break;
  539. case OP_NOT: sNot(sc); break;
  540. case OP_AND: BOOL_OP(bool, Bool, &&); break;
  541. case OP_OR: BOOL_OP(bool, Bool, ||); break;
  542. case OP_BIT_NOT_INT32: sBitNotInt32(sc); break;
  543. case OP_BIT_NOT_INT64: sBitNotInt64(sc); break;
  544. case OP_LINE: sLine(sc); break;
  545. case OP_GOTO: sGoTo(sc); break;
  546. case OP_IF_GOTO: sIfGoTo(sc); break;
  547. case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); break;
  548. case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); break;
  549. case OP_GOSUB: sGoSub(sc); break;
  550. case OP_RETURN: sReturn(sc); break;
  551. case OP_RETURN_POINTER: sReturnPointer(sc); break;
  552. case OP_RESERVE: sReserveBytes(sc); break;
  553. case OP_GRESERVE: sGlobalReserveBytes(sc); break;
  554. case OP_DEREFERENCE_VAR: sDereference(sc); break;
  555. case OP_DEREFERENCE_GVAR: sGlobalDereference(sc); break;
  556. case OP_REFERENCE: sLoad(sc, sizeof(Pointer)); break;
  557. case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); break;
  558. case OP_ADD_REFERENCE: sAddReference(sc); break;
  559. case OP_LOAD: sLoadSize(sc); break;
  560. case OP_NEW: sNewArray(sc); break;
  561. case OP_DELETE: sDeleteArray(sc); break;
  562. case OP_LENGTH: sLength(sc); break;
  563. case OP_STORE_POINTER: sStore(sc, sizeof(Pointer)); break;
  564. case OP_EQUAL_POINTER: sEqualPointer(sc, true); break;
  565. case OP_NOT_EQUAL_POINTER: sEqualPointer(sc, false); break;
  566. case OP_CALL: sCall(sc); break;
  567. }
  568. }
  569. static bool sHasData(Script* sc) {
  570. return sc->readIndex < sc->code->length;
  571. }
  572. Script* sInit(ByteCode* code) {
  573. Script* sc = malloc(sizeof(Script));
  574. sc->error[0] = '\0';
  575. sc->code = code;
  576. sc->readIndex = 0;
  577. sc->stackIndex = 0;
  578. sc->stackVarIndex = 0;
  579. sc->line = 0;
  580. asInit(&sc->arrays);
  581. return sc;
  582. }
  583. void sDelete(Script* sc) {
  584. bcDelete(sc->code);
  585. asDelete(&sc->arrays);
  586. free(sc);
  587. }
  588. void sRun(Script* sc) {
  589. while(sHasData(sc)) {
  590. sConsumeInstruction(sc);
  591. if(sc->error[0] != '\0') {
  592. return;
  593. }
  594. }
  595. }