aboutsummaryrefslogtreecommitdiff
path: root/src/js/extract_lsp.js
blob: 14ee2792651f4c293aff89fa77b9289f5ba6d559 (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
// writes the language server's javascript image: the genome build/lsp/genome.h,
// made by src/tools/mklsp.sh, and the map it was built for, read from
// build/lsp/map/memmap.h. the vm needs the layout, because the server's map is
// not the compiler's. the image is generated, so it goes to build/js/ and
// nothing is left in the tree.
//
//   node src/js/extract_lsp.js [--out PATH]
//
// build the lsp first (`make lsp`, or src/tools/mklsp.sh); it needs
// build/lsp/genome.h.
const fs = require('fs');
const path = require('path');
const root = path.join(__dirname, '..', '..');

const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
    console.log([
        'usage: node src/js/extract_lsp.js [--out PATH]',
        'converts the language server genome and its memory map into a javascript image.',
        'options:',
        '  --out PATH   where to write; defaults to build/js/lsp.json',
        '  --help, -h   show this help and exit',
    ].join('\n'));
    process.exit(0);
}
let out = path.join(root, 'build', 'js', 'lsp.json');
for (let i = 0; i < args.length; i++) {
    if (args[i] === '--out' && args[i + 1]) out = args[i + 1];
    else {
        console.error('extract_lsp: unexpected arguments; use --help for usage');
        process.exit(2);
    }
}

const genomePath = path.join(root, 'build', 'lsp', 'genome.h');
const mapPath = path.join(root, 'build', 'lsp', 'map', 'memmap.h');
if (!fs.existsSync(genomePath)) {
    console.error('extract_lsp: no language server image at ' + genomePath + '; run `make lsp` first');
    process.exit(1);
}
const genome = fs.readFileSync(genomePath, 'utf8');

const { mapValues } = require('./mapvalues.js');
const layout = mapValues(mapPath, ['GORG', 'DS0', 'DSLIM', 'RS0', 'RSLIM', 'SYSGATE', 'ZFP', 'HEAP_LO', 'HEAP_HI']);

const gorg = parseInt(genome.match(/#define\s+GORG\s+(\d+)/)[1], 10);
const glen = parseInt(genome.match(/#define\s+GLEN\s+(\d+)/)[1], 10);
const arr = genome.match(/genome\[\]\s*=\s*\{([\s\S]*?)\};/);
const words = arr[1].split(',').map(s => s.trim()).filter(s => s.length > 0);
if (words.length !== glen) throw new Error('genome has ' + words.length + ' words, GLEN says ' + glen);
if (gorg !== layout.GORG) throw new Error('genome GORG ' + gorg + ' is not the map GORG ' + layout.GORG);

fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, JSON.stringify({ gorg, glen, layout, words }));
console.log('wrote ' + path.relative(root, out) + ': genome ' + glen + ' words at ' + gorg +
    ', heap ' + layout.HEAP_LO + '..' + layout.HEAP_HI);