home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 221_01 / cc11.c < prev    next >
Text File  |  1979-12-31  |  10KB  |  411 lines

  1. /* >>>>> start cc1 <<<<<<  */
  2. /*     */
  3. /* Compiler begins execution here */
  4. /*     */
  5. #ifdef CMD_LINE
  6. main(argc, argv) int argc, *argv[];
  7. {
  8.   argcs=argc;
  9.   argvs=argv;
  10. #else
  11. main()
  12. {
  13. #endif
  14.  swend=(swnext=swq)+SWTABSZ+SWSIZ;
  15.  first_func=1; /* first function */
  16.  declared = -1;
  17.  glbptr=startglb; /* clear global symbols */
  18.  glbptr=startglb+symsiz*5;
  19.  statptr=startstat;
  20.  startcomp=endsearch=locptr=startloc; /* clear local symbols */
  21.  stagelast=stage+stagesize-1; /* set to end of stageing buffer */
  22.  wqptr=wq;  /* clear while queue */
  23.  litptr=  /* clear literal pool */
  24.  stkp =  /* stack ptr (relative) */
  25.  errcnt=  /* no errrpts */
  26.  eof=  /* not eof yet */
  27.  input=  /* no input file */
  28.  input2=  /* or include file */
  29.  output=  /* no open units */
  30.  ncmp=  /* no open compound states */
  31.  lastst=  /* no last statement yet */
  32.  iflevel= /* no ifdef yet */
  33.  skiplevel= /* also no skip of course */
  34.  stagenext=
  35.  quote[1]=
  36.  0;  /*  ...all set to zero.... */
  37.  quote[0]='"';  /* fake a quote literal */
  38.  cmode= /* enable preprocessing */
  39.  macptr=  /* clear the macro pool */
  40.  1;
  41.  /*    */
  42.  /* compiler body  */
  43.  /*    */
  44.  ask();   /* get user options */
  45. #ifndef CMD_LINE
  46.  openout();  /* get an output file */
  47.  openin();  /* and initial input file */
  48. #endif
  49.  header();  /* intro code */
  50.  parse();   /* process ALL input */
  51.  trailer();  /* follow-up code */
  52.  closeout();  /* close the output (if any) */
  53.  errrptsummary();  /* summarize errrpts */
  54.  return; /* then exit to system */
  55.  }
  56. /*     */
  57. /* Process all input text  */
  58. /*     */
  59. /* At this level, only static declarations, */
  60. /* defines, includes, and function */
  61. /* definitions are legal... */
  62. parse()
  63.  {
  64.  int storclass;
  65.  while (eof==0)  /* do until no more input */
  66.   {
  67.   storclass = automatic;
  68.   if(amatch("extern",6)) storclass = EXTERNAL;
  69.   if(amatch("static",6)) storclass = statik;
  70.   if(amatch(_char,4)) {declglb(cchar, storclass);ns();}
  71.   else if(amatch(_int,3)){declglb(cint,storclass);ns();}
  72.   else if(match("#asm"))doasm();
  73.   else if(match("#include"))doinclude();
  74.   else if(match("#define"))addmac();
  75.   else newfunc(storclass);
  76.   blanks(); /* force eof if pending */
  77.   }
  78.  }
  79. /*     */
  80. /* Dump the literal pool  */
  81. /*     */
  82. dumplits(siz) int siz;
  83.  {int j,k;
  84.  k=0;   /* init an index... */
  85.  while (k<litptr) /*  to loop with */
  86.   {defstorage(siz); /* pseudo-op to define byte */
  87.   j=10;  /* max bytes per line */
  88.   while(j--)
  89.    {outdec(getint(litq+k, siz));
  90.     k=k+siz;
  91.     if ((j==0) | (k>=litptr))
  92.     {nl();  /* need <cr> */
  93.     break;
  94.     }
  95.    outbyte(','); /* separate bytes */
  96.    }
  97.   }
  98.   litptr=0; 
  99.  }
  100.  
  101. /*     */
  102. /* Report errrpts for user  */
  103. /*     */
  104. errrptsummary()
  105.  {
  106.  /* see if anything left hanging... */
  107.  if (ncmp) errrpt("missing closing bracket");
  108.   /* open compound statement ... */
  109.  output=stdout;
  110.  cnl();
  111.  outdec(errcnt); /* total # errrpts */
  112.  outstr(" error(s) in compilation.");
  113.  }
  114. /*
  115. ** get run options
  116. */
  117. #ifdef CMD_LINE
  118. ask() {
  119.   int i;
  120.   char *argptr;
  121.   i=nxtlab=0;
  122.   litlab=getlabel(); /* first label=literal pool */
  123.   kill();   /* erase line buffer */
  124.   output=stdout;
  125. #ifdef OPTIMIZE
  126.   optimize=
  127. #endif
  128. #ifdef PHASE2
  129.   monitor=pause=DEFDEBUG=
  130. #endif
  131.   ctext=NO;
  132.   if(argcs==1) {
  133.     sout("\nusage: cc <infile [>outfile] [-c]",stderr);
  134. #ifdef OPTIMIZE
  135.     sout(" [-o]",stderr);
  136. #endif
  137. #ifdef PHASE2
  138.     sout(" [-m] [-p] [-t]",stderr);
  139. #endif
  140.     sout("\n",stderr);
  141.     exit(0);
  142.     }
  143.   while(--argcs) {  /* process all input */
  144.     argptr=argvs[++i];
  145.     if(*argptr=='<')
  146.       if((input=fopen(++argptr,"r"))==NULL) {
  147.         errrpt("input file error");
  148.         exit(0);
  149.       }
  150.       else continue;
  151.     else if(*argptr=='>')
  152.       if((output=fopen(++argptr,"w"))==NULL) {
  153.         errrpt("output file errrpt");
  154.         exit(0);
  155.       }
  156.       else continue;
  157.     else if(*argptr++ == '-')
  158.       if(upper(*argptr)=='C') ctext=YES;
  159. #ifdef OPTIMIZE
  160.       else if(upper(*argptr)=='O') optimize=YES;
  161. #endif
  162. #ifdef PHASE2
  163.       else if(upper(*argptr)=='M') monitor=YES;
  164.       else if(upper(*argptr)=='P') pause=YES;
  165.       else if(upper(*argptr)=='T') DEFDEBUG=YES;
  166. #endif
  167.   }
  168.   if(input==NULL) {
  169.     sout("\nno input file...\n",stderr);
  170.     exit(0);
  171.     }
  172. }
  173. #else
  174. /*     */
  175. /* Get options from user  */
  176. /*     */
  177. ask()
  178.  {
  179.  int k,num[1];
  180.  output=stdout;  /* init output to stdout */
  181.  kill();   /* clear input line */
  182. #ifndef VMS
  183.  outbyte(CLS);  /* clear the screen */
  184. #endif
  185.  cnl();cnl();cnl(); /* print banner */
  186.  pl("   * * *  small-c compiler  * * *");
  187.  cnl();cnl();
  188.  /* see if user wants to interleave the c-text */
  189.  /* in form of comments (for clarity) */
  190.  pl("Do you wish the c-text to appear? ");
  191.  gets(line);  /* get answer */
  192.  ctext=0;  /* assume no */
  193.  if(upper(ch())=='Y')
  194.   ctext=1; /* user said yes */
  195.  litlab=getlabel(); /* first label=literal pool */
  196.  kill();   /* erase line */
  197.  }
  198. /*     */
  199. /* Get output filename  */
  200. /*     */
  201. openout()
  202.  {
  203.  kill();   /* erase line */
  204.  output=0;  /* start with none */
  205.  pl("Output filename? "); /* ask...*/
  206.  gets(line); /* get a filename */
  207.  if(ch()==0) {
  208.    output=stdout;
  209.    return; /* none given... */
  210.  }
  211. #ifndef VMS
  212.   if((output=fopen(line,"w"))==NULL) /* Changed from UNIX to VMS  */
  213. #else
  214.  if((output=fdopen(creat(line,0,"rat=cr","rfm=var"),"w"))==NULL)
  215. #endif
  216.   {output=0; /* can't open */
  217.   errrpt("Open failure");
  218.   }
  219.  kill();   /* erase line */
  220. }
  221. /*     */
  222. /* Get (next) input file  */
  223. /*     */
  224. openin()
  225. {
  226.  input=0;  /* none to start with */
  227.  while (input==0) /* any above 1 allowed */
  228.   {kill(); /* clear line */
  229.   if(eof)break; /* if user said none */
  230.   pl("Input filename? ");
  231.   gets(line); /* get a name */
  232.   if(ch()==0)
  233.    {eof=1;break;} /* none given... */
  234.   if((input=fopen(line,"r"))==NULL)
  235.    {input=0; /* can't open it */
  236.    pl("Open failure");
  237.    }
  238.   }
  239.  kill();  /* erase line */
  240.  }
  241. #endif
  242. /*     */
  243. /* Open an include file  */
  244. /*     */
  245. doinclude()
  246. {
  247.  blanks(); /* skip over to name */
  248.  if((input2=fopen(line+lptr,"r"))==NULL)
  249.   {input2=0;
  250.   errrpt("Open failure on include file");
  251.   }
  252.  kill();  /* clear rest of line */
  253.    /* so next read will come from */
  254.    /* new file (if open */
  255. }
  256. /*     */
  257. /* Close the output file  */
  258. /*     */
  259. closeout()
  260. { if(output)fclose(output); /* if open, close it */
  261.  output=0;  /* mark as closed */
  262. }
  263. /*     */
  264. /* Declare a static variable */
  265. /*   (i.e. define for use)  */
  266. /*     */
  267. /* makes an entry in the symbol table so subsequent */
  268. /*  references can call symbol by name */
  269. declglb(typ,class)  /* typ is cchar or cint */
  270.  int typ, class;
  271. { int k,j;char sname[namesize];
  272.  while(1)
  273.    {if(endst())return; /* do line */
  274.    if(match("*")) { /* pointer ? */
  275.     j=pointer; /* yes */
  276.     k=0;
  277.     }
  278.    else {
  279.     j=variable; /* no */
  280.     k=1;
  281.     }
  282.    if (symname(sname)==0) /* name ok? */
  283.     illname(); /* no... */
  284.    if(findglb(sname)) /* already there? */
  285.     multidef(sname);
  286.    if(match("()")) j=function;
  287.    else if (match("["))  /* array? */
  288.     {k=needsub(); /* get size */
  289.     j=array; /* !0=array */
  290.     }
  291.    if(class==EXTERNAL) declexternal(sname);
  292.    else {
  293.      entry(sname, class);
  294.      j=initials(typ>>2, j, k);
  295.    }
  296.    addglb(sname,j,typ,k,class); /* add symbol */
  297.    if(match(",")==0) return;  /* more? */
  298.   }
  299. }
  300.  
  301. /*
  302. ** declare local variables
  303. ** changed for static declaration
  304. ** dieter h. flunkert
  305. ** 29-oct-86
  306. */
  307. declloc(typ, class)  int typ, class;  {
  308.   int k,j; char sname[namesize], numstring[namesize];
  309. #ifdef STGOTO
  310.   if(noloc) errrpt("not allowed with goto");
  311. #endif
  312.   if(declared < 0) errrpt("must declare first in block");
  313.   while(1) {
  314.     while(1) {
  315.       if(endst()) return;
  316.       if(match("*")) j=pointer;
  317.       else j=variable;
  318.       if (symname(sname)==0) illname();
  319.       endsearch=startcomp;
  320.       if(findloc(sname) != 0) multidef(sname);
  321.       endsearch=startloc;
  322.       k=2;
  323.       if (match("[")) {
  324.         k=needsub();
  325.         if(k || class == statik) {
  326.          j=array;
  327.          if(typ==cint)k=k+k;
  328.         }
  329.         else {j=pointer;
  330.           k=2;
  331.      }
  332.       }
  333.       else if(match("()")) j=function;
  334.       else if((typ==cchar)&&(j==variable)) k=1;
  335.       if(class == statik) {    /* dhf 29-oct-86 */
  336.    convert(statlab++, numstring);
  337.    entry(numstring, class);
  338.    if(typ==cint) k = k>>1;
  339.    j=initials(typ>>2, j, k);
  340.    addstatic(sname, numstring, j, typ, k);
  341.    addloc(sname, j, typ, 0); /* making it known as local */
  342.       }
  343.       else {
  344.        declared = declared + k;
  345.        addloc(sname, j, typ, stkp - declared);
  346.       }
  347.       break;
  348.       }
  349.     if (match(",")==0) return;
  350.     }
  351.   }
  352.  
  353. convert(n,s) int n; char *s; {
  354. char *c, ch;
  355.  
  356.    c=s;
  357.    do {
  358.     *s++ = n % 10 + '0';
  359.    } while((n /= 10) > 0);
  360.    *s-- = '\0';
  361.    while(c < s) {
  362.     ch = *c;
  363.     *c++ = *s;
  364.     *s-- = ch;
  365.    }
  366. }
  367.  
  368. /*
  369. ** initialize global objects
  370. */
  371. initials(siz, id, dim) int siz, id, dim; {
  372.   int savedim;
  373.   if(dim==0) dim = -1;
  374.   savedim=dim;
  375.   if(match("=")) {
  376.     if(match("{")) {
  377.       while(dim) {
  378.         init(siz, id, &dim);
  379.         if(match(",")==0) break;
  380.         }
  381.       needbrack("}");
  382.       }
  383.     else init(siz, id, &dim);
  384.     }
  385.   if((dim == -1)&&(dim==savedim)) {
  386.      stowlit(0, siz=2);
  387.     id=pointer;
  388.     }
  389.   dumplits(siz);
  390.   dumpzero(siz*dim);
  391.   return id;
  392.   }
  393.  
  394. /*
  395. ** evaluate one initializer
  396. */
  397. init(siz, id, dim) int siz, id, *dim; {
  398.   int value;
  399.   if(qstr(&value)) {
  400.     if((id==variable)||(siz!=1))
  401.       errrpt("must assign to char pointer or array");
  402.     *dim = - (litptr - value) + *dim;
  403.     if(id==pointer) point();
  404.     }
  405.   else if(constexpr(&value)) {
  406.     if(id==pointer) errrpt("cannot assign to pointer");
  407.     stowlit(value, siz);
  408.     *dim = -1 + *dim;
  409.     }
  410.   }
  411.