Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

In specifications, Murphy's Law supersedes Ohm's.


devel / comp.lang.forth / How to define structs in forth that C understands?

SubjectAuthor
* How to define structs in forth that C understands?Troy Jacobs
`* Re: How to define structs in forth that C understands?Anton Ertl
 +* Re: How to define structs in forth that C understands?D Jacobs
 |`- Re: How to define structs in forth that C understands?Troy Jacobs
 `* Re: How to define structs in forth that C understands?none
  `- Re: How to define structs in forth that C understands?Troy Jacobs

1
How to define structs in forth that C understands?

<1c29153f-5056-45ef-ab5f-4eb6387b12e3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17718&group=comp.lang.forth#17718

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:620a:2886:b0:699:bab7:ae78 with SMTP id j6-20020a05620a288600b00699bab7ae78mr13086945qkp.618.1650474018165;
Wed, 20 Apr 2022 10:00:18 -0700 (PDT)
X-Received: by 2002:a05:620a:318e:b0:69e:afae:c64b with SMTP id
bi14-20020a05620a318e00b0069eafaec64bmr7945356qkb.485.1650474017799; Wed, 20
Apr 2022 10:00:17 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 20 Apr 2022 10:00:17 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=132.177.238.68; posting-account=IXTsPgoAAAAN-Due6ITniBLUa_e3nCQf
NNTP-Posting-Host: 132.177.238.68
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1c29153f-5056-45ef-ab5f-4eb6387b12e3n@googlegroups.com>
Subject: How to define structs in forth that C understands?
From: tmj9...@gmail.com (Troy Jacobs)
Injection-Date: Wed, 20 Apr 2022 17:00:18 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 11
 by: Troy Jacobs - Wed, 20 Apr 2022 17:00 UTC

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.

Re: How to define structs in forth that C understands?

<2022Apr20.231618@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17720&group=comp.lang.forth#17720

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: How to define structs in forth that C understands?
Date: Wed, 20 Apr 2022 21:16:18 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 35
Message-ID: <2022Apr20.231618@mips.complang.tuwien.ac.at>
References: <1c29153f-5056-45ef-ab5f-4eb6387b12e3n@googlegroups.com>
Injection-Info: reader02.eternal-september.org; posting-host="d7445588bf2ad18723a93ecc78c2337e";
logging-data="22825"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Qdl6B4ZDgLi9tIAYROjNr"
Cancel-Lock: sha1:LnIRrTCvpcTHpOnvzpDIYkrB6SQ=
X-newsreader: xrn 10.00-beta-3
 by: Anton Ertl - Wed, 20 Apr 2022 21:16 UTC

Troy Jacobs <tmj9001@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

Re: How to define structs in forth that C understands?

<88a73c15-b974-4a2c-afaf-3ff119253b89n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17721&group=comp.lang.forth#17721

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a37:a50:0:b0:69c:7024:7090 with SMTP id 77-20020a370a50000000b0069c70247090mr15192926qkk.48.1650503622882;
Wed, 20 Apr 2022 18:13:42 -0700 (PDT)
X-Received: by 2002:ac8:3d49:0:b0:2eb:cbf8:4140 with SMTP id
u9-20020ac83d49000000b002ebcbf84140mr15673847qtf.279.1650503622620; Wed, 20
Apr 2022 18:13:42 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 20 Apr 2022 18:13:42 -0700 (PDT)
In-Reply-To: <2022Apr20.231618@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=173.44.80.176; posting-account=rcx0zgoAAADswy7t4iLp5dPdUYC0mCPM
NNTP-Posting-Host: 173.44.80.176
References: <1c29153f-5056-45ef-ab5f-4eb6387b12e3n@googlegroups.com> <2022Apr20.231618@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <88a73c15-b974-4a2c-afaf-3ff119253b89n@googlegroups.com>
Subject: Re: How to define structs in forth that C understands?
From: fleasin...@gmail.com (D Jacobs)
Injection-Date: Thu, 21 Apr 2022 01:13:42 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 34
 by: D Jacobs - Thu, 21 Apr 2022 01:13 UTC

On Wednesday, April 20, 2022 at 5:31:50 PM UTC-4, Anton Ertl 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.
>
> - 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
Thanks, I appreciate the help! :)

Re: How to define structs in forth that C understands?

