Pure FB Runtime Library (in progress)

User projects written in or related to FreeBASIC.
Post Reply
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

TeeEmCee wrote:Wow, you've done a lot of work already! Have you tried running it at all?

I notice a mistake in the translation: you've been translating 'int' to 'integer', but the equivalent of C's 'int' in FB is 'long'.
Also, you need to wrap everything in Extern "C" if you're not doing that already.
I have not tried running anything yet. As I said in the first post, I check to see if it will compile. Since almost every part relies on another part, it is hard to find one part I could test without having the whole thing intact.

As for the integer/long thing, I was originally doing everything that way, but thenfound that we have functions that, in FB, return an integer and in the library code also return a C integer (such as CVI). I went back and changed them all back to Integer.

I did not set Extern C as most function use FBCALL (STDCALL). I have been manually setting CDECL for all functions that are not using STDCALL. Not sure if that is super required here, though since most of the functions that use CDECL are internal functions only used to simplify code. I probably did not need to use FBCALL either, as that is freeBASIC's default, but my current goal is to convert the code as is, and work on making it work/better later.

That being said, if my base assumptions are wrong here, I can make changes. I just wanted to explain my reasoning.
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: Pure FB Runtime Library (in progress)

Post by integer »

Imortis wrote: ...
Since almost every part relies on another part, it is hard to find one part I could test without having the whole thing intact...
...
I just wanted to explain my reasoning.
much appreciated.
Those inter/intra dependencies are a real PITA load of fun.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Pure FB Runtime Library (in progress)

Post by TeeEmCee »

Imortis wrote:I have not tried running anything yet. As I said in the first post, I check to see if it will compile. Since almost every part relies on another part, it is hard to find one part I could test without having the whole thing intact.
There are two approaches you could follow to test your code before you're translated everything:
1st: only translate the parts that are needed to link to minimal testcases
2nd: link against the C versions of all those modules you haven't translated yet. This is pretty easy; you don't need to use FB's makefile to compile the rtlib.

As for dependencies, yes there are plenty from the initialisation code to other parts, but it's actually not that bad: the rtlib is mostly specifically organised to minimise the number of modules that need to linked if they aren't used. So you don't need that much of the rtlib. On my system, compiling an empty .bas file 'print "hello world"' produces a 22.8 kB byte binary (which is stripped). Compiling an empty "int main(){return 0;}" C file is 5.9 kB stripped. libfb.a is 5894 kB (516 kB when stripped), and linking all of libfb.a into the binary using ld's --whole-archive option and then stripping results in 185 kB bytes (...rather odd). So hello-world pulls in < 10% of the rtlib. If you check, you'll find that what gets pulls in is mainly the init code, console stuff like dev_scrn_init.c, core string functions, and locking/unlocking.
As for the integer/long thing, I was originally doing everything that way, but thenfound that we have functions that, in FB, return an integer and in the library code also return a C integer (such as CVI). I went back and changed them all back to Integer.
That's because calls to fb_CVI are never emitted by the compiler. CVI doesn't even exist in the compiler's list of rtlib functions. fb_CVI only exists in the rtlib in case you're linking to object files produced by an ancient version of fbc - it could just be deleted. The comment in front of fb_CVI notes that it's obsolete. So if you keep fb_CVI, it doesn't actually matter whether it returns long or integer, but returning long is more consistent.

Do you have any other examples aside from CVI?
You do have to use long instead of integer.
I did not set Extern C as most function use FBCALL (STDCALL).
...
I probably did not need to use FBCALL either, as that is freeBASIC's default, but my current goal is to convert the code as is, and work on making it work/better later.
FBCALL is only STDCALL on x86 Windows, XBox and cygwin. It's CDECL everywhere else.

You still need EXTERN "C". STDCALL only changes the calling convention, while EXTERN "C" also prevents symbols from being capitalised (equivalent to using ALIAS).

Numerous functions in the rtlib which are called from emitted code, such fb_CHR, aren't FBCALL. I don't know why some are FBCALL and some aren't. But you will have to keep all the FBCALLs. If something isn't FBCALL, it isn't immediately obvious whether it needs to CDECL or it just doesn't matter. But since you'll need EXTERN "C" anyway, there's no need to mark them as CDECL.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Pure FB Runtime Library (in progress)

Post by St_W »

TeeEmCee wrote:Numerous functions in the rtlib which are called from emitted code, such fb_CHR, aren't FBCALL. I don't know why some are FBCALL and some aren't.
I don't know either and haven't looked into the fb_CHR code, but maybe those accept a variable-length argument list.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

TeeEmCee wrote:Do you have any other examples aside from CVI?
Yes:
fb_StrCompare as it's unicode partner fbWStrCompare
fb_VALINT


All the internal functions for converting string to int or wstring to int also return int.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Pure FB Runtime Library (in progress)

Post by TeeEmCee »

