00001
00002
00003
00004 # include <stdio.h>
00005 # include "../parm.h"
00006 # include "tables.h"
00007
00008 # define MAX_TMPS 4096
00009 # define BITS_PER_LONG 32
00010 # define LONGS_TO_BITS(n) ((n) << 5)
00011 # define BITS_TO_LONGS(n) ((n) >> 5)
00012 # define ALL_ONES (-1L)
00013
00014 # define SZ_TMPS_ALLOCD ((MAX_TMPS + BITS_PER_LONG)/BITS_PER_LONG)
00015
00016 long tmps_allocd[SZ_TMPS_ALLOCD];
00017
00018
00019
00020
00021 int max_tmp = -1;
00022
00023
00024
00025 int get_next_free()
00026 {
00027 register int i,j;
00028 register long w;
00029
00030 for (i = 0; (w = tmps_allocd[i]) == ALL_ONES; i++) {
00031 if (i > SZ_TMPS_ALLOCD) {
00032 fprintf(stderr, "Too many temporaries\n");
00033 exit(1);
00034 }
00035 }
00036 for (j = 0; w & 1; w >>= 1) j++;
00037 tmps_allocd[i] |= (1 << j);
00038 j += LONGS_TO_BITS(i);
00039 if (j > max_tmp) max_tmp = j;
00040 return(j);
00041 }
00042
00043
00044 void free_tmp(i)
00045 int i;
00046 {
00047 int word_no = BITS_TO_LONGS(i);
00048 int bit_no = i - LONGS_TO_BITS(word_no);
00049
00050 tmps_allocd[word_no] &= ~(1 << bit_no);
00051 }
00052
00053
00054 void reset_tmps()
00055 {
00056 register int i,lim;
00057 register long w;
00058 int j;
00059
00060 lim = BITS_TO_LONGS(max_tmp + 1);
00061 for (i = 0; i < lim; i++) {
00062 if ((w = tmps_allocd[i]) != 0) {
00063 for (j = 0; !(w & 1); w >>= 1) j++;
00064 fprintf(stderr, "Temporary %d not deallocated\n",
00065 LONGS_TO_BITS(i) + j);
00066 tmps_allocd[i] = 0;
00067 }
00068 }
00069 max_tmp = -1;
00070 }
00071
00072
00073 char * tmp_name(i)
00074 long i;
00075 {
00076 register char * result;
00077
00078 if (i < 100) {
00079
00080 result = (char *) GC_malloc_atomic(6);
00081 result[0] = 't';
00082 result[1] = 'm';
00083 result[2] = 'p';
00084 result[3] = i / 10 + '0';
00085 result[4] = i % 10 + '0';
00086 result[5] = '\0';
00087 } else if (i & ARG_FLAG) {
00088 result = (char *) GC_malloc_atomic(12);
00089 sprintf(result, "arg%d", i & ~ARG_FLAG);
00090 } else {
00091 result = (char *) GC_malloc_atomic(12);
00092 sprintf(result, "tmp%d", i);
00093 }
00094 return(result);
00095 }
00096
00097
00098
00099 typedef struct tmp_list {
00100 long tl_no;
00101 struct tmp_list * tl_next;
00102 } tl;
00103 #define TL_NIL ((tl *) 0)
00104
00105 tl * rmd_tmps;
00106
00107
00108
00109 void rem_tmps()
00110 {
00111 register tl * p;
00112
00113 for (p = rmd_tmps; p != TL_NIL; p = p -> tl_next) {
00114 free_tmp(p -> tl_no);
00115 }
00116 rmd_tmps = TL_NIL;
00117 }
00118
00119
00120 void dead_tmp(n)
00121 long n;
00122 {
00123 tl * result = (tl *) GC_malloc(sizeof (tl));
00124
00125 result -> tl_no = n;
00126 result -> tl_next = rmd_tmps;
00127 rmd_tmps = result;
00128 }
00129