home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / hcesource / top / source / io.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  293 lines

  1. /* Copyright (c) 1988,1991 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  * Modified by Detlef Wuerkner for AMIGA
  12.  * Changes marked with TETISOFT
  13.  */
  14. #include "top.h"
  15.  
  16. /* ADDED BY TETISOFT */
  17. #include <ctype.h>
  18.  
  19. /*
  20.  * Low-level i/o routines.
  21.  */
  22.  
  23. /*
  24.  * mode tells what kind of stuff we're reading at the moment.
  25.  */
  26. static    int    mode;
  27.  
  28. /* ADDED BY TETISOFT */
  29. static int cseen, dseen, bseen;
  30. #define    REFS    3
  31.  
  32. #define    BSS    0
  33. #define    DATA    1
  34. #define    TEXT    2
  35.  
  36. static    char    *mnames[] = {
  37.     ".bss",
  38.     ".data",
  39.     ".text"
  40. };
  41.  
  42. /* ADDED BY TETISOFT: names for output */
  43. static char    *outmnames[] = {
  44.     "BSS",
  45.     "DATA",
  46.     "BSS\tCHIPBSS,CHIP",
  47.     "DATA\tCHIPDATA,CHIP",
  48. };
  49.  
  50. /* ADDED BY TETISOFT: reference flags */
  51. #define XDEF    1        /* found .globl  (export) */
  52. #define    PUBLIC    2        /* found .PUBLIC (import or export,
  53.                           BUT NO STATICS) */
  54. #define    STATIC    4        /* found .STATIC (DON'T EXPORT!) */
  55.  
  56. /* ADDED BY TETISOFT: reference list node */
  57. struct refnode
  58. {
  59.     struct refnode *next;
  60.     int type;        /* see above */
  61.     char *name;
  62. };
  63.  
  64. /* ADDED BY TETISOFT */
  65. static struct refnode firstref;
  66. static struct refnode *refsptr = &firstref;
  67.  
  68. /* ADDED BY TETISOFT */
  69. static addref (s, type)
  70. char *s;
  71. {
  72.     register struct refnode *rp;
  73.  
  74.     for (rp = refsptr; rp->next; rp = rp->next) {
  75.        if (strcmp(rp->name, s) == 0) {
  76.         rp->type |= type;
  77.         return;
  78.        }
  79.     }
  80.     if (strcmp(rp->name, s) == 0) {
  81.         rp->type |= type;
  82.         return;
  83.     }
  84.     rp->next = (struct refnode *)alloc(sizeof(struct refnode));
  85.        rp = rp->next;
  86.     rp->name = alloc(strlen(s)+1);
  87.     strcpy(rp->name, s);
  88.     rp->type = type;
  89.     rp->next = NULL;
  90. }
  91.  
  92. /* ADDED BY TETISOFT */
  93. static void printrefs()
  94. {
  95.     struct refnode *rp;
  96.  
  97.     rp = refsptr->next;
  98.     while (rp) {
  99.         if (rp->type == PUBLIC)
  100.             fprintf(rfp, "\tXREF\t%s\n", rp->name);
  101.         rp = rp->next;
  102.     };
  103.     fprintf(rfp, "\n");
  104.     rp = refsptr->next;
  105.     while (rp) {
  106.         switch (rp->type) {
  107.         case XDEF:
  108.         case XDEF|PUBLIC:
  109.             fprintf(rfp, "\tXDEF\t%s\n", rp->name);
  110.             break;
  111.         case XDEF|STATIC:
  112.         case XDEF|STATIC|PUBLIC:
  113.             fprintf(stderr, "invalid reftype combination of %s\n",
  114.                      rp->name);
  115.             exit(EXIT_FAILURE);
  116.         }
  117.         rp = rp->next;
  118.     };
  119. }
  120.  
  121. /*
  122.  * Tokens from the current line...
  123.  */
  124. char    *t_line;        /* the entire line */
  125. char    *t_lab;            /* label, if any */
  126. char    *t_op;            /* opcode */
  127. char    *t_arg;            /* arguments */
  128.  
  129. #define    ISWHITE(c)    ((c) == '\t' || (c) == ' ' || (c) == '\n')
  130.  
  131. /* CHANGED BY TETISOFT (WAS 2048) */
  132. #define    LSIZE    200    /* max. size of an input line */
  133.  
  134. /*
  135.  * readline() - read the next line from the file
  136.  *
  137.  * readline passes data and bss through to the output, only returning
  138.  * when a line of text has been read. Returns FALSE on end of file.
  139.  *
  140.  * This function heavily modified by TETISOFT
  141.  */
  142.  
  143. bool
  144. readline()
  145. {
  146.     char    *fgets();
  147.     static    void    tokenize();
  148.     static    char    buf[LSIZE];
  149.  
  150.     /*
  151.      * Keep looping until we get a line of text
  152.      */
  153.     for (;;) {
  154.         if (fgets(buf, LSIZE, ifp) == NULL) {
  155.             ofp = cfp;    /* TETISOFT: last function to code */
  156.             printrefs();    /* TETISOFT: Output references */
  157.             return FALSE;
  158.         }
  159.     
  160.         t_line = buf;
  161.     
  162.         /*
  163.          * Find out if the mode is changing.
  164.          */
  165.         tokenize(buf);
  166.     
  167.         /*
  168.          * If we see a "var" hint from the compiler, call addvar()
  169.          * to remember it for later use.
  170.          */
  171.         if (t_lab[0] == ';') {
  172.             if (strcmp(t_lab, ";var") == 0)
  173.                 addvar(atoi(t_op), atoi(t_arg));
  174.             continue;
  175.         }
  176.  
  177.         if (t_op[0] == '.') {    /* is it a pseudo-op? */
  178.             if (strcmp(t_op, mnames[BSS]) == 0) {
  179.                mode = BSS;
  180.                ofp = bfp;
  181.                if (!bseen) {
  182.                 bseen = TRUE;
  183.                 fprintf(ofp, "\n\t%s\n\n",
  184.                        outmnames[(2*dest_hunk)+mode]);
  185.                }
  186.                continue;
  187.             } else if (strcmp(t_op, mnames[DATA]) == 0) {
  188.                mode = DATA;
  189.                ofp = dfp;
  190.                if (!dseen) {
  191.                 dseen = TRUE;
  192.                 fprintf(ofp, "\n\t%s\n\n",
  193.                     outmnames[(2*dest_hunk)+mode]);
  194.                }
  195.                continue;
  196.             } else if (strcmp(t_op, mnames[TEXT]) == 0) {
  197.                mode = TEXT;
  198.                ofp = cfp;
  199.                if (!cseen) {
  200.                 cseen = TRUE;
  201.                 fprintf(ofp, "\n\tCODE\n");
  202.                }
  203.                continue;
  204.             } else if (strcmp(t_op, ".PUBLIC") == 0) {
  205.                 addref(t_arg, PUBLIC);
  206.                 continue;
  207.             } else if (strcmp(t_op, ".STATIC") == 0) {
  208.                 addref(t_arg, STATIC);
  209.                 continue;
  210.             }
  211.         }
  212.         if (mode == TEXT) {
  213.            if (t_op[0] == '.') {    /* is it a pseudo-op? */
  214.             if (strcmp(t_op, ".globl") == 0) {
  215.                 addref(t_arg, XDEF);
  216.                 return TRUE;
  217.             } else if (strcmp(t_op, ".dc.l") == 0) {
  218.                 sprintf(t_op, "DC.L");
  219.                 return TRUE;
  220.             }
  221.            } else {
  222.             if ( (strcmp(t_op, "jsr") == 0) && (t_arg[0]!='(') ) {
  223.                 addref(t_arg, PUBLIC);
  224.             }
  225.             return TRUE;
  226.            }
  227.         } else {
  228.            if (t_op[0] == '.') {    /* is it a pseudo-op? */
  229.             if (strcmp(t_op, ".globl") == 0) {
  230.                 addref(t_arg, XDEF);
  231.             } else if (strcmp(t_op, ".comm") == 0) {
  232.                 for (; *t_arg != ','; t_arg++)
  233.                     fputc(*t_arg, ofp);
  234.                 fprintf(bfp, ":\n\tDS.B\t%s\n", ++t_arg);
  235.             } else {
  236.                 fprintf(ofp, "%s\t", t_lab);
  237.                 for (t_op++; *t_op != 0; t_op++)
  238.                     fputc(toupper(*t_op), ofp);
  239.                 fprintf(ofp,"\t%s\n", t_arg);
  240.             }
  241.  
  242.            } else {
  243.             fputs(buf, ofp);
  244.            }
  245.         }
  246.     }
  247. }
  248.  
  249. static void
  250. tokenize(s)
  251. register char    *s;
  252. {
  253.     static    char    label[LSIZE], opcode[LSIZE], args[LSIZE];
  254.     register int    i;
  255.  
  256.     /*
  257.      * Grab the label, if any
  258.      */
  259.     i = 0;
  260.     while (*s && !ISWHITE(*s) && *s != ':')
  261.         label[i++] = *s++;
  262.     label[i] = '\0';
  263.  
  264.     if (*s == ':')
  265.         s++;
  266.  
  267.     while (ISWHITE(*s))
  268.         s++;
  269.  
  270.     /*
  271.      * Grab the opcode
  272.      */
  273.     i = 0;
  274.     while (*s && !ISWHITE(*s))
  275.         opcode[i++] = *s++;
  276.     opcode[i] = '\0';
  277.  
  278.     while (ISWHITE(*s))
  279.         s++;
  280.  
  281.     /*
  282.      * Grab the arguments
  283.      */
  284.     i = 0;
  285.     while (*s && !ISWHITE(*s))
  286.         args[i++] = *s++;
  287.     args[i] = '\0';
  288.  
  289.     t_lab = label;
  290.     t_op = opcode;
  291.     t_arg = args;
  292. }
  293.