• How to define structs in forth that C understands?

    From Troy Jacobs@21:1/5 to All on Wed Apr 20 10:00:17 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to Troy Jacobs on Wed Apr 20 21:16:18 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From D Jacobs@21:1/5 to Anton Ertl on Wed Apr 20 18:13:42 2022
    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! :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Troy Jacobs@21:1/5 to D Jacobs on Wed Apr 20 19:51:07 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to Anton Ertl on Thu Apr 21 10:19:23 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Troy Jacobs@21:1/5 to none albert on Thu Apr 21 06:14:00 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)