5

What in the holy hell is a pointer to function and function pointer.I guess they have a purpose, I hate their existence. It's solely because it is complicated

Comments
  • 5
    They aren’t so bad.
  • 8
    They're necessary for implementing callbacks, closures, vtables, coroutines, threads, runtime incrementally optimized functions (think JIT compilers), and other low level hackery.

    I mean it's literally just like any other pointer.
  • 6
    What is a function pointer?
    `jmp (eax)`
  • 3
    Any function is basically just a pointer to a region in memory where the function starts. That's it.

    Pointers are essentially as complicated as you make them to be while really being a very simple concept.
  • 1
    @RememberMe do you know resources for learning about jit?
  • 3
    @iiii I use LLVM for stuff sometimes. Found that the standard tutorial (kaleidoscope) includes using the inbuilt JIT compiler (it's literally like 5 extra lines or something). You can play around with that. I also built a basic lisp interpreter that had an incredibly crap JIT compiler. Didn't use any resource as such, you just write your compiled binary output to a page (I just used clang to compile the C output of my lisp compiler...like I said, crap), mark it as executable, and jump to that pointer, literally all there is to it.

    I believe LuaJIT is a relatively simple thing you can look at too.
  • 1
    @RememberMe I've heard LLVM being pretty slow to compile on the fly 🤔
  • 4
    @iiii yeah I don't think anyone uses LLVM for JIT, it's just there for convenience in case you want to hot reload and stuff. A good JIT compiler is intimately tied to a good runtime system, and LLVM does not provide a runtime system, that's not in its scope.

    Making a good one is really really tricky. I'd say JavaScript engines do it best, along with JVMs and the CLR. Not something I know much about though.
  • 0
    You people won't let me rant peacefully 😂. I know their importance, it's just a rant. That's what I told in my initial rant. I guess I am that guy who just complains about everything, no matter what 🥱
  • 1
    Your program is just data that happens to correspond to CPU instructions.

    A pointer to a function is just that - a pointer to some of that data. Like a pointer to a variable or whatnot.

    What's confusing about it?
  • 0
    @RememberMe Definitely use LLVM for JIT. Check out Terra - we use it there for JIT.
  • 0
    @junon I meant using it as is. LLVM's JIT isn't terribly fast from what I remember, and you have to build tracing and on-the-fly optimization etc. on top of it anyway, that's the actually hard part of JIT. I could imagine it being used just for the compile step.
  • 0
    @RememberMe What do you mean used "as-is"?
Add Comment