Page 1 of 2

embedding C code

Posted: Tue Jun 06, 2023 10:50 pm
by jassing
With the C backend, is it possible to embed C code like you can with ASM?

Re: embedding C code

Posted: Wed Jun 07, 2023 1:51 am
by Demivec
jassing wrote: Tue Jun 06, 2023 10:50 pm With the C backend, is it possible to embed C code like you can with ASM?
Yes.

Re: embedding C code

Posted: Wed Jun 07, 2023 2:03 am
by jassing
OK, how do I pass a variable between PB and C?

Re: embedding C code

Posted: Wed Jun 07, 2023 2:15 am
by Demivec
jassing wrote: Wed Jun 07, 2023 2:03 am OK, how do I pass a variable between PB and C?
The same as described in the help file under 'Inline x86 ASM'.

https://www.purebasic.com/documentation/reference/inlinedasm.html

Here's an excerpt:

Code: Select all

- The used variables or pointers must be declared before to use them in an assembler keyword. Their names in assembler are 'v_variablename' and 'p_pointername', and in a procedure their names are 'p.v_variablename' and 'p.p_pointername'.
- Labels: The labels need to be referenced in lowercase when using the in-line ASM. When you reference a label, you must put the prefix 'l_' before the name.
If the label is defined in a procedure, then its prefix is 'll_procedurename_', in lowercase.
When you reference a module item, you must put the prefix 'module_name.l_' all in lowercase before the item.
If the label is defined in a procedure inside a module, then its prefix is 'module_name.ll_procedurename_', in lowercase.

Re: embedding C code

Posted: Wed Jun 07, 2023 2:34 am
by jassing
Demivec wrote: Wed Jun 07, 2023 2:15 am The same as described in the help file under 'Inline x86 ASM'.
well that explains why I couldn't find it - I was searching for "C" in help, not asm.

thanks.

Re: embedding C code

Posted: Wed Jun 07, 2023 7:16 am
by idle
inline c is useful but it's limited depending upon what your trying to do

take a look at the Raylib4 example in pbcex for instance, it facilitates you to use the library without importing the whole lib

viewtopic.php?t=79332

if you try to embed any c that has #include in it, pb adds it to the main {} which will lead to errors

Re: embedding C code

Posted: Wed Jun 07, 2023 8:16 am
by mk-soft
Global vars was change from v_xxx to g_xxx

Code: Select all

Global iVal.i
Define lVal.l

!g_ival = 100;
!v_lval = 200;

Debug iVal
Debug lVal

Procedure foo(p1, p2)
  Protected r1
  ;r1 = p1 + p2
  !v_r1 = v_p1 + v_p2;
  ProcedureReturn r1
EndProcedure

iVal = foo(1, 2)
Debug iVal
Best way is is show "--commented" c-code

Re: embedding C code

Posted: Wed Jun 07, 2023 11:01 am
by idle
mk-soft wrote: Wed Jun 07, 2023 8:16 am Global vars was change from v_xxx to g_xxx

Code: Select all

Global iVal.i
Define lVal.l

!g_ival = 100;
!v_lval = 200;

Debug iVal
Debug lVal

Procedure foo(p1, p2)
  Protected r1
  ;r1 = p1 + p2
  !v_r1 = v_p1 + v_p2;
  ProcedureReturn r1
EndProcedure

iVal = foo(1, 2)
Debug iVal
Best way is is show "--commented" c-code
that will make things a bit harder, I was quite happily abusing it to get unsigned types and now it probably won't work.

Re: embedding C code

Posted: Wed Jun 07, 2023 3:06 pm
by mk-soft
Type conversion from signed to unsigned is also possible.

Code: Select all

Global iVal.i
Procedure foo(p1.l, p2.l)
  Protected r1
  ;r1 = p1 + p2
  !v_r1 = (unsigned long)((unsigned long)v_p1 + (unsigned long)v_p2);
  ProcedureReturn r1
EndProcedure

iVal = foo(1, -2)
Debug iVal
Or misunderstood what you mean

Re: embedding C code

Posted: Wed Jun 07, 2023 8:51 pm
by idle
That will work to, I was redefining a variable so it became unsigned and I haven't checked to see if it still works since the bug fix was done. 6.02

Re: embedding C code

Posted: Wed Jun 07, 2023 9:55 pm
by jassing
or just simply...

Code: Select all

t=-3 
! v_t = (unsigned int) v_t;
seems to work nicely...

Re: embedding C code

Posted: Wed Jun 07, 2023 11:14 pm
by idle
it still works :D

Code: Select all

Global a.l 
Global b.l 
!unsigned long g_a; 
a = -1 
b = -1 

Debug a 
Debug b 

Re: embedding C code

Posted: Wed Jun 07, 2023 11:36 pm
by jassing
Even simpler! how the obvious escapes us...

Of course,you could keep it one line to make it a little more 'together'

Code: Select all

Global a.l  : !unsigned long g_a; 

Re: embedding C code

Posted: Thu Jun 08, 2023 1:06 am
by jassing
How do you access global variables declared inside a module?

This works as expected.

Code: Select all

Global myval1=4096
DeclareModule abc
  Declare test()
EndDeclareModule
Module abc
  Procedure test()
    ! int test1 = g_myval1;
  EndProcedure
EndModule
abc::test()
But I can't access a global var in a module...

Code: Select all

DeclareModule xyz
  Declare test()
EndDeclareModule
Module xyz
  Global myval2=1024
  Procedure test()
    ! int test2 = g_myval2; 
  EndProcedure
EndModule
xyz::test()
Somewhat related, this shouldn't work, right?

Code: Select all

Global myval1 = 2048
DeclareModule xyz
  Declare test()
EndDeclareModule
Module xyz
  Procedure test()
    Protected size
    !v_size = g_myval1;
    Debug size
  EndProcedure
EndModule
xyz::test()
Or this:

Code: Select all

Define myval1 = 2048
DeclareModule xyz
  Declare test()
EndDeclareModule
Module xyz
  Procedure test()
    Protected size
    !v_size = v_myval1;
    Debug size
  EndProcedure
EndModule
xyz::test()

Re: embedding C code

Posted: Thu Jun 08, 2023 3:35 am
by jassing
Can you call a function defined in C directly from PB?
or
can you call a PB procedure from w/in C? (Declare with procedurec)?