udem-dlteam / pnut

🥜 A Self-Compiling C Transpiler Targeting Human-Readable POSIX Shell
https://pnut.sh
BSD 2-Clause "Simplified" License
424 stars 14 forks source link

Preparing ACM artifact #82

Closed laurenthuberdeau closed 2 months ago

monnier commented 2 months ago
  • Add pnut mode to do only expansion of #include

I must be missing something: what could it do other than expansion?

laurenthuberdeau commented 2 months ago

Add pnut mode to do only expansion of #include

I must be missing something: what could it do other than expansion?

I didn't have enough space to give a meaningful title to the commit but the description has more information:

Using the DEBUG_EXPAND_INCLUDES option makes Pnut read the input file, tokenize it (reading files included in the process), and then output the characters as they are read.

This gives a very accurate view of the total lines that are read by Pnut and allows us to compute the size of pnut.c for different backends.

As an example, giving the following program to Pnut compiled with DEBUG_EXPAND_INCLUDES:

// Some comment
#include "file.c"

// And now we include another file, this time in a #if block
#if 1
#include "other_file.c"
#endif

#if 0
#include "conditional_file.c"
#endif

void main() {
...
}

will produce:

// Some comment
#include "file.c" // INCLUDED
[Content of file.c]

// And now we include another file, this time in a #if block
#if 1
#include "other_file.c" // INCLUDED
[Content of file.c]
#endif

#if 0
#include "conditional_file.c"
#endif

void main() {
...
}

And then by removing the lines ending in // INCLUDED, we get a valid C file where active #include directives are replaced by the content of the file they include.

This is used by the analysis/measure-file-size.sh script to compute the size of the source code used by the different pnut compilers we have (pnut-sh, pnut-exe):

➜  ./analysis/measure-file-size.sh
########## pnut-sh ##########
By file:
    3251   10551   81401 pnut.c
    2510   12257   99333 sh.c
     918    4004   36919 sh-runtime.c
    6679   26812  217653 total
By file (without comments or blank lines):
    2443    7656   63766 build/measure/pnut.c
    1950    8658   77587 build/measure/sh.c
     861    3802   35480 build/measure/sh-runtime.c
    5254   20116  176833 total
Expanded includes:
    6679   26808  217615 build/measure/pnut-sh.c
    7171   31257  230897 build/measure/pnut-sh.sh
Ratio (Original): 7171/6679 = 1.073
Ratio (Cleaned):  7171/5254 = 1.364

########## pnut-i386_linux ##########
By file:
    3251   10551   81401 pnut.c
     707    3181   23341 x86.c
    2213    7884   65691 exe.c
     103     564    4896 elf.c
    6274   22180  175329 total
By file (without comments or blank lines):
    2443    7656   63766 build/measure/pnut.c
     441    1158    9348 build/measure/x86.c
    1766    5941   54394 build/measure/exe.c
      80     188    2125 build/measure/elf.c
    4730   14943  129633 total
Expanded includes:
    6274   22174  175280 build/measure/pnut-i386_linux.c
    6131   25368  178213 build/measure/pnut-i386_linux.sh
Ratio (Original): 6131/6274 = .977
Ratio (Cleaned):  6131/4730 = 1.296