A Partial Evaluator for C
100 points by measurablefunc 19 hours ago | 28 comments
  • RossBencina 17 hours ago |
    While unsuccessfully searching for the Tempo source code I found LLPE, a partial evaluator for LLVM bitcode: https://www.llpe.org/
  • dmitrygr 15 hours ago |
    GCC does quite a bit of this nowadays. It is quite impressive to see when you disassemble its output.
    • rramadass 14 hours ago |
      AFAIK, GCC toolchain does not have a separate partial evaluator, so how do you do it?
      • pjmlp 14 hours ago |
        GDB, calling C code from the interactive prompt?

        Even though it is its own project, most folks use it alongside GCC.

        https://sourceware.org/gdb/current/onlinedocs/gdb.html/Calli...

        • rramadass 13 hours ago |
          That is not Partial Evaluation but interactive REPL evaluation. See also https://news.ycombinator.com/item?id=50609165

          For Partial Evaluation, you must generate a "residual" version of the original program by specialization where all static data is already bound and evaluated.

          Simplest example would be compiling in configuration parameters, so a specific code-path is preselected before runtime. Another classic example would be C++ template meta-programming where that part of the program is not evaluated at runtime.

          I first came to know of these techniques from the classic Generative Programming: Methods, Tools, and Applications by Krzysztof Czarnecki and Ulrich W. Eisenecker which is still relevant today.

          Checking with Google, it seems GCC does not have a separate partial evaluator but one can use coding techniques+aggressive optimization switches to approximate it. For separate "Futamura Projection" like partial evaluators there seem to be C-Mix/LLPE etc.

      • 1718627440 12 hours ago |
        Isn't inlining followed by constant propagation an example of applying a partial evaluator?
        • dmitrygr 7 hours ago |
          That’s basically how it works. And the passes are applied repeatedly
          • 1718627440 6 hours ago |
            Thanks. The rogative aspect was whether the statements truthiness isn't sufficient indication to support your statement, not whether my statement itself is true.
        • rramadass 5 hours ago |
          Just FYI - There is lots more that GCC can apparently do w.r.t. partial evaluation. Google AI mode gave me 1) Code level constructs 2) Compile time switches and 3) Runtime partial evaluation using "libgccjit"!
          • 1718627440 10 minutes ago |
            I honestly don't understand your first two points, and I think the third point is something different, as it is a deliberate by the programmer and also it just invokes the compiler at runtime completely, meaning it does not partially compile it at actual compile time and only parametrize it at runtime. Or am I wrong on the last point?
  • MiroslavPokorny 14 hours ago |
    > The Tempo specializer has been made publicly available under a licence in April 1998. At of March 99, it was being used by about thirty people around the world. The binary version of Tempo has just (July 2000) been made available without any restrictions, and we expect a full unrestricted source release under GPL to follow soon.
  • rramadass 14 hours ago |
    Partial Evaluation - https://en.wikipedia.org/wiki/Partial_evaluation

    Partial Evaluation and Automatic Program Generation by Neil Jones et al. - https://web.archive.org/web/20071012081310/http://www.itu.dk...

  • looperhacks 14 hours ago |
    I know this has nothing to do with TFA, just want to check my understanding of C. Doesn't the code example have a bug?

      miniprintf(char fmt[], int val[])
      {
        int i = 0;
        while( *fmt != '\0' ) {
          if( *fmt != '%' )
             putchar(*fmt);
          else
            switch(*++fmt) {
              case 'd' : putint(val[i++]); break;             
              case '%' : putchar('%');     break;
              default  : prterror(*fmt);   break;
            }
          fmt++;
        }
      }
    
    If the static string was "%", the else branch would move past the end of the string. Assuming prterror doesn't crash the program, this reads out-of-bounds, right?
    • carlosneves 14 hours ago |
      Was suspecting the same thing. But I think, because C strings are null-terminated, they allow reading up to 1 byte after its end.

      In your example, "%" would cause switch ('\0') which would fall in the default case.

      • ginko 14 hours ago |
        Yeah, but that only prints an error and keeps going, so next loop it will read past the null terminator. So I think looperhacks is correct.

        The function should probably return on error.

    • ChrisRR 14 hours ago |
      ++fmt increments the counter before it's read, so it'll point at the null terminator and enter the default case.

      Edit: I guess it may not technically be a bug as it's just a small function, but using a single % instead of %% (which is what printf expects) would be a format error, hence prterror()

      • RicardoLuis0 9 hours ago |
        but then it's incremented again after the else, and the while condition reads one-past-the end, entirely skipping the null terminator
        • ChrisRR 8 hours ago |
          I guess it's the embedded engineer in me that stupidly assumed that prterror() would've been a fatal error handler and so wouldn't continue to run after that.

          If it's not a fatal error, then yes it would continue past the end of the string

    • dcuthbertson 13 hours ago |
      Yup, it's a bug. The '%' character gets you into the while loop and into the else clause. The switch increments to the nul terminator, which then executes prterror(0). Then fmt is incremented again so it points just outside of the string. As long as that out-of-bounds byte is not also the nul value the loop continues into out-of-bounds territory.

      Wait - does prterror cause the program to exit? It's not defined, so maybe the program does terminate, maybe it doesn't.

    • oguz-ismail2 13 hours ago |
      Right. If fmt was "%d%d" and val had only one element that'd cause an out-of-bounds read as well. Can't say it's a bug without seeing the rest of the code though; if the function is used correctly everywhere, there's no bug.
      • tgv 12 hours ago |
        > there's no bug

        That's a bit of an ontological conundrum: does a bug exist when it never occurs? I'd say there is a bug, unless the function has a contract that guarantees there's always a character following every first % (and something about the length of the second array, of course). So I see a bug as something wrong with the design.

        But if you see a bug as something purely pertaining to runtime, then you are right. But when is "never"? If it is proven not to occur, or is it enough that the circumstances in which it is used just happen no to trigger it? (Note that in the last phrase, "it" refers to the bug, and implies it exists...)

        BTW, in the example, the partial evaluator could prove the presence or absence of bugs.

    • reillyrider 12 hours ago |
      Format strings in C are generally assumed to be valid, so it's a precondition to use the function.
    • 1718627440 12 hours ago |
      That's kinda what you get for writing non-idiomatic terse code in a language with footguns. There are no braces around an else branch, that spans multiple lines. It uses a while loop instead of a for loop, meaning that the check gets separated from the increment and thus also a second unchecked increment sneaked in.
    • rramadass 4 hours ago |
      Note that C/C++ explicitly allows you to form a pointer to one past the last element of a string/array. You can compare with it but you cannot dereference it.

      It is used extensively in STL algorithms so that raw pointers and container.end() behave the same.

  • hizlikovboy27 13 hours ago |
    Does anyone know if there's a modern build of it? the download on the page is an i386 Linux binary tested on RedHat 6.1
  • alex7o 12 hours ago |
    We need a partial evaluator for llvm bytecode imo so it works with all languages
    • Harvesterify 12 hours ago |
      • alex7o 7 hours ago |
        Oh ty I didn't know about it