C
C#2mo ago
hutonahill

Open Source C to Arm using C#

Working on a project with a buddy of mine. We are trying to write a C compiler that handles custom op codes and one or two other things for a bigger project. To be totally honest, this is not my world. I am more comfortable higher up the abstraction tree, so I don't have all the details, but here is my best understanding of the problem. Because of how clang handles strings (storing them in separate memory addresses), we can't use the general C compiler, as it would cause major slowdowns down the line by orders of magnitude. Our solution was to write our own C# compiler, but we are running into so many edge cases, and we worry we are going to forget about something. We would rather take an existing compiler and modify it. We figure we will get better performance and will be less likely to forget something. Is there a C to ARM compiler written in C# that already exists? The project is in C#, and it's a language we both know. Also is there a better place to put this? i am planning on cross posting this on r/compilers.
9 Replies
Xymanek
Xymanek2mo ago
There is https://github.com/ForNeVeR/Cesium that compiles C to run on CoreCLR. You could potentially reuse the "parse C" part and write your own emit part
GitHub
GitHub - ForNeVeR/Cesium: C compiler for the CLI platform
C compiler for the CLI platform. Contribute to ForNeVeR/Cesium development by creating an account on GitHub.
hutonahill
hutonahillOP2mo ago
nifty. thanks
dreadfullydistinct
Because of how clang handles strings (storing them in separate memory addresses)
could you elaborate on this a bit? what do you need to happen, reuse of memory addresses for e.g. substrings? like, for example, in this program, would you require that only the first string literal is used and the rest use substrings?
dreadfullydistinct
Compiler Explorer - C (armv8-a clang 21.1.0)
int main() { printf("foobar\n"); printf("foo\n"); printf("bar\n"); }
hutonahill
hutonahillOP2mo ago
Our system doesn't assemble to binary, we are assembling to a 3rd language with some weird restrictions. concating in this language is very expensive and we are trying to avoiding needing to concat an array of characters into a string.\
dreadfullydistinct
Ah so you definitely need a custom compiler and it’s not just a solution you’ve come up with Because what I was going to suggest was whether some c++ compile time programming like compile time string manipulation could help
hutonahill
hutonahillOP2mo ago
we are taking a look at other solutions. Our project would be way cooler if we could use some kind of off the shelf intermediary language instead of assembly with a custom opcode some way to make it language agnostic
dreadfullydistinct
so in c++ you can do stuff like this to perform all concatenation at compile time https://godbolt.org/z/1vPWcTqWo
Compiler Explorer - C++ (x86-64 gcc 15.2)
template <std::string_view const&... Strs> struct join { // Join all strings into a single std::array of chars static constexpr auto impl() noexcept { constexpr std::size_t len = (Strs.size() + ... + 0); std::array<char, len + 1> arr{}; auto append = [i = 0, &arr](auto const& s) mutabl...
dreadfullydistinct
i am not sure if it's what you're looking for though as you obviously need to know the strings at compile time

Did you find this page helpful?