|
@@ -0,0 +1,75 @@
|
|
|
|
|
+#include "Code.h"
|
|
|
|
|
+
|
|
|
|
|
+#include <string.h>
|
|
|
|
|
+
|
|
|
|
|
+static u8 code[MAX_CODE];
|
|
|
|
|
+static size_t codeIndex = 0;
|
|
|
|
|
+static size_t codeExecutionIndex = 0;
|
|
|
|
|
+
|
|
|
|
|
+static bool pushU8(u8 u) {
|
|
|
|
|
+ if(codeIndex >= MAX_CODE) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ code[codeIndex++] = u;
|
|
|
|
|
+ return false;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static bool push(const void* p, size_t n) {
|
|
|
|
|
+ if(codeIndex + n >= MAX_CODE) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ memcpy(code + codeIndex, p, n);
|
|
|
|
|
+ codeIndex += n;
|
|
|
|
|
+ return false;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool pushInstruction(Instruction i) {
|
|
|
|
|
+ return pushU8(i);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool pushI64(i64 i) {
|
|
|
|
|
+ return push(&i, sizeof(i));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool pushSize(size_t i) {
|
|
|
|
|
+ return push(&i, sizeof(i));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool pushConstantString(const char* c) {
|
|
|
|
|
+ size_t n = strlen(c) + 1;
|
|
|
|
|
+ return pushSize(n) || push(c, n);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Instruction readInstruction() {
|
|
|
|
|
+ return codeExecutionIndex < codeIndex ? code[codeExecutionIndex++] : STOP;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void read(void* p, size_t n) {
|
|
|
|
|
+ if(codeExecutionIndex + n <= codeIndex) {
|
|
|
|
|
+ memcpy(p, code + codeExecutionIndex, n);
|
|
|
|
|
+ codeExecutionIndex += n;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+i64 readI64() {
|
|
|
|
|
+ i64 i;
|
|
|
|
|
+ read(&i, sizeof(i));
|
|
|
|
|
+ return i;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+size_t readSize() {
|
|
|
|
|
+ size_t i;
|
|
|
|
|
+ read(&i, sizeof(i));
|
|
|
|
|
+ return i;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const char* readConstantString() {
|
|
|
|
|
+ size_t n = readSize();
|
|
|
|
|
+ if(codeExecutionIndex + n <= codeIndex &&
|
|
|
|
|
+ code[codeExecutionIndex + n - 1] == '\0') {
|
|
|
|
|
+ const char* s = (char*)(code + codeExecutionIndex);
|
|
|
|
|
+ codeExecutionIndex += n;
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }
|
|
|
|
|
+ return "";
|
|
|
|
|
+}
|