home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 213a.lha / Sozobon-C / jas / cbuf.c < prev    next >
C/C++ Source or Header  |  1996-02-14  |  3KB  |  139 lines

  1.  
  2. /*
  3.  * Copyright (c) 1988 by Sozobon, Limited.  Author: Joseph M Treat
  4.  *
  5.  * Permission is granted to anyone to use this software for any purpose
  6.  * on any computer system, and to redistribute it freely, with the
  7.  * following restrictions:
  8.  * 1) No charge may be made other than reasonable charges for reproduction.
  9.  * 2) Modified versions must be clearly marked as such.
  10.  * 3) The authors are not responsible for any harmful consequences
  11.  *    of using this software, even if they result from defects in it.
  12.  */
  13.  
  14. #include "jas.h"
  15.  
  16. extern long newdot;
  17. extern int line;
  18.  
  19. extern SYM dot;
  20.  
  21. CLIST    *t_head = (CLIST *) NULL,
  22.     *d_head = (CLIST *) NULL;
  23. CLIST    *txtptr = (CLIST *) NULL,
  24.     *datptr = (CLIST *) NULL;
  25.  
  26. bufhead() 
  27. {
  28.     txtptr = t_head;
  29.     datptr = d_head;
  30.     if ( txtptr )
  31.         txtptr->inx = 0;
  32.     if ( datptr )
  33.         datptr->inx = 0;
  34. }
  35.  
  36. CBUF *
  37. cget( seg )
  38.     unsigned short seg;
  39. {
  40.     register CLIST *xc, *oxc;
  41.  
  42.     xc = seg == TXT ? txtptr : datptr;
  43.     if ( xc == (CLIST *) NULL )
  44.         return (CBUF *) NULL;
  45.  
  46.     if ( xc->inx >= xc->cnt ) {
  47.         /*
  48.          * the current CLIST is used up
  49.          */
  50.         oxc = xc;
  51.         xc = xc->next;
  52.         *( seg == TXT ? &txtptr : &datptr ) = xc;
  53.         free( (char *) oxc );
  54.         if (! xc ) {
  55.             return (CBUF *) NULL;
  56.         }
  57.         xc->inx = 0;
  58.     }
  59.     return &xc->cblock[xc->inx++];
  60. }
  61.  
  62. CBUF *
  63. generate( n, act, val, psym )
  64.     short n, act;
  65.     long val;
  66.     SYM *psym;
  67. {
  68.     register CLIST *xc;
  69.     register CBUF *cp;
  70.  
  71.     if ( act == GENRELOC && psym == (SYM *) NULL )
  72.         act = GENVALUE;
  73.  
  74. /* out for now ...
  75.     fprintf( stderr, "generate( [%x], %d, %d, %lx, ",
  76.                         dot.flags, n, act, val );
  77.     if ( psym ) 
  78.         fprintf( stderr, "\"%s\" )\n", psym->name );
  79.     else
  80.         fprintf( stderr, "\"\" )\n" );
  81. ... */
  82.  
  83.     switch ( dot.flags ) {
  84.     case TXT:
  85.         if ( txtptr == (CLIST *) NULL )
  86.             txtptr = t_head = ALLO(CLIST);
  87.         xc = txtptr;
  88.         break;
  89.     case BSS:
  90.         if ( act != GENVALUE || val != 0L || psym != (SYM *) NULL )
  91.             Yerror( "Attempt to initialize bss" );
  92.         newdot += n / 8;
  93.         return;
  94.     case DAT:
  95.         if ( datptr == (CLIST *) NULL )
  96.             datptr = d_head = ALLO(CLIST);
  97.         xc = datptr;
  98.         break;
  99.     }
  100.  
  101.     if ( xc->cnt == CBLEN || (act == GENBRNCH && xc->cnt >= CBLEN-1) ) {
  102.         xc->next = ALLO(CLIST);
  103.         xc = xc->next;
  104.         *( dot.flags == TXT ? &txtptr : &datptr ) = xc;
  105.         xc->cnt = 0;
  106.         xc->next = (CLIST *) NULL;
  107.     }
  108.  
  109.     cp = &xc->cblock[xc->cnt++];
  110.  
  111.     if ( psym ) 
  112.         psym->flags |= DEFINED;
  113.     cp->action = act == GENBRNCH ? GENSTMT : act;
  114.     cp->nbits = n;
  115.     cp->value.value = val;
  116.     cp->value.psym = psym;
  117.     cp->line = line;
  118.  
  119.     newdot += n / 8;
  120.     return cp;
  121. }
  122.  
  123. zeros( n )
  124.     register long n;
  125. {
  126.     if ( newdot & 1 ) {
  127.         generate( 8, GENVALUE, 0L, (SYM *) NULL );
  128.         n--;
  129.     }
  130. /* out for now ...
  131.     for (; n >= 4; n -= 4 )
  132.         generate(32, GENVALUE, 0L, (SYM *) NULL );
  133. ... */
  134.     for (; n >= 2; n -= 2 )
  135.         generate( 16, GENVALUE, 0L, (SYM *) NULL );
  136.     if ( n )
  137.         generate( 8, GENVALUE, 0L, (SYM *) NULL );
  138. }
  139.