Re: Source to source compiling

toconnor@vcd.hp.com (Thomas O'Connor)
Sun, 18 Sep 1994 04:34:16 GMT

          From comp.compilers

Related articles
Source to source compiling casper@snakemail.hut.fi (Casper Gripenberg) (1994-09-11)
Re: Source to source compiling eanders+@CMU.EDU (Eric A. Anderson) (1994-09-12)
Re: Source to source compiling norman@flaubert.bellcore.com (1994-09-14)
Re: Source to source compiling mabp@bga.com (1994-09-13)
Re: Source to source compiling f1rederc@iia.org (1994-09-13)
Re: source to source compiling ghiya@vani.cs.mcgill.ca (1994-09-18)
Re: Source to source compiling bernecky@eecg.toronto.edu (Robert Bernecky) (1994-09-18)
Re: Source to source compiling toconnor@vcd.hp.com (1994-09-18)
Re: Source to source compiling pjj@cs.man.ac.uk (1994-09-16)
Re: Source to source compiling rgg@tidos.tid.es (1994-09-19)
Re: Source to source compiling lexa@adam.botik.yaroslavl.su (1994-09-22)
Re: Source to source compiling md@pact.srf.ac.uk (1994-09-22)
| List of all articles for this month |
Newsgroups: comp.compilers
From: toconnor@vcd.hp.com (Thomas O'Connor)
Keywords: optimize, translator
Organization: Hewlett-Packard VCD
References: 94-09-031
Date: Sun, 18 Sep 1994 04:34:16 GMT

Casper Gripenberg (casper@snakemail.hut.fi) wrote:
: I am currently working on a source to source compiler. The source
: language is a kind of state langage. A program containes 1-n states. All
: states have the same kind of structure, ie. in the beginning you initilaze
: variables, then comes some code to execute and at last a bunch of
: conditional jumps to other states.


: Example:


: STATE1:
: Do something;
: if (a) goto STATE3;
: if (b) goto STATE2;
: etc...


: STATE2:
: ....


: STATE3:
: ....


: The destination langage is very c-like. With while loops and if-then-else
: statements.


: Now the problem is that I don't know how to convert these conditional goto
: statements in the source language to while/if-then-else constructs in the
: destination language. How do I check for eg. loops, and presumably some of
: the goto statements just cannot be translated into if-them-else statements.


I have considered this problem to some extent, though I will admit
that I have yet to implemented it. In my case the translation language
looks very similar to yours, except that I add the concept of a task.
In other words, I have a state engine, which contains one or more
stage machines. Each state machine is identified by a particular task
name, each task consists of a set of one or more states. i.e.:


Task_1:
    STATE1:
    Do something;
    if (a) goto STATE3;
    if (b) goto STATE2;
    etc...


    STATE2:
    ....


    STATE3:
    ....


Task_2:
    STATE1:
    Do something else;
    if (a) goto STATE3;
    if (b) goto STATE2;
    etc...


    STATE2:
    ....


    STATE3:
    ....


The processing of the language is sequential, processing the statements in
the current state of each task until the end of the state, or until a goto
is encountered, then the thread of execution moves on to the next task
until all tasks have been processed, and starts again.


In the target language, C, while and do loops are actually mapped to an if
statement. Because the processing is sequential, when the execution gets
to the end of the while statement, processing for this task ends, and
execution moves on to the next task. When while loop is again visited on
the next scan, the while loop is again executed to the end of the while
loop.


Tasks are mapped to elements in an array of tasks in the main function of
the target language. Tasks are structures which contain, among other
things, the current state state of the task (an integer), and a pointer to
a function which performs the processing for that task. On startup, the
main function enters a while loop which calls the task function, with the
current state as an argument. The function returns the new state of the
task.


Each function consists of a switch statement keyed on the current state of
the task. The statements from the state code map pretty well to C
statements in the switch structure. A GOTO in the state code is mapped to
new_state= ; break; statement set in the switch. The function returns
with the value of the new_state variable, or the current state if no goto
was encountered.


The targe source might look something like:


int Task_1(int state)
{
    switch (state) {
        case 1:
            Do something;
            if (a) new_state = 3; break;
            if (b) new_state = 2; break;
            etc...
            break;


        case 2:
            ....
            break;
        case 3:
            ....
            break;
      }


void main()
{
    struct task {
        int state;
        int (*tfunct)(int state);
    }


    struct task tasks[NO_OF_TASKS];


    [ some initialization code to assign functions to the array]


    while (true) {
        read_inputs();
        for (i=0; i<NO_OF_TASKS; i++)
            tasks[i].state = (*tasks[i].tfunct(tasks[i].state));
        write_outputs
    }
}




I hope this gives you some ideas. As I say, I haven't had a chance to
do any real implementation. If you do get around to implementing, or
even devising a formal grammer, lexical analyzer, yacc file, etc. I'd
be interested.


Good luck.


--
Thomas J. O'Connor
----------------------------------------------------------------------
Computer Integrated Manufacturing (CIM) Technical Support
Hewlett-Packard Company
Printer Manufacturing Division,
Vancouver Washington


Email: toconnor@vcd.hp.com
Fax: (206) 212-3550
HP External: (206) 212-5031
HP Telnet: 212-5031
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.