1- import { Opcode , arg as Arg } from "./instructions"
1+ import { arg as Arg , Opcode } from "./instructions"
22
3- export function generateTlb (
4- name : string ,
5- instruction : Opcode ,
6- operandNames : readonly string [ ] = [ ] ,
7- includeNames : boolean = true ,
8- ) : string {
3+ export function generateTlb ( instruction : Opcode , operandNames : readonly string [ ] ) : string {
94 let result = ""
105
116 const nextVariableName = ( ) => {
12- const names = [ "i" , "j" , "k" ]
137 let index = 0
148 return ( ) => {
15- if ( operandNames . length > 0 && index < operandNames . length ) {
16- const name = operandNames [ index ]
17- index ++
18- return name
19- }
20-
21- const name = names [ index ]
9+ const name = operandNames [ index ]
2210 index ++
2311 return name
2412 }
@@ -30,20 +18,9 @@ export function generateTlb(
3018 opcode = instruction . prefix >> ( instruction . skipLen - instruction . checkLen )
3119 }
3220
33- if ( includeNames ) {
34- const baseName = name . toLowerCase ( )
35- if ( baseName . startsWith ( "2" ) ) {
36- result += `_${ baseName } `
37- } else if ( baseName . includes ( "#" ) ) {
38- result += baseName . replace ( / # / g, "_" )
39- } else {
40- result += baseName
41- }
42- }
43-
4421 result += "#" + opcode . toString ( 16 ) + " "
4522
46- function generateArg ( rawArg : Arg ) {
23+ for ( const rawArg of instruction . args ) {
4724 const arg = unwrapDelta ( rawArg )
4825
4926 switch ( arg . $ ) {
@@ -67,14 +44,11 @@ export function generateTlb(
6744 case "slice" : {
6845 const refs = unwrapDelta ( arg . refs )
6946 if ( refs . $ === "uint" && refs . len !== 0 ) {
70- const restriction = refs . range . min !== 0n ? ` { ${ refs . range . min } <= r }` : ""
71- result += `r: (## ${ refs . len } )${ restriction } `
47+ result += `r: (## ${ refs . len } ) `
7248 }
7349 const bits = unwrapDelta ( arg . bits )
7450 if ( bits . $ === "uint" ) {
75- const restriction =
76- bits . range . min !== 0n ? ` { ${ bits . range . min } <= bits }` : ""
77- result += `bits: (## ${ bits . len } )${ restriction } `
51+ result += `bits: (## ${ bits . len } ) `
7852 }
7953
8054 if ( refs . $ === "uint" && refs . len !== 0 ) {
@@ -92,14 +66,11 @@ export function generateTlb(
9266 case "codeSlice" : {
9367 const refs = unwrapDelta ( arg . refs )
9468 if ( refs . $ === "uint" ) {
95- const restriction = refs . range . min !== 0n ? ` { ${ refs . range . min } <= r }` : ""
96- result += `r: (## ${ refs . len } )${ restriction } `
69+ result += `r: (## ${ refs . len } ) `
9770 }
9871 const bits = unwrapDelta ( arg . bits )
9972 if ( bits . $ === "uint" ) {
100- const restriction =
101- bits . range . min !== 0n ? ` { ${ bits . range . min } <= bits }` : ""
102- result += `bits: (## ${ bits . len } )${ restriction } `
73+ result += `bits: (## ${ bits . len } ) `
10374 }
10475
10576 let delta = 0
@@ -113,9 +84,7 @@ export function generateTlb(
11384 case "inlineCodeSlice" : {
11485 const bits = unwrapDelta ( arg . bits )
11586 if ( bits . $ === "uint" ) {
116- const restriction =
117- bits . range . min !== 0n ? ` { ${ bits . range . min } <= bits }` : ""
118- result += `bits:(## ${ bits . len } )${ restriction } `
87+ result += `bits:(## ${ bits . len } ) `
11988 }
12089
12190 const name = variableNameGenerator ( ) ?? "data"
@@ -127,16 +96,14 @@ export function generateTlb(
12796 result += `${ name } : ^Cell `
12897 break
12998 }
130- case "delta" : {
131- generateArg ( arg . arg )
132- break
133- }
13499 case "plduzArg" : {
135- result += "i: (## 3)"
100+ const name = variableNameGenerator ( ) ?? "i"
101+ result += `${ name } : (## 3) `
136102 break
137103 }
138104 case "tinyInt" : {
139- result += "i: (## 4)"
105+ const name = variableNameGenerator ( ) ?? "i"
106+ result += `${ name } : (## 4) `
140107 break
141108 }
142109 case "largeInt" : {
@@ -148,10 +115,11 @@ export function generateTlb(
148115 break
149116 }
150117 case "dict" : {
151- const name = variableNameGenerator ( )
118+ const name = variableNameGenerator ( ) ?? "d"
152119 result += `${ name } : ^Cell `
153120 break
154121 }
122+ case "delta" :
155123 case "minusOne" :
156124 case "s1" :
157125 case "setcpArg" :
@@ -161,30 +129,7 @@ export function generateTlb(
161129 }
162130 }
163131
164- if ( name === "XCHG_IJ" ) {
165- result += "i: (## 4) j: (## 4) { 1 <= i } { i + 1 <= j }"
166- } else {
167- for ( const arg of instruction . args ) {
168- generateArg ( arg )
169- }
170- }
171-
172- if ( includeNames ) {
173- result += "= "
174-
175- if ( name . startsWith ( "2" ) ) {
176- result += `_${ name } `
177- } else if ( name . includes ( "#" ) ) {
178- result += name . replace ( / # / g, "_" )
179- } else {
180- result += name
181- }
182- result += ";"
183- } else {
184- result = result . trim ( )
185- }
186-
187- return result
132+ return result . trim ( )
188133}
189134
190135function unwrapDelta ( arg : Arg ) : Arg {
0 commit comments