Monday, October 30, 2006

A simple pascal compiler

The subject Compiler's Principle, the professor asked me to build a simple pascal's compiler, from tokenlization to code generation, except the optimization part, to earn the full scores in experiment. and I did, that doesn't spend me too much time, all codes I wrote manually, no any connections with yacc,lex,flex,bision,antlr, and all this sorts of tools.

After all the experiments are done, I may open the source of the compiler, that's not complex, and that's a good tutorial for the beginners.

May be the professor will ask me to build a coressponding virtual machine to run the generated code. that'll be a big problem :(
_________________________________________________________________
The architecture's description (Without the instruction set):
Architecture of the Virtual Machine:

The VM is a stack-based structure, have no any general registers, all the operation are done via stack operations.
Inner registers(inaccessible in the the assembly):
Instruction Pointer Register
Return value Register
Call Stack Register
Stack Frame Register

The VM's architecture depends on two sorts of stack, one is Call Stack, element of the stack is StackFrame,
and the Stack Frame contains the second stack, the operation stack.
That's, whatevery what's in the current stack, it will not be able to affect the next invokation.


VM Context will contains:
Call Stack - Stores all stack frames, when the size is 0, the program will end.
Registers - Including the instruction pointer register, return value register
Code Segment- Points to the actual code in the memory.


Stack Frame will contains:
Procedure Configure Block - Configure the resources used by the current procedure
Instruction Pointer - Used to store temporarily IP when a new stack frame pushed into the call stack
Parameters - The caller will put the top data in the stack into this place, the number of parameters is specified by the PCB.
Data Stack - The data stack, size will be specified by the PCB
Local Variants - Same as Parameters, but it will not copy top datas in the caller's stack

The definition of return value: A running procedure, the top data in the stack will be sent to the RV register

Procedure Configure Block
Stack Configure:
maxstack n 16 bits, the maximum of stack (can be ignored in the VM, only needed while optimization and JIT generation used in high performance)
arguments n 16 bits, the count of parameters of current procedure
locals n 16bits, the count of local variants.

1 comment:

Anonymous said...

Can we have the source of this ? please