Script.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. static const char* VALUE_TYPE_NAMES[] = {"?", "int", "float", "pointer",
  10. "array"};
  11. void sError(Script* sc, const char* format, ...) {
  12. va_list args;
  13. va_start(args, format);
  14. vsnprintf(sc->error, SCRIPT_ERROR_SIZE, format, args);
  15. va_end(args);
  16. }
  17. static bool sRead(Script* sc, void* buffer, int length) {
  18. if(sc->readIndex + length > sc->code->length) {
  19. sError(sc, "cannot read expected %d bytes of data from bytecode");
  20. return true;
  21. }
  22. memcpy(buffer, sc->code->code + sc->readIndex, (size_t)length);
  23. sc->readIndex += length;
  24. return false;
  25. }
  26. static Operation sReadOperation(Script* sc) {
  27. unsigned char c;
  28. if(sRead(sc, &c, 1)) {
  29. return OP_NOTHING;
  30. }
  31. return (Operation)c;
  32. }
  33. static Value* sPeekStack(Script* sc, int type) {
  34. if(sc->stackIndex <= 0) {
  35. sError(sc, "stack underflow");
  36. return NULL;
  37. }
  38. int stackType = sc->stack[sc->stackIndex - 1].type;
  39. if(stackType != type) {
  40. sError(sc, "expected %s on stack but got %s", VALUE_TYPE_NAMES[type],
  41. VALUE_TYPE_NAMES[stackType]);
  42. return NULL;
  43. }
  44. return sc->stack + (sc->stackIndex - 1);
  45. }
  46. bool sPopInt32(Script* sc, int32* i) {
  47. Value* v = sPeekStack(sc, VT_INT);
  48. if(v == NULL) {
  49. return true;
  50. }
  51. *i = v->data.intValue;
  52. sc->stackIndex--;
  53. return false;
  54. }
  55. bool sPopFloat(Script* sc, float* f) {
  56. Value* v = sPeekStack(sc, VT_FLOAT);
  57. if(v == NULL) {
  58. return true;
  59. }
  60. *f = v->data.floatValue;
  61. sc->stackIndex--;
  62. return false;
  63. }
  64. static bool sReadInt32(Script* sc, int32* i) {
  65. return sRead(sc, i, sizeof(int32));
  66. }
  67. static bool sReadFloat(Script* sc, float* f) {
  68. return sRead(sc, f, sizeof(float));
  69. }
  70. static Value* sPushStack(Script* sc, int values) {
  71. if(sc->stackIndex + values > SCRIPT_STACK_SIZE) {
  72. sError(sc, "stack overflow");
  73. return NULL;
  74. }
  75. Value* v = sc->stack + sc->stackIndex;
  76. for(int i = 0; i < values; i++) {
  77. v[i].type = VT_NOT_SET;
  78. }
  79. sc->stackIndex += values;
  80. return v;
  81. }
  82. bool sPushInt32(Script* sc, int32 i) {
  83. Value* v = sPushStack(sc, 1);
  84. if(v == NULL) {
  85. return true;
  86. }
  87. v->type = VT_INT;
  88. v->data.intValue = i;
  89. return false;
  90. }
  91. bool sPushFloat(Script* sc, float f) {
  92. Value* v = sPushStack(sc, 1);
  93. if(v == NULL) {
  94. return true;
  95. }
  96. v->type = VT_FLOAT;
  97. v->data.floatValue = f;
  98. return false;
  99. }
  100. static void sPushInt32Value(Script* sc) {
  101. int32 value = 0;
  102. if(!sReadInt32(sc, &value)) {
  103. sPushInt32(sc, value);
  104. }
  105. }
  106. static void sPushFloatValue(Script* sc) {
  107. float value = 0;
  108. if(!sReadFloat(sc, &value)) {
  109. sPushFloat(sc, value);
  110. }
  111. }
  112. #define ZERO_CHECK(name) \
  113. if(values[0] == 0) { \
  114. sError(sc, name " by 0"); \
  115. return; \
  116. }
  117. #define OP_BASE(type, Type, RType, op, check) \
  118. { \
  119. type values[2]; \
  120. if(!sPop##Type(sc, values) && !sPop##Type(sc, values + 1)) { \
  121. check; \
  122. sPush##RType(sc, values[1] op values[0]); \
  123. } \
  124. }
  125. #define CHECKED_NUMBER_OP(type, Type, op, check) \
  126. OP_BASE(type, Type, Type, op, check)
  127. #define NUMBER_OP(type, Type, op) CHECKED_NUMBER_OP(type, Type, op, )
  128. #define BOOL_OP(type, Type, op) OP_BASE(type, Type, Int32, op, )
  129. #define DIVISION(type, Type) \
  130. CHECKED_NUMBER_OP(type, Type, /, ZERO_CHECK("division"));
  131. #define MODULE(type, Type) \
  132. CHECKED_NUMBER_OP(type, Type, %, ZERO_CHECK("module"));
  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. int32 values = 0;
  142. int32 offset = 0;
  143. if(sReadInt32(sc, &values) || sReadInt32(sc, &offset)) {
  144. return;
  145. }
  146. int32 oldIndex = sc->stackVarIndex;
  147. sc->stackVarIndex = sc->stackIndex - offset;
  148. sPushStack(sc, values - offset);
  149. sPushInt32(sc, oldIndex);
  150. }
  151. static void sGlobalReserveBytes(Script* sc) {
  152. int32 values = 0;
  153. if(sReadInt32(sc, &values)) {
  154. return;
  155. }
  156. sc->stackVarIndex = sc->stackIndex;
  157. if(values >= 0) {
  158. sPushStack(sc, values);
  159. } else {
  160. sc->stackIndex += values;
  161. if(sc->stackIndex < 0) {
  162. sError(sc, "invalid global free %d", values);
  163. }
  164. }
  165. }
  166. static void sNot(Script* sc) {
  167. int32 value = 0;
  168. if(!sPopInt32(sc, &value)) {
  169. sPushInt32(sc, !value);
  170. }
  171. }
  172. static void sBitNotInt32(Script* sc) {
  173. int32 value = 0;
  174. if(!sPopInt32(sc, &value)) {
  175. sPushInt32(sc, ~value);
  176. }
  177. }
  178. static void sLine(Script* sc) {
  179. sRead(sc, &sc->line, 2);
  180. }
  181. static void sGoTo(Script* sc) {
  182. int32 gotoIndex;
  183. if(!sReadInt32(sc, &gotoIndex)) {
  184. sc->readIndex = gotoIndex;
  185. }
  186. }
  187. static void sGoSub(Script* sc) {
  188. int32 gotoIndex;
  189. int32 offset;
  190. if(sReadInt32(sc, &gotoIndex) || sReadInt32(sc, &offset)) {
  191. return;
  192. }
  193. int index = sc->stackIndex - offset - 1;
  194. if(index < 0 || index >= SCRIPT_STACK_SIZE) {
  195. sError(sc, "invalid gosub offset");
  196. return;
  197. } else if(sc->stack[index].type != VT_INT) {
  198. sError(sc, "gosub expects an int got %s",
  199. VALUE_TYPE_NAMES[sc->stack[index].type]);
  200. return;
  201. }
  202. sc->stack[index].data.intValue = sc->readIndex;
  203. sc->readIndex = gotoIndex;
  204. }
  205. static void sReturn(Script* sc) {
  206. int32 values = 0;
  207. int32 varIndex = 0;
  208. if(sReadInt32(sc, &values) || sPopInt32(sc, &varIndex)) {
  209. return;
  210. }
  211. sc->stackVarIndex = varIndex;
  212. if(sc->stackIndex < values) {
  213. sError(sc, "invalid return index");
  214. return;
  215. }
  216. sc->stackIndex -= values;
  217. if(sPopInt32(sc, &sc->readIndex) || sc->readIndex < 0) {
  218. sError(sc, "read index is corrupt");
  219. }
  220. }
  221. static void sReturnPointer(Script* sc) {
  222. Value* v = sPeekStack(sc, VT_ARRAY);
  223. if(v == NULL) {
  224. return;
  225. }
  226. sc->stackIndex--;
  227. sReturn(sc);
  228. Value* p = sPushStack(sc, 1);
  229. if(p != NULL) {
  230. *p = *v;
  231. }
  232. }
  233. #define RETURN(type, Type) \
  234. { \
  235. type value; \
  236. if(!sPop##Type(sc, &value)) { \
  237. sReturn(sc); \
  238. sPush##Type(sc, value); \
  239. } \
  240. }
  241. static void sIfGoTo(Script* sc) {
  242. int32 gotoIndex = 0;
  243. int32 value = false;
  244. if(!sReadInt32(sc, &gotoIndex) && !sPopInt32(sc, &value) && !value) {
  245. sc->readIndex = gotoIndex;
  246. }
  247. }
  248. static void sPeekFalseGoTo(Script* sc) {
  249. int32 gotoIndex = 0;
  250. if(sReadInt32(sc, &gotoIndex)) {
  251. return;
  252. }
  253. Value* v = sPeekStack(sc, VT_INT);
  254. if(v != NULL && v->data.intValue == 0) {
  255. sc->readIndex = gotoIndex;
  256. }
  257. }
  258. static void sPeekTrueGoTo(Script* sc) {
  259. int32 gotoIndex = 0;
  260. if(sReadInt32(sc, &gotoIndex)) {
  261. return;
  262. }
  263. Value* v = sPeekStack(sc, VT_INT);
  264. if(v != NULL && v->data.intValue != 0) {
  265. sc->readIndex = gotoIndex;
  266. }
  267. }
  268. static void sNewArray(Script* sc) {
  269. int32 length = 0;
  270. int32 size = 0;
  271. if(sReadInt32(sc, &size) || sPopInt32(sc, &length)) {
  272. return;
  273. }
  274. Value* p = sPushStack(sc, 1);
  275. if(p == NULL) {
  276. return;
  277. }
  278. p->type = VT_ARRAY;
  279. p->offset = 0;
  280. p->data.intValue = asAllocate(&sc->arrays, size, length);
  281. if(p->data.intValue == -1) {
  282. sError(sc, "out of memory");
  283. } else if(p->data.intValue == -2) {
  284. sError(sc, "bad allocation");
  285. }
  286. }
  287. static void sLength(Script* sc) {
  288. Value* v = sPeekStack(sc, VT_ARRAY);
  289. if(v == NULL) {
  290. return;
  291. }
  292. sc->stackIndex--;
  293. SnuviArray* a = asGet(&sc->arrays, v->data.intValue);
  294. if(a == NULL) {
  295. sError(sc, "invalid heap pointer %d", v->data.intValue);
  296. return;
  297. }
  298. sPushInt32(sc, a->length);
  299. }
  300. static void sDereference(Script* sc) {
  301. int32 address = 0;
  302. if(sReadInt32(sc, &address)) {
  303. return;
  304. }
  305. Value* v = sPushStack(sc, 1);
  306. if(v != NULL) {
  307. v->type = VT_POINTER;
  308. v->offset = ((unsigned int)(address + sc->stackVarIndex)) & 0x0FFFFFFF;
  309. v->data.intValue = -1;
  310. }
  311. }
  312. static void sGlobalDereference(Script* sc) {
  313. int32 address = 0;
  314. if(sReadInt32(sc, &address)) {
  315. return;
  316. }
  317. Value* v = sPushStack(sc, 1);
  318. if(v != NULL) {
  319. v->type = VT_POINTER;
  320. v->offset = ((unsigned int)address) & 0x0FFFFFFF;
  321. v->data.intValue = -1;
  322. }
  323. }
  324. static void sDuplicateReference(Script* sc) {
  325. Value* v = sPeekStack(sc, VT_POINTER);
  326. if(v != NULL) {
  327. Value* copy = sPushStack(sc, 1);
  328. if(copy != NULL) {
  329. *copy = *v;
  330. }
  331. }
  332. }
  333. static void sAddReference(Script* sc) {
  334. int32 size = 0;
  335. int32 add = 0;
  336. if(sReadInt32(sc, &size) || sPopInt32(sc, &add)) {
  337. return;
  338. }
  339. Value* v = sPeekStack(sc, VT_POINTER);
  340. if(v != NULL) {
  341. v->offset = ((unsigned int)(v->offset + add * size)) & 0x3FFFFFF;
  342. } else {
  343. sc->error[0] = '\0';
  344. v = sPeekStack(sc, VT_ARRAY);
  345. if(v != NULL) {
  346. v->type = VT_POINTER;
  347. v->offset = ((unsigned int)(v->offset + add * size)) & 0x3FFFFFF;
  348. }
  349. }
  350. }
  351. static void sPushStructReference(Script* sc) {
  352. int32 address = 0;
  353. if(sReadInt32(sc, &address)) {
  354. return;
  355. }
  356. Value* v = sPushStack(sc, 1);
  357. if(v != NULL) {
  358. int index = address + sc->stackVarIndex;
  359. if(index < 0 || index >= SCRIPT_STACK_SIZE) {
  360. sError(sc, "invalid struct reference address");
  361. return;
  362. }
  363. *v = sc->stack[index];
  364. }
  365. }
  366. static Value* sLoadFromPointer(Script* sc, Value* p, int type) {
  367. if(p->data.intValue < 0) {
  368. if(p->offset < 0 || p->offset >= SCRIPT_STACK_SIZE) {
  369. sError(sc, "load offset overflow");
  370. return NULL;
  371. }
  372. Value* v = sc->stack + p->offset;
  373. if(v->type != type && v->type != VT_NOT_SET) {
  374. sError(sc, "pointer did not point to %s but %s",
  375. VALUE_TYPE_NAMES[type], VALUE_TYPE_NAMES[v->type]);
  376. return NULL;
  377. }
  378. return v;
  379. }
  380. SnuviArray* a = asGet(&sc->arrays, p->data.intValue);
  381. if(a == NULL) {
  382. sError(sc, "invalid heap pointer %d", p->data.intValue);
  383. return NULL;
  384. }
  385. if(p->offset < 0 || p->offset >= a->realLength) {
  386. sError(sc, "invalid heap pointer offset %d %d", p->offset,
  387. a->realLength);
  388. return NULL;
  389. }
  390. return a->data + p->offset;
  391. }
  392. SnuviArray* sGetArray(Script* sc) {
  393. Value* v = sPeekStack(sc, VT_ARRAY);
  394. if(v == NULL) {
  395. return NULL;
  396. }
  397. sc->stackIndex--;
  398. return asGet(&sc->arrays, v->data.intValue);
  399. }
  400. Value* sPopStructPointer(Script* sc, int type) {
  401. Value* v = sPeekStack(sc, VT_POINTER);
  402. if(v == NULL) {
  403. return NULL;
  404. }
  405. sc->stackIndex--;
  406. return sLoadFromPointer(sc, v, type);
  407. }
  408. static void sStoreInt32(Script* sc) {
  409. int32 i = 0;
  410. if(sPopInt32(sc, &i)) {
  411. return;
  412. }
  413. Value* v = sPeekStack(sc, VT_POINTER);
  414. if(v == NULL) {
  415. return;
  416. }
  417. sc->stackIndex--;
  418. v = sLoadFromPointer(sc, v, VT_INT);
  419. if(v != NULL) {
  420. v->data.intValue = i;
  421. v->type = VT_INT;
  422. }
  423. }
  424. static void sStoreFloat(Script* sc) {
  425. float f = 0;
  426. if(sPopFloat(sc, &f)) {
  427. return;
  428. }
  429. Value* v = sPeekStack(sc, VT_POINTER);
  430. if(v == NULL) {
  431. return;
  432. }
  433. sc->stackIndex--;
  434. v = sLoadFromPointer(sc, v, VT_FLOAT);
  435. if(v != NULL) {
  436. v->data.floatValue = f;
  437. v->type = VT_FLOAT;
  438. }
  439. }
  440. static void sStoreArray(Script* sc) {
  441. Value* array = sPeekStack(sc, VT_ARRAY);
  442. if(array == NULL) {
  443. return;
  444. }
  445. sc->stackIndex--;
  446. Value* v = sPeekStack(sc, VT_POINTER);
  447. if(v == NULL) {
  448. return;
  449. }
  450. sc->stackIndex--;
  451. v = sLoadFromPointer(sc, v, VT_ARRAY);
  452. if(v != NULL) {
  453. *v = *array;
  454. }
  455. }
  456. static void sLoadInt32(Script* sc) {
  457. Value* v = sPeekStack(sc, VT_POINTER);
  458. if(v == NULL) {
  459. return;
  460. }
  461. sc->stackIndex--;
  462. v = sLoadFromPointer(sc, v, VT_INT);
  463. if(v != NULL) {
  464. sPushInt32(sc, v->data.intValue);
  465. }
  466. }
  467. static void sLoadFloat(Script* sc) {
  468. Value* v = sPeekStack(sc, VT_POINTER);
  469. if(v == NULL) {
  470. return;
  471. }
  472. sc->stackIndex--;
  473. v = sLoadFromPointer(sc, v, VT_FLOAT);
  474. if(v != NULL) {
  475. sPushFloat(sc, v->data.floatValue);
  476. }
  477. }
  478. static void sLoadArray(Script* sc) {
  479. Value* v = sPeekStack(sc, VT_POINTER);
  480. if(v == NULL) {
  481. return;
  482. }
  483. sc->stackIndex--;
  484. v = sLoadFromPointer(sc, v, VT_ARRAY);
  485. if(v != NULL) {
  486. Value* array = sPushStack(sc, 1);
  487. if(array != NULL) {
  488. *array = *v;
  489. }
  490. }
  491. }
  492. static void sEqualArrays(Script* sc, bool wanted) {
  493. Value* a = sPeekStack(sc, VT_ARRAY);
  494. if(a == NULL) {
  495. return;
  496. }
  497. sc->stackIndex--;
  498. Value* b = sPeekStack(sc, VT_ARRAY);
  499. if(b == NULL) {
  500. return;
  501. }
  502. sc->stackIndex--;
  503. sPushInt32(sc, (a->offset == b->offset &&
  504. a->data.intValue == b->data.intValue) == wanted);
  505. }
  506. static void sCall(Script* sc) {
  507. int32 function = 0;
  508. if(sReadInt32(sc, &function)) {
  509. return;
  510. }
  511. if(gfsCall(sc, function)) {
  512. sError(sc, "invalid function call");
  513. }
  514. }
  515. static void sPushText(Script* sc) {
  516. int32 length;
  517. if(sReadInt32(sc, &length)) {
  518. return;
  519. }
  520. if(length < 0 || length > 65535) {
  521. sError(sc, "too large string");
  522. return;
  523. }
  524. Value* p = sPushStack(sc, 1);
  525. if(p == NULL) {
  526. return;
  527. }
  528. p->type = VT_ARRAY;
  529. p->offset = 0;
  530. p->data.intValue = asAllocate(&sc->arrays, 1, length);
  531. if(p->data.intValue == -1) {
  532. sError(sc, "out of memory");
  533. return;
  534. } else if(p->data.intValue == -2) {
  535. sError(sc, "bad allocation");
  536. return;
  537. }
  538. SnuviArray* a = asGet(&sc->arrays, p->data.intValue);
  539. if(a == NULL) {
  540. sError(sc, "cannot find text array");
  541. return;
  542. }
  543. for(int i = 0; i < length; i++) {
  544. sReadInt32(sc, &(a->data[i].data.intValue));
  545. a->data[i].type = VT_INT;
  546. }
  547. }
  548. static Value* sChangeBase(Script* sc, char* c) {
  549. if(sRead(sc, c, sizeof(char))) {
  550. return NULL;
  551. }
  552. Value* p = sPeekStack(sc, VT_POINTER);
  553. if(p == NULL) {
  554. return NULL;
  555. }
  556. sc->stackIndex--;
  557. return sLoadFromPointer(sc, p, VT_INT);
  558. }
  559. static void sChange(Script* sc) {
  560. char c;
  561. Value* v = sChangeBase(sc, &c);
  562. if(v != NULL) {
  563. v->data.intValue += c;
  564. }
  565. }
  566. static void sPushBeforeChange(Script* sc) {
  567. char c;
  568. Value* v = sChangeBase(sc, &c);
  569. if(v != NULL) {
  570. sPushInt32(sc, v->data.intValue);
  571. v->data.intValue += c;
  572. }
  573. }
  574. static void sPushAfterChange(Script* sc) {
  575. char c;
  576. Value* v = sChangeBase(sc, &c);
  577. if(v != NULL) {
  578. v->data.intValue += c;
  579. sPushInt32(sc, v->data.intValue);
  580. }
  581. }
  582. static void sFloatToInt32(Script* sc) {
  583. Value* v = sPeekStack(sc, VT_FLOAT);
  584. if(v != NULL) {
  585. v->data.intValue = (int)v->data.floatValue;
  586. v->type = VT_INT;
  587. }
  588. }
  589. static void sInt32ToFloat(Script* sc) {
  590. Value* v = sPeekStack(sc, VT_INT);
  591. if(v != NULL) {
  592. v->data.floatValue = (float)v->data.intValue;
  593. v->type = VT_FLOAT;
  594. }
  595. }
  596. #define CASE_NUMBER_OP(name, op) \
  597. case OP_##name##_INT: NUMBER_OP(int32, Int32, op); return; \
  598. case OP_##name##_FLOAT: \
  599. NUMBER_OP(float, Float, op); \
  600. return;
  601. #define CASE_BOOL_OP(name, op) \
  602. case OP_##name##_INT: BOOL_OP(int32, Int32, op); return; \
  603. case OP_##name##_FLOAT: \
  604. BOOL_OP(float, Float, op); \
  605. return;
  606. #define CASE_TYPE(TYPE, Type, type) \
  607. case OP_RETURN_##TYPE: RETURN(type, Type); return; \
  608. case OP_EQUAL_##TYPE: BOOL_OP(type, Type, ==); return; \
  609. case OP_NOT_EQUAL_##TYPE: BOOL_OP(type, Type, !=); return;
  610. static void sConsumeInstruction(Script* sc) {
  611. Operation op = sReadOperation(sc);
  612. switch(op) {
  613. CASE_NUMBER_OP(ADD, +);
  614. CASE_NUMBER_OP(SUB, -);
  615. CASE_NUMBER_OP(MUL, *);
  616. CASE_BOOL_OP(LESS, <);
  617. CASE_BOOL_OP(LESS_EQUAL, <=);
  618. CASE_BOOL_OP(GREATER, >);
  619. CASE_BOOL_OP(GREATER_EQUAL, >=);
  620. CASE_TYPE(INT, Int32, int32);
  621. #pragma GCC diagnostic push
  622. #pragma GCC diagnostic ignored "-Wfloat-equal"
  623. CASE_TYPE(FLOAT, Float, float);
  624. case OP_DIV_FLOAT: DIVISION(float, Float); return;
  625. #pragma GCC diagnostic pop
  626. case OP_DIV_INT: DIVISION(int32, Int32); return;
  627. case OP_STORE_INT: sStoreInt32(sc); return;
  628. case OP_STORE_FLOAT: sStoreFloat(sc); return;
  629. case OP_LOAD_INT: sLoadInt32(sc); return;
  630. case OP_LOAD_FLOAT: sLoadFloat(sc); return;
  631. case OP_PUSH_PRE_CHANGE_INT: sPushAfterChange(sc); return;
  632. case OP_PUSH_POST_CHANGE_INT: sPushBeforeChange(sc); return;
  633. case OP_CHANGE_INT: sChange(sc); return;
  634. case OP_FLOAT_TO_INT: sFloatToInt32(sc); return;
  635. case OP_INT_TO_FLOAT: sInt32ToFloat(sc); return;
  636. case OP_BIT_AND_INT: NUMBER_OP(int32, Int32, &); return;
  637. case OP_BIT_OR_INT: NUMBER_OP(int32, Int32, |); return;
  638. case OP_BIT_XOR_INT: NUMBER_OP(int32, Int32, ^); return;
  639. case OP_LEFT_SHIFT_INT: NUMBER_OP(int32, Int32, <<); return;
  640. case OP_RIGHT_SHIFT_INT: NUMBER_OP(int32, Int32, >>); return;
  641. case OP_NOTHING: return;
  642. case OP_PUSH_INT: sPushInt32Value(sc); return;
  643. case OP_PUSH_FLOAT: sPushFloatValue(sc); return;
  644. case OP_PUSH_TEXT: sPushText(sc); return;
  645. case OP_MOD_INT: MODULE(int32, Int32); return;
  646. case OP_INVERT_SIGN_INT: INVERT_SIGN(int32, Int32); return;
  647. case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); return;
  648. case OP_NOT: sNot(sc); return;
  649. case OP_AND: BOOL_OP(int32, Int32, &&); return;
  650. case OP_OR: BOOL_OP(int32, Int32, ||); return;
  651. case OP_BIT_NOT_INT: sBitNotInt32(sc); return;
  652. case OP_LINE: sLine(sc); return;
  653. case OP_GOTO: sGoTo(sc); return;
  654. case OP_IF_GOTO: sIfGoTo(sc); return;
  655. case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); return;
  656. case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); return;
  657. case OP_GOSUB: sGoSub(sc); return;
  658. case OP_RETURN: sReturn(sc); return;
  659. case OP_RETURN_POINTER: sReturnPointer(sc); return;
  660. case OP_RESERVE: sReserveBytes(sc); return;
  661. case OP_GRESERVE: sGlobalReserveBytes(sc); return;
  662. case OP_DEREFERENCE_VAR: sDereference(sc); return;
  663. case OP_DEREFERENCE_GVAR: sGlobalDereference(sc); return;
  664. case OP_LOAD_ARRAY: sLoadArray(sc); return;
  665. case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); return;
  666. case OP_ADD_REFERENCE: sAddReference(sc); return;
  667. case OP_PUSH_STRUCT_REFERENCE: sPushStructReference(sc); return;
  668. case OP_NEW: sNewArray(sc); return;
  669. case OP_LENGTH: sLength(sc); return;
  670. case OP_STORE_ARRAY: sStoreArray(sc); return;
  671. case OP_EQUAL_POINTER: sEqualArrays(sc, true); return;
  672. case OP_NOT_EQUAL_POINTER: sEqualArrays(sc, false); return;
  673. case OP_CALL: sCall(sc); return;
  674. }
  675. sError(sc, "unknown operation %d", (int)op);
  676. }
  677. static bool sHasData(Script* sc) {
  678. return sc->readIndex < sc->code->length;
  679. }
  680. Script* sInit(ByteCode* code) {
  681. Script* sc = (Script*)malloc(sizeof(Script));
  682. sc->error[0] = '\0';
  683. sc->code = code;
  684. sc->readIndex = 0;
  685. sc->stackIndex = 0;
  686. sc->stackVarIndex = 0;
  687. sc->line = 0;
  688. asInit(&sc->arrays);
  689. return sc;
  690. }
  691. void sDelete(Script* sc) {
  692. bcDelete(sc->code);
  693. asDelete(&sc->arrays);
  694. free(sc);
  695. }
  696. void sRun(Script* sc) {
  697. while(sHasData(sc)) {
  698. sConsumeInstruction(sc);
  699. if(sc->error[0] != '\0') {
  700. return;
  701. }
  702. }
  703. }