summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/drccache.c
blob: 83b00da4907e134b0ebe1a183a384c758812d726 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/***************************************************************************

    drccache.h

    Universal dynamic recompiler cache management.

    Copyright Aaron Giles
    Released for general non-commercial use under the MAME license
    Visit http://mamedev.org for licensing and usage restrictions.

***************************************************************************/

#include <stddef.h>
#include "cpuintrf.h"
#include "drccache.h"



/***************************************************************************
    CONSTANTS
***************************************************************************/

/* largest block of code that can be generated at once */
#define CODEGEN_MAX_BYTES		65536

/* minimum alignment, in bytes (must be power of 2) */
#define CACHE_ALIGNMENT			8

/* largest permanent allocation we allow */
#define MAX_PERMANENT_ALLOC		1024

/* size of "near" area at the base of the cache */
#define NEAR_CACHE_SIZE			65536



/***************************************************************************
    MACROS
***************************************************************************/

/* ensure that all memory allocated is aligned to an 8-byte boundary */
#define ALIGN_PTR_UP(p)			((void *)(((FPTR)(p) + (CACHE_ALIGNMENT - 1)) & ~(CACHE_ALIGNMENT - 1)))
#define ALIGN_PTR_DOWN(p)		((void *)((FPTR)(p) & ~(CACHE_ALIGNMENT - 1)))



/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

/* out-of-bounds codegen handlers */
typedef struct _oob_handler oob_handler;
struct _oob_handler
{
	oob_handler *		next;				/* next handler */
	drccache_oob_func	callback;			/* callback function */
	void *				param1;				/* 1st pointer parameter */
	void *				param2;				/* 2nd pointer parameter */
	void *				param3;				/* 3rd pointer parameter */
};


/* a linked list of free items */
typedef struct _free_link free_link;
struct _free_link
{
	free_link *			next;				/* pointer to the next guy */
};


/* cache state */
struct _drccache
{
	/* core parameters */
	drccodeptr			near;				/* pointer to the near part of the cache */
	drccodeptr			neartop;			/* top of the near part of the cache */
	drccodeptr			base;				/* base pointer to the compiler cache */
	drccodeptr			top;				/* current top of cache */
	drccodeptr			end;				/* end of cache memory */
	drccodeptr			codegen;			/* start of generated code */
	size_t				size;				/* size of the cache in bytes */

	/* oob management */
	oob_handler *		ooblist;			/* list of oob handlers */
	oob_handler **		oobtail;			/* pointer to tail oob pointer */

	/* free lists */
	free_link *			free[MAX_PERMANENT_ALLOC / CACHE_ALIGNMENT];
	free_link *			nearfree[MAX_PERMANENT_ALLOC / CACHE_ALIGNMENT];
};



/***************************************************************************
    INITIALIZATION/TEARDOWN
***************************************************************************/

/*-------------------------------------------------
    drccache_alloc - allocate the cache itself
-------------------------------------------------*/

drccache *drccache_alloc(size_t bytes)
{
	drccache cache, *cacheptr;

	assert(bytes >= sizeof(cache) + NEAR_CACHE_SIZE);

	/* build a local structure first */
	memset(&cache, 0, sizeof(cache));
	cache.near = osd_alloc_executable(bytes);
	cache.neartop = cache.near;
	cache.base = cache.near + NEAR_CACHE_SIZE;
	cache.top = cache.base;
	cache.end = cache.near + bytes;
	cache.size = bytes;

	/* now allocate the cache structure itself from that */
	cacheptr = drccache_memory_alloc(&cache, sizeof(cache));
	*cacheptr = cache;

	/* return the allocated result */
	return cacheptr;
}


/*-------------------------------------------------
    drccache_free - free the cache
-------------------------------------------------*/

void drccache_free(drccache *cache)
{
	/* release the memory; this includes the cache object itself */
	osd_free_executable(cache->near, cache->size);
}



/***************************************************************************
    CACHE INFORMATION
***************************************************************************/

/*-------------------------------------------------
    drccache_contains_pointer - return true if a 
    pointer is within the cache
-------------------------------------------------*/

int drccache_contains_pointer(drccache *cache, const void *ptr)
{
	return ((const drccodeptr)ptr >= cache->near && (const drccodeptr)ptr < cache->near + cache->size);
}


/*-------------------------------------------------
    drccache_contains_near_pointer - return true 
    if a pointer is within the cache
-------------------------------------------------*/

int drccache_contains_near_pointer(drccache *cache, const void *ptr)
{
	return ((const drccodeptr)ptr >= cache->near && (const drccodeptr)ptr < cache->neartop);
}


/*-------------------------------------------------
    drccache_near - return a pointer to the near
    part of the cache
-------------------------------------------------*/

drccodeptr drccache_near(drccache *cache)
{
	return cache->near;
}


/*-------------------------------------------------
    drccache_base - return a pointer to the base
    of the cache
-------------------------------------------------*/

drccodeptr drccache_base(drccache *cache)
{
	return cache->base;
}


/*-------------------------------------------------
    drccache_top - return the current top of the
    cache
-------------------------------------------------*/

drccodeptr drccache_top(drccache *cache)
{
	return cache->top;
}



/***************************************************************************
    MEMORY MANAGEMENT
***************************************************************************/

/*-------------------------------------------------
    drccache_flush - flush the cache contents
-------------------------------------------------*/

void drccache_flush(drccache *cache)
{
	/* can't flush in the middle of codegen */
	assert(cache->codegen == NULL);

	/* just reset the top back to the base and re-seed */
	cache->top = cache->base;
}


