C Backend documentation?

Everything else that doesn't fall into one of the other PB categories.
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

C Backend documentation?

Post by Joubarbe »

Hey,

So I just realized that the C backend compiler was not the one by default, and that all my "tests" were actually done with ASM. I've now added the C compiler as default, but I cannot find anything about it in the documentation. There is a section about inline ASM, I think there should be one for C as well. And something about the C compiler at all, as the only was to know about that is to read the 6.0 changelog thread. Newcomers would probably completely miss that feature that took more than 1 year to develop; a bit of a shame no?

I'm trying to find something about how the C inline is implemented; like with SpiderBasic you have to add an underscore or a v_ for variables, etc. How does it work in PB?

Code: Select all

Procedure foo()
  Debug "Hello world"
EndProcedure

OpenConsole()

! void dbg(void) {
!   puts("oihjoiJ");
!   printf("oihjoiJ");
!   foo();
! }

! dbg();

Input()
I would like to have documentation to make this code works, for instance. (how can I access stdout, how can I call my own procedures, how can I use PB variables,etc)
User_Russian
Addict
Addict
Posts: 1582
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: C Backend documentation?

Post by User_Russian »

Code: Select all

Procedure foo()
  Debug "Hello world"
EndProcedure

OpenConsole()

! void dbg(void) {
!   puts("oihjoiJ");
!   printf("oihjoiJ");
!   f_foo();
! }

! dbg();

Input()
End
foo()
AZJIO
Addict
Addict
Posts: 2217
Joined: Sun May 14, 2017 1:48 am

Re: C Backend documentation?

Post by AZJIO »

Export your code to C and you will see how the variables are written.

Code: Select all

Compilers\pbcompilerc.exe -q /COMMENTED "File.pb"
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: C Backend documentation?

Post by juergenkulow »

Code: Select all

int main(int argc, char* argv[]) {
PB_ArgC = argc;
PB_ArgV = argv;
SYS_InitPureBasic();
SYS_InitString();
*(integer *)(&PB_DEBUGGER_ProcedureBank[14])=(integer)&f_foo;
*(integer *)(&PB_DEBUGGER_ProcedureBank[22])=(integer)&lmv_0;
*(integer *)(&PB_DEBUGGER_ProcedureBank[30])=(integer)&lma_0;
*(integer *)(&PB_DEBUGGER_ProcedureBank[38])=(integer)&lml_0;
*(integer *)(&PB_DEBUGGER_ProcedureBank[46])=(integer)&lmm_0;
PB_DEBUGGER_Start(0, 1);
PB_InitMemory();
PB_InitMap();
PB_InitNetworkInternal();
PB_InitConsole();
PB_InitArray();
tls=SYS_InitThreadedObject();
/// 
// OpenConsole()
DBG_Check(4);
PB_OpenConsole_DEBUG();
integer rr0=PB_OpenConsole();
// 
// ! void dbg(void) {
 void dbg(void) {
// !   puts("oihjoiJ");
   puts("oihjoiJ");
// !   printf("oihjoiJ");
   printf("oihjoiJ");
// !   f_foo();
   f_foo();
// ! }
 }
// 
// ! dbg();
 dbg();
// 
// 
// Input()
DBG_Check(15);
SYS_PushStringBasePosition();
SYS_PushStringBasePosition();
integer p0=SYS_PopStringBasePosition();
PB_Input_DEBUG(p0);
PB_Input(p0);
SYS_PopStringBasePositionUpdate();
// End
DBG_Check(16);
SYS_Quit();
// foo()
DBG_Check(17);
integer rr1=f_foo();
PB_DEBUGGER_SetLine(17);
DBG_Check(18);
SYS_Quit();
}
Fred: there is no doc, ...
Joubarbe
Enthusiast
Enthusiast
Posts: 713
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: C Backend documentation?

Post by Joubarbe »

Thank you very much :)

EDIT: I have an "error Linker: access is denied" message. Anyone knows the cause of this?
EDIT 2: Launching PowerShell with admin rights fixed it.
acreis
Enthusiast
Enthusiast
Posts: 231
Joined: Fri Jun 01, 2012 12:20 am

Re: C Backend documentation?

Post by acreis »

Hi,

Some progress about how to call C function from PB function?

The following example doesn't work.

Error: Linker
error: Unresolved external symbol '_multiplication'.
POLINK: fatal error: 1 unresolved external(s).

Code: Select all


! //test program 
!
! 


! int multiplication(int arg1, int arg2)
!{
! 
!   return (arg1 * arg2 );
! 
!}

Procedure Main()
  
  Protected a, b, c
  
  b = 10
  c = 20
  
  !v_a = multiplication( v_b, v_c);
  
EndProcedure

Main()

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;   RELEVAN PARTS OF C CODE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; // 
; // PureBasic 6.10 LTS - C Backend (Windows - x86) generated code
; // 
; // (c) 2024 Fantaisie Software
; // 
; // 
; // Procedure Main()
; static __stdcall integer f_main() {
; integer r=0;
; integer v_a=0;
; integer v_b=0;
; integer v_c=0;
; // 
; // Protected a, b, c;;;
; // 
; // b = 10
; v_b=10;
; // c = 20
; v_c=20;
; // 
; // !v_a = multiplication( v_b, v_c);
; v_a = multiplication( v_b, v_c);
; // 
; // EndProcedure
; r=0;
; end:
; return r;
; }



;  int multiplication(int arg1, int arg2)

; {

;    return (arg1 * arg2 );


; }

; // 
; // 
; // Main()
; integer r0=f_main();
; // 
; SYS_Quit();
; }
; 


User_Russian
Addict
Addict
Posts: 1582
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: C Backend documentation?

Post by User_Russian »

Code: Select all

DataSection
! int multiplication(int arg1, int arg2)
!{
! 
!   return (arg1 * arg2 );
! 
!}
EndDataSection

Procedure Main()
  
  Protected a, b, c
  
  b = 10
  c = 20
  
  !v_a = multiplication(v_b, v_c);
  Debug a
  
EndProcedure

Main()
acreis
Enthusiast
Enthusiast
Posts: 231
Joined: Fri Jun 01, 2012 12:20 am

Re: C Backend documentation?

Post by acreis »

Thanks User_Russian!

Works here!
Post Reply