summaryrefslogtreecommitdiffstats
path: root/src/register.c
blob: bfe347aaa5311e41456a7ad68a8dd87cf9289d4d (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
#include "register.h"

#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define REG_NUM 15
// #define REG_NUM 5
#define REG_ADDR "x9"
#define REG_SWAP "w10"
#define SPILL_MAX 32
#define REG_LOCK_MAX 3
#define REG_SIZE 4

static const char *reg_name[REG_NUM] = {
    "w11", "w12", "w13", "w14", "w15", "w19", "w20", "w21", "w22", "w23", "w24", "w25", "w26", "w27", "w28"};
    // "w11", "w12", "w13", "w14", "w15"};

CcmmcRegPool *ccmmc_register_init(FILE *asm_output)
{
    CcmmcRegPool *pool = malloc(sizeof(CcmmcRegPool));
    pool->num = REG_NUM;
    pool->list = malloc(sizeof(CcmmcReg*) * pool->num);
    for (int i = 0; i < pool->num; i++) {
        pool->list[i] = malloc(sizeof(CcmmcReg));
        pool->list[i]->tmp = NULL;
        pool->list[i]->lock = false;
        pool->list[i]->name = reg_name[i];
    }
    pool->spill_max = SPILL_MAX;
    pool->spill = malloc(sizeof(CcmmcTmp*) * pool->spill_max);
    pool->top = 0;
    pool->lock_max = REG_LOCK_MAX;
    pool->lock_cnt = 0;
    pool->print_buf = malloc(sizeof(char) * ((REG_NUM + 1) * 25));
    pool->asm_output = asm_output;
    return pool;
}

CcmmcTmp *ccmmc_register_alloc(CcmmcRegPool *pool, uint64_t *offset)
{
    CcmmcTmp *tmp = malloc(sizeof(CcmmcTmp));
    if (pool->top < pool->num) {
        // tmp
        tmp->addr = 0;
        tmp->reg = pool->list[pool->top];

        // reg
        tmp->reg->tmp = tmp;
        tmp->reg->lock = false;

        // pool
        pool->top++;
    }
    else {
        // gen code to alloc space on the stack
        fprintf(pool->asm_output,
            "\t\t/* ccmmc_register_alloc(): */\n"
            "\t\tsub\tsp, sp, #%d\n",
            REG_SIZE);

        // tmp and offset
        *offset += REG_SIZE;
        tmp->addr = *offset;
        tmp->reg = NULL;

        // spill
        if (pool->top - pool->num >= pool->spill_max) {
            pool->spill_max *= 2;
            pool->spill = realloc(pool->spill, sizeof(CcmmcTmp*) * pool->spill_max);
        }
        pool->spill[pool->top - pool->num] = tmp;

        // pool
        pool->top++;
    }
    return tmp;
}

const char *ccmmc_register_lock(CcmmcRegPool *pool, CcmmcTmp *tmp)
{
    const char *reg = NULL;
    if (pool->lock_cnt < pool->lock_max) {
        if (tmp->reg !=NULL) {
            if (!tmp->reg->lock) {
                // reg
                tmp->reg->lock = true;

                // pool
                pool->lock_cnt++;
            }
            reg = tmp->reg->name;
        }
        else {
            // find a unlocked reg
            int i, j;
            for (i = 0; i < pool->num && pool->list[i]->lock; i++);
            assert(i < pool->num); //must found

            // gen code to swap the tmp in the register and the tmp on the stack
            fprintf(pool->asm_output,
                "\t\t/* ccmmc_register_lock(): swap %s, [fp, #-%" PRIu64 "] */\n",
                pool->list[i]->name,
                tmp->addr);
            fprintf(pool->asm_output, // REG_ADDR holds the address on the stack
                "\t\tldr\t" REG_ADDR ", =%" PRIu64 "\n"
                "\t\tsub\t" REG_ADDR ", fp, " REG_ADDR "\n"
                "\t\tmov\t" REG_SWAP ", %s\n"
                "\t\tldr\t%s, [" REG_ADDR "]\n"
                "\t\tstr\t" REG_SWAP ", [" REG_ADDR "]\n",
                tmp->addr,
                pool->list[i]->name,
                pool->list[i]->name);

            // find the index of tmp in pool->spill
            for (j = 0; j < pool->top - pool->num && pool->spill[j] != tmp; j++);

            // spill
            pool->spill[j] = pool->list[i]->tmp;

            // old tmp of the reg
            pool->list[i]->tmp->reg = NULL;
            pool->list[i]->tmp->addr = tmp->addr;

            // tmp
            tmp->reg = pool->list[i];
            tmp->addr = 0;

            // register
            tmp->reg->tmp = tmp;
            tmp->reg->lock = true;

            // pool
            pool->lock_cnt++;

            reg = tmp->reg->name;
        }
    }
    return reg;
}

void ccmmc_register_unlock(CcmmcRegPool *pool, CcmmcTmp *tmp)
{
    if (tmp->reg != NULL && tmp->reg->lock) {
        // reg
        tmp->reg->lock = false;

        // pool
        pool->lock_cnt--;
    }
}

void ccmmc_register_free(CcmmcRegPool *pool, CcmmcTmp *tmp, uint64_t *offset)
{
    // find the index of register associated with tmp
    int i;
    for (i = 0; i < pool->num && pool->list[i]->tmp != tmp; i++);
    if (pool->top <= pool->num) {
        // tmp
        free(tmp);

        // pool
        pool->top--;
        assert(i < pool->num); //must found
        if (i < pool->top) {
            CcmmcReg *swap = pool->list[i];
            pool->list[i] = pool->list[pool->top];
            pool->list[pool->top] = swap;
        }
    }
    else if (i < pool->num) {
        // pool
        pool->top--;

        // gen code to move the last tmp to this register
        fprintf(pool->asm_output,
            "\t\t/* ccmmc_register_free(): mov %s, [fp, #-%" PRIu64 "] */\n",
            tmp->reg->name, pool->spill[pool->top - pool->num]->addr);
        fprintf(pool->asm_output, // REG_ADDR holds the address on the stack
            "\t\tldr\t" REG_ADDR ", =%" PRIu64 "\n"
            "\t\tsub\t" REG_ADDR ", fp, " REG_ADDR "\n"
            "\t\tldr\t%s, [" REG_ADDR "]\n"
            "\t\tadd\tsp, sp, #%d\n",
            pool->spill[pool->top - pool->num]->addr,
            tmp->reg->name,
            REG_SIZE);

        // offset
        *offset -= REG_SIZE;

        // reg
        tmp->reg->tmp = pool->spill[pool->top - pool->num];

        // the last tmp
        tmp->reg->tmp->reg = tmp->reg;
        tmp->reg->tmp->addr = 0;

        // tmp
        free(tmp);
    }
    else {
        for (i = 0; i < pool->top - pool->num && pool->spill[i] != tmp; i++);
        assert(i < pool->top - pool->num); //must found

        // pool
        pool->top--;

        if (i < pool->top - pool->num) {
            // gen code to move the last tmp to the hole
            fprintf(pool->asm_output,
                "\t\t/* ccmmc_register_free(): "
                "mov [fp, #-%" PRIu64 "] , [fp, #-%" PRIu64 "] */\n",
                pool->spill[i]->addr,
                pool->spill[pool->top - pool->num]->addr);
            fprintf(pool->asm_output,
                "\t\tldr\t" REG_ADDR ", =%" PRIu64 "\n"
                "\t\tsub\t" REG_ADDR ", fp, " REG_ADDR "\n"
                "\t\tldr\t" REG_SWAP ", [" REG_ADDR "]\n"
                "\t\tldr\t" REG_ADDR ", =%" PRIu64 "\n"
                "\t\tsub\t" REG_ADDR ", fp, " REG_ADDR "\n"
                "\t\tstr\t" REG_SWAP ", [" REG_ADDR "]\n"
                "\t\tadd\tsp, sp, #%d\n",
                pool->spill[pool->top - pool->num]->addr,
                pool->spill[i]->addr,
                REG_SIZE);

            // offset
            *offset -= REG_SIZE;

            // spill
            pool->spill[pool->top - pool->num]->addr = pool->spill[i]->addr;
            free(pool->spill[i]);
            pool->spill[i] = pool->spill[pool->top - pool->num];
        }
        else {
            // gen code to remove the last tmp
            fprintf(pool->asm_output,
                "\t\t/* ccmmc_register_free(): */\n"
                "\t\tadd\tsp, sp, #%d\n", REG_SIZE);

            // offset
            *offset -= REG_SIZE;

            // spill
            free(pool->spill[i]);
        }
    }
}

void ccmmc_register_extend_name(CcmmcTmp *tmp, char *extend_name)
{
    if (tmp->reg != NULL) {
        strcpy(extend_name, tmp->reg->name);
        extend_name[0] = 'x';
    }
    else extend_name[0] = '\0';
}

void ccmmc_register_caller_save(CcmmcRegPool *pool)
{
    char buf[25];
    int bound = pool->num;
    if (pool->top < bound)
        bound = pool->top;

    for (int i = 0; i < bound; i++)
        fprintf(pool->asm_output, "\tstr\t%s, [sp, #-%d]\n",
            pool->list[i]->name,
            (i + 1) * REG_SIZE);
    fprintf(pool->asm_output, "\tsub\tsp, sp, %d\n", bound * REG_SIZE);

    sprintf(pool->print_buf, "\tadd\tsp, sp, %d\n", bound * REG_SIZE);
    for (int i = 0; i < bound; i++) {
        sprintf(buf, "\tldr\t%s, [sp, #-%d]\n",
            pool->list[i]->name,
            (i + 1) * REG_SIZE);
        strcat(pool->print_buf, buf);
    }
}

void ccmmc_register_caller_load(CcmmcRegPool *pool)
{
    fputs(pool->print_buf, pool->asm_output);
}

void ccmmc_register_save_arguments(CcmmcRegPool *pool, int arg_count)
{
    if (arg_count <= 0)
        return;
    if (arg_count >= 8)
        arg_count = 8;
    for (int i = 0; i < arg_count; i++)
        fprintf(pool->asm_output, "\tstr\tx%d, [sp, #-%d]\n",
            i, (i + 1) * 8);
    fprintf(pool->asm_output, "\tsub\tsp, sp, %d\n", arg_count * 8);
}

void ccmmc_register_load_arguments(CcmmcRegPool *pool, int arg_count)
{
    if (arg_count <= 0)
        return;
    if (arg_count >= 8)
        arg_count = 8;
    fprintf(pool->asm_output, "\tadd\tsp, sp, %d\n", arg_count * 8);
    for (int i = 0; i < arg_count; i++)
        fprintf(pool->asm_output, "\tldr\tx%d, [sp, #-%d]\n",
            i, (i + 1) * 8);
}

void ccmmc_register_fini(CcmmcRegPool *pool)
{
    // TODO: free register pool
}

// vim: set sw=4 ts=4 sts=4 et: