Tasks of this project were:
- To make a binary translator from my language into x86 architecture.
- To compare run-time between JIT-compiler (binary translator) and my own proceccor
all:
gcc -g -masm=intel main.cpp func.cpp std_func.cpp -DNDEBUG -no-pie -lm -o main
- Processor
AMD Ryzen 5 4500U
- Language
C, C++
- Profiler
Kcachegrind (WSL)
- Compiler
GCC
- Timer
time ./program (WSL) total instruction fetch cost (kcachegrind)
To look into my language commands visit https://github.com/Ropho/CPU
asm/cpu cmd | x86 cmd | x86 bin code |
---|---|---|
end | 0xC3 | |
in | in | |
out | out | |
sqrt | sqrt | |
push 5 (number) | push + num (4 bytes) | 0x68 + num |
push ax | push rax | 0x50 |
push bx | push rbx | 0x53 |
push cx | push rcx | 0x51 |
push dx | push rdx | 0x52 |
pop ax | pop rax | 0x58 |
pop bx | pop rbx | 0x5B |
pop cx | pop rcx | 0x59 |
pop dx | pop rdx | 0x5A |
add | pop rdi | 0x57 |
pop rsi | 0x56 | |
add rdi, rsi | 0x48 | |
0x01 | ||
0xF7 | ||
sub | pop rdi | 0x57 |
pop rsi | 0x56 | |
sub rdi, rsi | 0x48 | |
0x29 | ||
0xF7 | ||
mul | pop rdx | 0x5A |
pop rax | 0x58 | |
imul | 0x48 | |
0xF7 | ||
0xEA | ||
del | pop rcx | 0x59 |
pop rax | 0x58 | |
cqo | 0x48 | |
0x99 | ||
idiv | 0x48 | |
0xF7 | ||
0xF9 | ||
call | call + addr (4 bytes) | 0xE8 + addr |
ret | 0xC3 | |
jump | jmp + addr (4 bytes) | 0xEA + addr |
jump (condition) | pop rsi | 0x56 |
pop rdi | 0x57 | |
cmp rdi, rsi | 0x48 | |
0x39 | ||
0xF7 | ||
je + addr (4 bytes) | 0x74 | |
jne + addr (4 bytes) | 0x75 | |
jl + addr (4 bytes) | 0x7C | |
jle + addr (4 bytes) | 0x7E | |
jg + addr (4 bytes) | 0x7F | |
jge + addr (4 bytes) | 0x7D |
++ip_bin;
/////////////////////////////////////////////////
SAVE_RAX;
SAVE_RBX;
SAVE_RCX;
SAVE_RDX;
/////////////////////////////////////////////////
//! mov r10, addr
trans->buff[ip++] = 0x41;
trans->buff[ip++] = 0xBA;
unsigned long ptr = (unsigned long) in;
*((int *)(trans->buff + ip)) = *((int *)&ptr);
ip += sizeof (int) / sizeof (char);
SAVE_RSP;
ALIGN_STACK;
CALL_R10;
RESTORE_RSP;
PUSH_RAX;
/////////////////////////////////////////////////
RESTORE_RAX;
RESTORE_RBX;
RESTORE_RCX;
RESTORE_RDX;
int in (void) {
int rax = 0;
scanf ("%d", &rax);
return rax;
}
++ip_bin;
POP_RDI;
//! mov r10, addr
trans->buff[ip++] = 0x41;
trans->buff[ip++] = 0xBA;
unsigned long ptr = (unsigned long)out;
*((int *)(trans->buff + ip)) = *((int *)&ptr);
ip += sizeof (int) / sizeof (char);
/////////////////////////////////////////////////
SAVE_RAX;
SAVE_RBX;
SAVE_RCX;
SAVE_RDX;
/////////////////////////////////////////////////
CALL_R10;
/////////////////////////////////////////////////
RESTORE_RAX;
RESTORE_RBX;
RESTORE_RCX;
RESTORE_RDX;
void out (long long input) {
printf ("%lld\n", input);
return;
}
++ip_bin;
POP_RDI;
trans->buff[ip++] = 0x41; //mov r10, out(adress)
trans->buff[ip++] = 0xBA;
unsigned long ptr = (unsigned long)r_sqrt;
*((int *)(trans->buff + ip)) = *((int *)&ptr);
ip += sizeof (int) / sizeof (char);
/////////////////////////////////////////////////
SAVE_RAX;
SAVE_RBX;
SAVE_RCX;
SAVE_RDX;
/////////////////////////////////////////////////
CALL_R10;
/////////////////////////////////////////////////
PUSH_RAX;
RESTORE_RAX;
RESTORE_RBX;
RESTORE_RCX;
RESTORE_RDX;
int r_sqrt (int input) {
int output = round (sqrt (input));
return output;
}
CPU (own) | JIT | |
---|---|---|
real (secs) | 0.0065 | 0.00008 |
Total Increase: 82 times.