Can C/C++ software be compiled into bytecode for later execution? (Architecture independent unix software.)

心已入冬 提交于 2019-12-03 09:59:03

问题


I would want to compile existing software into presentation that can later be run on different architectures (and OS).

For that I need a (byte)code that can be easily run/emulated on another arch/OS (LLVM IR? Some RISC assemby?)

Some random ideas:

  • Compiling into JVM bytecode and running with java. Too restricting? C-compilers available?
  • MS CIL. C-Compilers available?
  • LLVM? Can Intermediate representation be run later?
  • Compiling into RISC arch such as MMIX. What about system calls?

Then there is the system call mapping thing, but e.g. BSD have system call translation layers.

Are there any already working systems that compile C/C++ into something that can later be run with an interpreter on another architecture?


Edit

Could I compile existing unix software into not-so-lowlevel binary, which could be "emulated" more easily than running full x86 emulator? Something more like JVM than XEN HVM.


回答1:


There are several C to JVM compilers listed on Wikipedia's JVM page. I've never tried any of them, but they sound like an interesting exercise to build.

Because of its close association with the Java language, the JVM performs the strict runtime checks mandated by the Java specification. That requires C to bytecode compilers to provide their own "lax machine abstraction", for instance producing compiled code that uses a Java array to represent main memory (so pointers can be compiled to integers), and linking the C library to a centralized Java class that emulates system calls. Most or all of the compilers listed below use a similar approach.




回答2:


C compiled to LLVM bit code is not platform independent. Have a look at Google portable native client, they are trying to address that.

Adobe has alchemy which will let you compile C to flash.

There are C to Java or even JavaScript compilers. However, due to differences in memory management, they aren't very usable.




回答3:


LLVM is not a good solution for this problem. As beautiful as LLVM IR is, it is by no means machine independent, nor was it intended to be. It is very easy, and indeed necessary in some languages, to generate target dependent LLVM IR: sizeof(void*), for example, will be 4 or 8 or whatever when compiled into IR.

LLVM also does nothing to provide OS independence.

One interesting possibility might be QEMU. You could compile a program for a particular architecture and then use QEMU user space emulation to run it on different architectures. Unfortunately, this might solve the target machine problem, but doesn't solve the OS problem: QEMU Linux user mode emulation only works on Linux systems.

JVM is probably your best bet for both target and OS independence if you want to distribute binaries.




回答4:


As Ankur mentions, C++/CLI may be a solution. You can use Mono to run it on Linux, as long as it has no native bits. But unless you already have a code base you are trying to port at minimal cost, maybe using it would be counter productive. If it makes sense in your situation, you should go with Java or C#.

Most people who go with C++ do it for performance reasons, but unless you play with very low level stuff, you'll be done coding earlier in a higher level language. This in turn gives you the time to optimize so that by the time you would have been done in C++, you'll have an even faster version in whatever higher level language you choose to use.




回答5:


Web Assembly is trying to address that now by creating a standard bytecode format for the web, but unlike the JVM bytecode, Web Assembly is more low level, working at the abstraction level of C/C++, and not Java, so it's more like what's typically called an "assembly language", which is what C/C++ code is normally compiled to.




回答6:


The real problem is that C and C++ are not architecture independent languages. You can write things that are reasonably portable in them, but the compiler also hardcodes aspects of the machine via your code. Think about, for example, sizeof(long). Also, as Richard mentions, there's no OS independence. So unless the libraries you use happen to have the same conventions and exist on multiple platforms then it you wouldn't be able to run the application.

Your best bet would be to write your code in a more portable language, or provide binaries for the platforms you care about.



来源:https://stackoverflow.com/questions/6439259/can-c-c-software-be-compiled-into-bytecode-for-later-execution-architecture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!