Advertisement

generating asm code using AngelScript's Jit Capability feature

Started by November 10, 2016 07:31 PM
6 comments, last by WitchLord 7 years, 11 months ago

hello,
as the topic title says, i want to generate asm code using AngelScript's Jit capability feature using AsmJit
now, for example, i want to decrement a variable, i should do this:

        case asBC_DecVi:
            a.dec(ptr(par1, ASMJIT_OFFSET_OF(asSVMRegisters, stackFramePointer)- asBC_SWORDARG0(bc)));
            break;
par1 is the asSVMRegisters struct which is passed as the first parameter
and, ASMJIT_OFFSET_OF is like offsetof macro, but defined in asmjit
as far as i know, i must retrieve the asSVMRegisters as the first parameter (which i get on the AllocArgs function of the assembler)
now, i have this assembly code which is not correct:
dec [ebx+3]
par1 is on the ebx and ASMJIT_OFFSET_OF() return's 4, if decremented by 1 with asBC_SWORDARG0(), return's 3
ok, what am i doing wrong?
this is my JitEntry:
        case asBC_JitEntry:
            a.mov(dword_ptr(par2), a.getOffset()- firstEntry);
            break;
a is the assembler, getOffset return's the position of the asm code and firstEntry is the position of the function when it was started to execute

when you can't see well like me, you can't test your applications and you can't read something

Github

Unfortunately I don't know AsmJit (nor do I know much about JIT compilation at all).

I suggest you study the code for BlindMind's JIT compiler to get an understanding of how to make your own JIT compiler work with AngelScript.

In the VM asBC_DecVi is implemented as:

    // Decrement the local integer variable
    case asBC_DecVi:
        (*(int*)(l_fp - asBC_SWORDARG0(l_bc)))--;
        l_bc++;
        break;

If we translate this to how the JIT compiled function might see it (written as a C++ function) it would be:

void JITcompiledFunction(asSVMRegistrers *reg, asPWORD jitArg)
{
    /* prologue for loading CPU registers and jumping to the right spot in the code based on jitArg (defined by JIT compiler) */
 
    (*(int*)(regs->stackFramePointer - (*(((short*)regs->programPointer)+1)))--;
    (regs->programPointer++;
 
    /* epilogue for cleaning up and returning to the VM */
}

You can compile this function to assembler and determine the assembler instructions that the JIT compiler has to produce to get the correct result.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

okay, instead of using for example asBC_SWORDARG0, can i use +1 to retrieve parameters? because in assembly, everything is offset based
and, what about jumps? i want to use jmp, what do you recommend? i think it should be like this:

a.push(a.zax());
a.mov(a.zax(), ptr(regs, ASMJIT_OFFSET_OF(asSVMRegisters, programPointer)));
a.mov(a.zax(), dword_ptr(a.zax(), 2));
a.add(dword_ptr(a.zax()), 1); //asBC_INTARG
a.jmp(a.zax());
a.mov(ptr(regs, ASMJIT_OFFSET_OF(asSVMRegisters, programPointer)), dword_ptr(a.zax());
a.pop(a.zax());
unfortunately, the compiler's generated asm will not help because everything is turned into constance, and dirring the execution everything will be loaded into memory and i have to change the memory with asmjit

when you can't see well like me, you can't test your applications and you can't read something

Github

asBC_SWORDARG0 means: read the 16bit argument at the offset of 2 bytes after the current program pointer address. This is what you'll need to implement in assembler instructions.

Yes, you can use jmp instructions the JIT compiled assembler code. As the asBC_JMP instruction in AngelScript refers to the byte code program pointer you'll need to translate this into the corresponding offset in the JIT assembler code. To do that you'll most likely need to perform multiple passes over the JIT assembler code as it won't be easy to predict the exact size of the assembler instructions until you've actually generated them.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

so, instead of writing a.mov(a.zax(), dword_ptr(a.zax(), 2)); i have to write a.mov(a.zax(), word_ptr(a.zax(), 2));

when you can't see well like me, you can't test your applications and you can't read something

Github

Probably, yes. But don't take my word for it. You'll have to test it to make sure. :)

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

hi again,
i have a question:
in (for example asBC_JMP), do the code in ExecuteNext will be executed or not?
i mean, this code which AngelScript has in asBC_JMP:

        l_bc += 2 + asBC_INTARG(l_bc);
do this executed or not? if yes, i don't need to execute it in asm level and just jump in that part of the code

when you can't see well like me, you can't test your applications and you can't read something

Github

Yes, the code in ExecuteNext for the instruction asBC_JMP is executed.

l_bc += 2 + asBC_INTARG(l_bc);

This code means: move the program pointer 2 + whatever value in the instruction argument.

2 in this case refers to the size of the asBC_JMP instruction itself. So if the argument would be 0, the jump instruction would be the same as a nop instruction, i.e. do nothing and just proceed with the next instruction.

In the JIT compiled code you'll want to translate this into the corresponding jump instruction to the appropriate address in the JIT compiled code. Remember that if your JIT compiled code returns control to the VM you must make sure the asSVMRegisters::programPointer member is correctly updated so the VM can resume the execution from the correct bytecode instruction.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement