Question

Suppose that the purpose of an assignment is to write a compiler that works on a subset of C language (you can assume a subset of whatever language, just supporting basic scripting expressiveness without having complex things as objects).

What kind of intermediate code could be used to verify the correctness of the compiler? I was talking with a professor and he spoke about the fact that he didn't know what to give to his students as the VM to be used for the "compiled code" so I wondered which could be a good solution.

Subset of C -> Compiler -> Code? -> VM

in which code could be either in binary format or better in an ASCII format (something like pseudo-asm).

I'm looking for something already made, not how to structure this intermediate code and the VM, just an easy and simple one ready to be used to test some compiled programs..

Was it helpful?

Solution

You could describe some abstract machine design and then provide it an instruction set in list-format. I small LISP parser is a nobrainer in parsers.

(label add-two)
(init-stack-frame 2)
(load r1 0)
(load r2 1)
(add val r1 r2)
(goto cont)

Also, writing a lisp interpreter to read this in is a nobrainer.

load_labels (index, expr, env)
    if expr.first == 'label'
        env.set(expr.second, index)

interpret (machine, expr, env)
    return env.lookup(expr.first).eval(machine, expr.tail)

OTHER TIPS

You can find many examples of intermediate code/bytecode in existing VMs. Depending on your definition, they may or may not be simple. Examples:

How about compiling to a scripting language (e.g. JavaScript)? It's human-readable and already made.

How about targeting the Java Virtual Machine? Not sure how simple it is but it is very well documented, so if the students where curious, they could head over to amazon.com and get a book about what the intermediate code actually means and how the vm works.

You could also just create real 80x86 or 68000 assembly, use an assembler to get machine code and then use an emulator to run it. Real hardware doesn't strike me as more complicated than some made-up VM if you're already gone through writing a compiler and it has tons of debuggers and other utilities available already.

But I do like the LISP suggestion :-)

How about llvm?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top