Skip to content

Commit 5d84fbd

Browse files
committed
refactor: simplify TL-B generation
1 parent 51e6b88 commit 5d84fbd

File tree

4 files changed

+25
-80
lines changed

4 files changed

+25
-80
lines changed

data/debug.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"short": "",
107107
"long": "Padding instruction that does nothing.",
108108
"tags": [],
109-
"operands": []
109+
"operands": ["t"]
110110
},
111111
"signature": {
112112
"inputs": {
@@ -142,7 +142,7 @@
142142
"short": "",
143143
"long": "Padding instruction that does nothing.",
144144
"tags": [],
145-
"operands": []
145+
"operands": ["t"]
146146
},
147147
"signature": {
148148
"inputs": {

gen/tvm-specification.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55206,7 +55206,7 @@
5520655206
"kind": "fixed",
5520755207
"prefix": 6882,
5520855208
"prefix_str": "1AE2",
55209-
"tlb": "#1ae2 i: (## 3)"
55209+
"tlb": "#1ae2 c: (## 3)"
5521055210
},
5521155211
"signature": {
5521255212
"stack_string": "s:Slice -> s:Slice x:Int",
@@ -65737,7 +65737,7 @@
6573765737
"kind": "fixed",
6573865738
"prefix": 16,
6573965739
"prefix_str": "10",
65740-
"tlb": "#10 i: (## 4) j: (## 4) { 1 <= i } { i + 1 <= j }"
65740+
"tlb": "#10 i: (## 4) { 1 <= i } j: (## 4)"
6574165741
},
6574265742
"implementation": {
6574365743
"commit_hash": "4ebd7412c52248360464c2df5f434c8aaa3edfe1",
@@ -65970,7 +65970,7 @@
6597065970
"short": "",
6597165971
"long": "Padding instruction that does nothing.",
6597265972
"tags": [],
65973-
"operands": [],
65973+
"operands": ["t"],
6597465974
"gas": [
6597565975
{
6597665976
"value": 26,
@@ -65997,7 +65997,7 @@
6599765997
"kind": "fixed-range",
6599865998
"prefix": 65045,
6599965999
"prefix_str": "FE15",
66000-
"tlb": "#fe i: (## 8)"
66000+
"tlb": "#fe t: (## 8)"
6600166001
},
6600266002
"signature": {
6600366003
"stack_string": "∅ -> ∅",
@@ -66080,7 +66080,7 @@
6608066080
"short": "",
6608166081
"long": "Padding instruction that does nothing.",
6608266082
"tags": [],
66083-
"operands": [],
66083+
"operands": ["t"],
6608466084
"gas": [
6608566085
{
6608666086
"value": 26,
@@ -66107,7 +66107,7 @@
6610766107
"kind": "fixed-range",
6610866108
"prefix": 65072,
6610966109
"prefix_str": "FE30",
66110-
"tlb": "#fe i: (## 8)"
66110+
"tlb": "#fe t: (## 8)"
6611166111
},
6611266112
"signature": {
6611366113
"stack_string": "∅ -> ∅",

src/gen/gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const main = () => {
7777
subCategory: undefined,
7878
effects: undefined,
7979
prefix_str: opcode.prefix.toString(16).toUpperCase(),
80-
tlb: generateTlb(name, opcode, instr.description.operands, false),
80+
tlb: generateTlb(opcode, instr.description.operands),
8181
}
8282

8383
const gasCosts = calculateGasConsumptionWithDescription(opcode).filter(

src/instructions/tlb.ts

Lines changed: 16 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
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

190135
function unwrapDelta(arg: Arg): Arg {

0 commit comments

Comments
 (0)