Adding block scopes to an assembly language

Derek Ross <d_ross@iders.mb.ca>
27 Apr 2000 10:51:41 -0400

          From comp.compilers

Related articles
Adding block scopes to an assembly language d_ross@iders.mb.ca (Derek Ross) (2000-04-27)
Re: Adding block scopes to an assembly language d_ross@iders.mb.ca (Derek Ross) (2000-04-29)
Re: Adding block scopes to an assembly language rhyde@shoe-size.com (Randall Hyde) (2000-04-29)
Re: Adding block scopes to an assembly language htak@eskimo.com (2000-05-01)
| List of all articles for this month |

From: Derek Ross <d_ross@iders.mb.ca>
Newsgroups: comp.compilers
Date: 27 Apr 2000 10:51:41 -0400
Organization: MBnet Networking Inc.
Keywords: assembler, symbols

Hello,


I'm interested in giving block scopes to an assembly language, in
particular for the ADSP2181 dsp.


Like most assembly languages, everything is global: labels, jumps,
variables, etc.


What I want to do is to create two keywords, BEGIN and END, for the
code, and then apply a preprocessing step to properly expand the
labels.


For example, say I have the following code:


/* a loop */


LOOP_START:
    AR = 123;
    AR = AR - 1;
    ... do stuff ...
IF NE JUMP LOOP_START;


LOOP_START:
    AR = 234;
    AR = AR - 1;
    ... do stuff...
IF NE JUMP LOOP_START;


This code will not compile because the label LOOP_START has been
duplicated. Thus, the user has to manually ensure that all the labels
are unique. In this case, LOOP_START would be renamed to LOOP_START_A,
LOOP_START_B, etc.


I'd like to use the BEGIN and END keywords to do the following:


BEGIN
    LOOP_START:
        AR = 123;
        AR = AR - 1;
        ... do stuff ...
    IF NE JUMP LOOP_START;
END


BEGIN
    LOOP_START:
        AR = 234;
        AR = AR - 1;
        ... do stuff...
    IF NE JUMP LOOP_START;
END


THen I would run that through a processor, and it would remove the BEGIN
and END statements and automatically fix the labels, like so:




    LOOP_START_1:
        AR = 123;
        AR = AR - 1;
        ... do stuff ...
    IF NE JUMP LOOP_START_1;


    LOOP_START_2:
        AR = 234;
        AR = AR - 1;
        ... do stuff...
    IF NE JUMP LOOP_START_2;




This should compile because LOOP_START in both instances is in a
separate scope, and thus is processed to have unique names. Sort of like
local variables or local labels in C.


I hope that this posting had a modicum of coherence to it, as I am not
actually trained in the arts of compiler design.


But my question is, "what is the proper way to go about this?" I could
start hacking away with a C compiler, but I suspect that would take much
longer than is necessary.


Are LEX and YACC the way to go? If yes, what would be overall scheme to
accomplish this?


Any suggestions?


--
Derek Ross
[Forget lex and yacc, since you already have a lexer and a parser.
Adding simple block structure is not very hard. One way to do it is
to tag each symbol with the block where it's defined. Keep a block
counter and a stack of active blocks. On each BEGIN, increment the
counter and push the incremented value on the active stack. On each
END, pop the top entry from the stack. Whenever you define a symbol,
store the block number on the top of the stack as the block where the
symbol is defined. When looking up a symbol, search for the symbol
with the block on the top of the stack, if not found with the next one
on the stack, etc. until you run out of stack or find it. -John]







Post a followup to this message

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