Skip to content

Instantly share code, notes, and snippets.

@M4GNV5
Created December 2, 2018 23:50
Show Gist options
  • Save M4GNV5/629e5ca55dd449c9bcb4d5bb00de317a to your computer and use it in GitHub Desktop.
Save M4GNV5/629e5ca55dd449c9bcb4d5bb00de317a to your computer and use it in GitHub Desktop.
function optimized func(i1 : int) : int
incoming_reg(i1, rdi)
.L:
i3 = 42
l5 = retrieve_frame_pointer
outgoing_reg(i3, rsi)
outgoing_reg(l5, rdi)
call child
.L:
return_reg(i8, rax)
return_int(i8)
ends_in_dead
.L:
end
function optimized child([l1 : parent_frame], i2 : int) : int
incoming_reg(i2, rsi)
incoming_reg(l1, rdi)
.L:
l5 = retrieve_frame_pointer
outgoing_reg(i2, rsi)
outgoing_reg(l5, rdi)
call child
.L:
return_reg(i8, rax)
return_int(i8)
ends_in_dead
.L:
end
function optimized child2([l1 : parent_frame], i2 : int) : int
incoming_reg(i2, rsi)
incoming_reg(l1, rdi)
.L:
l5 = import(l1, {1}l1)
l7 = load_relative_long(l5, 0)
l8 = import(l7, {2}i3)
i9 = load_relative_int(l8, 0)
return_int(i9)
ends_in_dead
.L:
end
function func(int) : int
/tmp/libjit-dump.o: file format elf64-x86-64
Disassembly of section .text:
00007fa43c378425 <.text>:
7fa43c378425: 55 push %rbp
7fa43c378426: 48 8b ec mov %rsp,%rbp
7fa43c378429: 48 83 ec 10 sub $0x10,%rsp
7fa43c37842d: b8 2a 00 00 00 mov $0x2a,%eax
7fa43c378432: 48 8b cd mov %rbp,%rcx
7fa43c378435: 8b f0 mov %eax,%esi
7fa43c378437: 48 8b f9 mov %rcx,%rdi
7fa43c37843a: 89 45 f8 mov %eax,-0x8(%rbp)
7fa43c37843d: 48 89 4d f0 mov %rcx,-0x10(%rbp)
7fa43c378441: b8 08 00 00 00 mov $0x8,%eax
7fa43c378446: e8 77 ff ff ff callq 0x7fa43c3783c2
7fa43c37844b: 48 8b e5 mov %rbp,%rsp
7fa43c37844e: 5d pop %rbp
7fa43c37844f: c3 retq
end
function child(int) : int
/tmp/libjit-dump.o: file format elf64-x86-64
Disassembly of section .text:
00007fa43c3783c2 <.text>:
7fa43c3783c2: 55 push %rbp
7fa43c3783c3: 48 8b ec mov %rsp,%rbp
7fa43c3783c6: 48 83 ec 20 sub $0x20,%rsp
7fa43c3783ca: 89 75 f0 mov %esi,-0x10(%rbp)
7fa43c3783cd: 48 89 7d f8 mov %rdi,-0x8(%rbp)
7fa43c3783d1: 48 8b c5 mov %rbp,%rax
7fa43c3783d4: 8b 75 f0 mov -0x10(%rbp),%esi
7fa43c3783d7: 48 8b f8 mov %rax,%rdi
7fa43c3783da: 48 89 45 e8 mov %rax,-0x18(%rbp)
7fa43c3783de: b8 08 00 00 00 mov $0x8,%eax
7fa43c3783e3: e8 80 ff ff ff callq 0x7fa43c378368
7fa43c3783e8: 48 8b e5 mov %rbp,%rsp
7fa43c3783eb: 5d pop %rbp
7fa43c3783ec: c3 retq
end
function child2(int) : int
/tmp/libjit-dump.o: file format elf64-x86-64
Disassembly of section .text:
00007fa43c378368 <.text>:
7fa43c378368: 55 push %rbp
7fa43c378369: 48 8b ec mov %rsp,%rbp
7fa43c37836c: 48 83 ec 10 sub $0x10,%rsp
7fa43c378370: 48 89 7d f8 mov %rdi,-0x8(%rbp)
7fa43c378374: 48 8b 45 f8 mov -0x8(%rbp),%rax
7fa43c378378: 48 83 c0 f8 add $0xfffffffffffffff8,%rax
7fa43c37837c: 48 8b 00 mov (%rax),%rax
7fa43c37837f: 48 83 c0 f8 add $0xfffffffffffffff8,%rax
7fa43c378383: 8b 00 mov (%rax),%eax
7fa43c378385: 48 8b e5 mov %rbp,%rsp
7fa43c378388: 5d pop %rbp
7fa43c378389: c3 retq
end
func() = 42
#include <stdint.h>
#include <jit/jit.h>
#include <jit/jit-dump.h>
#define const(t, v) jit_value_create_nint_constant(func, jit_type_##t, v)
#define constl(t, v) jit_value_create_long_constant(func, jit_type_##t, v)
int main()
{
jit_init();
jit_context_t ctx = jit_context_create();
jit_context_build_start(ctx);
jit_type_t signature = jit_type_create_signature(jit_abi_cdecl, jit_type_int,
&jit_type_int, 1, 0);
jit_function_t func = jit_function_create(ctx, signature);
jit_function_t child = jit_function_create_nested(ctx, signature, func);
jit_function_t child2 = jit_function_create_nested(ctx, signature, child);
jit_value_t arg = jit_value_get_param(func, 0);
jit_value_t x = jit_value_create(func, jit_type_int);
jit_value_t ret;
// child2
ret = jit_insn_import(child2, x);
ret = jit_insn_load_relative(child2, ret, 0, jit_type_int);
jit_insn_return(child2, ret);
// child
ret = jit_value_get_param(child, 0);
ret = jit_insn_call(child, "child", child2, 0, &ret, 1, 0);
jit_insn_return(child, ret);
// func
jit_insn_store(func, x, const(int, 42));
ret = jit_insn_call(func, "child", child, 0, &x, 1, 0);
jit_insn_return(func, ret);
jit_optimize(child2);
jit_optimize(child);
jit_optimize(func);
jit_dump_function(stdout, func, "optimized func");
jit_dump_function(stdout, child, "optimized child");
jit_dump_function(stdout, child2, "optimized child2");
jit_function_compile(child2);
jit_function_compile(child);
jit_function_compile(func);
jit_context_build_end(ctx);
jit_dump_function(stdout, func, "func");
jit_dump_function(stdout, child, "child");
jit_dump_function(stdout, child2, "child2");
jit_int result;
jit_int applyArg = 1337;
void *applyArgPtr = &applyArg;
jit_function_apply(func, &applyArgPtr, &result);
printf("func() = %d\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment