JavaScript "worker"

         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {
cpu_xbar_offer, cpu_xbar_poll
} from "./sync.mjs";
let fs;
let workers;
if (typeof WorkerGlobalScope === 'undefined') {
fs = "";
workers = await import("node:worker_threads");
} else {
fs = undefined;
workers = undefined;
}
const MAX_TILT = 64;
function CpuMap() {
this.code = null;
this.offset = null;
this.comm = null;
this.state = null;
}
function CpuWarp() {
this.map = null;
this.from = 0;
this.to = 0;
this.cmd = null;
}
let warp = new CpuWarp();
function run(warp) {
let pi = warp.map;
for (let i = warp.from; i < warp.to; i++)
pi.state[pi.offset[i]-3] = i;
let active = true;
while (active) {
active = false;
for (let i = warp.from; i < warp.to; i++) {
if (run_id(pi, i))
active = true;
}
}
}
function run_id(pi, id) {
let base = pi.offset[id];
let pc = pi.state[base-2];
let accu = pi.state[base-1];
for (let k = 0; k < MAX_TILT; k++) {
if (pc < pi.code.length) {
let instr = pi.code[pc];
let value = hack_get(pi, instr, pc, base);
pc = hack_set(pi, instr, accu, pc, base);
accu = hack_fun(instr, accu, value);
} else {
pi.state[base-2] = pc;
pi.state[base-1] = accu;
return false;
}
}
pi.state[base-2] = pc;
pi.state[base-1] = accu;
return true;
}
function hack_get(pi, instr, pc, base) {
switch ((instr>>28) & 0x0F) {
case 0: /* cst */
let cst = (instr & 0x00FFFFFF) - 0x00800000;
return cst;
case 1: /* get */
let get = (instr & 0x00FFFFFF) - 0x00800000;
return pi.state[base+get];
case 2: /* ext */
return pi.code[pc+1];
case 3: /* out */
let out = (instr & 0x00FFFFFF) - 0x00800000;
return extra_output(pi, out, base);
case 4: /* in */
let min = (instr & 0x00FFFFFF) - 0x00800000;
return extra_input(pi, min, base);
case 5: /* set */
return 0;
case 6: /* bra */
return pc+1;
case 7: /* brz */
return pc+1;
case 8: /* jma */
return pc+1;
case 9: /* jmz */
return pc+1;
default:
throw new Error("illegal mode");
}
}
function hack_set(pi, instr, accu, pc, base) {
switch ((instr>>28) & 0x0F) {
case 0: /* cst */
return pc+1;
case 1: /* get */
return pc+1;
case 2: /* ext */
return pc+2;
case 3: /* out */
return pc+1;
case 4: /* in */
return pc+1;
case 5: /* set */
let set = (instr & 0x00FFFFFF) - 0x00800000
pi.state[base+set] = accu;
return pc+1;
case 6: /* bra */
let bra = (instr & 0x00FFFFFF) - 0x00800000;
return bra;
case 7: /* brz */
let brz = (instr & 0x00FFFFFF) - 0x00800000;
return (accu === 0 ? brz : pc+1);
case 8: /* jma */
let jma = (instr & 0x00FFFFFF) - 0x00800000;
return pi.state[base+jma];
case 9: /* jmz */
let jmz = (instr & 0x00FFFFFF) - 0x00800000;
return (accu === 0 ? pi.state[base+jmz] : pc+1);
default:
throw new Error("illegal mode");
}
}
function hack_fun(instr, accu, value) {
switch ((instr>>24) & 0x0F) {
case 0: /* mov */
return value;
case 1: /* neg */
return (- value) | 0;
case 2: /* add */
return (accu + value) | 0;
case 3: /* sub */
return (accu - value) | 0;
case 4: /* mul */
return Math.imul(accu, value);
case 5: /* zdiv */
return (accu / value) | 0;
case 6: /* rem */
return accu % value;
case 7: /* min */
return Math.min(accu, value);
case 8: /* max */
return Math.max(accu, value);
case 9: /* eq */
return (accu === value ? 1 : 0);
case 10: /* nq */
return (accu !== value ? 1 : 0);
case 11: /* ls */
return (accu < value ? 1 : 0);
case 12: /* gr */
return (accu > value ? 1 : 0);
case 13: /* lq */
return (accu <= value ? 1 : 0);
case 14: /* gq */
return (accu >= value ? 1 : 0);
default:
throw new Error("illegal fun");
}
}
/******************************************************************/
/* Hack Output/Input */
/******************************************************************/
function extra_output(pi, obj, base) {
let chan = pi.comm[obj];
return (cpu_xbar_offer(chan, pi.state, base+1, pi.state[base]) ? 1 : 0);
}
function extra_input(pi, obj, base) {
let chan = pi.comm[obj];
let len = pi.state[base];
let res = cpu_xbar_poll(chan, pi.state, base + 1, len);
if (res === -1)
return 0;
if (res !== len)
pi.state[base] = res;
return 1;
}
/******************************************************************/
/* Worker Utility */
/******************************************************************/
let pi = new CpuMap();
function thread_send(msg) {
if (fs !== undefined) {
workers.parentPort.postMessage(msg);
} else {
self.postMessage(msg);
}
}
async function thread_handler(msg) {
switch (msg.cmd) {
case "init":
pi.code = msg.code;
pi.offset = msg.offset;
pi.comm = msg.comm;
pi.state = msg.state;
break;
case "run":
warp.map = pi;
warp.from = msg.at;
warp.to = msg.at+msg.len;
run(warp);
thread_send({cmd: "finish"});
break;
default:
throw new Error("illegal cmd");
}
}
if (fs !== undefined) {
workers.parentPort.on('message', thread_handler);
} else {
self.onmessage = async (event) => thread_handler(event.data);
}