diff options
| Commit | 0932b770a7152e59695ec62bb7e39d58a5e7bb44 (patch) | |
|---|---|---|
| Author | 2022-10-30 11:33:02 +0200 | |
| Committer | 2022-10-30 11:33:29 +0200 | |
| Files | 7c5835a8af3329be6e19fc11c291d5cf08fd4e85 | |
| IPL-0932b770a7152e59695ec62bb7e39d58a5e7bb44.tar IPL-0932b770a7152e59695ec62bb7e39d58a5e7bb44.tar.gz IPL-0932b770a7152e59695ec62bb7e39d58a5e7bb44.zip | ||
| Parent | fa192bf9f51778e4c99526fa5ca1ae23079708b6 (diff) | |
Add demo for threaded interpretation
2 files changed, 171 insertions, 0 deletions
| -rw-r--r-- | demo/threaded/switch.cpp | 83 | ||||
| -rw-r--r-- | demo/threaded/threaded.cpp | 88 |
diff --git a/demo/threaded/switch.cpp b/demo/threaded/switch.cpp new file mode 100644 index 0000000..09b96e3 --- /dev/null +++ b/demo/threaded/switch.cpp @@ -0,0 +1,83 @@ +#include <cstdio>
+#include <cstdint>
+
+void run_switch(uint8_t* code, int limit)
+{
+ int counts[] = { 0, 0, 0, 0, 0, 0, 0 };
+
+ int ops = 0;
+ int pc = 0;
+
+ while (ops < limit)
+ {
+ switch (code[pc]) {
+ case 0:
+ {
+ ++counts[0];
+ ++ops;
+ ++pc;
+ break;
+ }
+ case 1:
+ {
+ ++counts[1];
+ ++ops;
+ ++pc;
+ break;
+ }
+ case 2:
+ {
+ ++counts[2];
+ ++ops;
+ ++pc;
+ break;
+ }
+ case 3:
+ {
+ ++counts[3];
+ ++ops;
+ ++pc;
+ break;
+ }
+ case 4:
+ {
+ ++counts[4];
+ ++ops;
+ ++pc;
+ break;
+ }
+ case 5:
+ {
+ ++counts[5];
+ ++ops;
+ break;
+ }
+ case 6:
+ {
+ ++counts[6];
+ ++ops;
+ pc = 0;
+ break;
+ }
+ }
+ }
+ std::printf("add: %d\n", counts[0]);
+ std::printf("sub: %d\n", counts[1]);
+ std::printf("mul: %d\n", counts[2]);
+ std::printf("div: %d\n", counts[3]);
+ std::printf("print: %d\n", counts[4]);
+ std::printf("halt: %d\n", counts[5]);
+ std::printf("restart: %d\n", counts[6]);
+}
+
+int main()
+{
+ uint8_t program[] = {
+ 0, 0, 0, 0,
+ 1, 2, 1, 2,
+ 4, 3, 4, 3,
+ 6,
+ };
+ run_switch(program, 32 * 1000 * 1000);
+ return 0;
+}
diff --git a/demo/threaded/threaded.cpp b/demo/threaded/threaded.cpp new file mode 100644 index 0000000..f263bd0 --- /dev/null +++ b/demo/threaded/threaded.cpp @@ -0,0 +1,88 @@ +#include <cstdio>
+#include <cstdint>
+
+void run_threaded(uint8_t* code, int limit)
+{
+ static void* opjump[] = {
+ &&add,
+ &&sub,
+ &&mul,
+ &&div,
+ &&print,
+ &&halt,
+ &&restart,
+ };
+
+ int counts[] = { 0, 0, 0, 0, 0, 0, 0 };
+
+ int ops = 0;
+ int pc = 0;
+ goto *opjump[code[pc]];
+
+ while (1)
+ {
+add:
+ {
+ ++counts[0];
+ if (++ops == limit) break;
+ goto *opjump[code[++pc]];
+ }
+sub:
+ {
+ ++counts[1];
+ if (++ops == limit) break;
+ goto *opjump[code[++pc]];
+ }
+mul:
+ {
+ ++counts[2];
+ if (++ops == limit) break;
+ goto *opjump[code[++pc]];
+ }
+div:
+ {
+ ++counts[3];
+ if (++ops == limit) break;
+ goto *opjump[code[++pc]];
+ }
+print:
+ {
+ ++counts[4];
+ if (++ops == limit) break;
+ goto *opjump[code[++pc]];
+ }
+halt:
+ {
+ ++counts[5];
+ if (++ops == limit) break;
+ break;
+ }
+
+restart:
+ {
+ ++counts[6];
+ if (++ops == limit) break;
+ pc = 0;
+ goto *opjump[code[pc]];
+ }
+ }
+ std::printf("add: %d\n", counts[0]);
+ std::printf("sub: %d\n", counts[1]);
+ std::printf("mul: %d\n", counts[2]);
+ std::printf("div: %d\n", counts[3]);
+ std::printf("print: %d\n", counts[4]);
+ std::printf("halt: %d\n", counts[5]);
+ std::printf("restart: %d\n", counts[6]);
+}
+
+int main()
+{
+ uint8_t program[] = {
+ 0, 0, 0, 0,
+ 1, 2, 1, 2,
+ 4, 3, 4, 3,
+ 6,
+ };
+ run_threaded(program, 32 * 1000 * 1000);
+ return 0;
+}
|
