‹ BackHN Continuity

Thread

C++ Insights – See your source code with the eyes of a Compiler

164 points · 35 comments · rramadass

Loading the complete thread in the background. This saved snapshot is available now. Refresh

  1. rramadass · · focus · HN ↗
    The tool can be found at - <a href="https:&#x2F;&#x2F;cppinsights.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;cppinsights.io&#x2F;
  2. [deleted] · · focus · HN ↗

    [deleted]

  3. StilesCrisis · · focus · HN ↗
    I wish the readme example showed a lambda capture. Now that&#x27;s where compiler magic REALLY shows up.
    1. mrlonglong · · focus · HN ↗
      I second this.
      1. rramadass · · focus · HN ↗
        <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49945197">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49945197
        1. mrlonglong · · focus · HN ↗
          Lambdas are just classes!? Never knew this.
    2. rramadass · · focus · HN ↗
      You can see it by typing in some sample code in the tool here - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49928362">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49928362
    3. kccqzy · · focus · HN ↗
      [delayed]
    4. WalterBright · · focus · HN ↗
      Lambdas in D are just nested functions:

          int foo(int i) {
              int add(int x) { return i + x; } &#x2F;&#x2F; nested function
              return add(3);
          }
      
          int bar(int i {
              alias lambda = (x) =&gt; x + i;  &#x2F;&#x2F; lambda
              return lambda(3);
          }
          }
    5. sltkr · · focus · HN ↗
      C++ lambdas are just structs that overload operator(); once you realize that they become a lot less mysterious. But it&#x27;s a great example of how this tool is useful to learn about those things (of course you could also read the C++ standard but who has time for that?)

      Here&#x27;s an example: <a href="https:&#x2F;&#x2F;cppinsights.io&#x2F;s&#x2F;f04b6896" rel="nofollow">https:&#x2F;&#x2F;cppinsights.io&#x2F;s&#x2F;f04b6896

      1. StilesCrisis · · focus · HN ↗
        I said shows a lambda _capture_. This involves the compiler automatically determining which variables cross the scope boundary, and injecting code for each one.
        1. frabert · · focus · HN ↗
          I&#x27;m curious to understand which part you think is particularly interesting. The compiler clearly knows which variables are declared in what scope, so what&#x27;s mysterious about figuring out which values need to be captured?
      2. Someone · · focus · HN ↗
        [delayed]
  4. alankarmisra · · focus · HN ↗
    Ooo I love these things. I wrote a C++ -&gt; Clang -&gt; JS transpiler so I could show value change states on top of the C++ source code in html and allow people to step through the whole state change. They can also vary the input and see the values change cause the JS layer could recompute the values - which is why I bothered with the transpiling - otherwise I could have just output the state changes and parsed them as CSV for the steps. Fun project!

    <a href="https:&#x2F;&#x2F;youtu.be&#x2F;ZPGysUX62OY" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;ZPGysUX62OY

    1. ranger_danger · · focus · HN ↗
      This is really cool. Is the source available?
  5. jsrcout · · focus · HN ↗
    Definitely trying this out. I&#x27;ve always had the problem of &quot;but what is the compiler thinking?&quot; with C++ in particular.
  6. IshKebab · · focus · HN ↗
    This is what let me understand lambdas. Great tool.
    1. drysine · · focus · HN ↗
      Good for C++&#x27;s coroutines too
      1. pjmlp · · focus · HN ↗
        It also helps to know the .NET model, as C++20 co-routines were inspired by how they work and related magic methods on awaitable types.
  7. f1shy · · focus · HN ↗
    I must be the only one who (while I like the tool) feel a little estrange, somewhere between sad, angry, annoyed and disappointed (can’t really express it), that a language needs (or at least is of good use) such a tool.

    Like: I wish what is the first interpretation of my code to be obvious. Is not about advanced optimization, or nitty gritty compiler tricks, is about the language. This tool is useful in any C++ compiler. How did we end with such a complex language?

    1. rs545837 · · focus · HN ↗
      I don&#x27;t think it&#x27;s only C++ though, Python decorators, Rust macros, almost every language&#x27;s compiler write code you never see. C++ just does more of it and makes it harder to guess. Being able to look at the expanded version is useful in any language that does this.
    2. kccqzy · · focus · HN ↗
      [delayed]
      1. f1shy · · focus · HN ↗
        &gt;&gt; needs (or at least is of good use) such a tool.
        1. drysine · · focus · HN ↗
          &gt;other languages doesn’t need such an explanation tool at all

          Are you sure about that?

        2. kccqzy · · focus · HN ↗
          [delayed]
    3. esikich · · focus · HN ↗
      In what language is compiling simple? Do you not like compiled languages? I&#x27;m not sure what you&#x27;re complaining about here.
      1. f1shy · · focus · HN ↗
        Lisp is simple to compile. Even C (at the level this tool works anyway), also basic, and maybe other 100 languages (again, not the whole compiler, but what this tool shows).

        Compilation per se is not difficult. Doing it with good error recovery, good feedback in case of error, and the optimization is difficult.

        This tool does not tell me those parts (for that is gotbolt) this is about how the first layer of interpretation works, and not for making compilers, but for understanding what the code actually means, because is not obvious at first sight.

        1. dasyatidprime · · focus · HN ↗
          I agree with an overall thrust of “C++ is overcomplicated and has many moving parts and a lot of implicit stuff happening in the background that makes it difficult to understand”. But I do want to briefly note that Lisp environments have macro expansion steppers for a reason too.
        2. pjmlp · · focus · HN ↗
          Only if you mean K&amp;R C or the early C89&#x2F;90 compilers, before the extension explosion, newer language standards, and modern ISAs.
    4. rramadass · · focus · HN ↗
      You have misunderstood the language and the tool.

      Compilers for all languages have the concept of &quot;lowering&quot; the level of source-language abstractions from high-level to low-level viz. Written Source -&gt; Simpler Source -&gt; AST -&gt; IR -&gt; Machine Code. One type is &quot;desugaring&quot; i.e. transforming complex syntax to simpler ones so that the following stages of compilation have to operate only on a smaller and hence more manageable language subset.

      This tool shows both desugaring and other lowering (eg. implicit transformations, type deductions etc.) within the C++ source language by regenerating the source from the AST thus giving you full visibility until that stage in the compilation process.

      PS: Explanatory video presentation by the author - <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=VJ6ZvDRYzNE&amp;t=558s" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=VJ6ZvDRYzNE&amp;t=558s

    5. jcelerier · · focus · HN ↗
      Which language do you think doesn&#x27;t need it? For instance for me I wish I had such a tool whenever I used any interpreted or JIT language, to understand clearly to which semantics whatever is happening maps and how their respective front-end optimizes. Things as simple as &quot;a&quot; + &quot;b&quot; in JavaScript: are you sure you know how every js runtime maps it? Do they fold the strings as an optimization during parsing ? Do they preallocate a larger native string type and push? Do they just do something that ends up an equivalent of std::u16string(&quot;a&quot;) + std:: u16string(&quot;b&quot;)?
  8. tweedler290 · · focus · HN ↗
    thats crazy. i made this <a href="https:&#x2F;&#x2F;github.com&#x2F;oooscoos&#x2F;Benzi" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;oooscoos&#x2F;Benzi which does the same thing but is language agnostic (supports 9 other languages). urs probs goes in deeper into c++ than mine tho
    1. abbefaria27 · · focus · HN ↗
      This is very different and doesn’t relate to what the linked repo does.
  9. kvemkon · · focus · HN ↗
    &gt; See your source code ...

    What I&#x27;d like to see, is how many time is spent to compile each block of C++ code (sliced as fine as possible).

  10. veexx103 · · focus · HN ↗
    I’ve worked on something similar, but the code reconstructed from the Clang AST fails to compile due to various issues.

    Can these issues be resolved?

  11. kizxc4395 · · focus · HN ↗
    This is so interesting
  12. jruohonen · · focus · HN ↗
    The online interface was buried somewhere there, but that is pretty nice for educational purposes:

    <a href="https:&#x2F;&#x2F;cppinsights.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;cppinsights.io&#x2F;

  13. aktenlage · · focus · HN ↗
    Wow, that&#x27;s awesome. I&#x27;ve been writing C++ since 20 years, and I still stumble over details where I ask myself what exactly is happening. Looking at the machine code is often quite opaque to me, and so is the reference, because it is overly formal. So this will be a great alternative to get a quick understanding of a concrete snippet. Thank you!
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.