Shop OBEX P1 Docs P2 Docs Learn Events
Compilers Supporting Packed Structs (GNU C style or pragma pack) on P2? — Parallax Forums

Compilers Supporting Packed Structs (GNU C style or pragma pack) on P2?

RbtsEvrywhr_RileyRbtsEvrywhr_Riley Posts: 32
edited 2025-04-28 21:15 in Propeller 2

Hey all,

I'm working on porting some pretty heavy libs to the p2 and packed structs are everywhere. They do have compiler type macros, but do we have any compiler that supports any variant of packed struct directives at all?

I'm still familiarizing myself with the different compilers available for the P2, so I figured I'd ask while I dig through documentation. If not, I'm not particularly worried, I'm pretty sure I only lose 9 bytes per transmission but it's a moderately significant rewrite.

Edit: I should probably ask about Spin2 integration as well because the control stack is going to all get written in spin.

Comments

  • RaymanRayman Posts: 15,224

    @ersmith would know better but think the riscv compiler should do that as can use recent GCC

  • RaymanRayman Posts: 15,224

    There are a couple ways to add spin2 code…

    One is to add to compiler…0

  • I see flexspin internally has a Module packed attribute, but I don't think that's plumbed through for C usage currently. It does get set on Spin2 structs though.

    There's really little reason to not pack structures on the P2, since unaligned access in hub RAM only has a 1-cycle penalty.

  • RaymanRayman Posts: 15,224

    Think asked @ersmith about this before and was non-starter for flexC

  • cgraceycgracey Posts: 14,280

    How packed do you need to be? Is BYTE the fundamental storage unit, or is it any number of bits?

  • RaymanRayman Posts: 15,224

    Just remembered that somebody here was working on a LLVM compiler. Think that was looking good, but haven't seen it in a while...

  • Haven't been able to get the LLVM compiler to build yet, and BYTE is reasonably fine. Frankly it's overkill and mostly for portability since this is a very large project.

    Also realizing that spin2cpp does not support pragma directives or namespaces, either, and it seems to be the only compiler that is reasonably mature. I'm going to see what I can do to isolate a few of these issues and help out, because at this point I think C compiler maturity, especially combined with spin integration, is the biggest thing holding the P2 back. Spin's cool and there are brilliant high level things that can be done with it, but the embedded world has written so many things in C already and the spin stuff, smart pin stuff, etc should be able to talk to those libs without it being a month long job to port things over. I think a lot of people industrially say the same thing of "I love this P2 chip but if we go with Cortex I'd already be done".

    Part of my problem is we need spin integration since the absolutely brilliant navigation ideas are done in spin2. MKB is back doing autopilots and this stuff is years ahead of what's out there on the drone market right now.

    P2 deserves its place in the sun, it's an absolutely brilliant hardware architecture.

  • @RbtsEvrywhr_Riley said:
    Also realizing that spin2cpp does not support pragma directives or namespaces, either, and it seems to be the only compiler that is reasonably mature. I

    There is pragma support, but probably not the particular ones you're using (#pragma once eg should work). C++ style namespaces aren't a thing, but you can compile C files into Spin-style objects (struct __using extension), which essentially isolates their names.

  • ersmithersmith Posts: 6,166

    I've started adding __attribute__((packed)) to structs in flexspin, and it's in the latest github, but it probably has bugs.

    If it's a particularly heavy set of libraries you're using with a lot of assumptions about compilers, you may well be better off using LLVM or riscvp2 (the GCC port I did a while back using a JIT from RISC-V instructions to P2). For spin integration you can try using spin2cpp to convert the Spin code to C++. Alas, the C/C++ output feature of spin2cpp has not kept up with the other back-ends, so while Spin1 is covered pretty well more recent Spin2 features may not work.

  • RaymanRayman Posts: 15,224
    edited 2025-05-01 00:23

    Riscvp2 at least has the ability to connect spin2 code.

    Be nice if one could compile a spin2 code as a binary file and then just launch it into a free cog. Maybe one can do this already?

  • ersmithersmith Posts: 6,166
    edited 2025-05-01 13:05

    @Rayman said:
    Riscvp2 at least has the ability to connect spin2 code.

    Only if the spin2 code is converted with a tool like spin2cpp (or compiled to a binary with flexspin), riscvp2 can't directly parse Spin2.

    Be nice if one could compile a spin2 code as a binary file and then just launch it into a free cog. Maybe one can do this already?

    Yes, flexspin -E -H 0xNNNN can compile the code into a binary blob intended to start at $NNNN. But getting that into the right address is non-trivial. If the code doesn't depend on any addresses then this doesn't really matter, so simple code is easy to use this way.

  • @ersmith said:

    @Rayman said:
    Be nice if one could compile a spin2 code as a binary file and then just launch it into a free cog. Maybe one can do this already?

    Yes, flexspin -E -H 0xNNNN can compile the code into a binary blob intended to start at $NNNN. But getting that into the right address is non-trivial. If the code doesn't depend on any addresses then this doesn't really matter, so simple code is easy to use this way.

    :+1: That's what I use with my emulators to add the Spin2+C loader/menu into what's otherwise a pure ASM program with micro-managed memory usage (compiler can't allocate 64K alignments or zeropage variables...)

    Communicating between multiple programs in the same memory space like that isn't easy though.

  • RaymanRayman Posts: 15,224

    @ersmith guess I’m thinking about riscvp2 drivers like vga and such. Aren’t these compiled as .spin2 files and then become part of the riscvp2 compiler?

  • ersmithersmith Posts: 6,166

    @Rayman said:
    @ersmith guess I’m thinking about riscvp2 drivers like vga and such. Aren’t these compiled as .spin2 files and then become part of the riscvp2 compiler?

    No, they're converted from .spin2 to .c via spin2cpp, and then compiled with riscvp2.

Sign In or Register to comment.