I have created my very own (very simple) byte code language, and a virtual machine to execute it. It works fine, but now I'd like to use gcc (or any other freely available compiler) to generate byte code for this machine from a normal c program. So the question is, how do I modify or extend gcc so that it can output my own byte code? Note that I do NOT want to compile my byte code to machine code, I want to "compile" c-code to (my own) byte code.
I realize that this is a potentially large question, and it is possible that the best answer is "go look at the gcc source code". I just need some help with how to get started with this. I figure that there must be some articles or books on this subject that could describe the process to add a custom generator to gcc, but I haven't found anything by googling.
The GNU Compiler Collection (GCC) was, from its inception, written in C and compiled by a C compiler. Beginning in 2008, an effort was undertaken to change GCC so that it could be compiled by a C++ compiler and take advantage of a subset of C++ constructs.
A back end for a target architecture in GCC has the following parts: A directory machine under gcc/config , containing a machine description machine . md file (see Machine Descriptions), header files machine . h and machine -protos. h and a source file machine .
GCC is the de facto compiler for GNU/Linux. This means that some version of GCC comes already installed with the operating system and is used to compile core components such as kernel modules, programs and libraries.
I am busy porting gcc to an 8-bit processor we design earlier. I is kind of a difficult task for our machine because it is 8-bit and we have only one accumulator, but if you have more resources it can became easy. This is how we are trying to manage it with gcc 4.9 and using cygwin:
config.sub
around line 250 look for # Decode aliases for certain CPU-COMPANY combinations.
In that list add | my_processor \
# Recognize the basic CPU types with company name.
add yourself to the list: | my_processor-* \
Search for the file gcc/config.gcc
, in the file look for case ${target} it is around line 880, add yourself in the following way:
;; my_processor*-*-*) c_target_objs="my_processor-c.o" cxx_target_objs="my_processor-c.o" target_has_targetm_common=no tmake_file="${tmake_file} my_processor/t-my_processor" ;;
gcc-4.9.0\gcc\config\my_processor
my_processor.c
my_processor.h
my_processor.md
my_processor.opt
my_processor-c.c
my_processor.def
my_processor-protos.h
constraints.md
predicates.md
README.txt
t-my_processor
gcc-4.9.0/build/object
../../configure --target=my_processor --prefix=path for my compiler --enable-languages="c"
It is hard work.
For example I also design my own "architecture" with my own byte code and wanted to generate C/C++ code with GCC for it. This is the way how I make it:
When you are finished you can use c or c++ only without os-dependet libraries (you have currently no running OS on your architecture) and you should now (if you need it) compile many other libraries with your cross compiler to have a good framework.
PS: LLVM (Clang) is easier to port... maybe you want to start there?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With