Skip to content

Commit

Permalink
fix bitlen
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Dec 9, 2023
1 parent e499f1b commit bc466d4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
51 changes: 31 additions & 20 deletions generated_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Builder } from 'ton'
import { Slice } from 'ton'
import { beginCell } from 'ton'
import { BitString } from 'ton'
export function bitLen(n: number) {
return n.toString(2).length;;
}
export type Unit = {
kind: 'Unit';
};
Expand Down Expand Up @@ -356,7 +359,7 @@ export function loadHmLabel(slice: Slice, m: number): HmLabel {
};
if ((slice.preloadUint(2) == 0b10)) {
slice.loadUint(2);
let n: number = slice.loadUint(m);
let n: number = slice.loadUint(bitLen(m));
let s: Array<BitString> = Array.from(Array(n).keys()).map((arg: number) => {
return slice.loadBits(1);
});
Expand All @@ -370,7 +373,7 @@ export function loadHmLabel(slice: Slice, m: number): HmLabel {
if ((slice.preloadUint(2) == 0b11)) {
slice.loadUint(2);
let v: BitString = slice.loadBits(1);
let n: number = slice.loadUint(m);
let n: number = slice.loadUint(bitLen(m));
return {
kind: 'HmLabel_hml_same',
m: m,
Expand All @@ -396,7 +399,7 @@ export function storeHmLabel(hmLabel: HmLabel): (builder: Builder) => void {
if ((hmLabel.kind == 'HmLabel_hml_long')) {
return (builder: Builder) => {
builder.storeUint(0b10, 2);
builder.storeUint(hmLabel.n, hmLabel.m);
builder.storeUint(hmLabel.n, bitLen(hmLabel.m));
hmLabel.s.forEach((arg: BitString) => {
builder.storeBits(arg);
});
Expand All @@ -406,7 +409,7 @@ export function storeHmLabel(hmLabel: HmLabel): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(0b11, 2);
builder.storeBits(hmLabel.v);
builder.storeUint(hmLabel.n, hmLabel.m);
builder.storeUint(hmLabel.n, bitLen(hmLabel.m));
};
};
throw new Error('');
Expand Down Expand Up @@ -1056,7 +1059,7 @@ export type Anycast = {
rewrite_pfx: BitString;
};
export function loadAnycast(slice: Slice): Anycast {
let depth: number = slice.loadUint(5);
let depth: number = slice.loadUint(bitLen(30));
let rewrite_pfx: BitString = slice.loadBits(depth);
if ((!(depth >= 1))) {
throw new Error('');
Expand All @@ -1069,7 +1072,7 @@ export function loadAnycast(slice: Slice): Anycast {
}
export function storeAnycast(anycast: Anycast): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(anycast.depth, 5);
builder.storeUint(anycast.depth, bitLen(30));
builder.storeBits(anycast.rewrite_pfx);
if ((!(anycast.depth >= 1))) {
throw new Error('');
Expand Down Expand Up @@ -1181,37 +1184,45 @@ export function storeMsgAddress(msgAddress: MsgAddress): (builder: Builder) => v
export type VarUInteger = {
kind: 'VarUInteger';
n: number;
len: number;
value: number;
};
export function loadVarUInteger(slice: Slice, n: number): VarUInteger {
let value: number = slice.loadUint((len * 8));
let len: number = slice.loadUint(bitLen((n - 1)));
let value: number = slice.loadUint((len * 8));
return {
kind: 'VarUInteger',
n: n,
len: len,
value: value
};
}
export function storeVarUInteger(varUInteger: VarUInteger): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(varUInteger.value, (varUInteger.len * 8));
builder.storeUint(varUInteger.len, bitLen((varUInteger.n - 1)));
builder.storeUint(varUInteger.value, (varUInteger.len * 8));
};
}
export type VarInteger = {
kind: 'VarInteger';
n: number;
len: number;
value: number;
};
export function loadVarInteger(slice: Slice, n: number): VarInteger {
let value: number = slice.loadInt((len * 8));
let len: number = slice.loadUint(bitLen((n - 1)));
let value: number = slice.loadInt((len * 8));
return {
kind: 'VarInteger',
n: n,
len: len,
value: value
};
}
export function storeVarInteger(varInteger: VarInteger): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeInt(varInteger.value, (varInteger.len * 8));
builder.storeUint(varInteger.len, bitLen((varInteger.n - 1)));
builder.storeInt(varInteger.value, (varInteger.len * 8));
};
}
export type Grams = {
Expand Down Expand Up @@ -1706,7 +1717,7 @@ export type IntermediateAddress_interm_addr_ext = {
export function loadIntermediateAddress(slice: Slice): IntermediateAddress {
if ((slice.preloadUint(1) == 0b0)) {
slice.loadUint(1);
let use_dest_bits: number = slice.loadUint(7);
let use_dest_bits: number = slice.loadUint(bitLen(96));
return {
kind: 'IntermediateAddress_interm_addr_regular',
use_dest_bits: use_dest_bits
Expand Down Expand Up @@ -1738,7 +1749,7 @@ export function storeIntermediateAddress(intermediateAddress: IntermediateAddres
if ((intermediateAddress.kind == 'IntermediateAddress_interm_addr_regular')) {
return (builder: Builder) => {
builder.storeUint(0b0, 1);
builder.storeUint(intermediateAddress.use_dest_bits, 7);
builder.storeUint(intermediateAddress.use_dest_bits, bitLen(96));
};
};
if ((intermediateAddress.kind == 'IntermediateAddress_interm_addr_simple')) {
Expand Down Expand Up @@ -2741,7 +2752,7 @@ export type DepthBalanceInfo = {
balance: CurrencyCollection;
};
export function loadDepthBalanceInfo(slice: Slice): DepthBalanceInfo {
let split_depth: number = slice.loadUint(5);
let split_depth: number = slice.loadUint(bitLen(30));
let balance: CurrencyCollection = loadCurrencyCollection(slice);
return {
kind: 'DepthBalanceInfo',
Expand All @@ -2751,7 +2762,7 @@ export function loadDepthBalanceInfo(slice: Slice): DepthBalanceInfo {
}
export function storeDepthBalanceInfo(depthBalanceInfo: DepthBalanceInfo): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(depthBalanceInfo.split_depth, 5);
builder.storeUint(depthBalanceInfo.split_depth, bitLen(30));
storeCurrencyCollection(depthBalanceInfo.balance)(builder);
};
}
Expand Down Expand Up @@ -4007,7 +4018,7 @@ export type ShardIdent = {
export function loadShardIdent(slice: Slice): ShardIdent {
if ((slice.preloadUint(2) == 0b00)) {
slice.loadUint(2);
let shard_pfx_bits: number = slice.loadUint(6);
let shard_pfx_bits: number = slice.loadUint(bitLen(60));
let workchain_id: number = slice.loadInt(32);
let shard_prefix: number = slice.loadUint(64);
return {
Expand All @@ -4022,7 +4033,7 @@ export function loadShardIdent(slice: Slice): ShardIdent {
export function storeShardIdent(shardIdent: ShardIdent): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(0b00, 2);
builder.storeUint(shardIdent.shard_pfx_bits, 6);
builder.storeUint(shardIdent.shard_pfx_bits, bitLen(60));
builder.storeInt(shardIdent.workchain_id, 32);
builder.storeUint(shardIdent.shard_prefix, 64);
};
Expand Down Expand Up @@ -8154,8 +8165,8 @@ export function loadVmCellSlice(slice: Slice): VmCellSlice {
let cell: Slice = slice1;
let st_bits: number = slice.loadUint(10);
let end_bits: number = slice.loadUint(10);
let st_ref: number = slice.loadUint(3);
let end_ref: number = slice.loadUint(3);
let st_ref: number = slice.loadUint(bitLen(4));
let end_ref: number = slice.loadUint(bitLen(4));
if ((!(st_bits <= end_bits))) {
throw new Error('');
};
Expand All @@ -8178,8 +8189,8 @@ export function storeVmCellSlice(vmCellSlice: VmCellSlice): (builder: Builder) =
builder.storeRef(cell1);
builder.storeUint(vmCellSlice.st_bits, 10);
builder.storeUint(vmCellSlice.end_bits, 10);
builder.storeUint(vmCellSlice.st_ref, 3);
builder.storeUint(vmCellSlice.end_ref, 3);
builder.storeUint(vmCellSlice.st_ref, bitLen(4));
builder.storeUint(vmCellSlice.end_ref, bitLen(4));
if ((!(vmCellSlice.st_bits <= vmCellSlice.end_bits))) {
throw new Error('');
};
Expand Down
19 changes: 11 additions & 8 deletions generated_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Builder } from 'ton'
import { Slice } from 'ton'
import { beginCell } from 'ton'
import { BitString } from 'ton'
export function bitLen(n: number) {
return n.toString(2).length;;
}
export type Simple = {
kind: 'Simple';
a: number;
Expand Down Expand Up @@ -531,8 +534,8 @@ export type LessThan = {
y: number;
};
export function loadLessThan(slice: Slice): LessThan {
let x: number = slice.loadUint(2);
let y: number = slice.loadUint(3);
let x: number = slice.loadUint(bitLen((4 - 1)));
let y: number = slice.loadUint(bitLen(4));
return {
kind: 'LessThan',
x: x,
Expand All @@ -541,8 +544,8 @@ export function loadLessThan(slice: Slice): LessThan {
}
export function storeLessThan(lessThan: LessThan): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(lessThan.x, 2);
builder.storeUint(lessThan.y, 3);
builder.storeUint(lessThan.x, bitLen((4 - 1)));
builder.storeUint(lessThan.y, bitLen(4));
};
}
export type OneComb<A> = {
Expand Down Expand Up @@ -1286,7 +1289,7 @@ export function loadHmLabel(slice: Slice, m: number): HmLabel {
};
if ((slice.preloadUint(2) == 0b10)) {
slice.loadUint(2);
let n: number = slice.loadUint(m);
let n: number = slice.loadUint(bitLen(m));
let s: Array<BitString> = Array.from(Array(n).keys()).map((arg: number) => {
return slice.loadBits(1);
});
Expand All @@ -1300,7 +1303,7 @@ export function loadHmLabel(slice: Slice, m: number): HmLabel {
if ((slice.preloadUint(2) == 0b11)) {
slice.loadUint(2);
let v: BitString = slice.loadBits(1);
let n: number = slice.loadUint(m);
let n: number = slice.loadUint(bitLen(m));
return {
kind: 'HmLabel_hml_same',
m: m,
Expand All @@ -1326,7 +1329,7 @@ export function storeHmLabel(hmLabel: HmLabel): (builder: Builder) => void {
if ((hmLabel.kind == 'HmLabel_hml_long')) {
return (builder: Builder) => {
builder.storeUint(0b10, 2);
builder.storeUint(hmLabel.n, hmLabel.m);
builder.storeUint(hmLabel.n, bitLen(hmLabel.m));
hmLabel.s.forEach((arg: BitString) => {
builder.storeBits(arg);
});
Expand All @@ -1336,7 +1339,7 @@ export function storeHmLabel(hmLabel: HmLabel): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(0b11, 2);
builder.storeBits(hmLabel.v);
builder.storeUint(hmLabel.n, hmLabel.m);
builder.storeUint(hmLabel.n, bitLen(hmLabel.m));
};
};
throw new Error('');
Expand Down
13 changes: 6 additions & 7 deletions src/codegen/combinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ export function handleCombinator(expr: ParserExpression, fieldName: string, isFi
}
} // TODO: handle other cases
} else if (expr.name == '#<') {
if (expr.arg instanceof NumberExpr) {
result.argLoadExpr = result.argStoreExpr = tNumericLiteral(bitLen(expr.arg.num - 1));
if (expr.arg instanceof NumberExpr || expr.arg instanceof NameExpr) {
result.argLoadExpr = tFunctionCall(tIdentifier('bitLen'), [tBinaryExpression(convertToAST(convertToMathExpr(expr.arg), constructor, true), '-', tNumericLiteral(1))])
result.argStoreExpr = tFunctionCall(tIdentifier('bitLen'), [tBinaryExpression(convertToAST(convertToMathExpr(expr.arg), constructor, true, tIdentifier(variableCombinatorName)), '-', tNumericLiteral(1))])
} // TODO: handle other cases
} else if (expr.name == '#<=') {
if (expr.arg instanceof NumberExpr) {
result.argLoadExpr = result.argStoreExpr = tNumericLiteral(bitLen(expr.arg.num));
} else if (expr.arg instanceof NameExpr) {
result.argLoadExpr = tIdentifier(expr.arg.name)
result.argStoreExpr = tMemberExpression(tIdentifier(variableCombinatorName), tIdentifier(expr.arg.name));
if (expr.arg instanceof NumberExpr || expr.arg instanceof NameExpr) {
result.argLoadExpr = tFunctionCall(tIdentifier('bitLen'), [convertToAST(convertToMathExpr(expr.arg), constructor, true)]);
result.argStoreExpr = tFunctionCall(tIdentifier('bitLen'), [convertToAST(convertToMathExpr(expr.arg), constructor, true, tIdentifier(variableCombinatorName))]);
} // TODO: handle other cases
}
} else if (expr instanceof CombinatorExpr) {
Expand Down
4 changes: 4 additions & 0 deletions src/codegen/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function generate(tree: Program) {
jsCodeDeclarations.push(tImportDeclaration(tIdentifier('beginCell'), tStringLiteral('ton')))
jsCodeDeclarations.push(tImportDeclaration(tIdentifier('BitString'), tStringLiteral('ton')))

jsCodeDeclarations.push(tFunctionDeclaration(tIdentifier('bitLen'), tTypeParametersExpression([]), null, [tTypedIdentifier(tIdentifier('n'), tIdentifier('number'))], [
tExpressionStatement(tIdentifier('return n.toString(2).length;'))
]))


let tlbCode: TLBCode = { types: new Map<string, TLBType>() }

Expand Down
8 changes: 5 additions & 3 deletions tests/tlbgen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ describe('Generating tlb code', () => {
checkSameOnStoreLoad(complexTypedField, loadComplexTypedField, storeComplexTypedField);
let cellTypedField: CellTypedField = {'kind': 'CellTypedField', a:{'kind': 'ExprArgUser', t: {'kind': 'ExprArg', x: 4, value: 10}}}
checkSameOnStoreLoad(cellTypedField, loadCellTypedField, storeCellTypedField);
let lessThan: LessThan = {kind: 'LessThan', x: 3, y: 5}
let lessThan: LessThan = {kind: 'LessThan', x: 3, y: 7}
checkSameOnStoreLoad(lessThan, loadLessThan, storeLessThan);
let lessThanIncorrect: LessThan = {kind: 'LessThan', x: 77, y: 5}
checkThrowOnStoreLoad(lessThanIncorrect, loadLessThan, storeLessThan);
let lessThanIncorrectX: LessThan = {kind: 'LessThan', x: 4, y: 7}
checkThrowOnStoreLoad(lessThanIncorrectX, loadLessThan, storeLessThan);
let lessThanIncorrectY: LessThan = {kind: 'LessThan', x: 3, y: 8}
checkThrowOnStoreLoad(lessThanIncorrectY, loadLessThan, storeLessThan);
})

test('Combinators', () => {
Expand Down

0 comments on commit bc466d4

Please sign in to comment.