aboutsummaryrefslogtreecommitdiffgithub
path: root/novice/assembler/codegen/base.ts
blob: 3d4d918616cfc2cb8912814f5cbc2beedd841f6d (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { AliasFields, AliasSpec, Assembly, Instruction, InstructionSpec,
         Isa, MachineCodeSection, PseudoOp, Section,
         SymbTable  } from '../../isa';
import { maskTo, maxUnsignedVal } from '../../util';
import { AsmContext, OpOperands, OpSpec, PseudoOpSpec } from '../opspec';
import { MachineCodeGenerator } from './codegen';

interface ReassembleVictim {
    // Index in words array in MachineCodeSection
    index: number;
    sectionIdx: number;
    instrIdx: number;
}

interface HasOp { op: string; }
type Lut<T extends HasOp> = {[op: string]: T[]};

class BaseMachineCodeGenerator implements MachineCodeGenerator {
    protected isa: Isa;
    protected opSpec: PseudoOpSpec;
    protected instrLut: Lut<InstructionSpec>;
    protected aliasLut: Lut<AliasSpec>;
    protected opLut: Lut<OpSpec>;

    public constructor(isa: Isa, opSpec: PseudoOpSpec) {
        this.isa = isa;
        this.opSpec = opSpec;
        this.instrLut = this.genLut(this.isa.instructions);
        this.aliasLut = this.genLut(this.isa.aliases);
        this.opLut = this.genLut(this.opSpec.ops);
    }

    public gen(asm: Assembly):
            [SymbTable, MachineCodeSection[]] {
        const sections: MachineCodeSection[] = [];
        const symbtable: SymbTable = {};
        const reassemble: ReassembleVictim[] = [];
        // Convert the map of labels in the assembly into a stack we can
        // check and pop in O(1). This is [label, sectionIdx, instrIdx]
        const labels: [string, number, number][] =
            (Object.keys(asm.labels)
                   .map(label => [label, asm.labels[label][0], asm.labels[label][1]]) as [string, number, number][])
                   .sort((left, right) => right[1] - left[1] || right[2] - left[2]);

        for (let i = 0; i < asm.sections.length; i++) {
            const asmSection = asm.sections[i];
            sections.push({startAddr: asmSection.startAddr, words: []});

            for (let j = 0; j < asmSection.instructions.length; j++) {
                const pc = sections[i].startAddr + sections[i].words.length;

                while (labels.length > 0 &&
                        labels[labels.length - 1][1] === i &&
                        labels[labels.length - 1][2] === j) {
                    symbtable[(labels.pop() as [string, number, number])[0]] = pc;
                }

                const instr = asmSection.instructions[j];
                const [words, hasLabel] = this.inflateInstr(instr, pc, null);

                // If this instruction uses a label, we'll need to
                // reassmble it
                if (hasLabel) {
                    reassemble.push({index: sections[i].words.length,
                                     sectionIdx: i,
                                     instrIdx: j});
                }

                for (const word of words) {
                    sections[i].words.push(word);
                }
            }
        }

        for (const victim of reassemble) {
            const section = asm.sections[victim.sectionIdx];
            const instr = section.instructions[victim.instrIdx];
            const pc = sections[victim.sectionIdx].startAddr + victim.index;
            const [words, _] = this.inflateInstr(instr, pc, symbtable);

            for (let k = 0; k < words.length; k++) {
                sections[victim.sectionIdx].words[victim.index + k] = words[k];
            }
        }

        // Check for overlapping sections
        const sortedSections = sections.slice(0);
        sortedSections.sort((left, right) => left.startAddr - right.startAddr);
        for (let i = 1; i < sortedSections.length; i++) {
            // TODO: support different addressability
            const left = sortedSections[i - 1];
            const right = sortedSections[i];
            if (right.startAddr >= left.startAddr &&
                    right.startAddr < left.startAddr + left.words.length) {
                throw new Error(`sections at x${left.startAddr.toString(16)} ` +
                                `and x${right.startAddr.toString(16)} overlap`);
            }
        }

        return [symbtable, sections];
    }

    protected genLut<T extends HasOp>(ops: T[]): Lut<T> {
        const lut: Lut<T> = {};

        for (const op of ops) {
            if (!(op.op in lut)) {
                lut[op.op] = [];
            }
            lut[op.op].push(op);
        }

        return lut;
    }

    private inflateInstr(instr: Instruction|PseudoOp,
                         pc: number,
                         symbtable: SymbTable|null): [number[], boolean] {
        if (instr.kind === 'pseudoop') {
            let match: OpSpec|null = null;
            const candidates = this.opLut[instr.op] || [];

            for (const op of candidates) {
                if (this.opMatch(instr, op)) {
                    match = op;
                    break;
                }
            }

            if (!match) {
                // TODO: multiple operands
                const operand = (instr.operand ? instr.operand.kind : 'no') + ' operand';

                throw new Error(`unknown assembler directive .${instr.op} ` +
                                `with ${operand} on line ${instr.line}`);
            }

            if (!symbtable && instr.operand &&
                    instr.operand.kind === 'label') {
                // TODO: make sure this is true
                const estimator = match.size as ((isa: Isa) => number);
                return [new Array<number>(estimator(this.isa)), true];
            } else {
                const ctx: AsmContext = {isa: this.isa, symbtable: symbtable as SymbTable};
                const operands: OpOperands = this.operandify(match, instr);
                return [match.asm(ctx, operands), false];
            }
        } else {
            // If it's an alias, expand that mf
            const aliasCandidates = this.aliasLut[instr.op] || [];

            for (const alias of aliasCandidates) {
                if (this.aliasMatch(instr, alias)) {
                    if (!symbtable) {
                        return [new Array<number>(alias.size), true];
                    } else {
                        const instrs = alias.asm(
                            {pc, line: instr.line, symbtable},
                            this.genAliasFields(instr, alias));
                        const allWords: number[] = [];

                        for (const subInstr of instrs) {
                            const newPc = pc + allWords.length;
                            const [words, hasLabel] =
                                this.inflateInstr(subInstr, newPc, symbtable);
                            for (const word of words) {
                                allWords.push(word);
                            }
                        }

                        return [allWords, false];
                    }
                }
            }

            let match: InstructionSpec|null = null;
            const candidates = this.instrLut[instr.op] || [];

            for (const isaInstr of candidates) {
                if (this.instrMatch(instr, isaInstr)) {
                    match = isaInstr;
                    break;
                }
            }

            if (!match) {
                // TODO: include reg prefixes
                const operands = (instr.operands.length > 0) ?
                    'operands ' + instr.operands.map(operand => operand.kind)
                                                .join(', ')
                    : 'no operands';

                throw new Error(`unknown instruction ${instr.op} with ` +
                                `${operands} on line ${instr.line}`);
            }

            const skip = !symbtable &&
                         instr.operands.some(op => op.kind === 'label');
            const instrBin = skip ? 0 : this.genInstrBin(instr, match, pc,
                                                         symbtable as SymbTable);
            return [[instrBin], skip];
        }
    }

    private operandify(opSpec: OpSpec, pseudoOp: PseudoOp): OpOperands {
        const operands: OpOperands = {ints: {}, labels: {}, strings: {}};

        // TODO: >1 operands
        if (pseudoOp.operand) {
            const spec = opSpec.operands[0];

            switch (pseudoOp.operand.kind) {
                case 'string':
                    operands.strings[spec.name] = pseudoOp.operand.contents;
                    break;
                case 'int':
                    operands.ints[spec.name] = pseudoOp.operand.val;
                    break;
                case 'label':
                    operands.labels[spec.name] = pseudoOp.operand.label;
                    break;
            }
        }

        return operands;
    }

    private genAliasFields(instr: Instruction, alias: AliasSpec): AliasFields {
        const fields: AliasFields = {regs: {}, ints: {}, labels: {}};

        for (let i = 0; i < instr.operands.length; i++) {
            const operand = instr.operands[i];
            const name = alias.operands[i].name;

            switch (operand.kind) {
                case 'reg':
                    fields.regs[name] = [operand.prefix, operand.num];
                    break;

                case 'int':
                    fields.ints[name] = operand.val;
                    break;

                case 'label':
                    fields.labels[name] = operand.label;
                    break;

                default:
                    const _: never = operand;
            }
        }

        return fields;
    }

    private genInstrBin(instr: Instruction, isaInstr: InstructionSpec,
                        pc: number, symbtable: SymbTable): number {
        let bin = 0;
        let o = 0;

        for (const field of isaInstr.fields) {
            if (field.kind === 'const') {
                const numBits = field.bits[0] - field.bits[1] + 1;
                bin |= maskTo(field.val, numBits) << field.bits[1];
            } else {
                const operand = instr.operands[o++];
                const numBits = field.bits[0] - field.bits[1] + 1;
                // max/min values.
                const maxUns = maxUnsignedVal(numBits);
                const maxTwos = maxUnsignedVal(numBits - 1);
                const minTwos = -maxUnsignedVal(numBits - 1) - 1;

                if (field.kind === 'reg' && operand.kind === 'reg') {
                    if (operand.num < 0) {
                        throw new Error(`negative register number ` +
                                        `${operand.num} on line ${instr.line}`);
                    }
                    if (operand.num > maxUns) {
                        throw new Error(`register number ${operand.num} ` +
                                        `requires too many bits (max is ` +
                                        `${maxUns}) on line ${instr.line}`);
                    }

                    bin |= operand.num << field.bits[1];
                } else if (field.kind === 'imm' && operand.kind === 'int') {
                    if (field.sext) {
                        if (operand.val < minTwos) {
                            throw new Error(`immediate value ${operand.val} ` +
                                            `is smaller than minimum ${minTwos} ` +
                                            `on line ${instr.line}`);
                        }
                        if (operand.val > maxTwos) {
                            throw new Error(`immediate value ${operand.val} ` +
                                            `is larger than maximum ${maxTwos} ` +
                                            `on line ${instr.line}`);
                        }
                    } else {
                        if (operand.val < 0) {
                            throw new Error(`negative immediate value ` +
                                            `${operand.val} in a ` +
                                            `non-sign-extended field on ` +
                                            `line ${instr.line}`);
                        }
                        if (operand.val > maxUns) {
                            throw new Error(`immediate value ${operand.val} ` +
                                            `is larger than maximum ${maxUns} ` +
                                            `on line ${instr.line}`);
                        }
                    }

                    bin |= maskTo(operand.val, numBits) << field.bits[1];
                } else if (field.kind === 'imm' && operand.kind === 'label') {
                    if (!symbtable.hasOwnProperty(operand.label)) {
                        throw new Error(`unknown label \`${operand.label}' ` +
                                        `on line ${instr.line}`);
                    }

                    const actualPc = pc + this.isa.pc.increment;
                    const offset = symbtable[operand.label] - actualPc;

                    if (field.sext) {
                        if (offset < minTwos) {
                            throw new Error(`required pc offset ${offset} to ` +
                                            `label ${operand.label} is smaller ` +
                                            `than minimum ${minTwos} on line ` +
                                            `${instr.line}`);
                        }
                        if (offset > maxTwos) {
                            throw new Error(`required pc offset ${offset} to ` +
                                            `label ${operand.label} is larger ` +
                                            `than maximum ${maxTwos} on line ` +
                                            `${instr.line}`);
                        }
                    } else {
                        if (offset < 0) {
                            throw new Error(`required pc offset ${offset} to ` +
                                            `label ${operand.label} is negative ` +
                                            `but field is not sign extended ` +
                                            `on line ${instr.line}`);
                        }
                        if (offset > maxUns) {
                            throw new Error(`required pc offset ${offset} to ` +
                                            `label ${operand.label} is larger ` +
                                            `than maximum ${maxTwos} on line ` +
                                            `${instr.line}`);
                        }
                    }

                    bin |= maskTo(offset, numBits) << field.bits[1];
                } else {
                    // TODO: non-garbage error message
                    throw new Error(`unknown operand ${operand.kind} ` +
                                    `on line ${instr.line}`);
                }
            }
        }

        return bin;
    }

    private opMatch(pseudoOp: PseudoOp, opSpec: OpSpec): boolean {
        return pseudoOp.op === opSpec.op && (
            !pseudoOp.operand && !opSpec.operands.length ||
            opSpec.operands.length === 1 && !!pseudoOp.operand &&
                pseudoOp.operand.kind === opSpec.operands[0].kind);
    }

    private instrMatch(instr: Instruction, isaInstr: InstructionSpec): boolean {
        if (instr.op !== isaInstr.op) {
            return false;
        }

        let ok = true;
        let o = 0;
        let f = 0;

        while (ok && f < isaInstr.fields.length) {
            const field = isaInstr.fields[f];

            if (field.kind === 'const') {
                f++;
            } else if (ok = o < instr.operands.length) {
                const operand = instr.operands[o];

                ok = operand.kind === 'reg' && field.kind === 'reg' &&
                     operand.prefix === field.prefix ||
                     operand.kind === 'int' && field.kind === 'imm' ||
                     operand.kind === 'label' && field.kind === 'imm'
                                              && field.label;
                f++;
                o++;
            }
        }

        ok = ok && o === instr.operands.length && f === isaInstr.fields.length;
        return ok;
    }

    private aliasMatch(instr: Instruction, alias: AliasSpec): boolean {
        let ok = instr.op === alias.op &&
                 instr.operands.length === alias.operands.length;

        for (let i = 0; ok && i < instr.operands.length; i++) {
            ok = ok && instr.operands[i].kind === alias.operands[i].kind;
        }

        return ok;
    }
}

export { BaseMachineCodeGenerator };