code – Creative Programming https://alllinuxdevices.com What a programmer needs to know Wed, 28 Apr 2021 12:15:45 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://alllinuxdevices.com/wp-content/uploads/2021/04/1bde620701d143c0b006dfd1f5bfb1f8-100x100.png code – Creative Programming https://alllinuxdevices.com 32 32 Analysis of programs and compilers in Compiler Explorer https://alllinuxdevices.com/2021/04/27/main-reasons-to-explain-fast-business-builder/ Tue, 27 Apr 2021 05:35:41 +0000 http://localhost/themedev/?p=67 If you have been programming for a long time, you probably were engaged in disassembling the code and asked the question "Why did the compiler optimize the code in one case, but not in the other?"

The post Analysis of programs and compilers in Compiler Explorer appeared first on Creative Programming.

]]>
If you have been programming for a long time, you probably were engaged in disassembling the code and asked the question “Why did the compiler optimize the code in one case, but not in the other?” Usually, to answer a question, you need to do a little research with small pieces of programs. Sometimes you need to compare the results of several compilers with different optimization flags.

If you teach computer disciplines, then it is very useful to bring the assembler code. For example, you told students about how variables are created “on the stack” or how the stack unwinds when an exception is thrown. It is worth showing that not always arguments are passed through the stack, which means that the stack unwinding mechanism must take this into account for each function. This can be done using the following program:

int bar (int a, int b) {
return a + b;
}

I would like to collect it with various optimization flags and compare the results. A very handy tool for this godbolt.

Here you can see that in this online environment you can:

  • enter pieces of code (not necessarily the whole program). And get an assembly view for them;
  • select a compiler (I chose gcc10.2) and set compilation options;
  • select several compilers at the same time – it is convenient to compare the results;
  • enter the program code in different languages. Supported by Pascal, Python and a sea of ​​less popular languages ​​like D and Haskell.
  • simultaneously enter several fragments of source code, for each of which add their own compilers. Sometimes this is convenient – for example, to show how the code optimizer can take into account constants or something like noexcept
  • in C ++.

If there is an entry point in the code (in C ++, the main
) – then you can see the result of the code execution. In this, this online environment is not much different from other online compilers, except for the ability to connect popular libraries such as boost, Google Benchmark, Google Test, Doctest, and so on to the project.

In addition, the results obtained can be shared (there is a Share button), while you share not only the source, but also the configured compilers. If you are inserting results from godbolt into your presentation, then you should add links there as well – this is convenient.

The post Analysis of programs and compilers in Compiler Explorer appeared first on Creative Programming.

]]>