embedded c, possible for functions?

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

embedded c, possible for functions?

Post by jassing »

is it possible to get functions defined in C to work in a procedure? (w/o declaring it IN the procedure?)
Or just a limitation of PB's embedded C?

this works:

Code: Select all

! int testC( int i ) {
!   return(++i);
! }
define v
!v_v = testC(3);
This doesn't

Code: Select all

! int testC( int i ) {
!   return(++i);
! }
Procedure test()
  Define v 
  !v_v = testC( 3 ); 
EndProcedure
test()
Last edited by jassing on Fri Jun 09, 2023 10:12 pm, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedded c, possible for functions?

Post by idle »

I cant see what the problem is from my phone.

maybe you need to define it. inlineC is in the main function scope.

It why I wrote my tool so I could use c libs directly but that's a slightly different use case. It doesn't compile it it just preprocess it so you have access to exported functions and symbols. Then you can call the c functions directly but they are functions from a lib.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedded c, possible for functions?

Post by jassing »

idle wrote: Fri Jun 09, 2023 9:40 pm I cant see what the problem is from my phone.

maybe you need to define it. inlineC is in the main function scope.
Hmm. that's the reason, then. You can't define a function w/in a function...
so if it's something like

Code: Select all

void inlineC() {
  void myfunc() {
    return 42;
  }
}
So that explains that. Would be a nice improvement to have.

FWIW, I did try

Code: Select all

! int testC( int i ) {
!   return(++i);
! }
Procedure test()
  Define v 
  ! int testC( int i );
  !v_v = testC( 3 ); 
EndProcedure
test()
in case the function was put at the end of the code... same out come, the compiler generated an error.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedded c, possible for functions?

Post by idle »

Well you kind of can as long as it's in the same scope but it's confusing look at fast hash function in my bloom filter

I will look at this when I get time later today.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: embedded c, possible for functions?

Post by jassing »

idle wrote: Fri Jun 09, 2023 10:20 pm Well you kind of can as long as it's in the same scope but it's confusing look at fast hash function in my bloom filter
Right, i discovered that.
idle wrote: Fri Jun 09, 2023 10:20 pm I will look at this when I get time later today.
No worries. Not critical; just seeing what I can and can't do with it. (Like you can exploit it to access variables outside of a module)
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: embedded c, possible for functions?

Post by idle »

The issue is simply that PB puts all inline c into the main functions scope or the scope of the current function
If Fred could just move !#include !#define #param() out of main scope we could do a lot more without needing to add hacks to get around the limitations.

Just so others reading the thread get it, you can declare and call a c function within the scope of a procedure but not externally, if that makes sense.

Code: Select all

Procedure test(x)
  Protected v 
  
  !int testC( int i ) {
  !   return(++ i);
  ! };
    
  !v_v = testC(v_x);
  
  ProcedureReturn v 
  
EndProcedure

Debug test(3)

Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: embedded c, possible for functions?

Post by Olli »

aha... I never made a function on X86/X64 backend.

But I maybe imagine an solve by using of public and/or extern C compiler directives.
Post Reply