Use Scala as a codesign language

Hi all,

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?

Thanks for your answers! :slight_smile:

1 Like

My only thought is, “Sure, why not?”

Scalajs transforms scala into JavaScript, and works very well. Scala was always intended to be compiled to different runtime environments.

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.

Brian Maso

Hello,

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.

There are some limitations to supporting multiple backends. If you write Scala code to target both JVM and Scala.js, for example, almost always you will still write different versions for different backends for some part of the code. Also, I was naively assuming that a Scala statement like a.x = y would look the same when compiled to JavaScript, but I found that it is not quite that simple.

Best, Oliver

Best, Oliver

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.

Slick library ( http://slick.lightbend.com/ ) translates Scala code to SQL without hooking into compiler output generation. It uses “lightweight modular staging” (LMS) technique for doing that. A JavaScript generator using LMS is: https://github.com/js-scala/js-scala

Looking at http://scala-lms.github.io/ it seems it was already used to develop Scala-to-C translator: https://acl.inf.ethz.ch/research/SpiralS/

Overall LMS looks pretty complex. Slick library (which uses LMS) looks much more complex than e.g. Quill ( https://getquill.io/ ) which is based on some other technique.

Hello,

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.

That’s not entirely true – see for example Scala.js, which compiles Scala to JavaScript and runs in the browser.

That said, the Scala.js project was huge and complex, and took a number of years to get solid. It’s not a small thing…