I am developing a scripting language that compiles for my own virtual machine, a simple one that has instructions for working with certain types of data, such as points, vectors, floats, etc. The memory cell is represented in this way.
struct memory_cell { u32 id; u8 type; union { u8 b; double f; struct { double x, y, z; } v; struct { double r, g, b; } c; struct { double r, g, b; } cw; struct { double x, y, z; } p; struct { u16 length; memory_cell **cells; } l; }; };
Instructions are general and can work with many different operands. for instance
ADD dest, src1, src2
can work with floats, vectors, dots, colors that specify the correct type of destination in accordance with the operands.
The main execution loop simply checks the instruction operation code (which is a structure containing unions to define any command) and executes it. I used a simplified approach in which I have no registers, but just a large array of memory cells.
I was wondering if JIT could help me achieve better results or not and how to achieve it.
As I said, the best implementation achieved so far is something like this:
void VirtualMachine::executeInstruction(instr i) { u8 opcode = (i.opcode[0] & (u8)0xFC) >> 2; if (opcode >= 1 && opcode <= 17) { memory_cell *dest; memory_cell *src1; memory_cell *src2; switch (i.opcode[0] & 0x03) { case 0: { break; } case MEM_CELL: { dest = memory[stack_pointer+i.rtl.dest.cell]; break; } case ARRAY_VAL: { dest = memory[stack_pointer+i.rtl.dest.cell]->l.cells[i.rtl.dest.index]; break; } case ARRAY_CELL: { dest = memory[stack_pointer+i.rtl.dest.cell]->l.cells[(int)i.rtl.dest.value]; break; } } switch (opcode) { case ADD: { if (src1->type == M_VECTOR && src2->type == M_VECTOR) { dest->type = M_VECTOR; dest->vx = src1->vx + src2->vx; dest->vy = src1->vy + src2->vy; dest->vz = src1->vz + src2->vz; }
Is it easy / convenient to try jit compilation? But I really don’t know where to start, so I ask a few tips.
Also, are there any other tips that I should consider when developing it?
This virtual machine should be fast enough to calculate the shaders for the ray tracer, but I still haven't done any benchmark.