Better code generation with PureBasic Windows x64
Posted: Wed Oct 31, 2012 10:23 am
PB 4.61 x64 generated code:
PureBasic inserts "MOV rax, rax" after every "load variable" instruction, if the variable is smaller than 64bit...
...and after every function call in expressions. Is this necessary everywhere in the code?
Also, it always pushes ALL parameters onto stack and then it POPs the first 4 arguments into its registers:
Couldn't this be changed/improved a little bit, so the first 4 arguments go directly into the registers RCX, RDX, R8 and R9?
This is done on every WinAPI call and adds many unnecessary instructions and code bloat.
Code: Select all
; MoveWindow_(WindowID(0),x,y,WindowWidth(0),WindowHeight(0),#True)
PUSH qword 1
SUB rsp,8
PUSH qword 0
POP rcx
SUB rsp,32
CALL PB_WindowHeight
ADD rsp,40
MOV rax,rax ; MOV rax,rax - after function call
PUSH rax
PUSH qword 0
POP rcx
SUB rsp,32
CALL PB_WindowWidth
ADD rsp,32
MOV rax,rax ; MOV rax,rax - after function call
PUSH rax
MOVSX rax,byte [v_y]
MOV rax,rax ; MOV rax,rax - after variable loading
PUSH rax
MOVSX rax,byte [v_x]
MOV rax,rax ; MOV rax,rax - after variable loading
PUSH rax
SUB rsp,8
PUSH qword 0
POP rcx
SUB rsp,32
CALL PB_WindowID
ADD rsp,40
MOV rax,rax ; MOV rax,rax - after function call
PUSH rax
POP rcx
POP rdx
POP r8
POP r9
SUB rsp,32
CALL MoveWindow
ADD rsp,48
;
; freq_l.l = 800 : duration_l.l = 200
MOV dword [v_freq_l],800
MOV dword [v_duration_l],200
; Beep_(freq_l, duration_l)
MOVSXD rax,dword [v_duration_l]
MOV rax,rax ; MOV rax, rax ??
PUSH rax
MOVSXD rax,dword [v_freq_l]
MOV rax,rax ; MOV rax, rax ??
PUSH rax
POP rcx
POP rdx
CALL Beep
;
; freq_i.i = 800 : duration_i.i = 200
MOV qword [v_freq_i],800
MOV qword [v_duration_i],200
; Beep_(freq_i, duration_i)
PUSH qword [v_duration_i]
PUSH qword [v_freq_i]
POP rcx
POP rdx
CALL Beep
...and after every function call in expressions. Is this necessary everywhere in the code?
Also, it always pushes ALL parameters onto stack and then it POPs the first 4 arguments into its registers:
Code: Select all
; Beep_(freq_i, duration_i)
PUSH qword [v_duration_i]
PUSH qword [v_freq_i]
POP rcx
POP rdx
CALL Beep
Code: Select all
MOV qword rdx, [v_duration_i]
MOV qword rcx, [v_freq_i]
CALL Beep