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 <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "cg.h"
#include "encode.h"
#include "error.h"
#include "symtab.h"
#include "todo.h"
#include <utils/loc.h>
#include <utils/printcode.h>

static enum CgSeg seg = CGSEG_TEXT;
static uint64_t addr[CGSEG_NUM_SEGMENTS];
static uint64_t alignment[CGSEG_NUM_SEGMENTS] = { 4, 1, 1 };

uint64_t cgSegStart[CGSEG_NUM_SEGMENTS];
uint64_t cgSegEnd[CGSEG_NUM_SEGMENTS];
bool cgSegFinalized;

static uint64_t
roundUp(uint64_t a, uint64_t m)
{
    return (a + m - 1) / m * m;
}

static uint64_t
gcd(uint64_t a, uint64_t b)
{
    while (b != 0) {
        size_t r = a % b;
        a = b;
        b = r;
    }
    return a;
}

enum
{
    PAGE_BITS = 16,
    PAGE_SIZE = 1 << PAGE_BITS,
    SET_BITS = 8,
    SET_SIZE = 1 << SET_BITS,
};

struct Page
{
    uint64_t tag;
    uint8_t byte[PAGE_SIZE];
    struct Page *next;
};

static struct Page *table[CGSEG_DATA + 1][SET_SIZE];
static struct Page *page[CGSEG_DATA + 1];
static uint64_t pageOffset[CGSEG_DATA + 1];

static struct Page *
getPage(enum CgSeg fromSeg, uint64_t addr, uint64_t *pageOffset)
{
    *pageOffset = addr & (PAGE_SIZE - 1);
    uint64_t set = (addr >> PAGE_BITS) & (SET_SIZE - 1);
    uint64_t tag = addr >> (PAGE_BITS + SET_BITS);

    for (struct Page *p = table[fromSeg][set]; p; p = p->next) {
        if (p->tag == tag) {
            return p;
        }
    }

    // create a clean new memory page
    struct Page *p = calloc(1, sizeof(*p));

    if (!p) {
        fprintf(stderr, "internal error: out of memory\n");
        exit(1);
    }

    p->tag = tag;
    if (table[fromSeg][set]) {
        p->next = table[fromSeg][set];
    }
    table[fromSeg][set] = p;
    return p;
}

void
cgFixBytes(enum CgSeg cgSeg, uint64_t addr, size_t numBytes, uint64_t val)
{
    uint64_t pageOffset;
    struct Page *p = getPage(cgSeg, addr, &pageOffset);
    assert(pageOffset + numBytes <= PAGE_SIZE);

    for (size_t i = 0; i < numBytes; ++i) {
        p->byte[pageOffset + numBytes - i - 1] |= val;
        val >>= 8;
    }
}

void
cgAppendBytes(size_t numBytes, uint64_t val)
{
    assert(numBytes <= 8);
    assert(!cgSegFinalized);
    if (addr[seg] % numBytes) {
        cgAlign(numBytes);
    }

    if (seg == CGSEG_BSS) {
        if (val) {
            error("can not write non-zero values into BSS segment\n");
        }
    } else {
        assert(pageOffset[seg] % numBytes == 0);

        if (!page[seg] || pageOffset[seg] >= PAGE_SIZE) {
            // get new page
            page[seg] = getPage(seg, addr[seg], &pageOffset[seg]);
        }

        // write bytes in big endian format
        for (size_t i = 0; i < numBytes; ++i) {
            page[seg]->byte[pageOffset[seg] + numBytes - i - 1] = val & 0xFF;
            val >>= 8;
        }
        pageOffset[seg] += numBytes;
    }
    addr[seg] += numBytes;
}

void
cgAppendString(const char *str)
{
    for (const char *s = str; *s; ++s) {
        cgAppendBytes(1, *s);
    }
    cgAppendBytes(1, 0);
}

void
cgAppendSpace(size_t numBytes)
{
    while (numBytes--) {
        cgAppendBytes(1, 0);
    }
}

void
cgSetActiveSegment(enum CgSeg cgSeg_)
{
    seg = cgSeg_;
}

enum CgSeg
cgGetActiveSegment(void)
{
    return seg;
}

uint64_t
cgGetAddr(void)
{
    return addr[seg];
}

void
cgAlign(size_t alignTo)
{
    if (alignTo == 0) {
        error("alignment has to be larger than 0\n");
    }
    uint64_t padding = roundUp(addr[seg], alignTo) - addr[seg];

    for (uint64_t i = 0; i < padding; ++i) {
        cgAppendBytes(1, 0x00);
    }
    if (padding) {
        cgAppendComment(" for alignment");
    }

    alignment[seg] *= alignTo / gcd(alignment[seg], alignTo);
}

