/* Tutorial 6 - (x + z) * 2 + y with internal call to SSE Builds and compiles the following function: int add_add(int x, int y, int z) { return (x + z) * 2 + y; } */ #include #include #include int main(int argc, char **argv) { jit_context_t context; jit_type_t params[3], params_2[3]; jit_type_t signature, signature2; jit_function_t function, function2; jit_value_t x, y, z; jit_value_t temp1; jit_int arg1, arg2, arg3; jit_value_t args[3]; jit_long result; /* Create a context to hold the JIT's primary state */ context = jit_context_create(); /* Lock the context while we build and compile the function */ jit_context_build_start(context); /* Build the function signature */ params[0] = jit_type_int; params[1] = jit_type_int; params[2] = jit_type_int; params_2[0] = jit_type_long; params_2[1] = jit_type_long; signature = jit_type_create_signature (jit_abi_cdecl, jit_type_long, params, 3, 1); signature2 = jit_type_create_signature (jit_abi_internal, jit_type_long, params_2, 2, 1); /* Create the function object */ function = jit_function_create(context, signature); function2 = jit_function_create(context, signature2); /* Construct the function body */ x = jit_value_get_param(function, 0); y = jit_value_get_param(function, 1); z = jit_value_get_param(function, 2); args[0] = jit_insn_convert(function, x, jit_type_long, 0); args[1] = jit_insn_convert(function, z, jit_type_long, 0); temp1 = jit_insn_call(function, "func", function2, signature2, args, 2, 0); jit_value_t temp2 = jit_insn_add(function, temp1, y); jit_insn_return(function, temp2); /* Construct the function2 body */ x = jit_value_get_param(function2, 0); y = jit_value_get_param(function2, 1); jit_value_t value3 = jit_insn_add(function2, x, y); jit_value_t value4 = jit_insn_add(function2, value3, value3); jit_insn_return(function2, value4); jit_dump_function(stdout, function, "main"); jit_dump_function(stdout, function2, "func"); /* Compile the function */ jit_function_compile(function2); jit_function_compile(function); jit_dump_function(stdout, function, "main"); jit_dump_function(stdout, function2, "func"); /* Unlock the context */ jit_context_build_end(context); /* Execute the function and print the result */ arg1 = 1; arg2 = 10; arg3 = 3; args[0] = &arg1; args[1] = &arg2; args[2] = &arg3; jit_function_apply(function, args, &result); printf("\nresult = %lld of f(%d, %d, %d)\n", result, arg1, arg2, arg3); /* Clean up */ jit_context_destroy(context); /* Finished */ return 0; } sh-2.05b$ ./t6 cdecl function main(i1 : int, i2 : int, i3 : int) : long incoming_frame_posn(i1, 8) incoming_frame_posn(i2, 12) incoming_frame_posn(i3, 16) l7 = expand_int(i1) l8 = expand_int(i3) .L0: push_long(l7) push_long(l8) long l9 = func(long l7, long l8) return_reg(l9, eax) l11 = expand_int(i2) l12 = l9 + l11 return_long(l12) ends_in_dead end internal function func(l1 : long, l2 : long) : long incoming_frame_posn(l1, 8) incoming_frame_posn(l2, 16) l5 = l1 + l2 l6 = l5 + l5 return_long(l6) ends_in_dead end cdecl function main(int, int, int) : long /tmp/libjit-dump.o: file format elf32-i386 Disassembly of section .text: 40219059 <.text>: 40219059: 55 push %ebp 4021905a: 8b ec mov %esp,%ebp 4021905c: 83 ec 10 sub $0x10,%esp 4021905f: 53 push %ebx 40219060: 8b 45 08 mov 0x8(%ebp),%eax 40219063: 8b d0 mov %eax,%edx 40219065: c1 fa 1f sar $0x1f,%edx 40219068: 89 45 f8 mov %eax,0xfffffff8(%ebp) 4021906b: 89 55 fc mov %edx,0xfffffffc(%ebp) 4021906e: 8b 45 10 mov 0x10(%ebp),%eax 40219071: 8b d0 mov %eax,%edx 40219073: c1 fa 1f sar $0x1f,%edx 40219076: 89 45 f0 mov %eax,0xfffffff0(%ebp) 40219079: 89 55 f4 mov %edx,0xfffffff4(%ebp) 4021907c: ff 75 fc pushl 0xfffffffc(%ebp) 4021907f: ff 75 f8 pushl 0xfffffff8(%ebp) 40219082: ff 75 f4 pushl 0xfffffff4(%ebp) 40219085: ff 75 f0 pushl 0xfffffff0(%ebp) 40219088: f3 0f 7e 0c 24 movq (%esp),%xmm1 4021908d: f3 0f 7e 44 24 08 movq 0x8(%esp),%xmm0 40219093: e8 85 ff ff ff call 0x4021901d 40219098: 83 c4 10 add $0x10,%esp 4021909b: 66 0f 7e c0 movd %xmm0,%eax 4021909f: 66 0f 70 c0 01 pshufd $0x1,%xmm0,%xmm0 402190a4: 66 0f 7e c2 movd %xmm0,%edx 402190a8: 8b 4d 0c mov 0xc(%ebp),%ecx 402190ab: 8b d9 mov %ecx,%ebx 402190ad: c1 fb 1f sar $0x1f,%ebx 402190b0: 03 c1 add %ecx,%eax 402190b2: 13 d3 adc %ebx,%edx 402190b4: 8b 5d ec mov 0xffffffec(%ebp),%ebx 402190b7: 8b e5 mov %ebp,%esp 402190b9: 5d pop %ebp 402190ba: c3 ret end internal function func(long, long) : long /tmp/libjit-dump.o: file format elf32-i386 Disassembly of section .text: 4021901d <.text>: 4021901d: 55 push %ebp 4021901e: 8b ec mov %esp,%ebp 40219020: 66 0f d4 c1 paddq %xmm1,%xmm0 40219024: 66 0f d4 c0 paddq %xmm0,%xmm0 40219028: 5d pop %ebp 40219029: c3 ret end result = 18 of f(1, 10, 3) sh-2.05b$