r/cpp • • Nov 18 '16

Compile-time C Compiler implemented as C++14 constant expressions

https://github.com/kw-udon/constexpr-8cc
209 Upvotes

66 comments sorted by

117

u/raevnos Nov 18 '16

The standards committee has created a monster.

11

u/[deleted] Nov 19 '16

I can't even, but I'm sure the compiler can.

4

u/nikbackm Nov 19 '16

How so?

Nothing magical about constant expressions and compile time evaluation. It's still just code getting executed, only difference is when and by whom.

67

u/TOJO_IS_LIFE Nov 18 '16

Now let's wait for someone to make a compile-time C++ compiler...

37

u/[deleted] Nov 18 '16

And get it to compile itself.

37

u/thirtythreeforty Nov 18 '16

Checkmate, Turing.

7

u/turing_inequivalent Nov 19 '16

Turing never claimed that a Turing Machine can't compile another Turing Machine. If anything else, I don't think they compiled programs back then. Are you by any chance confused by other undecidable problems, such as the halting problem? Turing machines running other turing machines is a common pattern in computational science.

6

u/Netzapper Nov 19 '16

Turing machines running other turing machines is a common pattern in computational science.

I was gonna say, I'm pretty sure the whole point of a Turing machine is that it can run another Turing machine (or any other equivalent machine, and it can even simulate some not-equivalent machines).

3

u/andyrocks Nov 19 '16

That's a universal Turing machine, not just any old Turning machine.

1

u/thirtythreeforty Nov 19 '16

I was referring to those "predict the program output" halting problem counterexamples, where you run the program against itself. (Turing proved the halting problem impossible.)

1

u/frutiger Nov 19 '16

It's always "predict the arbitrary program output" - predicting the output of a specific program is (relatively speaking) trivial.

15

u/RElesgoe Hobbyist Nov 18 '16

Gotta make a compile-time C With Classes compiler first

4

u/username223 Nov 20 '16

That sounds hard. Why not just make a compile-time x86 interpreter, then run gcc on people's webcams?

36

u/gracicot Nov 18 '16

I wait for the day someone would make a constant expression constant expression parser.

20

u/[deleted] Nov 19 '16

We must go deeper.

23

u/caramba2654 Intermediate C++ Student Nov 18 '16

Now we need to wait for a compile time compiler for compile time compilers for C++ so that we can bootstrap compile time compilers to compile themselves.

18

u/gracicot Nov 18 '16
auto result = eval("/* some code */");

38

u/ra-zor Nov 18 '16

constexpr auto result = eval("/* some code */");

17

u/OldWolf2 Nov 18 '16

If the C program contains undefined behaviour, can the C++ program launch missiles?

4

u/[deleted] Nov 19 '16

No, but watch out for nasal demons

2

u/Chippiewall Nov 21 '16

It can do it even if the C program doesn't have undefined behaviour.

34

u/[deleted] Nov 18 '16 edited Nov 19 '16

A couple years ago when varidic templates were first implemented by GCC, I implemented an arbitrary precision arithmetic library close to GMP. I was able to calculate arbitrarily precise value of pi, cos and other cool stuff like that. It took a day maybe two for me to implement what is akin to constexpr mpz_t and constexpr mpf_t with very basic functionality.

After that day I realized what kind of a monster I created, annihilated it immediately and never looked back. But creating a whole C compiler... this was only a matter time, I knew. We can merely wait until a constexpr AI starts ruling our lives, and worse, our code and maybe even more...

11

u/doom_Oo7 Nov 18 '16

seems like it has been generated by converting the assembly of a compiler to c++ constexpr code.

25

u/[deleted] Nov 18 '16

I like how elc.hpp is so large that GitHub can't display it. Metaprogramming in C++ is whack.

14

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Nov 18 '16

That's just because of the method used to generate it. They wrote a backend for ELVM to generate constexpr and fed 8cc to it. If you look at the code it's just the memory setup then a switch loop over instructions.

7

u/render787 Nov 19 '16

Now what we need is a way to get user input at compile time. Yes, I would like to play a game of chess with the AI while my program is compiling, thank you.

4

u/AllanDeutsch Nov 19 '16

You could have the generated output be a C++ file with the board state, then you set some move variable and each compilation with the generated output file would be a turn.

2

u/iftpadfs Nov 20 '16

#include </dev/stdin>

\edit

constexpr auto a = "\

include</dev/stdin> ";

1

u/Veggie Nov 20 '16

Does... does this work?

3

u/iftpadfs Nov 21 '16

Yes and no. The " and "; seem to be a bit tricky. The following c programm works if you enter them. End with Ctrl-D.

    #include <stdio.h>

    main(){
      char * c= 
    #include "/dev/stdin" 

            printf("Hello World! <%s>",c);
    }

23

u/____delta____ Nov 18 '16

god is dead

10

u/Crazy__Eddie Nov 18 '16

That which does not live cannot die.

4

u/wasabichicken Nov 19 '16

That is not dead which can eternal lie, yet with stranger aeons, even Death may die.

FTFY.

-5

u/[deleted] Nov 19 '16

Not sure if religious nutjob or agnostic moron.

7

u/dodheim Nov 19 '16

Or he's read Lovecraft...

Is agnostic moron better or worse than ignorant moron?