static struct CommentNode
{
    uint64_t addr;
    const char *str;
    struct CommentNode *next;
} * comment[CGSEG_DATA + 1], *commentLast[CGSEG_DATA + 1];

void
cgAppendComment(const char *str)
{
    if (!*str || seg == CGSEG_BSS) {
        return;
    }
    str = strdup(str);
    struct CommentNode *node = malloc(sizeof(*node));

    if (!str || !node) {
        fprintf(stderr, "addInstr: out of memory\n");
        exit(1);
    }

    node->addr = addr[seg];
    node->str = str;
    node->next = 0;

    if (comment[seg]) {
        commentLast[seg] = commentLast[seg]->next = node;
    } else {
        commentLast[seg] = comment[seg] = node;
    }
}

void
cgAppendInstr(uint32_t opCode, ...)
{
    va_list argp;
    va_start(argp, opCode);
    uint32_t code = encodeInstr(seg, addr[seg], opCode, argp);
    va_end(argp);

    cgAppendBytes(4, code);
}

void
cgAppendInteger(size_t numBytes, struct Expr *expr)
{
    uint64_t val = encodeExpr(seg, addr[seg], numBytes, expr);
    cgAppendBytes(numBytes, val);
}

void
cgSetLabel(struct Loc loc, const struct UStr *ident)
{
    enum ExprType type = seg == CGSEG_TEXT   ? REL_TEXT
                         : seg == CGSEG_DATA ? REL_DATA
                                             : REL_BSS;
    symtabSet(ident, makeValExpr(loc, type, addr[seg]));
}

static void
finalizeSegments()
{
    for (size_t i = CGSEG_DATA; i <= CGSEG_BSS; ++i) {
        cgSegEnd[i - 1] = cgSegStart[i - 1] + addr[i - 1];
        cgSegStart[i] = roundUp(cgSegEnd[i - 1], alignment[i]);
    }
    cgSegFinalized = true;
}

void
cgPrint(FILE *out)
{
    finalizeSegments();
    encodeFixables();

    uint64_t pageOffset;
    struct Page *p;

    p = 0;
    pageOffset = 0;
    printCode(out, 0, "#TEXT %" PRIu64 "\n", alignment[CGSEG_TEXT]);
    for (uint64_t i = 0; i < addr[CGSEG_TEXT]; i += 4, pageOffset += 4) {
        printCode(out, 0, "0x%016" PRIX64 ":", i + cgSegStart[CGSEG_TEXT]);

        if (!p || pageOffset >= PAGE_SIZE) {
            // get new page
            p = getPage(CGSEG_TEXT, i, &pageOffset);
        }

        printCode(out, 0, " %02" PRIX8, p->byte[pageOffset + 0]);
        printCode(out, 0, " %02" PRIX8, p->byte[pageOffset + 1]);
        printCode(out, 0, " %02" PRIX8, p->byte[pageOffset + 2]);
        printCode(out, 0, " %02" PRIX8, p->byte[pageOffset + 3]);

        while (comment[CGSEG_TEXT] && comment[CGSEG_TEXT]->addr <= i + 4) {
            printCode(out, 0, " #     %s", comment[CGSEG_TEXT]->str);
            comment[CGSEG_TEXT] = comment[CGSEG_TEXT]->next;
        }

        printCode(out, 0, "\n");
    }

    p = 0;
    pageOffset = 0;
    printCode(out, 0, "#DATA %" PRIu64 "\n", alignment[CGSEG_DATA]);

    bool printDataAddr = true;
    for (uint64_t i = 0; i < addr[CGSEG_DATA]; ++i, ++pageOffset) {
        if (printDataAddr) {
            printCode(out, 0, "0x%016" PRIX64 ":", i + cgSegStart[CGSEG_DATA]);
            printDataAddr = false;
        }

        if (!p || pageOffset >= PAGE_SIZE) {
            // get new page
            p = getPage(CGSEG_DATA, i, &pageOffset);
        }

        printCode(out, 0, " %02" PRIX8, p->byte[pageOffset]);

        while (comment[CGSEG_DATA] && comment[CGSEG_DATA]->addr <= i + 1) {
            printCode(out, 0, " #     %s\n", comment[CGSEG_DATA]->str);
            comment[CGSEG_DATA] = comment[CGSEG_DATA]->next;
            printDataAddr = true;
        }
    }
    if (!printDataAddr) {
        printCode(out, 0, "\n");
    }

    printCode(out, 0, "#BSS %" PRIu64 " %" PRIu64 "\n", alignment[CGSEG_BSS],
              addr[CGSEG_BSS]);
}