The basic outline of how Picoc interprets the code can be described briefly as follows:
1. It first Allocates enough space on heap for loading given program in memory.
2. It then tokenizes the given source code into different categories such as variables,
function declarations, function definitions etc.
3. It then parses the given source code as per the language grammar to find out if there
are any early stage syntax errors.
4. While parsing the source code it parses one statement at a time, and identifies dif-
ferent types of tokens in given statement.It also associates proper semantic meaning
to those tokens.
5. If while parsing a given statement, it is found that a call to external function is
made then definition of that function is first searched in local as well as global list
of built-in function libraries.
6. Once that function definition is found, a wrapper for that particular library function
is called. After some prepossessing, a standard library call inside that wrapper then
does the rest of work and returns back. Hence control of flow goes back to interpreter
parser engine again.
For example, if a call to printf is there in the source of given program then in first
step Picoc tokenizes it as Cprintf. In second step it is associated with semantic meaning
of function call to it. In third step actual definitions of Cprintf is searched in an list of
by default included library files. Here in this case this is in stdio.c. So a call to Cprintf
function from stdio.c will be replaced in place of printf. The implementation of Cprintf
will then make a call to standard library version of Printf.
Any intermediate stages of interpreter processing involves many crucial functionalities
such as tokenization, symbol generation, semantic linking etc. These all functionalities
make use of many of the standard string processing functions such as strtok, strlen,
strncmp etc. But currently when we build xv6 kernel these functions are not there in it’s
standard C library. Hence firstly we have implemented all of these functions specific to
xv6
Right now Picoc can only be build using GCC compiler bundle. As xv6 doesn’t have
that right now we have used following approach for building this interpreter.
1. First Picoc is built by giving -C option to GCC in standard Linux environment.
This option creates relocatable object file of final executable.
2. This relocatable executable of Picoc is then linked with the standard library of xv6.
So this arrangement makes sure that all the relocatable files are produced by GCC
but are finally linked against library provided in xv6.
3. While linking these relocatable files against xv6’s standard library all the function
definitions that are required or functions which are called by Picoc but whose defi-
nition is not found in current library have to be added to current library.