/*-------------------------------------------------
    drccache_memory_alloc - allocate permanent
    memory from the cache
-------------------------------------------------*/

void *drccache_memory_alloc(drccache *cache, size_t bytes)
{
	drccodeptr ptr;

	assert(bytes > 0);

	/* pick first from the free list */
	if (bytes < MAX_PERMANENT_ALLOC)
	{
		free_link **linkptr = &cache->free[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
		free_link *link = *linkptr;
		if (link != NULL)
		{
			*linkptr = link->next;
			return link;
		}
	}

	/* if no space, we just fail */
	ptr = ALIGN_PTR_DOWN(cache->end - bytes);
	if (cache->top > ptr)
		return NULL;

	/* otherwise update the end of the cache */
	cache->end = ptr;
	return ptr;
}


/*-------------------------------------------------
    drccache_memory_alloc_near - allocate 
    permanent memory from the near part of the
    cache
-------------------------------------------------*/

void *drccache_memory_alloc_near(drccache *cache, size_t bytes)
{
	drccodeptr ptr;

	assert(bytes > 0);

	/* pick first from the free list */
	if (bytes < MAX_PERMANENT_ALLOC)
	{
		free_link **linkptr = &cache->nearfree[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
		free_link *link = *linkptr;
		if (link != NULL)
		{
			*linkptr = link->next;
			return link;
		}
	}

	/* if no space, we just fail */
	ptr = ALIGN_PTR_UP(cache->neartop);
	if (ptr + bytes > cache->base)
		return NULL;

	/* otherwise update the top of the near part of the cache */
	cache->neartop = ptr + bytes;
	return ptr;
}


/*-------------------------------------------------
    drccache_memory_free - release permanent
    memory allocated from the cache
-------------------------------------------------*/

void drccache_memory_free(drccache *cache, void *memory, size_t bytes)
{
	free_link **linkptr;
	free_link *link = memory;

	assert(bytes < MAX_PERMANENT_ALLOC);
	assert(((drccodeptr)memory >= cache->near && (drccodeptr)memory < cache->base) || ((drccodeptr)memory >= cache->end && (drccodeptr)memory < cache->near + cache->size));
	
	/* determine which free list to add to */
	if ((drccodeptr)memory < cache->base)
		linkptr = &cache->nearfree[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
	else
		linkptr = &cache->free[(bytes + CACHE_ALIGNMENT - 1) / CACHE_ALIGNMENT];
 
	/* link is into the free list for our size */
	link->next = *linkptr;
	*linkptr = link;
}


/*-------------------------------------------------
    drccache_memory_alloc_temporary - allocate
    temporary memory from the cache
-------------------------------------------------*/

void *drccache_memory_alloc_temporary(drccache *cache, size_t bytes)
{
	drccodeptr ptr = cache->top;

	/* can't allocate in the middle of codegen */
	assert(cache->codegen == NULL);

	/* if no space, we just fail */
	if (ptr + bytes >= cache->end)
		return NULL;

	/* otherwise, update the cache top */
	cache->top = ALIGN_PTR_UP(ptr + bytes);
	return ptr;
}



/***************************************************************************
    CODE GENERATION
***************************************************************************/

/*-------------------------------------------------
    drccache_begin_codegen - begin code
    generation
-------------------------------------------------*/

drccodeptr *drccache_begin_codegen(drccache *cache, UINT32 reserve_bytes)
{
	drccodeptr ptr = cache->top;

	/* can't restart in the middle of codegen */
	assert(cache->codegen == NULL);
	assert(cache->ooblist == NULL);

	/* if still no space, we just fail */
	if (ptr + reserve_bytes >= cache->end)
		return NULL;

	/* otherwise, return a pointer to the cache top */
	cache->codegen = cache->top;
	cache->oobtail = &cache->ooblist;
	return &cache->top;
}


/*-------------------------------------------------
    drccache_end_codegen - complete code
    generation
-------------------------------------------------*/

drccodeptr drccache_end_codegen(drccache *cache)
{
	drccodeptr result = cache->codegen;

	/* run the OOB handlers */
	while (cache->ooblist != NULL)
	{
		/* remove us from the list */
		oob_handler *oob = cache->ooblist;
		cache->ooblist = oob->next;

		/* call the callback */
		(*oob->callback)(&cache->top, oob->param1, oob->param2, oob->param3);
		assert(cache->top - cache->codegen < CODEGEN_MAX_BYTES);

		/* release our memory */
		drccache_memory_free(cache, oob, sizeof(*oob));
	}

	/* update the cache top */
	cache->top = ALIGN_PTR_UP(cache->top);
	cache->codegen = NULL;

	return result;
}


/*-------------------------------------------------
    drccache_request_oob_codegen - request
    callback for out-of-band codegen
-------------------------------------------------*/

void drccache_request_oob_codegen(drccache *cache, drccache_oob_func callback, void *param1, void *param2, void *param3)
{
	oob_handler *oob;

	assert(cache->codegen != NULL);

	/* pull an item from the free list */
	oob = drccache_memory_alloc(cache, sizeof(*oob));
	assert(oob != NULL);

	/* fill it in */
	oob->next = NULL;
	oob->callback = callback;
	oob->param1 = param1;
	oob->param2 = param2;
	oob->param3 = param3;

	/* add to the tail */
	*cache->oobtail = oob;
	cache->oobtail = &oob->next;
}