6

I have read several times that early C++ compilers translated first C++ code into plain C before compiling it (or maybe needed a third-party C compiler).

Playing myself with grammar / language / compilation fields, I am curious to see how C++ was implemented in plain C, especially what can be one way to implement the class inheritance and [virtual] method calling.

Could you point me to such a compiler that would still be available nowadays?

I know that OO code can be simulated / emulated in plain C with structs and functions pointers, but I would like to see an actual implementation of the C++ language in C.

Seki
  • 11,135
  • 7
  • 46
  • 70
  • 1
    Keep in mind that even if you find it, it is not going to be C++ as we know it, but something simpler. I suspect it did things like putting an extra argument on all functions, which was the frame pointer. Or kept a global variable for that stuff. – Prof. Falken Apr 12 '13 at 11:59
  • 3
    http://stackoverflow.com/questions/737257/code-convert-from-c-to-c – zch Apr 12 '13 at 12:00
  • 1
    Just to be clear, by *an actual implementation of the C++ language in C*, you mean a C++ compiler written in C, not a tool that can translate C++ into C, right? – NPE Apr 12 '13 at 12:02
  • How about opening some library files and have a look at the code how its being done... – Hiren Pandya Apr 12 '13 at 12:09
  • @NPE: Yes, I am interested to see a C++ compiler that produce plain C code. Not sure if a translator would produce a similar code or some tricks to simulate C++. – Seki Apr 12 '13 at 12:12
  • As you can do almost anything (few things require inline asm) with C, there's nothing strange about compiling C++ into C, though resulting code would be rather horrible from human point of view, for example magic number offsets into function pointer tables. Probably not very exciting or interesting, but rather a brutish code to make C++ code happen. – hyde Apr 12 '13 at 12:16
  • @zch: not actually, as I have no C++ code to "translate", I am looking how C++ was implemented in its early days. Now I suppose that it is self-hosting. – Seki Apr 12 '13 at 12:18
  • What about the [`cfront`](http://www.sourcecodeprojects.com/599390/) source code. I'm not sure how kosher it is, but there it is. Also here [`cfront 3.0.3`](http://www.softwarepreservation.org/projects/c_plus_plus/cfront/). – Jonathan Leffler Apr 13 '13 at 22:05
  • I wrote an article ages ago about how to implement an OO language in C that may be of interest: http://orangejuiceliberationfront.com/runtime-time/ It also links to source code. – uliwitness Nov 11 '14 at 18:14

5 Answers5

5

You can try cfront. You can download old versions here. But it only supports a very limited C++ subset. Some features like exceptions can not be implemented this way.

Update: As Maxim Yegorushkin noted Exceptions might be implemented with setjmp/longjmp. But if I remember correctly exceptions can not be implemented as an library in C++. They have to be part of the core language.

Jan Herrmann
  • 2,717
  • 17
  • 21
  • _exceptions can not be implemented_ is not true. Early implementations used `setjmp`/`longjmp` for implementing exceptions. This method unwinds the stack while it is looking for an exception handler. Modern compilers first search for the exception handler and if one is found only then they unwind. This is the reason why the C++ standard says that it is implementation dependent whether the stack is unwound on an unhandled exception. Obviously, the new behaviour is more convenient for the user since the objects have not been destroyed when you look into the core dump. – Maxim Egorushkin Apr 12 '13 at 13:26
2

If you just want to see how C++ can be translated in C then you have several options available to you and the C++ FAQ has a section that covers this here. It cover all the major options I have ever seen suggested and should be updated when new options are available.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

Comeau compiler works this way. In its heyday everyone was praising its standard compliance and used Comeau online to test snippets of code, but few were using it for building production codes.

EDGE frontend also works this way. I hear it is used by both Intel C++ compiler and Comeau.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
1

The first Microsoft C++ compiler was exactly as you describe, I know because I remember using it. From my memory, I think it was version 7 of their C compiler. This would have been around 1992 (plus/minus 2 years).

Update: see http://en.wikipedia.org/wiki/Visual_C%2B%2B, the one that I'm referring to was indeed released in 1992 and called "C/C++ 7.0"

Stochastically
  • 7,616
  • 5
  • 30
  • 58
0

That's how the original implementation of C++, called cfront, worked. It was a front end to a C compiler. That's not so common these days, although EDG's C++ front end can be configured with a C backend so that it generates C code. That's useful when you're getting a new compiler running, but in a production compiler it's a wasted step, so commercial compilers typically go directly to the target platform's preferred form. And you really don't want to have to debug your program by reading the generated C code. I haven't done this for C++, but when I worked at Dinkumware we implemented the Java core libraries in conjunction with EDG's developing Java fronted. The C code is logically structured, but it looks a lot like LISP: Lots of Infernal Stupid Parentheses.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165