HN.zip

A Partial Evaluator for C

64 points by - 9 comments
[3 hidden]5 mins 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?
[3 hidden]5 mins ago
Yes, ++fmt increments the counter before it's read, so it'll point at the null terminator and enter the default case
[3 hidden]5 mins 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.

[3 hidden]5 mins 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.
[3 hidden]5 mins ago
While unsuccessfully searching for the Tempo source code I found LLPE, a partial evaluator for LLVM bitcode: https://www.llpe.org/
[3 hidden]5 mins 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...

[3 hidden]5 mins ago
GCC does quite a bit of this nowadays. It is quite impressive to see when you disassemble its output.
[3 hidden]5 mins ago
AFAIK, GCC toolchain does not have a separate partial evaluator, so how do you do it?
[3 hidden]5 mins 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...