Related articles |
---|
A novice in compiler construction wants feedback programmer@telia.com (Marcus Johansson) (2009-09-03) |
Re: A novice in compiler construction wants feedback derekrss@yahoo.ca (Derek) (2009-09-04) |
Re: A novice in compiler construction wants feedback programmer@telia.com (Marcus Johansson) (2009-09-05) |
Re: A novice in compiler construction wants feedback bc@freeuk.com (Bart) (2009-09-05) |
From: | "Marcus Johansson" <programmer@telia.com> |
Newsgroups: | comp.compilers |
Date: | Sat, 5 Sep 2009 10:35:12 +0200 |
Organization: | Compilers Central |
Keywords: | design |
Posted-Date: | 05 Sep 2009 13:31:11 EDT |
>> But why am I writing? I guess I simply want some feedback, since I have
>> no one to discuss this kind of work with. The compiler can be downloaded
>The problem is that you've not published the source code to the
>compiler itself and hence we can only comment on whether it works or
>not.
The source? That would probably cause permanent mass laughter. But
there are a couple of things that I'm really curious of. Please have
understanding if I'm naive.
My intepreter is written in C. It has got 8 registers and a stack. They're
of a union type, something like:
typedef union {
int i;
float f;
DString *s;
Array *a;
}AnyType;
The compiled instructions are loaded into an array of, let's say:
typedef struct {
int command;
int dst;
int src;
} Instruction;
I've set up an array with function pointers matching the command values of
the instructions:
void CMD_MOVE_RR(void);
void CMD_MOVE_RC(void);
...
gCommands[MOVE_RR] = CMD_MOVE_RR;
gCommands[MOVE_RC] = CMD_MOVE_RC;
...
I then interpret the program with:
while (gProgramRunning) {
gCommands[gInstructions[gCurrentInstruction].command]();
gCurrentInstruction++;
}
Is this the usual way of doing it? And, above all, is there a smarter and
faster way of interpreting the instructions? Can I somehow get rid of all
those function calls above?
/Marcus
[That seems like an entirely reasonable way of doing a stack-based
interpreter. There are faster ways of doing interpreter dispatch, but
most are pretty kludgy. One thing you could do at the cost of
readability is to move the various commands into one routine and use a
switch to dispatch, since function calls tend to be slow, e.g.:
while (gProgramRunning) {
switch(gInstructions[gCurrentInstruction].command) {
MOVE_RR: do something ; break;
MOVE_RC: do somethng else; break;
...
}
gCurrentInstruction++;
}
We were all novices one time, your code isn't likely to
be any worse than our early attempts. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.