shinh / elvm

EsoLangVM Compiler Infrastructure
MIT License
1.13k stars 143 forks source link

Add C++ Template Metaprogramming Backend #39

Closed keiichiw closed 7 years ago

keiichiw commented 7 years ago

I added C++ template metaprogramming(TMP) backend. You can test this backend by the following command:

$ CPP_TEMPLATE=1 make cpp_template

This backend generates a C++ program that performs a compile-time computation like C++14 constexpr backend. However, TMP is much more restricted than C++14 constexpr.

The largest restriction is that a huge amount of memory is needed to compile a program generated by this backend. For example, g++ uses about 3GB memory to compile a C++ template program translated from a simple program:

$ cat hello.c
#include <stdio.h>
int main () {
  printf("Hello World!\n");
  return 0;
}
$ out/8cc -S ./hello.c -o hello.eir -Ilibc
$ out/elc -cpp_template hello.eir >hello.cpp
$ g++ -std=c++11 ./hello.cpp -o hello.exe     # g++ uses about 3GB memory here
$ ./hello.exe
Hello World!

The more complex program you want to run, the more memory is needed. Thus, unfortunately, 8cc translated by this backend does not work at all.

shinh commented 7 years ago

Thanks!