From: | "Tommy Thorn" <tommy.thorn@gmail.com> |
Newsgroups: | comp.compilers |
Date: | 13 Nov 2006 16:34:31 -0500 |
Organization: | Compilers Central |
References: | 06-11-052 |
Keywords: | interpreter, performance |
Posted-Date: | 13 Nov 2006 16:34:31 EST |
PAolo wrote:
> I am writing a piece of software that accept in input the definition
> of a mathematical function and a list of points in which evaluate the
> function and puts the result in output. The main feature of the progam
> is that once the function definition is read, the program evaluates
> the function very fast
You may want to check out previous posts. My example in
http://compilers.iecc.com/comparch/article/06-09-093 does exactly
that. If the native code generation is too much, you can trivially
replace it with threaded code for a loss in performance.
> [Well this isn't done already...]. I was thinking to build a pair
> of stacks, one containig the operands (floats) and the other
> containig the operators (function pointers).
Why are you ignoring 50+ years of compiler research and teaching? What
you want to do is
1. parse and analyse the source.
2. generate code.
3. run code.
For the most trivial examples, you can do 1 and 2 (sometimes even 3)
together. In my example (quite standard btw),
1. parses the source to a tree, rewriting it on the fly.
2. traverses the tree to generate the code.
3. the host processor execute the code for me.
In your case, you may instead want to generate a code for simple stack
machine and in step 3 run the interpreter for that. Something like:
double run(int pc) {
double stack[STACKSIZE];
int sp = 0;
for (;;) switch (program[pc++]) {
case PUSH_CONST:
stack[sp++] = *(double *)stack + pc;
pc += sizeof (double);
break;
case ADD: stack[sp-2] = stack[sp-2] + stack[sp-1]; sp--; break;
case MUL: stack[sp-2] = stack[sp-2] * stack[sp-1]; sp--; break;
....
with lots of blanks to fill in.
Tommy
Return to the
comp.compilers page.
Search the
comp.compilers archives again.