Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Better Support for Embedded #1450

Open
scolsen opened this issue Jan 24, 2023 · 5 comments
Open

FR: Better Support for Embedded #1450

scolsen opened this issue Jan 24, 2023 · 5 comments

Comments

@scolsen
Copy link
Contributor

scolsen commented Jan 24, 2023

I'd like to use carp to generate some embedded code. This issue just tracks some quality of life improvements that I think would make that easier:

  1. Don't define Array/Box types in the compiler
  2. Only generate code that's actually used/called.
  3. Ensure qualifiers like volatile are easy to use

Will add further rationale in a bit.

@scolsen
Copy link
Contributor Author

scolsen commented Jan 24, 2023

re: (1.) the less we bundle with the compiler the easier it is to use it as a Carp in constrained environments. right now, we kind of force users to adopt our array and box types -- if we roll them entirely into the core lib instead, we'd be able to allow users to exclude them w/ no-core

re: (2.) code size matters in embedded. chances are C compilers would strip away unused C code anyway, we might as well help with that if we can by not generating unused C to begin with.

re: (3.) volatile and other qualifiers/attributes are important in embedded to ensure compilers don't e.g. eliminate unused bindings in register structs and such.

And another I thought of:

  • Make it possible to generate libraries in addition to binaries. This isn't really necessary for embedded, but it's a nice to have in order to better organize utilities and to provide bindings for C to libs written in carp. (2) would help support this since it'd move us toward generating C output that has a little bit better readability for users from C.

@scolsen
Copy link
Contributor Author

scolsen commented Jan 24, 2023

Also really curious to hear if @TimDeve has thoughts on this

@TimDeve
Copy link
Contributor

TimDeve commented Jan 25, 2023

Hey Scott,

I think these three improvement would be useful for sure.

  1. Being able to remove the part of carp that relies on allocation would be nice, on that point the proposal from @eriksvedang to make lambda stack allocated by default and make their heap allocation explicit via copy would also be quite useful. Another related thing is that managing core imports when using --no-core is quite painful, you have to maintain your own Core.carp and it breaks when the one in core gets updated. I was thinking a helper macro where you can pass the cores to omit might be nicer.
  2. In my experience gcc/clang are pretty good at discarding all the extra stuff we generate but for niche embedded use case you might not get one of the big compilers.
  3. You can certainly get around that with typedef at the moment but a cleaner way to annotate defs or let could be useful.

On the library point I think the current implicit thing with main is kinda confusing, and explicit flag could be better. I also don't even remember what we generate then? An object file or a dynlib?

@eriksvedang
Copy link
Collaborator

eriksvedang commented Jan 25, 2023

I also think all of these suggestions are great!

Perhaps the slightly extreme solution of not loading any core libraries by default could help? (I think Purescript does this..?) So it's all up to the user to decide what they bring in, except for stuff that is absolutely necessary for the compiler to start. One question then is what to do with the [ ] syntax for array, since that is surely the nicest syntax. In a way it would make more sense if the built in (static) array used that one, and dynamic arrays switched to $[] (or just a macro, like vec! in Rust). And yes, lambdas should definitely not have to allocate by default (I have been thinking about that recently, and I think the fix is even simpler than what I tried to do in the feature branch. But I can make a comment there instead...)

Regarding code generation (and executable/library generation) I think a neat UX could be to provide something like emit-exe / emit-library which would accept a module (or several) as an argument. In case of exe that module would have to have a main defined (or you'd pass the function that should act as main). Then the code generator would walk all the dependencies of that module (or function) and only emit those.

Regarding point 3 – we do support this for definitions, right? (using meta data) But I guess it has to go in more places to be truly useful? Perhaps a separate issue with more details could clarify this.

@scolsen
Copy link
Contributor Author

scolsen commented Jan 25, 2023

Regarding point 3 – we do support this for definitions, right? (using meta data) But I guess it has to go in more places to be truly useful? Perhaps a separate issue with more details could clarify this.

Yes, we do! If I remember correctly though the UX around it wasn't perfect. I recall having to use const qualifiers for a lot of stuff when making bindings to some Apple core libs and it was a bit awkward and didn't always work quite right for pointers, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants