2018-07-01 15:52:15 +00:00
|
|
|
/*
|
2023-01-07 21:03:35 +00:00
|
|
|
* Copyright (c) 2023 Calvin Rose
|
2018-07-01 15:52:15 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "regalloc.h"
|
2019-04-27 19:45:28 +00:00
|
|
|
#include "util.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-07-01 15:52:15 +00:00
|
|
|
|
2023-05-29 21:10:48 +00:00
|
|
|
/* The JanetRegisterAllocator is really just a bitset. */
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_regalloc_init(JanetcRegisterAllocator *ra) {
|
2018-07-01 15:52:15 +00:00
|
|
|
ra->chunks = NULL;
|
|
|
|
ra->count = 0;
|
|
|
|
ra->capacity = 0;
|
|
|
|
ra->max = 0;
|
2018-07-01 19:49:33 +00:00
|
|
|
ra->regtemps = 0;
|
2018-07-01 15:52:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_regalloc_deinit(JanetcRegisterAllocator *ra) {
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(ra->chunks);
|
2018-07-01 15:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fallbacks for when ctz not available */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define count_trailing_zeros(x) __builtin_ctz(x)
|
|
|
|
#define count_trailing_ones(x) __builtin_ctz(~(x))
|
|
|
|
#else
|
|
|
|
static int32_t count_trailing_ones(uint32_t x) {
|
|
|
|
int32_t ret = 0;
|
|
|
|
while (x & 1) {
|
|
|
|
ret++;
|
|
|
|
x >>= 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#define count_trailing_zeros(x) count_trailing_ones(~(x))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get ith bit */
|
|
|
|
#define ithbit(I) ((uint32_t)1 << (I))
|
|
|
|
|
|
|
|
/* Get N bits */
|
|
|
|
#define nbits(N) (ithbit(N) - 1)
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Copy a register allocator */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_regalloc_clone(JanetcRegisterAllocator *dest, JanetcRegisterAllocator *src) {
|
2018-07-01 15:52:15 +00:00
|
|
|
size_t size;
|
|
|
|
dest->count = src->count;
|
|
|
|
dest->capacity = src->capacity;
|
|
|
|
dest->max = src->max;
|
2020-01-03 04:02:57 +00:00
|
|
|
size = sizeof(uint32_t) * (size_t) dest->capacity;
|
2018-07-04 03:07:35 +00:00
|
|
|
dest->regtemps = 0;
|
2019-03-04 16:17:34 +00:00
|
|
|
if (size) {
|
2021-03-23 10:00:48 +00:00
|
|
|
dest->chunks = janet_malloc(size);
|
2019-03-04 16:17:34 +00:00
|
|
|
if (!dest->chunks) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(dest->chunks, src->chunks, size);
|
|
|
|
} else {
|
|
|
|
dest->chunks = NULL;
|
2018-07-01 15:52:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate one more chunk in chunks */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void pushchunk(JanetcRegisterAllocator *ra) {
|
2018-07-01 15:52:15 +00:00
|
|
|
/* Registers 240-255 are always allocated (reserved) */
|
|
|
|
uint32_t chunk = ra->count == 7 ? 0xFFFF0000 : 0;
|
|
|
|
int32_t newcount = ra->count + 1;
|
|
|
|
if (newcount > ra->capacity) {
|
|
|
|
int32_t newcapacity = newcount * 2;
|
2021-03-23 10:00:48 +00:00
|
|
|
ra->chunks = janet_realloc(ra->chunks, (size_t) newcapacity * sizeof(uint32_t));
|
2018-07-01 15:52:15 +00:00
|
|
|
if (!ra->chunks) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-01 15:52:15 +00:00
|
|
|
}
|
|
|
|
ra->capacity = newcapacity;
|
|
|
|
}
|
|
|
|
ra->chunks[ra->count] = chunk;
|
|
|
|
ra->count = newcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reallocate a given register */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_regalloc_touch(JanetcRegisterAllocator *ra, int32_t reg) {
|
2018-07-01 15:52:15 +00:00
|
|
|
int32_t chunk = reg >> 5;
|
|
|
|
int32_t bit = reg & 0x1F;
|
|
|
|
while (chunk >= ra->count) pushchunk(ra);
|
|
|
|
ra->chunks[chunk] |= ithbit(bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate one register. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t janetc_regalloc_1(JanetcRegisterAllocator *ra) {
|
2018-07-01 15:52:15 +00:00
|
|
|
/* Get the nth bit in the array */
|
|
|
|
int32_t bit, chunk, nchunks, reg;
|
|
|
|
bit = -1;
|
|
|
|
nchunks = ra->count;
|
|
|
|
for (chunk = 0; chunk < nchunks; chunk++) {
|
|
|
|
uint32_t block = ra->chunks[chunk];
|
|
|
|
if (block == 0xFFFFFFFF) continue;
|
|
|
|
bit = count_trailing_ones(block);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* No reg found */
|
|
|
|
if (bit == -1) {
|
|
|
|
pushchunk(ra);
|
|
|
|
bit = 0;
|
|
|
|
chunk = nchunks;
|
|
|
|
}
|
|
|
|
/* set the bit at index bit in chunk */
|
|
|
|
ra->chunks[chunk] |= ithbit(bit);
|
|
|
|
reg = (chunk << 5) + bit;
|
|
|
|
if (reg > ra->max)
|
|
|
|
ra->max = reg;
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a register. The register must have been previously allocated
|
|
|
|
* without being freed. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_regalloc_free(JanetcRegisterAllocator *ra, int32_t reg) {
|
2018-07-01 15:52:15 +00:00
|
|
|
int32_t chunk = reg >> 5;
|
|
|
|
int32_t bit = reg & 0x1F;
|
|
|
|
ra->chunks[chunk] &= ~ithbit(bit);
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:10:48 +00:00
|
|
|
/* Check if a register is set. */
|
|
|
|
int janetc_regalloc_check(JanetcRegisterAllocator *ra, int32_t reg) {
|
|
|
|
int32_t chunk = reg >> 5;
|
|
|
|
int32_t bit = reg & 0x1F;
|
|
|
|
while (chunk >= ra->count) pushchunk(ra);
|
|
|
|
return !!(ra->chunks[chunk] & ithbit(bit));
|
|
|
|
}
|
|
|
|
|
2018-07-01 15:52:15 +00:00
|
|
|
/* Get a register that will fit in 8 bits (< 256). Do not call this
|
2018-09-06 02:18:42 +00:00
|
|
|
* twice with the same value of nth without calling janetc_regalloc_free
|
2018-07-01 15:52:15 +00:00
|
|
|
* on the returned register before. */
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t janetc_regalloc_temp(JanetcRegisterAllocator *ra, JanetcRegisterTemp nth) {
|
2018-07-01 15:52:15 +00:00
|
|
|
int32_t oldmax = ra->max;
|
2018-07-04 03:07:35 +00:00
|
|
|
if (ra->regtemps & (1 << nth)) {
|
2020-05-09 17:00:01 +00:00
|
|
|
JANET_EXIT("regtemp already allocated");
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
2018-07-01 19:49:33 +00:00
|
|
|
ra->regtemps |= 1 << nth;
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t reg = janetc_regalloc_1(ra);
|
2018-07-01 15:52:15 +00:00
|
|
|
if (reg > 0xFF) {
|
|
|
|
reg = 0xF0 + nth;
|
|
|
|
ra->max = (reg > oldmax) ? reg : oldmax;
|
|
|
|
}
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janetc_regalloc_freetemp(JanetcRegisterAllocator *ra, int32_t reg, JanetcRegisterTemp nth) {
|
2018-07-04 03:07:35 +00:00
|
|
|
ra->regtemps &= ~(1 << nth);
|
|
|
|
if (reg < 0xF0)
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_regalloc_free(ra, reg);
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|