XTRAN Example — Analyze HP (Digital, Compaq) VAX MACRO

If you want to translate a body of assembly code to a higher level language, one of the tasks facing you is to identify all symbol definitions that are shared across separately assembled modules.

There are two common ways such definitions are shared in assembly code:

Therefore, in order to find all shared definitions, we must find and report every definition that is either global or defined in the body of macro.  For the latter case, we would like to know in which macro each shared name is defined.  This is complicated by the fact that in many assemblers, macro definitions can be nested; that is, a macro can define another macro when it is invoked.

The input to and output from XTRAN are untouched, except that line numbers have been added to the input for reference.


The following set of XTRAN rules automates this task.  The rules are an English paraphrase of the XTRAN rules language ("meta-code") actually used for the demonstration.  We use a push-down stack to track the name of each macro we find defined, so that when we exit from a nested macro definition, we know what macro definition we "pop" back up into.

Set macro level to 0
For each VAX MACRO statement:
    If start of macro definition
        Increment macro level ("push")
        Remember name of macro at this level
    Else if end of macro definition
        Decrement macro level ("pop")
        Restore name of macro at now current level
    Else if local symbol definition
        If macro level > 0
            Output analysis line, including macro name
    Else if global symbol definition
        Output analysis line, including macro name if any

Process Flowchart

Here is a flowchart for this process, in which the elements are color coded:

process flowchart

VAX MACRO code to be analyzed (demanl-a.mar):

 1         BITB    #1,R0
 2 VAR0    ==      0               ;GLOBAL, NOT IN .MACRO
 3         BEQU    10$
 5         MOVL    R0,R1
 6 VAR1    =       1               ;IN "MAC1" .MACRO
 7         MOVL    R1,R0
 8 VAR2    ==      2               ;GLOBAL, IN "MAC1" .MACRO
10 VAR3    =       3               ;IN "MAC2" .MACRO
11 .ENDM   MAC2
12 VAR4    =       4               ;IN "MAC1" .MACRO
13 .ENDM   MAC1
14 ILLVAR  =       ILLEGAL         ;NOT IN .MACRO
15 VAR5    ==      5               ;GLOBAL, NOT IN .MACRO
16         MOVL    #1, R0
17 10$:

Output from XTRAN:

File                                                Line Macro  Defn   G?
demanl-a.mar                                           2        VAR0   G
                                                       6 MAC1   VAR1    
                                                       8 MAC1   VAR2   G
                                                      10 MAC2   VAR3    
                                                      12 MAC1   VAR4    
                                                      15        VAR5   G