Skip to content

Commit d6e7262

Browse files
authored
feat: use special uint(0) type instead of refs hack (#17)
1 parent 898057d commit d6e7262

File tree

7 files changed

+22
-28
lines changed

7 files changed

+22
-28
lines changed

src/generator/gen-constructors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ const generateArg = (arg: $.arg): t.TSType => {
127127
switch (arg.$) {
128128
case "int":
129129
case "uint":
130-
case "refs":
131130
case "stack":
132131
case "control":
133132
case "plduzArg":

src/generator/gen-converter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ function isIntegerArg(arg: $.arg) {
213213
return (
214214
arg.$ === "int" ||
215215
arg.$ === "uint" ||
216-
arg.$ === "refs" ||
217216
arg.$ === "plduzArg" ||
218217
arg.$ === "tinyInt" ||
219218
arg.$ === "runvmArg" ||

src/generator/gen-printer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ const generateArg = (name: string, arg: $.arg): t.Statement[] => {
147147
switch (arg.$) {
148148
case "int":
149149
case "uint":
150-
case "refs":
151150
case "plduzArg":
152151
case "tinyInt":
153152
case "largeInt":

src/generator/gen-types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,6 @@ const generateArg = (arg: $.arg): t.Expression => {
227227
return generateTypeDescription("int", arg.len)
228228
case "uint":
229229
return generateTypeDescription("uint", arg.len)
230-
case "refs":
231-
return generateTypeDescription("refs", arg.count)
232230
case "stack":
233231
return generateTypeDescription("uint", arg.len)
234232
case "control":

src/generator/instructions.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export type arg =
22
| uint
33
| int
4-
| refs
54
| delta
65
| stack
76
| control
@@ -29,9 +28,6 @@ export const uint = (len: number, range: range): uint => ({$: "uint", len, range
2928
export type int = {$: "int", len: number, range: range}
3029
export const int = (len: number, range: range): int => ({$: "int", len, range})
3130

32-
export type refs = {$: "refs", count: number}
33-
export const refs = (count: number): refs => ({$: "refs", count})
34-
3531
export type delta = {$: "delta", delta: number, arg: arg}
3632
export const delta = (delta: number, arg: arg): delta => ({$: "delta", delta, arg})
3733

@@ -368,6 +364,7 @@ const uint14range = range(0n, BigInt(Math.pow(2, 14) - 1))
368364
const int8 = int(8, int8range)
369365
const int16 = int(16, int16range)
370366

367+
const uint0 = uint(0, {min: 0n, max: 0n})
371368
const uint2 = uint(2, uint2range)
372369
const uint3 = uint(3, uint3range)
373370
const uint4 = uint(4, uint4range)
@@ -1161,8 +1158,8 @@ export const instructions: Record<string, Opcode> = {
11611158
// SECTION: sdbegins
11621159
SDBEGINSX: cat("cell_deserialize", mksimple(0xd726, 16, `(_1) => exec_slice_begins_with(_1, false)`)),
11631160
SDBEGINSXQ: cat("cell_deserialize", mksimple(0xd727, 16, `(_1) => exec_slice_begins_with(_1, true)`)),
1164-
SDBEGINS: cat("cell_deserialize", mkext(0xd728 >> 2, 14, 7, seq(slice(refs(0), uint7, 3)), `exec_slice_begins_with_const`)),
1165-
SDBEGINSQ: cat("cell_deserialize", mkext(0xd72c >> 2, 14, 7, seq(slice(refs(0), uint7, 3)), `exec_slice_begins_with_const`)),
1161+
SDBEGINS: cat("cell_deserialize", mkext(0xd728 >> 2, 14, 7, seq(slice(uint0, uint7, 3)), `exec_slice_begins_with_const`)),
1162+
SDBEGINSQ: cat("cell_deserialize", mkext(0xd72c >> 2, 14, 7, seq(slice(uint0, uint7, 3)), `exec_slice_begins_with_const`)),
11661163
// END SECTION
11671164

11681165
STREFCONST: cat("cell_serialize", mkextrange(0xcf20, 0xcf21, 16, 0, seq(refCodeSlice), `exec_store_const_ref`)),
@@ -1308,7 +1305,7 @@ export const instructions: Record<string, Opcode> = {
13081305
CALLXARGS: cat("continuation_jump", mkfixedn(0xdb0, 12, 4, seq(uint4, minusOne), `exec_callx_args_p`)),
13091306
CALLXARGS_1: cat("continuation_jump", mkfixedn(0xda, 8, 8, seq(uint4, uint4), `exec_callx_args`)),
13101307

1311-
PUSHSLICE: cat("cell_const", mkext(0x8b, 8, 4, seq(slice(refs(0), uint4, 4)), `exec_push_slice`)),
1308+
PUSHSLICE: cat("cell_const", mkext(0x8b, 8, 4, seq(slice(uint0, uint4, 4)), `exec_push_slice`)),
13121309
PUSHSLICE_REFS: cat("cell_const", mkext(0x8c, 8, 7, seq(slice(delta(1, uint2), uint5, 1)), `exec_push_slice_r`)),
13131310
PUSHSLICE_LONG: cat("cell_const", mkextrange((0x8d << 3) << 7, ((0x8d << 3) + 5) << 7, 18, 10, seq(slice(uint3, uint7, 6)), `exec_push_slice_r2`)),
13141311

@@ -1324,7 +1321,7 @@ export const instructions: Record<string, Opcode> = {
13241321
// SETCP: cat("codepage", mkfixedrangen(0xff00, 0x10000, 16, 8, seq(setcpArg), `exec_set_cp`)),
13251322

13261323
PSEUDO_PUSHREF: cat("cell_const", mkfixedpseudo(0, seq(refCodeSlice))),
1327-
PSEUDO_PUSHSLICE: cat("cell_const", mkfixedpseudo(0, seq(slice(refs(1), uint(0, range(0n, 0n)), 0)))),
1324+
PSEUDO_PUSHSLICE: cat("cell_const", mkfixedpseudo(0, seq(slice(uint0, uint(0, range(0n, 0n)), 0)))),
13281325
PSEUDO_EXOTIC: cat("cell_const", mkfixedpseudo(0, seq(exoticCell))),
13291326

13301327
// TVM 11 instructions

src/runtime/types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7042,21 +7042,21 @@ export const SDBEGINSXQ: $.Type<c.SDBEGINSXQ> = {
70427042
export const SDBEGINS: $.Type<c.SDBEGINS> = {
70437043
load: s => {
70447044
s.skip(14)
7045-
return c.SDBEGINS($.slice($.refs(0), $.uint(7), 3).load(s))
7045+
return c.SDBEGINS($.slice($.uint(0), $.uint(7), 3).load(s))
70467046
},
70477047
store: (b, val, options) => {
70487048
b.storeInstructionPrefix(13770, 14, val)
7049-
$.slice($.refs(0), $.uint(7), 3).store(b, val.arg0, options)
7049+
$.slice($.uint(0), $.uint(7), 3).store(b, val.arg0, options)
70507050
},
70517051
}
70527052
export const SDBEGINSQ: $.Type<c.SDBEGINSQ> = {
70537053
load: s => {
70547054
s.skip(14)
7055-
return c.SDBEGINSQ($.slice($.refs(0), $.uint(7), 3).load(s))
7055+
return c.SDBEGINSQ($.slice($.uint(0), $.uint(7), 3).load(s))
70567056
},
70577057
store: (b, val, options) => {
70587058
b.storeInstructionPrefix(13771, 14, val)
7059-
$.slice($.refs(0), $.uint(7), 3).store(b, val.arg0, options)
7059+
$.slice($.uint(0), $.uint(7), 3).store(b, val.arg0, options)
70607060
},
70617061
}
70627062
export const STREFCONST: $.Type<c.STREFCONST> = {
@@ -8221,11 +8221,11 @@ export const CALLXARGS_1: $.Type<c.CALLXARGS_1> = {
82218221
export const PUSHSLICE: $.Type<c.PUSHSLICE> = {
82228222
load: s => {
82238223
s.skip(8)
8224-
return c.PUSHSLICE($.slice($.refs(0), $.uint(4), 4).load(s))
8224+
return c.PUSHSLICE($.slice($.uint(0), $.uint(4), 4).load(s))
82258225
},
82268226
store: (b, val, options) => {
82278227
b.storeInstructionPrefix(139, 8, val)
8228-
$.slice($.refs(0), $.uint(4), 4).store(b, val.arg0, options)
8228+
$.slice($.uint(0), $.uint(4), 4).store(b, val.arg0, options)
82298229
},
82308230
}
82318231
export const PUSHSLICE_REFS: $.Type<c.PUSHSLICE_REFS> = {

src/runtime/util.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export type Type<T> = {
4545
}
4646

4747
export function uint(bits: number): Type<number> {
48+
if (bits === 0) {
49+
return {
50+
store: (_b, _t, _options) => {},
51+
load: _s => 0,
52+
}
53+
}
4854
return {
4955
store: (b, t, _options) => b.storeUint(t, bits),
5056
load: s => s.loadUint(bits),
@@ -227,16 +233,11 @@ export const inlineCodeSlice = (bits: Type<number>): Type<Code> => {
227233
}
228234
}
229235

230-
export const slice = (
231-
refs: Type<number> | number, // TODO: remove union
232-
bits: Type<number>,
233-
pad: number,
234-
): Type<Slice> => {
236+
export const slice = (refs: Type<number>, bits: Type<number>, pad: number): Type<Slice> => {
235237
return {
236238
store: (b, slice, options) => {
237-
if (typeof refs !== "number") {
238-
refs.store(b, slice.remainingRefs, options)
239-
}
239+
// For instructions without refs, we use uint(0) that doesn't write anything
240+
refs.store(b, slice.remainingRefs, options)
240241
const length = slice.remainingBits + 1
241242
const y = Math.ceil((length - pad) / 8)
242243
bits.store(b, y, options)
@@ -246,7 +247,8 @@ export const slice = (
246247
b.storeUint(0x0, realLength - length)
247248
},
248249
load: s => {
249-
const countRefs = typeof refs === "number" ? refs : refs.load(s)
250+
// For instructions without refs, we use uint(0) that will return a zero from load
251+
const countRefs = refs.load(s)
250252
const y = bits.load(s)
251253
const realLength = y * 8 + pad
252254
const r = s.loadBits(realLength)

0 commit comments

Comments
 (0)