Page 1 of 1
Sneak Peek into Generated C Code Blog
Posted: Fri Apr 30, 2021 2:05 pm
by swhite
Hi
Perhaps I am missing something by why does the code include "goto end;" and "end:"? Could it not just say "return r;" or "return 6"?
Thanks,
Simon
Code: Select all
static unsigned short _S1[]={72,101,108,108,111,32,119,111,114,108,100,0};
static unsigned short _S2[]={72,101,108,108,111,0};
static integer f_test()
{
integer r=0;
integer v_counter=0;
v_counter=(v_counter+5);
if (v_counter==5)
{
integer rr0=PB_MessageRequester(_S1,_S2);
}
r=6;
goto end;
end:
return r;
}
Re: Sneak Peek into Generated C Code Blog
Posted: Fri Apr 30, 2021 2:18 pm
by gekkonier
There was a talk about it in german forum too:
https://www.purebasic.fr/german/viewtop ... =1&t=32375
My conclusion:
Imagine that you can return from a procedure at multiple positions (that kinda sounds strange, but has nothing arousing).
In pb->asm you can do that with that jump marks.:
Code: Select all
Procedure.i gonzo()
If 3 > 2
ProcedureReturn 5
Else
ProcedureReturn 6
EndIf
EndProcedure
gonzo()
=
Code: Select all
; Procedure.i gonzo()
_Procedure0:
PUSH r15
PS0=64
XOR rax,rax
PUSH rax
SUB rsp,40
; If 3 > 2
; ProcedureReturn 5
MOV rax,5
JMP _EndProcedure1
; Else
JMP _EndIf1
_EndIf2:
; ProcedureReturn 6
MOV rax,6
JMP _EndProcedure1
; EndIf
_EndIf1:
; EndProcedure
_EndProcedureZero1:
XOR rax,rax
_EndProcedure1:
ADD rsp,48
POP r15
RET
now you have
pb->c: althought you wouldn't need it the overall structure looks kinda same.
I guess it's a historical thing.
Re: Sneak Peek into Generated C Code Blog
Posted: Fri Apr 30, 2021 2:21 pm
by Marc56us
swhite wrote: Fri Apr 30, 2021 2:05 pm
Perhaps I am missing something by why does the code include "goto end;" and "end:"? Could it not just say "return r;" or "return 6"?
Fred has replied
viewtopic.php?p=569115#p569115
"
goto end: is always generated when using ProcedureReturn, even if there is no code in between (last statement of the procedure). It is suppressed by the C optimizer, don't worry. The debugger works the same, but some functions/check are not yet supported."

Re: Sneak Peek into Generated C Code Blog
Posted: Fri Apr 30, 2021 4:46 pm
by Fred
The idea is to let the C optimizer do its job and don't add too much optimization at C code generation level. For example the above procedure is translated like this by VC++ with /02 flag:
Code: Select all
f_test PROC ; COMDAT
; File c:\users\fred\appdata\local\temp\purebasic29892937\purebasic.c
; Line 80
sub rsp, 40 ; 00000028H
; Line 89
lea rdx, OFFSET FLAT:_S2
lea rcx, OFFSET FLAT:_S1
call PB_MessageRequester
; Line 98
mov eax, 6
$end$5:
; Line 99
add rsp, 40 ; 00000028H
ret 0
f_test ENDP
As you can see, it removes the 'If' because its always true, remove the counter variable because its not really needed, remove the useless goto etc.
Re: Sneak Peek into Generated C Code Blog
Posted: Fri Apr 30, 2021 5:50 pm
by skywalk
Is there any advantage using VC++ vs C compilation?
Are you reserving the VC++ compiler for future C++ external lib support?
Re: Sneak Peek into Generated C Code Blog
Posted: Sat May 01, 2021 1:59 pm
by swhite
Thanks Fred for the explanation.
Simon
Re: Sneak Peek into Generated C Code Blog
Posted: Sun May 02, 2021 5:11 pm
by Fred
skywalk wrote: Fri Apr 30, 2021 5:50 pm
Is there any advantage using VC++ vs C compilation?
Are you reserving the VC++ compiler for future C++ external lib support?
Microsoft doesn't ship a pure C compiler anymore, that's why

Re: Sneak Peek into Generated C Code Blog
Posted: Sun May 02, 2021 5:55 pm
by Keya
Fred which C compiler will you be using for Windows? thankyou!

Re: Sneak Peek into Generated C Code Blog
Posted: Thu May 06, 2021 11:07 pm
by skywalk
Microsoft Windows C compiler via command prompt.
This is the same approach I use to compile the latest SQLite lib.
It will be interesting to see the header PB v6 is creating in the C emit, since there are C concepts that do not play in C++ and vice versa.
How are you making variable length arrays and structure unions and default parameters in Functions work?