<66e754e7-b2e6-414e-be79-51abf8e7fc83n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17722&group=comp.lang.forth#17722

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a0c:f885:0:b0:444:416a:3f7a with SMTP id u5-20020a0cf885000000b00444416a3f7amr18174979qvn.127.1650509468322;
Wed, 20 Apr 2022 19:51:08 -0700 (PDT)
X-Received: by 2002:a05:6214:e64:b0:446:3d57:d320 with SMTP id
jz4-20020a0562140e6400b004463d57d320mr17331672qvb.87.1650509468149; Wed, 20
Apr 2022 19:51:08 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Wed, 20 Apr 2022 19:51:07 -0700 (PDT)
In-Reply-To: <88a73c15-b974-4a2c-afaf-3ff119253b89n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=173.44.80.176; posting-account=IXTsPgoAAAAN-Due6ITniBLUa_e3nCQf
NNTP-Posting-Host: 173.44.80.176
References: <1c29153f-5056-45ef-ab5f-4eb6387b12e3n@googlegroups.com>
<2022Apr20.231618@mips.complang.tuwien.ac.at> <88a73c15-b974-4a2c-afaf-3ff119253b89n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <66e754e7-b2e6-414e-be79-51abf8e7fc83n@googlegroups.com>
Subject: Re: How to define structs in forth that C understands?
From: tmj9...@gmail.com (Troy Jacobs)
Injection-Date: Thu, 21 Apr 2022 02:51:08 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 36
 by: Troy Jacobs - Thu, 21 Apr 2022 02:51 UTC

On Wednesday, April 20, 2022 at 9:13:44 PM UTC-4, D Jacobs wrote:
> On Wednesday, April 20, 2022 at 5:31:50 PM UTC-4, Anton Ertl 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.
> >
> > - 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
> Thanks, I appreciate the help! :)
Apologies for the confusion, I have multiple Google accounts, and sometimes I accidentally use the wrong one. That last comment was my other account.

Re: How to define structs in forth that C understands?

<nnd$2eb141d1$6d79e88b@fffea950a2ec61ee>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17726&group=comp.lang.forth#17726

  copy link   Newsgroups: comp.lang.forth
Newsgroups: comp.lang.forth
References: <1c29153f-5056-45ef-ab5f-4eb6387b12e3n@googlegroups.com> <2022Apr20.231618@mips.complang.tuwien.ac.at>
Subject: Re: How to define structs in forth that C understands?
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: alb...@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$2eb141d1$6d79e88b@fffea950a2ec61ee>
Organization: KPN B.V.
Date: Thu, 21 Apr 2022 10:19:23 +0200
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!94.232.112.245.MISMATCH!abe005.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 84
Injection-Date: Thu, 21 Apr 2022 10:19:23 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
 by: none - Thu, 21 Apr 2022 08:19 UTC

In article <2022Apr20.231618@mips.complang.tuwien.ac.at>,
Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>Troy Jacobs <tmj9001@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<--------------------
>
>- 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

--
"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

Re: How to define structs in forth that C understands?

<4169d71e-fac8-45c8-a8a1-cb3fc88685aan@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17727&group=comp.lang.forth#17727

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:6214:2a82:b0:443:e2fc:c209 with SMTP id jr2-20020a0562142a8200b00443e2fcc209mr18514986qvb.59.1650546841095;
Thu, 21 Apr 2022 06:14:01 -0700 (PDT)
X-Received: by 2002:aed:3148:0:b0:2ed:55a5:7a92 with SMTP id
66-20020aed3148000000b002ed55a57a92mr17135800qtg.104.1650546840873; Thu, 21
Apr 2022 06:14:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 21 Apr 2022 06:14:00 -0700 (PDT)
In-Reply-To: <nnd$2eb141d1$6d79e88b@fffea950a2ec61ee>
Injection-Info: google-groups.googlegroups.com; posting-host=173.44.80.176; posting-account=IXTsPgoAAAAN-Due6ITniBLUa_e3nCQf
NNTP-Posting-Host: 173.44.80.176
References: <1c29153f-5056-45ef-ab5f-4eb6387b12e3n@googlegroups.com>
<2022Apr20.231618@mips.complang.tuwien.ac.at> <nnd$2eb141d1$6d79e88b@fffea950a2ec61ee>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4169d71e-fac8-45c8-a8a1-cb3fc88685aan@googlegroups.com>
Subject: Re: How to define structs in forth that C understands?
From: tmj9...@gmail.com (Troy Jacobs)
Injection-Date: Thu, 21 Apr 2022 13:14:01 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 81
 by: Troy Jacobs - Thu, 21 Apr 2022 13:14 UTC

On Thursday, April 21, 2022 at 4:19:27 AM UTC-4, none albert wrote:
> 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<--------------------
> >
> >- 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
> --
> "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
This is also super helpful, thank you! :D

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor