Kinx Library-JIT Compiler Library (Extra Edition)

Kinx Library-JIT Library (Extra Edition)

Introduction

** "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.

sample

First is a sample.

okay.kx


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) {
    Jit.dump(code);
    var runner = new Jit.Runner(code);
    System.println(runner.run(100));
}
$ ./kinx okay.kx
       0:   48 89 f8                                    mov rax, rdi
       3:   c3                                          ret
100

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

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_THUMB2-WIN" O
"ARM_V7-WIN" O
"ARM_V5-WIN" O
"ARM_64-WIN" O
"X86_32"
"X86_64"
"ARM_THUMB2"
"ARM_V7"
"ARM_V5"
"ARM_64"
"PPC_64"
"PPC_32"
"MIPS_32"
"MIPS_64"
"SPARC_32"
"TILEGX"
"UNSUPPORTED"

is.

What you shouldn't do

crash.kx


using Jit;
var code = <0x48, 0x31, 0xc0,   // xor rax, rax
            0x48, 0x8b, 0x00>   // mov rax, [rax]
            ;
Jit.dump(code);
var runner = new Jit.Runner(code);
System.println(runner.run());
$ ./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)