I would like to know if you think I could use Scala for the use-case I have in mind, if you think this is appropriate and if you already know some similar already developed/in development ideas.
The use-case would be to use Scala as the primary language of a hardware/software codesign development environment (to program System-on-Chips with both a CPU and a FPGA). The idea is to code one .scala which would be used to generate a .c and a .vhd (some parts of the Scala code being loaded into the processor (.c), some others into the FPGA (.vhd) ). Ideally this could be applied to any languages, not only C and VHDL.
So my questions are:
Could Scala be used to generate C code? Has it already been done somehow/somewhere? (I know that SpinalHDL did the Scala to VHDL part of the problem.)
Has this codesign topic already been addressed and/or discussed in the Scala community?
Another technique used by a few scala-based projects relies on compile-time macros to implement problem domain-specific runtime behaviors. I’m not the biggest fan of macros because they introduce magical behavior at runtime instead of logically consistent, FP behavior. This is similar to Java’s preponderance of magic annotation-based runtime behavior. (I will never stop being thankful to Scala from saving the JVM world from Spring annotation madness.)
But I have to admit sometimes runtime magic is pretty cool and convenient; I’m currently really enjoying using mill (a macro-based Scala build tool) to implement a build pipeline, not unlike a good old-fashioned *nix makefile.
I don’t see any reason why you couldn’t do what you ate describing with Scala.
Why do you need C? In principle, Scala can be compiled to C, but at a high price.
Features that C has, but Scala does not, cannot be used. This includes manual memory management, pointer arithmetic, hard casts.
Features that Scala has, but C does not, need to be implemented by the Scala-to-C compiler. This includes OO support,
automatic memory management (garbage collection), reflection.
The only backends for Scala I am aware of are JVM, Scala.js and Scala Native. Only the JVM backend fully implements reflection, because fully implementing reflection means more or less implementing a full JVM.
It’s also worth noting that SpinalHDL isn’t a compiler-plugin that compiles regular scala code to VHDL: Instead, it’s a library that has an API that can be used to decribe data flow, and when run generate the VHDL. That’s a very different architecture.
Thank you all for these really interesting answers!
I would need C to program an embedded processor. Today C is the most commonly used language for this application and the targets I plan to use.
Since the use of Scala implies deploying a JVM on target, maybe C is not the solution in the end (?).
Thanks for these links, especially LSM. Seems like they handle both C code and VHDL (or at least they plan to). I will dig into details about LSM.