Script.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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(int32, Int32)
  113. READ_POP_PUSH(int64, Int64)
  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 INVERT_SIGN(type, Type) \
  127. { \
  128. type value = 0; \
  129. if(sPop##Type(sc, &value)) { \
  130. sPush##Type(sc, -value); \
  131. } \
  132. }
  133. static void sReserveBytes(Script* sc) {
  134. int32 bytes = 0;
  135. int32 offset = 0;
  136. if(sReadInt32(sc, &bytes) && sReadInt32(sc, &offset)) {
  137. int32 oldIndex = sc->stackVarIndex;
  138. sc->stackVarIndex = sc->stackIndex - offset;
  139. sReserve(sc, bytes - offset);
  140. sPushInt32(sc, oldIndex);
  141. }
  142. }
  143. static void* sCheckAddress(Script* sc, Pointer* p, int length) {
  144. if(p->array >= 0) {
  145. Array* a = asGet(&sc->arrays, p->array);
  146. if(a == NULL) {
  147. sError(sc, "invalid heap pointer");
  148. return NULL;
  149. } else if(p->offset < 0 || p->offset >= a->size) {
  150. sError(sc, "address %d is out of array bounds", p->offset);
  151. return NULL;
  152. }
  153. return ((char*)a->data) + p->offset;
  154. }
  155. if(p->offset < 0 || p->offset + length > sc->stackIndex) {
  156. sError(sc, "address %d is out of stack bounds", p->offset);
  157. return NULL;
  158. }
  159. return sc->stack + p->offset;
  160. }
  161. static void sNot(Script* sc) {
  162. bool value = false;
  163. if(sPopBool(sc, &value)) {
  164. sPushBool(sc, !value);
  165. }
  166. }
  167. static void sBitNotInt32(Script* sc) {
  168. int32 value = 0;
  169. if(sPopInt32(sc, &value)) {
  170. sPushInt32(sc, ~value);
  171. }
  172. }
  173. static void sBitNotInt64(Script* sc) {
  174. int64 value = 0;
  175. if(sPopInt64(sc, &value)) {
  176. sPushInt64(sc, ~value);
  177. }
  178. }
  179. static void sLine(Script* sc) {
  180. sRead(sc, &sc->line, 2);
  181. }
  182. static void sGoTo(Script* sc) {
  183. int32 gotoIndex;
  184. if(sReadInt32(sc, &gotoIndex)) {
  185. sc->readIndex = gotoIndex;
  186. }
  187. }
  188. static void sGoSub(Script* sc) {
  189. int32 gotoIndex;
  190. int32 offset;
  191. if(sReadInt32(sc, &gotoIndex) && sReadInt32(sc, &offset)) {
  192. Pointer p = {.array = -1,
  193. .offset = sc->stackIndex - offset - sizeof(int32)};
  194. void* dest = sCheckAddress(sc, &p, sizeof(int32));
  195. if(dest != NULL) {
  196. memcpy(dest, &sc->readIndex, sizeof(int32));
  197. sc->readIndex = gotoIndex;
  198. }
  199. }
  200. }
  201. static void sReturn(Script* sc) {
  202. int32 bytes = 0;
  203. int32 varIndex = 0;
  204. if(sReadInt32(sc, &bytes) && sPopInt32(sc, &varIndex)) {
  205. sc->stackVarIndex = varIndex;
  206. sFree(sc, bytes);
  207. if(!sPopInt32(sc, &sc->readIndex) || sc->readIndex < 0) {
  208. sError(sc, "read index is corrupt");
  209. }
  210. }
  211. }
  212. static void sReturnPointer(Script* sc) {
  213. Pointer p;
  214. if(sPopPointer(sc, &p)) {
  215. sReturn(sc);
  216. sPushPointer(sc, &p);
  217. }
  218. }
  219. #define RETURN(type, Type) \
  220. { \
  221. type value; \
  222. if(sPop##Type(sc, &value)) { \
  223. sReturn(sc); \
  224. sPush##Type(sc, value); \
  225. } \
  226. }
  227. static void sIfGoTo(Script* sc) {
  228. int32 gotoIndex = 0;
  229. bool value = false;
  230. if(sReadInt32(sc, &gotoIndex) && sPopBool(sc, &value) && !value) {
  231. sc->readIndex = gotoIndex;
  232. }
  233. }
  234. static void sPeekFalseGoTo(Script* sc) {
  235. int32 gotoIndex = 0;
  236. bool value = false;
  237. if(sReadInt32(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) &&
  238. !value) {
  239. sc->readIndex = gotoIndex;
  240. }
  241. }
  242. static void sPeekTrueGoTo(Script* sc) {
  243. int32 gotoIndex = 0;
  244. bool value = false;
  245. if(sReadInt32(sc, &gotoIndex) && sPeek(sc, &value, sizeof(bool)) && value) {
  246. sc->readIndex = gotoIndex;
  247. }
  248. }
  249. static void sNewArray(Script* sc) {
  250. int32 length = 0;
  251. int32 size = 0;
  252. if(sReadInt32(sc, &size) && sPopInt32(sc, &length)) {
  253. Pointer p = {.array = asAllocate(&sc->arrays, size, length),
  254. .offset = 0};
  255. if(p.array == -1) {
  256. sError(sc, "out of memory");
  257. } else if(p.array == -2) {
  258. sError(sc, "bad allocation");
  259. } else {
  260. sPushPointer(sc, &p);
  261. }
  262. }
  263. }
  264. static void sDeleteArray(Script* sc) {
  265. Pointer p;
  266. if(sPopPointer(sc, &p)) {
  267. if(p.offset != 0) {
  268. sError(sc, "delete of array with offset: %d", p.offset);
  269. return;
  270. }
  271. Array* a = asGet(&sc->arrays, p.array);
  272. if(a == NULL) {
  273. sError(sc, "delete of invalid array");
  274. return;
  275. }
  276. asDeleteArray(&sc->arrays, a, p.array);
  277. }
  278. }
  279. static void sLength(Script* sc) {
  280. Pointer p;
  281. if(sPopPointer(sc, &p)) {
  282. if(p.array == -1) {
  283. sPushInt32(sc, p.offset >= 0);
  284. return;
  285. }
  286. Array* a = asGet(&sc->arrays, p.array);
  287. if(a == NULL) {
  288. sError(sc, "invalid heap pointer");
  289. return;
  290. }
  291. sPushInt32(sc, a->length);
  292. }
  293. }
  294. static void sDereference(Script* sc) {
  295. int32 address = 0;
  296. if(sReadInt32(sc, &address)) {
  297. Pointer p = {.array = -1, .offset = address + sc->stackVarIndex};
  298. sPushPointer(sc, &p);
  299. }
  300. }
  301. static void sLoad(Script* sc, int length) {
  302. Pointer p;
  303. if(sPopPointer(sc, &p)) {
  304. void* src = sCheckAddress(sc, &p, length);
  305. if(src != NULL) {
  306. sPush(sc, src, length);
  307. }
  308. }
  309. }
  310. static void sDuplicateReference(Script* sc) {
  311. Pointer p;
  312. if(sPeek(sc, &p, sizeof(Pointer))) {
  313. sPushPointer(sc, &p);
  314. }
  315. }
  316. static void sAddReference(Script* sc) {
  317. int32 size = 0;
  318. int32 add = 0;
  319. Pointer p;
  320. if(sReadInt32(sc, &size) && sPopInt32(sc, &add) && sPopPointer(sc, &p)) {
  321. p.offset += add * size;
  322. sPushPointer(sc, &p);
  323. }
  324. }
  325. static void sLoadSize(Script* sc) {
  326. int32 size = 0;
  327. Pointer p;
  328. if(sReadInt32(sc, &size) && sPopPointer(sc, &p)) {
  329. void* src = sCheckAddress(sc, &p, size);
  330. if(src != NULL) {
  331. sPush(sc, src, size);
  332. }
  333. }
  334. }
  335. static void sStore(Script* sc, int length) {
  336. int index = sc->stackIndex - sizeof(Pointer) - length;
  337. if(index < 0) {
  338. sError(sc, "stack underflow");
  339. return;
  340. }
  341. Pointer p;
  342. memcpy(&p, sc->stack + index, sizeof(Pointer));
  343. void* dest = sCheckAddress(sc, &p, length);
  344. if(dest != NULL) {
  345. sPop(sc, dest, length);
  346. sc->stackIndex -= sizeof(Pointer);
  347. }
  348. }
  349. static void sEqualPointer(Script* sc, bool wanted) {
  350. Pointer a;
  351. Pointer b;
  352. if(sPopPointer(sc, &a) && sPopPointer(sc, &b)) {
  353. sPushBool(sc, (a.array == b.array && a.offset == b.offset) == wanted);
  354. }
  355. }
  356. static void sCall(Script* sc) {
  357. int32 function = 0;
  358. if(sReadInt32(sc, &function) && gfsCall(sc, function)) {
  359. sError(sc, "invalid function call");
  360. }
  361. }
  362. #define CHANGE_OP(type, op) \
  363. { \
  364. char c = 0; \
  365. Pointer p; \
  366. if(sRead(sc, &c, sizeof(char)) && sPopPointer(sc, &p)) { \
  367. void* data = sCheckAddress(sc, &p, sizeof(type)); \
  368. if(data != NULL) { \
  369. type current; \
  370. memcpy(&current, data, sizeof(type)); \
  371. op \
  372. } \
  373. } \
  374. }
  375. #define PUSH_PRE_CHANGE(Type, type) \
  376. CHANGE_OP(type, current += c; sPush##Type(sc, current); \
  377. memcpy(data, &current, sizeof(type));)
  378. #define PUSH_POST_CHANGE(Type, type) \
  379. CHANGE_OP(type, sPush##Type(sc, current); current += c; \
  380. memcpy(data, &current, sizeof(type));)
  381. #define CHANGE(type) \
  382. CHANGE_OP(type, current += c; memcpy(data, &current, sizeof(type));)
  383. #define CAST(From, from, To) \
  384. { \
  385. from value; \
  386. if(sPop##From(sc, &value)) { \
  387. sPush##To(sc, value); \
  388. } \
  389. }
  390. #define CASE_CAST(TYPE1, Type1, type1, TYPE2, Type2, type2) \
  391. case OP_##TYPE1##_TO_##TYPE2: CAST(Type1, type1, Type2); break; \
  392. case OP_##TYPE2##_TO_##TYPE1: \
  393. CAST(Type2, type2, Type1); \
  394. break;
  395. #define CASE_CHANGE(TYPE, Type, type) \
  396. case OP_PUSH_PRE_CHANGE_##TYPE: PUSH_PRE_CHANGE(Type, type); break; \
  397. case OP_PUSH_POST_CHANGE_##TYPE: PUSH_POST_CHANGE(Type, type); break; \
  398. case OP_CHANGE_##TYPE: \
  399. CHANGE(type); \
  400. break;
  401. #define CASE_INTEGRAL_OP(name, op) \
  402. case OP_##name##_INT32: NUMBER_OP(int32, Int32, op); break; \
  403. case OP_##name##_INT64: \
  404. NUMBER_OP(int64, Int64, op); \
  405. break;
  406. #define CASE_NUMBER_OP(name, op) \
  407. CASE_INTEGRAL_OP(name, op) \
  408. case OP_##name##_FLOAT: \
  409. NUMBER_OP(float, Float, op); \
  410. break;
  411. #define CASE_BOOL_OP(name, op) \
  412. case OP_##name##_INT32: BOOL_OP(int32, Int32, op); break; \
  413. case OP_##name##_INT64: BOOL_OP(int64, Int64, 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_EQUAL_##TYPE: BOOL_OP(type, Type, ==); break; \
  421. case OP_NOT_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(LESS_EQUAL, <=);
  430. CASE_BOOL_OP(GREATER, >);
  431. CASE_BOOL_OP(GREATER_EQUAL, >=);
  432. CASE_TYPE(INT32, Int32, int32);
  433. CASE_TYPE(INT64, Int64, int64);
  434. CASE_TYPE(BOOL, Bool, bool);
  435. CASE_TYPE(FLOAT, Float, float);
  436. CASE_CHANGE(INT32, Int32, int32);
  437. CASE_CHANGE(INT64, Int64, int64);
  438. CASE_INTEGRAL_OP(BIT_AND, &);
  439. CASE_INTEGRAL_OP(BIT_OR, |);
  440. CASE_INTEGRAL_OP(BIT_XOR, ^);
  441. CASE_INTEGRAL_OP(LEFT_SHIFT, <<);
  442. CASE_INTEGRAL_OP(RIGHT_SHIFT, >>);
  443. CASE_CAST(INT32, Int32, int32, FLOAT, Float, float);
  444. CASE_CAST(INT32, Int32, int32, INT64, Int64, int64);
  445. CASE_CAST(INT64, Int64, int64, FLOAT, Float, float);
  446. case OP_NOTHING: break;
  447. case OP_PUSH_INT32: PUSH_CONSTANT(int32, Int32); break;
  448. case OP_PUSH_INT64: PUSH_CONSTANT(int64, Int64); break;
  449. case OP_PUSH_FLOAT: PUSH_CONSTANT(float, Float); break;
  450. case OP_PUSH_TRUE: sPushBool(sc, true); break;
  451. case OP_PUSH_FALSE: sPushBool(sc, false); break;
  452. case OP_PUSH_NULLPTR: sPushNullPointer(sc); break;
  453. case OP_DIV_INT32: DIVISION(int32, Int32); break;
  454. case OP_DIV_INT64: DIVISION(int64, Int64); break;
  455. case OP_DIV_FLOAT: DIVISION(float, Float); break;
  456. case OP_MOD_INT32: MODULE(int32, Int32); break;
  457. case OP_MOD_INT64: MODULE(int64, Int64); break;
  458. case OP_INVERT_SIGN_INT32: INVERT_SIGN(int32, Int32); break;
  459. case OP_INVERT_SIGN_INT64: INVERT_SIGN(int64, Int64); break;
  460. case OP_INVERT_SIGN_FLOAT: INVERT_SIGN(float, Float); break;
  461. case OP_NOT: sNot(sc); break;
  462. case OP_AND: BOOL_OP(bool, Bool, &&); break;
  463. case OP_OR: BOOL_OP(bool, Bool, ||); break;
  464. case OP_BIT_NOT_INT32: sBitNotInt32(sc); break;
  465. case OP_BIT_NOT_INT64: sBitNotInt64(sc); break;
  466. case OP_LINE: sLine(sc); break;
  467. case OP_GOTO: sGoTo(sc); break;
  468. case OP_IF_GOTO: sIfGoTo(sc); break;
  469. case OP_PEEK_FALSE_GOTO: sPeekFalseGoTo(sc); break;
  470. case OP_PEEK_TRUE_GOTO: sPeekTrueGoTo(sc); break;
  471. case OP_GOSUB: sGoSub(sc); break;
  472. case OP_RETURN: sReturn(sc); break;
  473. case OP_RETURN_POINTER: sReturnPointer(sc); break;
  474. case OP_RESERVE: sReserveBytes(sc); break;
  475. case OP_DEREFERENCE_VAR: sDereference(sc); break;
  476. case OP_REFERENCE: sLoad(sc, sizeof(Pointer)); break;
  477. case OP_DUPLICATE_REFERENCE: sDuplicateReference(sc); break;
  478. case OP_ADD_REFERENCE: sAddReference(sc); break;
  479. case OP_LOAD: sLoadSize(sc); break;
  480. case OP_NEW: sNewArray(sc); break;
  481. case OP_DELETE: sDeleteArray(sc); break;
  482. case OP_LENGTH: sLength(sc); break;
  483. case OP_STORE_POINTER: sStore(sc, sizeof(Pointer)); break;
  484. case OP_EQUAL_POINTER: sEqualPointer(sc, true); break;
  485. case OP_NOT_EQUAL_POINTER: sEqualPointer(sc, false); break;
  486. case OP_CALL: sCall(sc); break;
  487. }
  488. }
  489. static bool sHasData(Script* sc) {
  490. return sc->readIndex < sc->code->length;
  491. }
  492. Script* sInit(ByteCode* code) {
  493. Script* sc = malloc(sizeof(Script));
  494. sc->error[0] = '\0';
  495. sc->code = code;
  496. sc->readIndex = 0;
  497. sc->stackIndex = 0;
  498. sc->stackVarIndex = 0;
  499. sc->line = 0;
  500. asInit(&sc->arrays);
  501. return sc;
  502. }
  503. void sDelete(Script* sc) {
  504. bcDelete(sc->code);
  505. asDelete(&sc->arrays);
  506. free(sc);
  507. }
  508. void sRun(Script* sc) {
  509. while(sHasData(sc)) {
  510. sConsumeInstruction(sc);
  511. if(sc->error[0] != '\0') {
  512. puts("error:");
  513. printf(" - info: %s\n", sc->error);
  514. printf(" - line: %d\n", sc->line);
  515. return;
  516. }
  517. }
  518. }