aboutsummaryrefslogtreecommitdiffgithub
path: root/novice/cli.ts
blob: f1ada7a4c32593f0d8b12fd27640ad5581473194 (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
import { ArgumentParser } from 'argparse';
import * as fs from 'fs';
import { Readable, Writable } from 'stream';
import { Assembler, getConfig, getParser, getSerializer } from './assembler';
import { getIsa, StreamIO } from './isa';
import { CliDebugger, getSimulatorConfig, Simulator, SimulatorConfig } from './simulator';

async function main(argv: string[], stdin: Readable, stdout: Writable,
                    stderr: Writable): Promise<number> {
    const parser = new ArgumentParser({ prog: 'novice', description: 'toy assembler' });
    const sub = parser.addSubparsers({ dest: 'subcmd',
                                       help: 'subcommand to run. try ' +
                                             '`novice subcmd --help\' for more info' });

    const asmParser = sub.addParser('asm', { description:
                                             'assemble to an object file' });
    asmParser.addArgument(['file'], { help: 'file to assemble' });
    asmParser.addArgument(['-c', '--config'],
                          { defaultValue: 'lc3',
                            help: 'assembler configuration to use. ' +
                                  'default: %(defaultValue)s' });
    asmParser.addArgument(['-o', '--output-file'],
                          { metavar: 'PATH',
                            dest: 'outputFile',
                            defaultValue: null,
                            help: 'path to output file. default: ' +
                                  'for input file X.asm, output to X.EXT, ' +
                                  'where EXT is the default extension for ' +
                                  'the selected output format' });
    asmParser.addArgument(['-f', '--output-format'],
                          { metavar: 'FMT',
                            dest: 'outputFormat',
                            defaultValue: null,
                            help: 'desired output format. default: ' +
                                  'the default output format for the ' +
                                  'the selected assembler configuration' });

    const simParser = sub.addParser('sim', { description:
                                             'simulate an assembly/object file' });
    simParser.addArgument(['file'], { help: 'assembly/object file to simulate' });
    simParser.addArgument(['-c', '--config'],
                          { defaultValue: 'lc3',
                            help: 'simulator configuration to use. ' +
                                  'default: %(defaultValue)s' });
    simParser.addArgument(['-x', '--max-exec'],
                          { dest: 'maxExec',
                            type: 'int',
                            defaultValue: 1 << 13,
                            help: 'max instructions to execute. pass -1 for ' +
                                  'no limit. default: %(defaultValue)s' });

    const dbgParser = sub.addParser('dbg', { description:
                                             'interactively debug an object file'});
    dbgParser.addArgument(['file'], { help: 'object file to debug' });
    dbgParser.addArgument(['-c', '--config'],
                          { defaultValue: 'lc3',
                            help: 'simulator configuration to use. ' +
                                  'default: %(defaultValue)s' });

    const tablegenParser = sub.addParser('tablegen');
    tablegenParser.addArgument(['parser'],
                               { help: 'parser whose table to generate' });

    const args = parser.parseArgs(argv);
    switch (args.subcmd) {
        case 'asm':
            return await asm(args.config, args.file, args.outputFile, args.outputFormat, stdout, stderr);
        case 'sim':
            return await sim(args.config, args.file, args.maxExec, stdin, stdout, stderr);
        case 'dbg':
            return await dbg(args.config, args.file, stdin, stdout, stderr);
        case 'tablegen':
            return tablegen(args.parser, stdout, stderr);
        default:
            stderr.write(`novice: unsupported subcommand ${args.subcmd}\n`);
            return 1;
    }
}

async function asm(configName: string, inPath: string,
                   outPath: string|null, outFmt: string|null,
                   stdout: Writable, stderr: Writable):
        Promise<number> {
    try {
        const cfg = getConfig(configName);

        if (outFmt) {
            cfg.serializer = getSerializer(outFmt);
        }
        if (!outPath) {
            outPath = removeExt(inPath) + '.' + cfg.serializer.fileExt();
        }
        const symbPath = removeExt(outPath) + '.' + cfg.serializer.symbFileExt();

        const inFp = fs.createReadStream(inPath);
        await new Promise((resolve, reject) => {
            inFp.on('readable', resolve);
            inFp.on('error', reject);
        });
        const outFp = fs.createWriteStream(outPath);
        const symbFp = fs.createWriteStream(symbPath);
        const assembler = new Assembler(cfg);

        // Buffer so we don't make 1,000,000 syscalls
        outFp.cork();
        symbFp.cork();
        await assembler.assembleTo(inFp, outFp, symbFp);
        outFp.uncork();
        symbFp.uncork();
        outFp.end();

        return 0;
    } catch (err) {
        stderr.write(`asm error: ${err.message}\n`);
        return 1;
    }
}

function removeExt(path: string): string {
    const slashIdx = path.lastIndexOf('/');
    const dotIdx = path.lastIndexOf('.');
    const hasExt = dotIdx !== -1 && (slashIdx === -1 || slashIdx < dotIdx);
    return hasExt ? path.substr(0, dotIdx) : path;
}

function hasObjectFileExt(path: string, cfg: SimulatorConfig) {
    return path.endsWith('.' + cfg.loader.fileExt());
}

async function sim(configName: string, path: string, maxExec: number,
                   stdin: Readable, stdout: Writable, stderr: Writable):
        Promise<number> {

    try {
        const cfg = getSimulatorConfig(configName);
        const fp = fs.createReadStream(path);
        await Promise.all([fp, stdin].map(
            f => new Promise((resolve, reject) => {
                f.on('readable', resolve);
                f.on('error', reject);
            }),
        ));

        const io = new StreamIO(stdin, stdout);
        const simulator = new Simulator(cfg.isa, io, maxExec);

        const isObjectFile = hasObjectFileExt(path, cfg);
        if (isObjectFile) {
            await cfg.loader.load(cfg.isa, fp, simulator);
        } else {
            const asmCfg = getConfig(configName);
            const assembler = new Assembler(asmCfg);
            const [symbtable, sections] = await assembler.assemble(fp);
            simulator.loadSections(sections);
        }

        await simulator.run();
        return 0;
    } catch (err) {
        stderr.write(`sim error: ${err.message}\n`);
        return 1;
    }
}

async function dbg(configName: string, path: string, stdin: Readable,
                   stdout: Writable, stderr: Writable): Promise<number> {
    let cfg;
    let fp: Readable;
    let symbFp: Readable|null = null;
    let isObjectFile: boolean;

    try {
        cfg = getSimulatorConfig(configName);
        isObjectFile = hasObjectFileExt(path, cfg);
        fp = fs.createReadStream(path);

        await new Promise((resolve, reject) => {
            fp.on('readable', resolve);
            fp.on('error', reject);
        });

        if (isObjectFile) {
            const symbPath = `${removeExt(path)}.${cfg.loader.symbFileExt()}`;
            symbFp = fs.createReadStream(symbPath);

            await new Promise((resolve, reject) => {
                (symbFp as Readable).on('readable', resolve);
                (symbFp as Readable).on('error', err => {
                    stderr.write(`warning: could not open symbol file ` +
                                 `\`${symbPath}'. reason: \`${err.message}'. ` +
                                 `proceeding without debug symbols...\n`);
                    symbFp = null;
                    resolve();
                });
            });
        }
    } catch (err) {
        stderr.write(`dbg: setup error: ${err.message}\n`);
        return 1;
    }

    const debug = new CliDebugger(cfg.isa, stdin, stdout);

    try {
        if (isObjectFile) {
            const loadPromise = cfg.loader.load(cfg.isa, fp, debug);
            if (symbFp) {
                await Promise.all([
                    loadPromise,
                    cfg.loader.loadSymb(symbFp, debug),
                ]);
            } else {
                await loadPromise;
            }
        } else {
            const asmCfg = getConfig(configName);
            const assembler = new Assembler(asmCfg);
            const [symbtable, sections] = await assembler.assemble(fp);
            debug.loadSections(sections);
            debug.setSymbols(symbtable);
        }

        await debug.run();
        debug.close();
        return 0;
    } catch (err) {
        debug.close();
        stderr.write(`dbg error: ${err.message}\n`);
        return 1;
    }
}

function tablegen(parserName: string, stdout: Writable,
                  stderr: Writable): number {
    let table: object;
    try {
        const parser = getParser(parserName, getIsa('dummy'));
        table = parser.genTable();
    } catch (err) {
        stderr.write(`error generating LR(1) table: ${err.message}\n`);
        return 1;
    }

    stdout.write(JSON.stringify(table));
    return 0;
}

export default main;