Page 1 of 1

C Backend documentation?

Posted: Thu Feb 16, 2023 12:22 pm
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)

Re: C Backend documentation?

Posted: Thu Feb 16, 2023 1:40 pm
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()

Re: C Backend documentation?

Posted: Thu Feb 16, 2023 1:57 pm
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"

Re: C Backend documentation?

Posted: Thu Feb 16, 2023 4:22 pm
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, ...

Re: C Backend documentation?

Posted: Thu Feb 16, 2023 5:37 pm
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.

Re: C Backend documentation?

Posted: Fri Jul 19, 2024 10:40 am
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();
; }
; 



Re: C Backend documentation?

Posted: Fri Jul 19, 2024 1:09 pm
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()

Re: C Backend documentation?

Posted: Fri Jul 19, 2024 4:25 pm
by acreis
Thanks User_Russian!

Works here!