diff options
Diffstat (limited to 'src/js/mklayout.js')
| -rw-r--r-- | src/js/mklayout.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/js/mklayout.js b/src/js/mklayout.js new file mode 100644 index 0000000..cf95091 --- /dev/null +++ b/src/js/mklayout.js @@ -0,0 +1,61 @@ +// writes the javascript vm's view of a memory map: the map's constants as +// typescript and commonjs modules. the vm reads its genome image itself, from +// genome.bin beside tinyvm.js, so only the layout is generated here. everything +// it writes is generated, so it goes to build/js/ and nothing is left in the tree. +// +// node src/js/mklayout.js [--map PATH] [--out DIR] +// +// the map is the default one `make` generates in build/core/map/memmap.h. +// outputs, in the out directory: +// layout.ts the map as typescript constants +// layout.js the map as commonjs constants +const fs = require('fs'); +const path = require('path'); +const root = path.join(__dirname, '..', '..'); + +const args = process.argv.slice(2); +const opt = (name) => { + const i = args.indexOf(name); + return i >= 0 && args[i + 1] ? args[i + 1] : null; +}; +if (args.includes('--help') || args.includes('-h')) { + console.log([ + 'usage: node src/js/mklayout.js [--map PATH] [--out DIR]', + 'converts a memory map header into the javascript vm layout files.', + 'options:', + ' --map PATH the memory map header to read; defaults to build/core/map/memmap.h', + ' --out DIR where to write; defaults to build/js', + ' --help, -h show this help and exit', + ].join('\n')); + process.exit(0); +} +for (let i = 0; i < args.length; i += 2) { + if (!['--map', '--out'].includes(args[i]) || args[i + 1] === undefined) { + console.error('mklayout: unexpected arguments: ' + args.join(' ') + '; use --help for usage'); + process.exit(2); + } +} + +const mapPath = opt('--map') || path.join(root, 'build', 'core', 'map', 'memmap.h'); +const outDir = opt('--out') || path.join(root, 'build', 'js'); +if (!fs.existsSync(mapPath)) { + console.error('mklayout: no memory map at ' + mapPath + '; run `make` first'); + process.exit(1); +} +fs.mkdirSync(outDir, { recursive: true }); + +// the compiler's map, as constants: the js vm reads them instead of its own copies. +const { mapValues } = require('./mapvalues.js'); +const names = ['MW', 'PW', 'ZFP', 'GORG', 'DS0', 'DSLIM', 'RS0', 'RSLIM', 'SYSGATE', 'HEAP_LO', 'HEAP_HI']; +const map = mapValues(mapPath, names); + +const lines = names.map(k => ` ${k}: ${map[k]},`).join('\n'); +const layoutTs = path.join(outDir, 'layout.ts'); +fs.writeFileSync(layoutTs, + '// generated by src/js/mklayout.js from ' + path.relative(root, mapPath) + '. do not edit.\n' + + 'export const MAP = {\n' + lines + '\n} as const;\n'); +const layoutJs = path.join(outDir, 'layout.js'); +fs.writeFileSync(layoutJs, + '// generated by src/js/mklayout.js from ' + path.relative(root, mapPath) + '. do not edit.\n' + + 'exports.MAP = ' + JSON.stringify(map, null, 4) + ';\n'); +console.log('wrote ' + path.relative(root, layoutJs) + ' and layout.ts'); |
