Ducktape rust to c compiler via assembly?

192 Views Asked by At

For an university course I write some c/c++ programs which are then sent to an online judge and tested on inputs.

I hate c++. I am researching ways to compile rust to c. Afaik you can straight up embed assembly snippets in c programs. With a little bit of poking around I should be able to figure out the assembly flavor (or I'll just ask someone who made the online judge). I should be able to compile rust to assembly.

But then how practically feasible is this? Anything that means writing compiler parts is out of the question. Can it be as simple as:

  1. compile rust to a defined target
  2. create a C program from the rust binary with just string substitutions?

Something like:

# main.c
int main() {
  asm {
    <rust binary piped through some pretty printer?> 
  }
}

Is there a way for this to work?

I only need to define main in rust, I don't care for any interoperability or passing arguments to the rust function.

Importantly, the rust binary needs to be compiled statically. Then C code is compiled with

gcc -static -O2 -Wall solution.c -lm -osolution.x  # gcc 5.4.0

by the online tester.

Also I would assume that no systemcalls are allowed by the judge, is it possible to instruct Rust to emit such binaries?

Thanks!

EDIT: The judge is something along the lines of codeforces. You submit single source code file. It can't do things like spawn processes or open files.

1

There are 1 best solutions below

2
On

Can it be as simple as:

  1. compile rust to a defined target
  2. create a C program from the rust binary with just string substitutions?

No, it's not that straightforward. Once code has been compiled into a binary, trying to reverse-engineer it back into the original source code is incredibly challenging. Simple string substitutions won't cut it.

The process of decompiling a binary to a different programming language, especially one different from the original source, is typically met with poor results. And if you try to compile the decompiled source back into a binary, it will likely introduce significant changes to how the program behaves.

Is there a way for this to work?

In theory, your best bet is to use a transpiler or a source-to-source compiler. These tools usually work by translating the source code of one language into an intermediate representation and then, instead of producing assembly code, they output code in another target language.

Projects like C2Rust can convert C to Rust, but the reverse is not does not exist (afaik) because it often doesn't make practical sense.

Some older implementations exist for converting LLVM Intermediate Representation (IR) into C, but these are largely unmaintained and have limitations that may hinder their usefulness.

For an university course I write some c/c++ programs. I hate c++.

The simplest and most effective way to "transpile" a small Rust program to C++ is probably to do it manually.

Alternatively, you could experiment by requesting ChatGPT to perform the conversion and see what results it produces .