1 /*
2 * Copyright (c) 2021 Calvin Rose
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22 
23 /* Implements a simple first fit register allocator for the compiler. */
24 
25 #ifndef JANET_REGALLOC_H
26 #define JANET_REGALLOC_H
27 
28 #include <stdint.h>
29 
30 /* Placeholder for allocating temporary registers */
31 typedef enum {
32     JANETC_REGTEMP_0,
33     JANETC_REGTEMP_1,
34     JANETC_REGTEMP_2,
35     JANETC_REGTEMP_3,
36     JANETC_REGTEMP_4,
37     JANETC_REGTEMP_5,
38     JANETC_REGTEMP_6,
39     JANETC_REGTEMP_7
40 } JanetcRegisterTemp;
41 
42 typedef struct {
43     uint32_t *chunks;
44     int32_t count; /* number of chunks in chunks */
45     int32_t capacity; /* amount allocated for chunks */
46     int32_t max; /* The maximum allocated register so far */
47     int32_t regtemps; /* Hold which temp. registers are allocated. */
48 } JanetcRegisterAllocator;
49 
50 void janetc_regalloc_init(JanetcRegisterAllocator *ra);
51 void janetc_regalloc_deinit(JanetcRegisterAllocator *ra);
52 
53 int32_t janetc_regalloc_1(JanetcRegisterAllocator *ra);
54 void janetc_regalloc_free(JanetcRegisterAllocator *ra, int32_t reg);
55 int32_t janetc_regalloc_temp(JanetcRegisterAllocator *ra, JanetcRegisterTemp nth);
56 void janetc_regalloc_freetemp(JanetcRegisterAllocator *ra, int32_t reg, JanetcRegisterTemp nth);
57 void janetc_regalloc_clone(JanetcRegisterAllocator *dest, JanetcRegisterAllocator *src);
58 void janetc_regalloc_touch(JanetcRegisterAllocator *ra, int32_t reg);
59 
60 #endif
61