Demystifying C++

From emmtrix Wiki
Jump to navigation Jump to search
This page contains changes which are not marked for translation.
Other languages:
Demystifying C++

In the world of programming, there is a constant evolution and adaptation of languages and tools to meet the ever-growing demands of software development. While C++ is considered one of the most powerful and versatile programming languages, its complexity can often be intimidating and challenging. In this article, we take a look behind the scenes and demystify the sometimes complex C++ constructs through the lens of a C++ to C transpiler. Whether you are an experienced developer or someone just delving into the depths of C++, this article offers fascinating insights into the machinery that drives this powerful language.

C++ to C Transpiler

A transpiler, also known as a source-to-source compiler, is a special tool that translates the source code of one programming language into the source code of another programming language. Unlike traditional compilers that translate source code into machine code, transpilers convert code from one high-level language to another.

At emmtrix, a C++ to C transpiler is currently under development [1]. It is based on the tried-and-true clang frontend, a state-of-the-art compiler frontend that is part of the broader LLVM project. A standout feature is ensuring semantic equivalence between the original C++ code and the generated C code. This means that despite the differences between the two languages, the translated code exhibits the same behavior as the original code.

To ensure semantic equivalence, a comparison tool is used. This tool translates both the original C++ code and the generated C code into the LLVM IR (Intermediate Representation) and compares these two representations. Through this comparison, it can be ensured that the generated code not only produces the same output in test cases but also that semantically identical code is produced on every run (or an error is displayed). This approach is recognized as a method for ensuring error-free operation in both ISO26262 and DO-178C.

The transpiler supports all C++14 language features as well as common GCC/Clang language extensions. Only exceptions are not fully supported because they cannot be replicated in C on a 1-to-1 basis. In addition to the C++ standard, the Itanium C++ ABI [2] is used, which specifies the binary interface (i.e., application binary interface, ABI), for example, how classes with inheritance are implemented.

Within the article, the transpiler is used to demystify C++ because the C output makes the implementation of the C++ constructs by the compiler easily understandable.

C++ Construct

Ideas for Future

  • Member function pointers
  • Array new and delete
  • Exceptions
  • Direct call to `operator =` vs. `=` operator and parameter evaluation order