XTRAN Example — Translate PL/M BASED() Attribute, Using Cast Pointers

The following example uses the standard set of XTRAN rules for parsing PL/M and translating it to C.  The input and output are untouched.

NOTE that the translation shown below was done with default conditions.  XTRAN provides many options for controlling the way it translates.



Process Flowchart

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

process flowchart

Input to XTRAN:  demcst.plm

DECLARE pubptr POINTER;                                 /*public POINTER*/

prc:    PROCEDURE;
        DECLARE ptrarr POINTER (10);                    /*pointer array*/
        DECLARE atptr1 POINTER AT (@ptrarr(5));         /*AT() pointer*/
        DECLARE atptr2 POINTER AT (@ptrarr(6));         /*AT() pointer*/
        DECLARE (ptr3, ptr4) POINTER;                   /*normal pointers*/
        DECLARE bvar1 BASED(atptr1) WORD;               /*BASED() on AT()*/
        DECLARE bvar2 BASED(ptr3) WORD;                 /*BASED() on non-AT()*/
        DECLARE bstr1 BASED(atptr2) STRUCTURE(          /*BASED() on AT()*/
            memb1 WORD,
            memb2 BYTE);
        DECLARE bstr2 BASED(atptr2) STRUCTURE(          /*BASED() on same AT()*/
            memb1 WORD,
            memb2 BYTE);
        DECLARE bstr3 BASED(ptr4) STRUCTURE(            /*BASED() on non-AT()*/
            memb1 WORD,
            memb2 BYTE);
        DECLARE bvar3 BASED(ptr4) WORD;                 /*BASED() on same non-AT()*/
$INCLUDE (demcst1.plm)                                  /*declare ptr5*/
        DECLARE bvar4 BASED(ptr5) WORD;                 /*decl'd in diff files*/
        DECLARE bvar5 BASED(pubptr) WORD;               /*decl'd in diff scopes*/
        
        bvar1 = 1;                                      /*bvar1 = 1;*/
        bvar2 = bvar1;                                  /*bvar2 = bvar1;*/
        bvar3 = bvar4;                                  /*bvar3 = bvar4;*/
        bvar5 = 1;                                      /*bvar5 = 1;*/
        bstr1.memb1 = 1;                                /*bstr1.memb1 = 1;*/
        bstr2.memb1 = 1;                                /*bstr2.memb1 = 1;*/
        bstr3.memb1 = 1;                                /*bstr3.memb1 = 1;*/
        END prc;

Input to XTRAN:  demcst1.plm

        DECLARE ptr5 POINTER;                           /*diff file from BASED()*/

Output from XTRAN:

        static void *pubptr;                    /*public POINTER*/

static void prc(void)
{
        static void *ptrarr[10];                /*pointer array*/
#define ATPTR1 ptrarr[5]                        /*AT() pointer*/
#define ATPTR2 ptrarr[6]                        /*AT() pointer*/
        static void *ptr3;                      /*normal pointers*/
        static void *ptr4;
                                                /*BASED() on AT()*/
                                                /*BASED() on non-AT()*/
        typedef struct bstr1                    /*BASED() on AT()*/
            {
            unsigned short memb1;
            unsigned char memb2;
            }
            Bstr1;
        typedef struct bstr2                    /*BASED() on same AT()*/
            {
            unsigned short memb1;
            unsigned char memb2;
            }
            Bstr2;
        typedef struct bstr3                    /*BASED() on non-AT()*/
            {
            unsigned short memb1;
            unsigned char memb2;
            }
            Bstr3;
                                                /*BASED() on same non-AT()*/
#include "demcst1.h"                            /*declare ptr5*/
                                                /*decl'd in diff files*/
                                                /*decl'd in diff scopes*/

        *(unsigned short *) ATPTR1 = 1;         /*bvar1 = 1;*/
        *(unsigned short *) ptr3 = *(unsigned
          short *) ATPTR1;                      /*bvar2 = bvar1;*/
        *(unsigned short *) ptr4 = *(unsigned
          short *) ptr5;                        /*bvar3 = bvar4;*/
        *(unsigned short *) pubptr = 1;         /*bvar5 = 1;*/
        ((Bstr1 *) ATPTR2)->memb1 = 1;          /*bstr1.memb1 = 1;*/
        ((Bstr2 *) ATPTR2)->memb1 = 1;          /*bstr2.memb1 = 1;*/
        ((Bstr3 *) ptr4)->memb1 = 1;            /*bstr3.memb1 = 1;*/
}