home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 156_01 / c80v.c < prev    next >
Text File  |  1985-08-21  |  40KB  |  1,636 lines

  1. /************************************************/
  2. /*                        */
  3. /*        small-c compiler        */
  4. /*                        */
  5. /*          by Ron Cain            */
  6. /*          and James Van Zandt        */
  7. /*                        */
  8. /************************************************/
  9.  
  10. #define VERSION "     2 August 1984"
  11. /*    history...
  12.         2 Aug 84  Allocating symbol table and literal
  13.             pool from heap.
  14.         31 Jul 84  No GLOBAL directives for macros.
  15.         30 Jul 84  Input file extension capitalized.
  16.          29 Jul 84  Displaying input file names.
  17.         28 Jul 84  Getting file names and options from
  18.             command line.
  19.         14 Jul 84  outdec() is now recursive & smaller.
  20.             raise() not called, since ZMAC converts
  21.             to upper case. When profiling, the
  22.             appropriate GLOBAL statements are
  23.             automagically emitted.
  24.         28 Jun 84  Adding CR after GLOBAL statement.
  25.         25 Jun 84  In addglb(), generating GLOBAL
  26.     statement. Generating 9 character labels (so 1st 8
  27.     characters in a c symbol name are significant).
  28.     Allowing 800 bytes of literals per function.
  29.         2 Sept 83  In doreturn(), changed 'ccleavin'
  30.     to 'ccleavi'.  Introduced 'leave'. 'numeric' &
  31.     'outbyte' optimized. Optimized: 'nch', 'gch', 'keepch',
  32.     'streq', 'astreq', 'raise'.
  33.         1 Sept 83  Initializing firstfct & lastfct
  34.     after calling ask().  Trace & profile enabled together.
  35.         27 Aug 83  Allowing 3 bytes for call count.
  36.         26 Aug 83  renamed: leaving => ccleavi,
  37.     registe => ccregis. Added code to link the call count
  38.     cells (main, header, trailer, newfunction).
  39.         22 Aug 83  converted "," to "'", corrected
  40.     loading of name pointer.
  41.         21 Aug 83  Trace and profile are available.
  42.     Using clibv.h & a:float.h
  43.         1 Aug 83  6 function names are now
  44.     "nospread", A now set to # words on stack rather
  45.     than adding another parameter. 
  46.         29 Mar 83  "callfunction" now reserves
  47.     symsize bytes rather than namesize bytes for sym.
  48.     When "printf" is called, the top word on the stack
  49.     points to the first argument.
  50.         7 Mar 83  "callfunction" now adds pointer
  51.     to first argument for nospread functions. "nospread"
  52.     introduced (returns true only for "printf").
  53.         10 Nov 82  Rewrote "an" for speed.
  54.         24 Oct 82  In "preprocess", searching macro
  55.     table only with strings beginning with alpha. (Allows
  56.     long numeric literals.) Rewrote "alpha" for speed.
  57.         10 Oct 82  Updated date in signon message.
  58.     Coersing function values to proper type.
  59.         4 Sept 82  Generating colons again.
  60.         3 Sep 82  "#includ"ing floating point
  61.     library.
  62.         30 Aug 82  Changed "number" calling sequence
  63.     back. Colons are optional.
  64.         12 Aug 82  Changed "number" calling sequence.
  65.         11 Aug 82  Allowing typed function
  66.     declarations.
  67.         7 Aug 82  Correct length of double in
  68.     local variables, preserving calling addr when
  69.     calling through TOS & using double argument.
  70.         5 Aug 82 Started installing floating point
  71.         3 Aug 82    generating no colons
  72.     after labels. Generating only 7 character labels.
  73.         20 Jul 82    Removed the unused
  74.     variable "iptr".
  75.         18 Jul 82    Changed comment
  76.     recognizer per J. E. Hendrix (ddj n56 p6).
  77.         17 Jul 82    Implemented \" and
  78.     \' sequences.  Corrected newfunc & getarg per
  79.     P. L. Woods (ddj n52 p32) & J. E. Hendrix (ddj n56 p6).
  80.         14 Jul 82    "#include"ing
  81.     clibv.asm & c80v-2.c
  82.         28 Jun 82    Skipping first byte
  83.     in macro table, so index won't be zero.
  84.         27 Jun 82    Masking out high
  85.     order bits of characters extracted from
  86.     a symbol table entry.
  87.         21 Jun 82    Dumping literals
  88.     at end of each function, per Rodney Black
  89.     (DDJ n61 p51).
  90.         19 Jun 82    Updated symtabsiz.
  91.     Updated dumpglbs to handle new symbol table.
  92.     Placing macro names in global symbol table,
  93.     using smaller macro table.
  94.         16 Jun 82    using hash table
  95.     for global symbols.
  96.         18 Apr 81    Changed names so
  97.     first 5 characters are unique:
  98.     heir10 => heira        heir11 => heirb
  99.     input2 => inpt2        errorsum => errsum
  100. */
  101. #include iolib.h
  102. #include float.h
  103.  
  104. #define BANNER  "* * *  Small-C  V1.2  * * *"
  105.  
  106. #define AUTHOR "       By Ron Cain  and  James Van Zandt"
  107.  
  108. /*    Define system dependent parameters    */
  109.  
  110. /*    Stand-alone definitions            */
  111.  
  112. #define NULL 0
  113. #define EOL 13
  114.  
  115. /*    UNIX definitions (if not stand-alone)    */
  116.  
  117. /* #include <stdio.h>    */
  118. /* #define EOL 10    */
  119.  
  120. /*    Define the symbol table parameters    */
  121.  
  122. #define    SYMSIZ    14
  123. #define    SYMTBSZ    8008
  124. /*            =14*(NUMGLBS+60)    */
  125. #define NUMGLBS 512
  126. #define MASKGLBS 511
  127. /*            formerly 300 globals    */
  128. #define    STARTGLB symtab
  129. #define    ENDGLB    STARTGLB+NUMGLBS*SYMSIZ
  130. #define    STARTLOC ENDGLB+SYMSIZ
  131. #define    ENDLOC    symtab+SYMTBSZ-SYMSIZ
  132.  
  133. /*    Define symbol table entry format    */
  134.  
  135. #define    name    0
  136. #define    ident    9
  137. #define    type    10
  138. #define    storage    11
  139. #define    offset    12
  140.  
  141. /*    System wide name size (for symbols)    */
  142.  
  143. #define    namesize 9
  144. #define namemax  8
  145.  
  146. /*    Define possible entries for "ident"    */
  147.  
  148. #define    variable 1
  149. #define    array    2
  150. #define    pointer    3
  151. #define    function 4
  152. #define MACRO 5
  153.             /* added 6/19/82, JRVZ */
  154.  
  155. /*    Define possible entries for "type"    */
  156.  
  157. #define    cchar    1
  158. #define    cint    2
  159. #define DOUBLE    3
  160.  
  161. /*    Define possible entries for "storage"    */
  162.  
  163. #define    statik    1
  164. #define    stkloc    2
  165.  
  166. /*    Define the "while" statement queue    */
  167.  
  168. #define    wqtabsz    100
  169. #define    wqsiz    4
  170. #define    wqmax    wq+wqtabsz-wqsiz
  171.  
  172. /*    Define entry offsets in while queue    */
  173.  
  174. #define    wqsym    0
  175. #define    wqsp    1
  176. #define    wqloop    2
  177. #define    wqlab    3
  178.  
  179. /*    Define the literal pool            */
  180.  
  181. #define    litabsz 1000
  182. /*        formerly 2000            */
  183. #define    litmax    litabsz-1
  184.  
  185. /*    Define the input line            */
  186.  
  187. #define    linesize 80
  188. #define    linemax    linesize-1
  189. #define    mpmax    linemax
  190.  
  191. /*    Define the macro (define) pool        */
  192.  
  193. #define    macqsize 500
  194. /*            formerly 1000   JRVZ 6/19/82  */
  195. #define    macmax    macqsize-1
  196.  
  197. /*    Define statement types (tokens)        */
  198.  
  199. #define    stif    1
  200. #define    stwhile    2
  201. #define    streturn 3
  202. #define    stbreak    4
  203. #define    stcont    5
  204. #define    stasm    6
  205. #define    stexp    7
  206.  
  207. /* Define how to carve up a name too long for the assembler */
  208.  
  209. #define ASMPREF    8
  210. #define ASMSUFF    0
  211.  
  212. /*    Now reserve some storage words        */
  213.  
  214. char    *symtab;        /* symbol table */
  215. char    *glbptr,*locptr;    /* ptrs to next entries */
  216.  
  217. int    wq[wqtabsz];        /* while queue */
  218. int    *wqptr;            /* ptr to next entry */
  219.  
  220. char    *litq;            /* literal pool */
  221. int    litptr;            /* ptr to next entry */
  222.  
  223. char    macq[macqsize];        /* macro string buffer */
  224. int    macptr;            /* and its index */
  225.  
  226. char    line[linesize];        /* parsing buffer */
  227. char    mline[linesize];    /* temp macro buffer */
  228. int    lptr,mptr;        /* ptrs into each */
  229.  
  230. /*    Misc storage    */
  231.  
  232. int    nxtlab,        /* next avail label # */
  233.     litlab,        /* label # assigned to literal pool */
  234.     Zsp,        /* compiler relative stk ptr */
  235.     undeclared,    /* # function arguments
  236.             not yet declared  jrvz 8/6/82 */
  237.     ncmp,        /* # open compound statements */
  238.     errcnt,        /* # errors in compilation */
  239.     errstop,    /* stop on error            gtf 7/17/80 */
  240.     eof,        /* set non-zero on final input eof */
  241.     input,        /* iob # for input file */
  242.     output,        /* iob # for output file (if any) */
  243.     inpt2,        /* iob # for "include" file */
  244.     glbflag,    /* non-zero if internal globals */
  245.     ctext,        /* non-zero to intermix c-source */
  246.     cmode,        /* non-zero while parsing c-code */
  247.             /* zero when passing assembly code */
  248.     lastst,        /* last executed statement type */
  249.     mainflg,    /* output is to be first asm filegtf 4/9/80 */
  250.     saveout,    /* holds output ptr when diverted to console       */
  251.             /*                gtf 7/16/80 */
  252.     fnstart,    /* line# of start of current fn.gtf 7/2/80 */
  253.     lineno,        /* line# in current file    gtf 7/2/80 */
  254.     infunc,        /* "inside function" flag    gtf 7/2/80 */
  255.     savestart,    /* copy of fnstart "    "    gtf 7/16/80 */
  256.     saveline,    /* copy of lineno  "    "    gtf 7/16/80 */
  257.     saveinfn,    /* copy of infunc  "    "    gtf 7/16/80 */
  258.     trace,        /* nonzero if traceback info needed
  259.                         jrvz 8/21/83    */
  260.     profile,    /* nonzero if profile needed      */
  261.     caller,        /* stack offset for caller links...
  262.         local[caller] points to name of current fct
  263.         local[caller-1] points to link for calling fct,
  264.         where local[0] is 1st word on stack after ret addr  */
  265.     firstfct,    /* label for 1st function */
  266.     lastfct,    /* label for most recent fct  jrvz 8/83 */
  267.     fname;        /* label for name of current fct  */
  268. char   *currfn,        /* ptr to symtab entry for current fn.    gtf 7/17/80 */
  269.        *savecurr;    /* copy of currfn