aboutsummaryrefslogtreecommitdiff
path: root/backendC/CleanCompilerSources/backendsupport.c
blob: e38bc544992950eee9a41aa2b08567c88058831a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# include "compiledefines.h"
# include "types.t"
# include "system.h"
# include "comsupport.h"
# include "backendsupport.h"


/*
	Utilities
	=========
*/
# ifdef _WINDOWS_
# undef _WINDOWS_
# include <windows.h>
# define	Debugger()	DebugBreak();
# else
# define	Debugger()	{ * (int *) NULL = 0; }
# endif

void
AssertionFailed (char *conditionString, char *file, int line)
{	
	FPrintF (StdError, "Error in backend: File %s, Line %d (%s)\n", file, line, conditionString);

# ifdef _WINDOWS_
	{
		static char error[200];

		sprintf (error, "Error in backend: File %s, Line %d (%s)\nDebug ?", file, line, conditionString);
	
		if (MessageBox (NULL,error,"AssertionFailed",MB_YESNO)==IDYES)
			Debugger ();
	}
#else
# ifdef _MAC_
	{
		FILE *f;
	
		f=fopen ("AssertionFailedError","w");
		if (f!=NULL){
			FPrintF (f, "Error in backend: File %s, Line %d (%s)\n", file, line, conditionString);
			fclose (f);
		}
	}
# endif
	Debugger ();
#endif
} /* AssertionFailed */

void
fatal_backend_error (char *s)
{
	FPrintF (StdError, "Error in backend: %s\n", s);

#ifdef _MAC_
	{
		FILE *f;
	
		f=fopen ("AssertionFailedError","w");
		if (f!=NULL){
			FPrintF (f, "Error in backend: %s\n", s);
			fclose (f);
		}
	}
#endif	
	Debugger ();
}

void debug_message (const char *format,...)
{
	va_list ap;
	
	va_start (ap,format);
	vfprintf (StdError,format,ap);
	va_end (ap);

#ifdef _MAC_
	{
		FILE *f;
	
		f=fopen ("DebugMessages","a");
		if (f!=NULL){
			va_start (ap,format);
			vfprintf (f,format,ap);
			va_end (ap);
			fclose (f);
		}
	}
#endif	
}

#if 1
/*
	Memory management
	=================
*/

static enum {kMemoryInitClear, kMemoryInitSet} gMemoryInit = kMemoryInitSet;

# define	kDefaultConvertBufferSize	(32 * 1024)

typedef struct convert_buffer ConvertBufferS, *ConvertBufferP;

struct convert_buffer
{
	ConvertBufferP	cb_next;
	int				cb_size;
	char			cb_memory [kDefaultConvertBufferSize]; /* or more bytes */
};

static void
InvalidateMemory (void *memory, size_t size)
{
	char	value, *p;
	int		i;

	switch (gMemoryInit)
	{
		case kMemoryInitClear:
			value	= 0;
			break;
		case kMemoryInitSet:
			value	= ~0;
			break;
		default:
			Assert (False);
			break;
	}

	p	= memory;
	for (i = 0; i < size; i++)
		*p++	= value;
} /* InvalidateMemory */

static ConvertBufferP	gFirstBuffer = NULL, gCurrentBuffer = NULL;
static char 			*gMemory;
static long gBytesLeft = 0;

static void
AllocConvertBuffer (int min_size)
{
	ConvertBufferP	newBuffer;
	int new_convert_buffer_size;

	new_convert_buffer_size=kDefaultConvertBufferSize;
	while (new_convert_buffer_size<min_size)
		new_convert_buffer_size+=kDefaultConvertBufferSize;

	newBuffer	= (ConvertBufferP) malloc (sizeof (ConvertBufferS)+(new_convert_buffer_size-kDefaultConvertBufferSize));
	if (newBuffer == NULL)
		FatalCompError ("backendsupport.c", "AllocConvertBuffer", "out of memory");

	newBuffer->cb_size=new_convert_buffer_size;
	
	if (gFirstBuffer == NULL)
		gCurrentBuffer	= gFirstBuffer				= newBuffer;
	else
		gCurrentBuffer	= gCurrentBuffer->cb_next	= newBuffer;

	gCurrentBuffer->cb_next	=	NULL;

	gBytesLeft	= new_convert_buffer_size;
	gMemory		= gCurrentBuffer->cb_memory;

	InvalidateMemory (gMemory, new_convert_buffer_size);

	if (gFirstBuffer == NULL)
		gFirstBuffer	= gCurrentBuffer;
} /* AllocConvertBuffer */

void
FreeConvertBuffers (void)
{
	ConvertBufferP	buffer;

	buffer	= gFirstBuffer;

	while (buffer != NULL)
	{
		ConvertBufferP	nextBuffer;

		nextBuffer	= buffer->cb_next;

		InvalidateMemory (buffer,buffer->cb_size);
		free (buffer);

		buffer	= nextBuffer;
	}

	gFirstBuffer	= NULL;
	gCurrentBuffer	= NULL;
	gBytesLeft	= 0;
} /* FreeConvertBuffers */

void *
ConvertAlloc (SizeT size)
{
	void	*memory;

	size	= (size+3) & ~3;

	if (size > gBytesLeft){
		AllocConvertBuffer (size);

		if (size>gBytesLeft){
			static char s[100];
			
			sprintf (s,"ConvertAlloc: size = %d, gBytesLeft = %d",size,gBytesLeft);
			fatal_backend_error (s);
		}
	}

	Assert (size <= gBytesLeft);

	memory	= gMemory;	
	gBytesLeft	-= size;
	gMemory	+=	size;

	return ((void *) memory);
} /* ConvertAlloc */
#endif