Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to define structs in forth that C understands?

143 views
Skip to first unread message

Troy Jacobs

unread,
Apr 20, 2022, 1:00:19 PM4/20/22
to
Hi, this is my first post to this group. I've recently started using and enjoying forth, specifically the Gforth implementation. I'm currently trying to use the Vulkan graphics API through forth via Gforth's C-interface.
In doing this, I've encountered a situation where I need to define a struct in forth (using the forth-2012 begin-structure & end-structure words) that I will then pass to a C function. My question is, how do I know how much memory & padding each field should have? Is there some resource that explains how C handles struct creation? Do I just need to play around with sizeof() a bunch?
Also, if it matters, the platform I am using is Linux 64-bit using GCC. I'd like to get a portable solution to my problem, but for now I'm fine with something that just works on my machine.

Anton Ertl

unread,
Apr 20, 2022, 5:31:50 PM4/20/22
to
Troy Jacobs <tmj...@gmail.com> writes:
> In doing this, I've encountered a situation where I need to define a stru=
>ct in forth (using the forth-2012 begin-structure & end-structure words) th=
>at I will then pass to a C function. My question is, how do I know how muc=
>h memory & padding each field should have?

The best way is to ask the C compiler, with the offsetof macro, and
use the resulting numbers in Forth with +FIELD.

Gerald Wodni has extended SWIG to automate much of this work (but he
is in the process of revising his SWIG approach, so don't invest more
than necessary in the current approach). You can find more about this
in
<https://gforth.org/manual/Automated-interface-generation-using-SWIG.html>.

Following this, I find
<https://github.com/GeraldWodni/forth-c-interfaces/tree/master/glfw3>
("LFW is an Open Source, multi-platform library for OpenGL, OpenGL ES
and Vulkan development on the desktop. It provides a simple API for
creating windows, contexts and surfaces, receiving input and
events."), which may be of interest to you (or you can use it as an
example for how to use SWIG for the C interface).

>Is there some resource that exp=
>lains how C handles struct creation?

Maybe the ABI documentation, but the best bet is to ask the C
compiler, as discussed above.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2021: https://euro.theforth.net/2021

D Jacobs

unread,
Apr 20, 2022, 9:13:44 PM4/20/22
to
Thanks, I appreciate the help! :)

Troy Jacobs

unread,
Apr 20, 2022, 10:51:09 PM4/20/22
to
Apologies for the confusion, I have multiple Google accounts, and sometimes I accidentally use the wrong one. That last comment was my other account.

none albert

unread,
Apr 21, 2022, 4:19:27 AM4/21/22
to
In article <2022Apr2...@mips.complang.tuwien.ac.at>,
Anton Ertl <an...@mips.complang.tuwien.ac.at> wrote:
>Troy Jacobs <tmj...@gmail.com> writes:
>> In doing this, I've encountered a situation where I need to define a stru=
>>ct in forth (using the forth-2012 begin-structure & end-structure words) th=
>>at I will then pass to a C function. My question is, how do I know how muc=
>>h memory & padding each field should have?
>
>The best way is to ask the C compiler, with the offsetof macro, and
>use the resulting numbers in Forth with +FIELD.
>
>Gerald Wodni has extended SWIG to automate much of this work (but he
>is in the process of revising his SWIG approach, so don't invest more
>than necessary in the current approach). You can find more about this
>in
><https://gforth.org/manual/Automated-interface-generation-using-SWIG.html>.
>
>Following this, I find
><https://github.com/GeraldWodni/forth-c-interfaces/tree/master/glfw3>
>("LFW is an Open Source, multi-platform library for OpenGL, OpenGL ES
>and Vulkan development on the desktop. It provides a simple API for
>creating windows, contexts and surfaces, receiving input and
>events."), which may be of interest to you (or you can use it as an
>example for how to use SWIG for the C interface).
>
>>Is there some resource that exp=
>>lains how C handles struct creation?
>
>Maybe the ABI documentation, but the best bet is to ask the C
>compiler, as discussed above.

In addition to this you, want the actual value of symbolic constants
that are generally not documented in man pages.
You may find the macro's in the following program useful.
For Forth you may replace EQU with CONSTANT and 0x with $.

----------------------------8<-----------------------------------
/* This c-source is intended to be print a number of EQUATES */
/* that can be included in assembler files. */
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
... etc ...

/* Steal the information what value B has, leave it in A */
#define STEAL(A,B) printf("%s\tEQU\t0x%x\n", A, B );

/* Have a A that has the same value than in C. */
#define STEALNAME(A) STEAL( #A, A)

/* Have a A that has the number of system call B */
#define STEALSYS(A) STEAL( #A, __NR_##A)

int main()
{
printf("include(stealconstant.m4) \n");
STEALNAME(SEEK_CUR)
STEALNAME(SEEK_SET)
.... etc ....
STEAL("SIZE_TERMIO",sizeof(struct termios))

printf("_C{ https://github.com/torvalds/linux/tree/master/arch/x86/entry/syscalls}\n");
STEALSYS(exit)
STEALSYS(unlink)
.... etc ...
STEALNAME(__NR_times)
exit(0);
}
-------------------------- 8<--------------------
--
"in our communism country Viet Nam, people are forced to be
alive and in the western country like US, people are free to
die from Covid 19 lol" duc ha
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Troy Jacobs

unread,
Apr 21, 2022, 9:14:02 AM4/21/22
to
This is also super helpful, thank you! :D
0 new messages