Thesis Threading

I’m doing the finishing strokes on my diploma thesis these days. I won’t do much implementation anymore, only some testing, benchmarking and writing. So far, the benchmarks (Dacapo) show a 40% improvement in performance compared to when I started, which is significantly better than I had hoped in the first place.

One annoyance is left though. The implementation of threaded dispatching uses GCC specific syntax extension for C, namely the ‘label pointers’ (&&label) and ‘goto *address ‘ things. Unfortunately, JamaicaVM has also ports for WindowsCE and OS9, where we don’t use GCC, but Microsoft C Compiler and some OS9 only thingy (Ultra-C). I don’t know of any way to implement threading in ANSI-C, any ideas are very much appreciated. So if you know of any hacks to implement threaded dispatching on these platforms/compilers, or even in ANSI-C, please tell me!

Advertisements

5 Responses to Thesis Threading

  1. One interesting technique I’ve come across is the use of continuation-passing style. If your compiler performs good tail-call optimization, then I think it becomes equivalent to the label-jumping technique.

    typedef void* Word;
    typedef void (*Instr)(Word* pc);

    static int acc;

    static void Add(Word* pc) {
    acc += (int) *pc++;
    ((Instr)(*pc))(pc+1);
    }

    static void Print(Word* pc) {
    printf(“Accumulator = %d\n”, acc);
    ((Instr)(*pc))(pc+1);
    }

    static void Halt(Word* pc) { exit(0); }

    int main(int argc, char** argv) {
    Word* pc;

    Word program[] = {
    (Word) &Add, (Word)3,
    (Word) &Print,
    (Word) &Add, (Word)5,
    (Word) &Print,
    (Word) &Halt,
    };

    acc = 0; // the “acc” register is a global variable
    pc = &program[0]; // the “pc” register is threaded through calls

    ((Instr)(*pc))(pc+1); // Start!

    printf(“Error!\n”); return 1;
    }

    Unfortunately, I can’t seem to get GCC to generate good code for this. I’ve tried using the “noreturn” attribute but GCC still puts in a lot of unnecessary code. Would be interesting to benchmark this against the standard switch-based interpreter.

  2. roman says:

    Yeah this is interesting. I have to try this on the Microsoft Compiler. This one has a concept of ‘naked’ functions, that is without the leading and trailing function boilerplate code. My guess is that this could work pretty well.

  3. Can’t you provide the functionality with some inline assembly?

  4. roman says:

    Robert, I could, but I would like to avoid it for portability’s sake. We support a couple of different platforms and inserting assembly seems like a maintenance nightmare.

  5. Pingback: This note’s for you » Blog Archive » Threading in ANSI-C

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: