Sneak Peek into Generated C Code Blog

Everything else that doesn't fall into one of the other PB categories.
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Sneak Peek into Generated C Code Blog

Post 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;
}
Simon White
dCipher Computing
gekkonier
User
User
Posts: 78
Joined: Mon Apr 23, 2007 9:42 am

Re: Sneak Peek into Generated C Code Blog

Post 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.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Sneak Peek into Generated C Code Blog

Post 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."
:wink:
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Sneak Peek into Generated C Code Blog

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Sneak Peek into Generated C Code Blog

Post by skywalk »

Is there any advantage using VC++ vs C compilation?
Are you reserving the VC++ compiler for future C++ external lib support?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Sneak Peek into Generated C Code Blog

Post by swhite »

Thanks Fred for the explanation.

Simon
Simon White
dCipher Computing
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Sneak Peek into Generated C Code Blog

Post 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 :D
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Sneak Peek into Generated C Code Blog

Post by Keya »

Fred which C compiler will you be using for Windows? thankyou! :)
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Sneak Peek into Generated C Code Blog

Post 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?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply