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

#include "error.h"
#include "fmt.h"
#include <utils/printcode.h>
#include <utils/str.h>

/*
    List of formats linke 'RRR (OP u 8) (X u 8) (Y  u  8) (Z u  8)'
    Each format node has:
    - an identifier
    - a list with its fields
    - the number of fields
    - a list with opcodes using this format
    - a pointer to the next format node
*/
static struct FmtNode
{
    const struct UStr *id;

    // struct FmtField represents fields like (OP u 8)
    struct FmtFieldNode
    {
        const struct UStr *id;
        // position in list
        size_t index;
        // bitPos = position of least significant bit
        size_t bitPos, numBits;
        enum FmtFieldType type;
        struct FmtFieldNode *next;
    } * field, *fieldLast;

    size_t numFieldNodes;

    struct OpCodeNode
    {
        uint32_t cached;
        struct OpCodeNode *next;
    } * opCode, *opCodeLast;

    struct FmtNode *next;

} * fmtNode, *fmtNodeLast;

// size of Op code needs to be the same for all formats
static size_t sizeOpCode;

struct FmtNode *
addFmt(const struct UStr *fmtId)
{
    if (getFmt(fmtId)) {
        error("Format '%s' already defined\n", fmtId->cstr);
        return 0;
    }
    struct FmtNode *node = malloc(sizeof(*node));
    if (!node) {
        fprintf(stderr, "addFmt: out of memory\n");
        exit(1);
    }
    node->id = fmtId;
    node->field = node->fieldLast = 0;
    node->numFieldNodes = 0;
    node->opCode = node->opCodeLast = 0;
    node->next = 0;

    if (fmtNode) {
        fmtNodeLast = fmtNodeLast->next = node;
    } else {
        fmtNode = fmtNodeLast = node;
    }
    return fmtNodeLast;
}

struct FmtNode *
getFmt(const struct UStr *fmtId)
{
    for (struct FmtNode *n = fmtNode; n; n = n->next) {
        if (n->id == fmtId) {
            return n;
        }
    }
    return 0;
}

const char *
getFmtId(const struct FmtNode *fmt)
{
    return fmt->id->cstr;
}

void
appendFmtField(struct FmtNode *fmt, const struct UStr *fieldId, size_t numBits,
               enum FmtFieldType type)
{
    assert(fmt);
    assert(fmtNode);

    if (!fmt->field) {
        // this is the first field with the op code
        if (fmtNode == fmt) {
            // this is the first format and defines the size of the op code
            sizeOpCode = numBits;
        } else {
            if (numBits != sizeOpCode) {
                error("Size of the op code fiels can not vary. Its size was "
                      "specified to have  %zu bits by format '%s'.\n",
                      sizeOpCode, fmtNode->id->cstr);
            }
        }
    }

    size_t prevFieldSize = 0;
    size_t index = 0;
    for (const struct FmtFieldNode *f = fmt->field; f; f = f->next, ++index) {
        if (f->id == fieldId) {
            error("Format '%s' already has a field called '%s'\n",
                  fmt->id->cstr, fieldId->cstr);
        }
        prevFieldSize += f->numBits;
    }
    if (prevFieldSize + numBits > 32) {
        error("previous fields have a total size of %zu bits. Appending field "
              "'%s' of with %zu bits exceeds 32 bits.\n",
              prevFieldSize, fieldId->cstr, numBits);
    }

    struct FmtFieldNode *node = malloc(sizeof(*node));
    if (!node) {
        fprintf(stderr, "appendFmtField: out of memory\n");
        exit(1);
    }
    node->id = fieldId;
    node->index = index;
    node->bitPos = 32 - (prevFieldSize + numBits);
    node->numBits = numBits;
    node->type = type;
    node->next = 0;

    // append field
    if (fmt->field) {
        fmt->fieldLast = fmt->fieldLast->next = node;
    } else {
        fmt->field = fmt->fieldLast = node;
    }
    ++fmt->numFieldNodes;
}

size_t
getFmtNumFields(const struct FmtNode *fmt)
{
    return fmt->numFieldNodes;
}

const struct FmtFieldNode *
getFmtField(const struct FmtNode *fmt, const struct UStr *fieldId)
{
    for (const struct FmtFieldNode *f = fmt->field; f; f = f->next) {
        if (f->id->cstr == fieldId->cstr) {
            return f;
        }
    }
    return 0;
}

const char *
getFmtFieldId(const struct FmtFieldNode *field)
{
    assert(field);
    return field->id->cstr;
}

size_t
getFmtFieldIndex(const struct FmtFieldNode *field)
{
    assert(field);
    return field->index;
}

void
printRefmanFieldsDescription(FILE *out)
{
    const char *fType[] = { "Signed", "Unsigned", "JumpOffset" };
    for (const struct FmtNode *n = fmtNode; n; n = n->next) {
        printCode(out, 0, "namespace %s {\n", n->id->cstr);

        for (const struct FmtFieldNode *f = n->field; f; f = f->next) {
            printCode(out, 1,
                      "FormatFieldNode %s{\"%s\", \"%s\", %zu, %zu, "
                      "FormatFieldNode::%s};\n",
                      f->id->cstr, n->id->cstr, f->id->cstr, f->bitPos,
                      f->numBits, fType[f->type]);
        }

        printCode(out, 0, "} // namespace %s\n\n", n->id->cstr);
    }
}

void
printRefmanFormatDescription(FILE *out)
{
    for (const struct FmtNode *n = fmtNode; n; n = n->next) {
        printCode(out, 0, "namespace ULM_FMT { namespace %s {\n", n->id->cstr);
        for (const struct FmtFieldNode *f = n->field; f; f = f->next) {
            printCode(
              out, 1,
              "auto %s = ulmdoc::declareBitField(\"%s\", Expr::%s, %zu);\n",
              f->id->cstr, f->id->cstr,
              f->type == SIGNED     ? "SIGNED"
              : f->type == UNSIGNED ? "UNSIGNED"
                                    : "JUMP_OFFSET",
              f->numBits);
        }
        printCode(out, 1,
                  "auto ulm_instrFmt = ulmdoc::InstrFmt({");
        for (const struct FmtFieldNode *f = n->field; f; f = f->next) {
            printCode(out, 0, "%s", f->id->cstr);
            if (f->next) {
                printCode(out, 0, ", ", f->id->cstr);
            }
        }
        printCode(out, 0, "});\n");

        printCode(out, 0, "}} // namespace ULM_FMT::%s\n\n", n->id->cstr);
    }

    /*
    for (const struct FmtNode *n = fmtNode; n; n = n->next) {
        printCode(out, 0, "addFormat(\"%s\", {", n->id->cstr);
        for (const struct FmtFieldNode *f = n->field; f; f = f->next) {
            printCode(out, 0, "%s::%s", n->id->cstr, f->id->cstr);
            if (f->next) {
                printCode(out, 0, ", ");
            }
        }
        printCode(out, 0, "});\n");
        for (const struct FmtFieldNode *f = n->field; f; f = f->next) {
            printCode(out, 0, "mathOperator.insert(\"%s\");\n", f->id->cstr);
        }
    }
    */
}

void
addOpCodeToFmt(struct FmtNode *fmt, uint32_t opCode)
{
    struct OpCodeNode *node = malloc(sizeof(*node));
    if (!node) {
        fprintf(stderr, "addOpCodeToFmt: out of memory\n");
        exit(1);
    }
    node->cached = opCode;
    node->next = 0;

    if (fmt->opCode) {
        fmt->opCodeLast = fmt->opCodeLast->next = node;
    } else {
        fmt->opCode = fmt->opCodeLast = node;
    }
}

void
printOpcodeDef(FILE *out, const char *macroIdent, const char *instrRegId)
{
    printCode(out, 0, "#define %s ((%s) >> %zu)\n", macroIdent, instrRegId,
              32 - sizeOpCode);
}

void
printFmtDef(FILE *out, const struct FmtNode *fmt, const char *instrRegId)
{
    for (const struct FmtFieldNode *f = fmt->field; f; f = f->next) {
        printCode(out, 0, "#define %s \t(((%s) (%s) << %zu) >> %zu)\n",
                  f->id->cstr, f->type == UNSIGNED ? "uint32_t" : "int32_t",
                  instrRegId, 32 - (f->bitPos + f->numBits),
                  32 - f->numBits - (f->type == JMP_OFFSET ? 2 : 0));
    }
}

void
printFmtUndef(FILE *out, const struct FmtNode *fmt)
{
    for (const struct FmtFieldNode *f = fmt->field; f; f = f->next) {
        printCode(out, 0, "#undef %s \n", f->id->cstr);
    }
}

void
printFmtList(FILE *out)
{
}

void
printFmtInstrEncoding(FILE *out)
{
    const char *ft[NUM_FIELD_TYPES] = { "SIGNED", "UNSIGNED", "JMP_OFFSET" };
    static const struct UStr *opFieldId;
    static bool first = true;

    if (first) {
        opFieldId = makeUStr("OP");
        first = false;
    }

    printCode(out, 0, "uint32_t\n");
    printCode(out, 0,
              "encodeInstr(enum CgSeg cgSeg, uint64_t addr, uint32_t opCode, "
              "va_list vl)\n");
    printCode(out, 0, "{\n");
    printCode(out, 1, "uint32_t instr = 0;\n");
    printCode(out, 1, "switch (opCode) {\n");

    for (const struct FmtNode *n = fmtNode; n; n = n->next) {
        bool fmtUsed = false;
        for (const struct OpCodeNode *op = n->opCode; op; op = op->next) {
            printCode(out, 3, "case 0x%02" PRIX32 ":\n", op->cached);
            fmtUsed = true;
        }
        if (!fmtUsed) {
            continue;
        }
        assert(n->field); // we need at least the OP field
        for (const struct FmtFieldNode *f = n->field; f; f = f->next) {
            if (f->id == opFieldId) {
                // type is always UNSIGNED
                printCode(out, 3, "encodeOperand(&instr, %zu, %zu, opCode);\n",
                          f->bitPos, f->numBits);
            } else {
                printCode(out, 3,
                          "encodeExprOperand(cgSeg, addr, &instr, %zu, %zu, "
                          "%s, va_arg(vl, struct Expr *));\n",
                          f->bitPos, f->numBits, ft[f->type]);
            }
        }
        printCode(out, 2, "break;\n");
    }

    printCode(out, 2, "default:;\n");
    printCode(out, 1, "}\n");
    printCode(out, 0, "\n");
    printCode(out, 1, "return instr;\n");
    printCode(out, 0, "}\n");
}