Kinx Library-JIT Compiler Library (Extra Edition)

Kinx Library-JIT Library (Extra Edition)


** "Looks like JavaScript, brain (contents) is Ruby, (stability is AC / DC)" ** Scripting language Kinx ). This time is the JIT library extra edition.

This is a newly added function to the JIT library introduced in here.

I added a function to execute arbitrary binary code to JIT library. For those who really want fine control. This is heavily architecture dependent. And it's easy to crash.


First is a sample.


using Jit;

var code
    = System.PLATFORM == "X86_64-WIN" ? <0x48, 0x89, 0xc8, 0xc3>    // mov rax, rcx | ret
    : System.PLATFORM == "X86_64"     ? <0x48, 0x89, 0xf8, 0xc3>    // mov rax, rdi | ret
    : null;
if (code.isBinary) {
    var runner = new Jit.Runner(code);
$ ./kinx okay.kx
       0:   48 89 f8                                    mov rax, rdi
       3:   c3                                          ret

By the way, it is a function that simply returns the numerical value given as an argument, and since there is no register to destroy, there is no prologue or epilogue, and the value that comes to the first argument is set to the return value (rax) and ret is performed. It's just for you.


System.PLATFORM is used to isolate x64 and Windows or non-Windows. With a raw assembler, it is troublesome to have to separate it in this way. However, you can fully enjoy the dangerous and sweet scent of ** you can do anything **.

On Windows, the first argument comes in the rcx register because it is based on the Microsoft calling convention. In contrast, the System V calling convention, which is mostly adopted by non-Microsoft, puts the first argument in the rdi register.

What is specifically returned by System.PLATFORM is ...

Value Window?
"X86_32-WIN" O
"X86_64-WIN" O
"ARM_64-WIN" O


What you shouldn't do


using Jit;
var code = <0x48, 0x31, 0xc0,   // xor rax, rax
            0x48, 0x8b, 0x00>   // mov rax, [rax]
var runner = new Jit.Runner(code);
$ ./kinx crash.kx
       0:   48 31 c0                                    xor rax, rax
       3:   48 8b 00                                    mov rax, [rax]
Segmentation fault (core dumped)

Fufufu ... It's dangerous.

in conclusion

In a nutshell, it's like ** "Take your own risk." **. However, I don't think there are many scripting languages that go into this kind of place, so it may be worth it. For example, if you have this library, Xbyak seems to be portable! Kinx also has operator overrides (not called overloads).

JIT is becoming more and more familiar.

see you.

Recommended Posts

Kinx Library-JIT Compiler Library (Extra Edition)
Kinx Library-JIT Compiler Library
Kinx Library-Parser Combinator (Extra Edition)
Kinx Library --REPL
Kinx Library --Getopt
Kinx Library --DateTime
Kinx Library --Process
Let's try Zoomdata (extra edition)