From ebd6c69e9eb69a6abd7ee861311dd2cef8deba8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Fri, 13 Sep 2024 21:17:23 +0200 Subject: [PATCH 1/8] Add assembler. --- package-lock.json | 13 ++- package.json | 3 +- src/components/ProgramUpload/Assembly.tsx | 133 ++++++++++++++++++++++ src/components/ProgramUpload/index.tsx | 5 + vite.config.ts | 3 +- 5 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 src/components/ProgramUpload/Assembly.tsx diff --git a/package-lock.json b/package-lock.json index 5576aed..5a8538a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,7 +56,8 @@ "prettier": "3.3.3", "tailwindcss": "^3.4.6", "typescript": "^5.2.2", - "vite": "^5.3.4" + "vite": "^5.3.4", + "vite-plugin-wasm": "^3.3.0" } }, "node_modules/@alloc/quick-lru": { @@ -5845,6 +5846,16 @@ } } }, + "node_modules/vite-plugin-wasm": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/vite-plugin-wasm/-/vite-plugin-wasm-3.3.0.tgz", + "integrity": "sha512-tVhz6w+W9MVsOCHzxo6SSMSswCeIw4HTrXEi6qL3IRzATl83jl09JVO1djBqPSwfjgnpVHNLYcaMbaDX5WB/pg==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "vite": "^2 || ^3 || ^4 || ^5" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index bf95ac4..253b353 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,8 @@ "prettier": "3.3.3", "tailwindcss": "^3.4.6", "typescript": "^5.2.2", - "vite": "^5.3.4" + "vite": "^5.3.4", + "vite-plugin-wasm": "^3.3.0" }, "lint-staged": { "**/*.{ts,tsx}": [ diff --git a/src/components/ProgramUpload/Assembly.tsx b/src/components/ProgramUpload/Assembly.tsx new file mode 100644 index 0000000..1e10a9b --- /dev/null +++ b/src/components/ProgramUpload/Assembly.tsx @@ -0,0 +1,133 @@ +import { useCallback, useEffect, useMemo, useState } from "react"; +import { Textarea } from "../ui/textarea"; +import { ProgramUploadFileOutput } from "./types"; +import classNames from "classnames"; +import { compile_assembly, disassemble } from "@typeberry/spectool-wasm"; +import { mapUploadFileInputToOutput } from "./utils"; + +const DEFAULT_ASSEMBLY = `pre: a0 = 9 +pre: ra = 0xffff0000 + +pub @main: + // first & second + a1 = 1 + a2 = 1 + jump @loop + trap + +@loop: + a0 = a0 - 1 + jump @end if a0 == 0 + a3 = a1 + a1 = a1 + a2 + a2 = a3 + jump @loop + +@end: + a0 = a1 + a1 = 0 + a2 = 0 + +pub @expected_exit: + ret +`; + +function assemblyFromInputProgram(program: number[]) { + if (program.length === 0) { + return DEFAULT_ASSEMBLY; + } + try { + const raw = disassemble(program); + const lines = raw.split("\n"); + const fixedLines: string[] = lines.map((l: string) => { + // remove leading whitespace + l = l.trim(); + // replace labels + l = l.replace(/: @(.+)$/, "@block$1:"); + return l; + }); + + // make a map of line targets into basic block labels + const basicBlocks = new Map(); + for (let i = 0; i < fixedLines.length; i += 1) { + if (fixedLines[i].startsWith("@")) { + const blockName = fixedLines[i].replace(":", ""); + const number = fixedLines[i + 1].split(":")[0]; + basicBlocks.set(number, blockName); + } + // remove line number + fixedLines[i] = fixedLines[i].replace(/[0-9]+: /, "\t"); + } + + // fix jumps + for (let i = 0; i < fixedLines.length; i += 1) { + fixedLines[i] = fixedLines[i].replace(/jump ([0-9]+)/, (_, num) => { + return `jump ${basicBlocks.get(num)}`; + }); + } + return fixedLines.join("\n"); + } catch (e) { + console.error("Error disassembling input: ", e); + return DEFAULT_ASSEMBLY; + } +} + +export const Assembly = ({ + onFileUpload, + program, +}: { + onFileUpload: (val: ProgramUploadFileOutput) => void; + program: number[]; +}) => { + const defaultAssembly = useMemo(() => { + return assemblyFromInputProgram(program); + }, [program]); + + const compile = useCallback( + (input: string) => { + setAssembly(input); + try { + const programJson = compile_assembly(input); + const program = JSON.parse(programJson); + const output = mapUploadFileInputToOutput(program); + onFileUpload(output); + setError(undefined); + } catch (e) { + console.log(e); + setError(`${e}`); + } + }, + [onFileUpload], + ); + + const [error, setError] = useState(); + const [assembly, setAssembly] = useState(defaultAssembly); + + // compile the assembly for the first time + useEffect(() => { + compile(assembly); + }, [compile, assembly]); + + const isError = !!error; + + return ( +
+
+