The compiler expects all of fb_StrCompare, fb_WstrCompare, fb_VALINT, fb_WstrValInt, etc. to return long, not integer. I meant examples that return int in C but integer in FB (there shouldn't be any). Note that the compiler might promote the result from the rtlib function to an integer in cases like fb_StrCompare where you might assume it returns an integer. VALINT is actually documented as returning a long on the wiki anyway.

Ah, yes, fb_CHR does take a variable number of arguments. I can't immediately find any functions that aren't FBCALL which I positively know are actually called from emitted code.

So anyway, I grabbed your code from git and tried to compile it. You hadn't translated fb_unix.h, so I did that. Then I ran into the problem that FB's Linux headers are far more incomplete than even the Windows POSIX emulation layer headers. So I've been working on translating some of the critical linux/glibc headers, with help from fbbindings (fbfrog).

Bug in fb_wstr_CalcDiff in fb_unicode.bi: intptr_t translates to integer (aka ssize_t), not to ulong.

Also, I don't know how to get around this problem:

Code: Select all

str_chr.bas(15) error 288: Unsupported statement in -gen gcc mode in 'ap = va_first()'
AFAIU FB's va_arg is incompatible with C's va_arg, so it's not actually possible to add support for va_arg to the gcc backend without changing the semantics of FB's va_arg? Not sure about that.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

TeeEmCee wrote:w how to get around this problem:

Code: Select all

str_chr.bas(15) error 288: Unsupported statement in -gen gcc mode in 'ap = va_first()'
AFAIU FB's va_arg is incompatible with C's va_arg, so it's not actually possible to add support for va_arg to the gcc backend without changing the semantics of FB's va_arg? Not sure about that.
Where I ran into that, I replaced it with FB's var_arg stuff. I just duplicated the intent of the code, if not the exact function.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Pure FB Runtime Library (in progress)

Post by TeeEmCee »

Point is that va_arg isn't supported by FB on most platforms.
I don't know if it's possible to add support, but fbc's todo.txt lists, (for the LLVM backend):
- va_*() macros can't be implemented, so must be disallowed just like with -gen gcc
This looks bad. As a kludge we could use libffi (or a small amount of C glue code) to emulate va_arg. Or maybe add c_va_arg, c_va_start, etc functions to the language as an alternative to the existing ones, with C semantics.
Or just not care, since if we're using -gen gcc, we're stuck with C anyway.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

TeeEmCee wrote:Point is that va_arg isn't supported by FB on most platforms.
I don't know if it's possible to add support, but fbc's todo.txt lists, (for the LLVM backend):
- va_*() macros can't be implemented, so must be disallowed just like with -gen gcc
This looks bad. As a kludge we could use libffi (or a small amount of C glue code) to emulate va_arg. Or maybe add c_va_arg, c_va_start, etc functions to the language as an alternative to the existing ones, with C semantics.
Or just not care, since if we're using -gen gcc, we're stuck with C anyway.
Hmm... I did not notice that bit of the docs... For now, I will continue as I have been. If we plan to continue the C backend, then we are going to have to fix that issue at some point anyway.

Extra question:

Code: Select all

FB_ERRORCTX *ctx = FB_TLSGETCTX( ERROR );
What does ERROR here mean? I can't find where it is defined anywhere in the code.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Pure FB Runtime Library (in progress)

Post by St_W »

Imortis wrote:What does ERROR here mean? I can't find where it is defined anywhere in the code.
See the definition of the macro - it is concatenated to a longer identifier (FB_TLSKEY_ERROR and FB_ERRORCTX):

Code: Select all

#define FB_TLSGETCTX(id) ((FB_##id##CTX *)fb_TlsGetCtx( FB_TLSKEY_##id, sizeof( FB_##id##CTX ) ))
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

Duh. I know one thing for sure: I am learning. I have not felt so stupid while working on a programming project in a long time. This means I am learning.

Thanks for pointing it out.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

Added Extern "C"
fixed fb_wstr_CalcDiff
changed all instances of integer to long

Thanks for the suggestions and fixes.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Pure FB Runtime Library (in progress)

Post by TeeEmCee »

Just to double check: were you only using 'integer' where the original C used 'int'? Or were you also using integer in other places? Because, where 'integer' is used on the FB side (eg in tables of rtlib functions in the compiler), the rtlib uses ssize_t. So I guess you could do a replacement of all ssize_t to integer, at least for all the parameters/return values of public functions. It's certainly more idiomatic for FB. Not 100% certain about replacing ssize_t absolutely everywhere... afterall, it is used in some of FB's headers instead of integer. fbfrog does not convert ssize_t to integer.

(Trivia: unlike size_t, ssize_t (signed size_t) isn't a standard C type; it's POSIX)
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

TeeEmCee wrote:Just to double check: were you only using 'integer' where the original C used 'int'? Or were you also using integer in other places? Because, where 'integer' is used on the FB side (eg in tables of rtlib functions in the compiler), the rtlib uses ssize_t. So I guess you could do a replacement of all ssize_t to integer, at least for all the parameters/return values of public functions. It's certainly more idiomatic for FB. Not 100% certain about replacing ssize_t absolutely everywhere... afterall, it is used in some of FB's headers instead of integer. fbfrog does not convert ssize_t to integer.

(Trivia: unlike size_t, ssize_t (signed size_t) isn't a standard C type; it's POSIX)

Yes. I was only using Integer where int was in the C. If it says ssize_t I keep it at that.
Imortis
Moderator
Posts: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Pure FB Runtime Library (in progress)

Post by Imortis »

I am seeing this fairly often in this code:

Code: Select all

#if 0
...
#endif
I get what it is supposed to do, but why would you choose that over a multi-line comment? Is there some benefit that I don't know about?
Post Reply