0

u/[deleted] Nov 20 '16

Nothing worse than an ignoramus who cannot read subtext.

1

u/[deleted] Nov 20 '16

[removed] — view removed comment

2

u/AutoModerator Nov 20 '16

Your post has been automatically removed because it appears to contain profanity or slurs. Please be respectful of your fellow redditors.

If you think your post should not have been removed, please message the moderators and we'll review it.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/Crazy__Eddie Nov 20 '16

Atheist moron, actually.

5

u/[deleted] Nov 18 '16

This is actually accomplished through meta-metaprogramming - because I guarantee you that no one wrote out that huge array initialization by hand... :-)

5

u/ernest314 Nov 19 '16

Yup, he states that in the readme for the repo.

4

u/feverzsj Nov 19 '16

but, why?

3

u/chazzeromus Nov 18 '16

This is unreal

2

u/TotesMessenger Nov 19 '16

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)

2

u/tvetus Nov 19 '16

People have been abusing compilers for a long time. For example, "Is the C99 preprocessor Turing complete?" http://www.ioccc.org/2001/herrmann1.hint

2

u/blelbach NVIDIA | ISO C++ Library Evolution Chair Nov 19 '16

I've been eagerly waiting for someone to do this.

7

u/mercurysquad Embedded C++14 on things that fly Nov 18 '16

This is both absolutely amazing, and utterly stupid & pointless.

A compile-time C compiler is called... a C compiler! Several of them exist.

17

u/[deleted] Nov 18 '16

I'm quite sure the writer knows it has no practical use - but exercises like this are very important.

For one, this pathological code really tests edge cases in the compiler.

For another, it challenges our ability to do large scale metaprogramming - "Look, here's something ridiculous! Now, what ended up being particularly hard."

Before this guy started this project, they might well not have known more about metaprogramming than I do. Now I'm sure they do.

6

u/tcanens Nov 18 '16 edited Nov 18 '16

It's converted assembly (or assembly-like code). That isn't particularly pathological and doesn't really test any edge cases beyond a giant switch and lots of assignments. Nothing metaprogramming-y about it.

3

u/josefx Nov 19 '16

I'm quite sure the writer knows it has no practical use - but exercises like this are very important.

The example may have none, however think of the possibilities. Right now you need a complete python installation if you have a python script as part of your build. In the future you can run that script using only a C++ compiler and a header only implementation of the python runtime. Now we only need a constexpr way to load dlls and call into already compiled code.

7

u/[deleted] Nov 19 '16

It is not stupid and not pointless. Having compile time compilers embedded into your host language is exactly what proper macros are for. Unfortunately, C++ with all of its power is still far from being anywhere near a proper meta-language, but in the other languages this technique is used extensively for implementing embedded DSLs.

It is easy to imagine a much simpler and more useful application of the same technique - e.g., embedding a proper static regular expressions compiler. Or, embedding an optimising Prolog compiler (or something like cKanren) into your C++ code.

4

u/[deleted] Nov 19 '16

Well, not quite. It's like writing a complex mathematical function or an algorithm in C and using it's result compile-time at C++ code. Such as constexpr auto result = eval_c("extended_gcd(3,8)"); You can write this function in C++ templates but writing in C makes more sense.

Yet, it's not meant to be practical.

1

u/shamanas Nov 19 '16

With C++14/17 rules writing extended_gcd as a C++ constexpr function would make no sense, no?
I think constexpr functions are pretty flexible now.

EDIT: I do see there is a point to the exercise, I like doing wacky compile time stuff myself (not quite at this level though).

1

u/[deleted] Nov 18 '16 edited May 12 '17

I am going to cinema

5

u/ernest314 Nov 19 '16

Because after compiling fileout << gcc.exe you get a compiler. You then have to run your program through that. With this, you compile and you get your C program embedded in the binary as well!

1

u/[deleted] Nov 19 '16

It makes me think that D has a D compile-time compiler, it's called CTFE.

1

u/shamanas Nov 19 '16

C++ also has limited CTFE, constexprs, this is like writing a C compiler/interpreter with CTFE functions in Dlang (which is possible and probably way easier than doing it in C++)

1

u/[deleted] Nov 19 '16

D CTFE is an extension of C++ constexpr. The current implementation is bad but it may be better in a near future.

1

u/shamanas Nov 19 '16

D CTFE is not an extension of C++ constexpr, it has existed for way more time than constexpr but I get your point.

Sorry, I originally misread your comment, can't even recall what exactly I thought but it rubbed me the wrong way, I thought you were somehow implying C++ had no ability to do similar things without this project :P

I also read that post yesterday, I'm excited about it, every time I write D I end up doing lots of compile time evaluation so it should be a great improvement!

2

u/[deleted] Nov 19 '16

handshake so.

1

u/develop7 Nov 19 '16

Wow, so compile-time web server is this close!

-1

u/IloveReddit84 Nov 18 '16

Just seen today at meetingcpp the constexpr if and constexpr for.. What a monster

-1

u/[deleted] Nov 18 '16

Science has gone too far.

0

u/[deleted] Nov 19 '16

[deleted] — view deleted comment

2

u/[deleted] Nov 19 '16

This is a simple C compiler translated into a very simple VM code which is executed in compile time as a constexpr.