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