Using GCC's Nested Functions with Wide Pointers and No Trampolines II

(uecker.codeberg.page)

33 points | by uecker 4 hours ago

3 comments

  • mbeavitt 20 minutes ago
    Why would someone want to use a nested function, practically speaking?
    • anta40 6 minutes ago
      Say to strictly enforce modularity, e.g helper functions that can only be accessed within its function.

      Pascal supports it (at least Turbo Pascal, no idea about ISO Pascal).

    • kloop 15 minutes ago
      So that you can name a section of code without polluting the namespace.
  • tpoacher 28 minutes ago
    What's a "trampoline"?
    • mananaysiempre 12 minutes ago
      Could be a number of things depending on context. In this case it’s a short function that adjusts some things and jumps to the actual functions (a “thunk” is another term for this). Specifically, if in GCC you write

        int f(int x) {
            int g(int y) { ... use x and y ... }
            ...
            h(&g);
            ...
        }
      
      then what the compiled code for f does is construct on the stack a short piece of machine code:

        mov <well-known register>, <frame pointer>
        jmp <start of g’s code>
      
      and &g points to the start not of g’s code but of this snippet on the stack, which has the parent function’s frame pointer compiled into it as a literal constant. The snippet is called a trampoline.
    • dgellow 11 minutes ago
    • monster_truck 21 minutes ago
      It's where you jump and then get immediately bounced back. Basically GOTOs with params
  • mananaysiempre 1 hour ago
    What about your older patch where -fno-trampolines meant a function pointer could either be a code pointer or a closure (descriptor) pointer, distinguished by a tag?
    • uecker 30 minutes ago
      My old patch from 2018? This was not accepted to GCC because it relied on function pointers being aligned and there were concerns with this.

      But I prefer this approach anyhow, as it does not impose any run-time cost for checking the tag, and is easier to optimize.