home *** CD-ROM | disk | FTP | other *** search
- /*
- * 68K/386 32-bit C compiler.
- *
- * copyright (c) 1996, David Lindauer
- *
- * This compiler is intended for educational use. It may not be used
- * for profit without the express written consent of the author.
- *
- * It may be freely redistributed, as long as this notice remains intact
- * and sources are distributed along with any executables derived from them.
- *
- * The author is not responsible for damages, either direct or consequential,
- * that may arise from use of this software.
- *
- * v1.5 August 1996
- * David Lindauer, gclind01@starbase.spd.louisville.edu
- *
- * Credits to Mathew Brandt for original K&R C compiler
- *
- */
- #include <stdio.h>
- #include <malloc.h>
- #include "expr.h"
- #include "c.h"
- #include "gen.h"
- #include "cglbdec.h"
-
- extern TABLE oldlsym;
-
- typedef struct _blk_ {
- struct _blk_ *next;
- char m[1]; /* memory area */
- } BLK;
-
- static int glbsize = 0, /* size left in current global block */
- locsize = 0, /* size left in current local block */
- glbindx = 0, /* global index */
- locindx = 0; /* local index */
-
- static BLK *locblk = 0, /* pointer to local block */
- *glbblk = 0; /* pointer to global block */
-
- void memini(void)
- {
- glbsize = 0;
- locsize = 0;
- glbindx = 0;
- locindx = 0;
- locblk = 0;
- glbblk = 0;
- }
- char *xalloc(int siz)
- { BLK *bp;
- char *rv;
- if( siz & 1 ) /* if odd size */
- siz += 1; /* make it even */
- if( global_flag ) {
- if( glbsize >= siz ) {
- rv = &(glbblk->m[glbindx]);
- glbsize -= siz;
- glbindx += siz;
- return rv;
- }
- else {
- bp = calloc(1,sizeof(BLK) + 2047);
- if( bp == NULL )
- {
- printf(" not enough memory.\n");
- exit(1);
- }
- bp->next = glbblk;
- glbblk = bp;
- glbsize = 2048 - siz;
- glbindx = siz;
- return glbblk->m;
- }
- }
- else {
- if( locsize >= siz ) {
- rv = &(locblk->m[locindx]);
- locsize -= siz;
- locindx += siz;
- return rv;
- }
- else {
- bp = calloc(1,sizeof(BLK) + 2047);
- if( bp == NULL )
- {
- printf(" not enough local memory.\n");
- exit(1);
- }
- bp->next = locblk;
- locblk = bp;
- locsize = 2048 - siz;
- locindx = siz;
- return locblk->m;
- }
- }
- }
-
- void release_local(void )
- { BLK *bp1, *bp2;
- int blkcnt;
- blkcnt = 0;
- bp1 = locblk;
- while( bp1 != 0 ) {
- bp2 = bp1->next;
- free( bp1 );
- ++blkcnt;
- bp1 = bp2;
- }
- locblk = 0;
- locsize = 0;
- lsyms.head = 0;
- oldlsym.head = oldlsym.tail = 0;
- }
-
- void release_global(void)
- { BLK *bp1, *bp2;
- int blkcnt;
- bp1 = glbblk;
- blkcnt = 0;
- while( bp1 != 0 ) {
- bp2 = bp1->next;
- free(bp1);
- ++blkcnt;
- bp1 = bp2;
- }
- glbblk = 0;
- glbsize = 0;
- gsyms.head = 0; /* clear global symbol table */
- strtab = 0; /* clear literal table */
- }