1
- import { useCallback , useEffect , useMemo , useState } from "react" ;
1
+ import { useCallback , useMemo , useState } from "react" ;
2
2
import { ProgramUploadFileOutput } from "./types" ;
3
- import { InitialState } from "../../types/pvm" ;
3
+ import { InitialState , RegistersArray } from "../../types/pvm" ;
4
4
import classNames from "classnames" ;
5
5
import { compile_assembly , disassemble } from "@typeberry/spectool-wasm" ;
6
6
import { mapUploadFileInputToOutput } from "./utils" ;
@@ -52,6 +52,8 @@ function assemblyFromInputProgram(initialState: InitialState, program: number[])
52
52
// 4. `ecalli` instructions seem to have `// INVALID` comments next to them, but
53
53
// the assembler does not handle comments at all.
54
54
// 5. disassembler produces unary minus (i.e. `r0 = -r7`), which isn't handled by the compiler.
55
+ // 6. disassembler produces <<r (rotate) which is not supported
56
+ // 7. diassembler produces only 32-bit representation of negative values `0xffffffff`
55
57
const fixedLines : string [ ] = lines . map ( ( l : string ) => {
56
58
// remove leading whitespace
57
59
l = l . trim ( ) ;
@@ -65,6 +67,12 @@ function assemblyFromInputProgram(initialState: InitialState, program: number[])
65
67
l = l . replace ( / = - / , "= 0 -" ) ;
66
68
// replace `invalid` instructions with a comment
67
69
l = l . replace ( "invalid" , "// invalid" ) ;
70
+ // set first block as entry point
71
+ l = l . replace ( "@block0" , "pub @main" ) ;
72
+ // replace rotate with shift (INCORRECT!)
73
+ l = l . replace ( "<<r" , "<<" ) . replace ( ">>r" , ">>" ) ;
74
+ // replace large values with their 64-bit extensions (INCORRECT!; just for fib)
75
+ l = l . replace ( "0xffffffff" , "0xffffffffffffffff" ) ;
68
76
return l ;
69
77
} ) ;
70
78
@@ -114,7 +122,7 @@ export const Assembly = ({
114
122
const newInitialState = {
115
123
...initialState ,
116
124
} ;
117
- newInitialState . regs = output . initial . regs ;
125
+ newInitialState . regs = truncateRegs ( output . initial . regs ) ;
118
126
// this is incorrect, but we would need to alter the
119
127
// assembly to include the actual data:
120
128
// pub @main : (pc)
@@ -130,11 +138,12 @@ export const Assembly = ({
130
138
if ( ( output . initial . pageMap ?. length ?? 0 ) !== 0 ) {
131
139
newInitialState . pageMap = output . initial . pageMap ;
132
140
}
133
- // we want to keep all of the old stuff to avoid re-rendering.
134
- output . initial = newInitialState ;
135
- // TODO [ToDr] Assembly for 64-bit is temporarily broken, so we don't trigger
136
- // the `onProgramLoad` here.
137
- // onProgramLoad(output);
141
+ // if there are no changes do not re-render.
142
+ if ( JSON . stringify ( output . initial ) !== JSON . stringify ( newInitialState ) ) {
143
+ // we want to keep all of the old stuff to avoid re-rendering.
144
+ output . initial = newInitialState ;
145
+ onProgramLoad ( output ) ;
146
+ }
138
147
setError ( undefined ) ;
139
148
} catch ( e ) {
140
149
if ( e instanceof Error ) {
@@ -157,15 +166,6 @@ export const Assembly = ({
157
166
158
167
const [ error , setError ] = useState < string > ( ) ;
159
168
const [ assembly , setAssembly ] = useState ( defaultAssembly ) ;
160
- const [ isFirstCompilation , setFirstCompilation ] = useState ( true ) ;
161
-
162
- // compile the assembly for the first time
163
- useEffect ( ( ) => {
164
- if ( isFirstCompilation ) {
165
- compile ( assembly ) ;
166
- setFirstCompilation ( false ) ;
167
- }
168
- } , [ compile , assembly , isFirstCompilation ] ) ;
169
169
170
170
const isError = ! ! error ;
171
171
@@ -195,8 +195,6 @@ export const Assembly = ({
195
195
className = "h-full"
196
196
height = "100%"
197
197
placeholder = "Try writing some PolkaVM assembly code."
198
- /* TODO [ToDr] Marking as readonly since the 64-bit assembly is not working correctly yet */
199
- readOnly
200
198
value = { assembly }
201
199
onChange = { compile }
202
200
/>
@@ -223,3 +221,15 @@ function isArrayEqual<T>(a: T[], b: T[]) {
223
221
224
222
return true ;
225
223
}
224
+
225
+ function truncateRegs ( regs : RegistersArray | undefined ) {
226
+ if ( ! regs ) {
227
+ return regs ;
228
+ }
229
+
230
+ for ( let i = 0 ; i < regs . length ; i ++ ) {
231
+ // currently the assembler requires that registers are provided as u32
232
+ regs [ i ] = regs [ i ] & 0xffff_ffffn ;
233
+ }
234
+ return regs ;
235
+ }
0 commit comments