Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: consolidate optimizer #1319

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ The constant evaluator is used as an optimizer to prevent some statically known

The constant evaluator supports a large subset of Tact and handles, for instance, constants defined in terms of other constants, built-in and user-defined functions, logical and arithmetic operations.

The main logic of the constant evaluator can be found in the file [src/interpreter.ts](./src/interpreter.ts).
The main logic of the constant evaluator can be found in the file [src/optimizer/interpreter.ts](./src/optimizer/interpreter.ts).

You can find the relevant tests in [src/test/e2e-emulated/contracts/constants.tact](./src/test/e2e-emulated/contracts/constants.tact) and the corresponding spec-file: [](./src/test/e2e-emulated/constants.spec.ts).
You can find the relevant tests in [src/test/e2e-emulated/contracts/constants.tact](./src/test/e2e-emulated/contracts/constants.tact) and the corresponding spec-file: [src/test/e2e-emulated/constants.spec.ts](./src/test/e2e-emulated/constants.spec.ts).

The negative tests for constant evaluation are contained in the Tact files prefixed with `const-eval` in the [src/test/compilation-failed/contracts](./src/test/compilation-failed/contracts) folder.

Expand Down
2 changes: 1 addition & 1 deletion src/abi/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ensureSimplifiedString,
ensureString,
interpretEscapeSequences,
} from "../interpreter";
} from "../optimizer/interpreter";
import { isLiteral } from "../grammar/ast";

export const GlobalFunctions: Map<string, AbiFunction> = new Map([
Expand Down
2 changes: 1 addition & 1 deletion src/generator/writers/writeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
import { ops } from "./ops";
import { writeCastedExpression } from "./writeFunction";
import { isLvalue } from "../../types/resolveStatements";
import { evalConstantExpression } from "../../constEval";
import { evalConstantExpression } from "../../optimizer/constEval";
import { getAstUtil } from "../../optimizer/util";

function isNull(wCtx: WriterContext, expr: AstExpression): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ export async function run(args: {

export { createNodeFileSystem } from "./vfs/createNodeFileSystem";

export { parseAndEvalExpression } from "./interpreter";
export { parseAndEvalExpression } from "./optimizer/interpreter";

export { showValue } from "./types/types";
2 changes: 1 addition & 1 deletion src/optimizer/associative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
AstOpBinary,
isLiteral,
} from "../grammar/ast";
import * as iM from "../interpreter";
import * as iM from "./interpreter";
import { ExpressionTransformer, Rule } from "./types";
import {
abs,
Expand Down
17 changes: 10 additions & 7 deletions src/constEval.ts → src/optimizer/constEval.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { CompilerContext } from "./context";
import { CompilerContext } from "../context";
import {
AstBinaryOperation,
AstExpression,
AstUnaryOperation,
isLiteral,
AstLiteral,
} from "./grammar/ast";
import { TactConstEvalError, throwInternalCompilerError } from "./error/errors";
import { AstUtil } from "./optimizer/util";
import { ExpressionTransformer } from "./optimizer/types";
import { StandardOptimizer } from "./optimizer/standardOptimizer";
} from "../grammar/ast";
import {
TactConstEvalError,
throwInternalCompilerError,
} from "../error/errors";
import { AstUtil } from "./util";
import { ExpressionTransformer } from "./types";
import { StandardOptimizer } from "./standardOptimizer";
import {
Interpreter,
InterpreterConfig,
Expand All @@ -18,7 +21,7 @@ import {
evalUnaryOp,
throwNonFatalErrorConstEval,
} from "./interpreter";
import { SrcInfo } from "./grammar";
import { SrcInfo } from "../grammar";

// Utility Exception class to interrupt the execution
// of functions that cannot evaluate a tree fully into a value.
Expand Down
18 changes: 9 additions & 9 deletions src/interpreter.ts → src/optimizer/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Address, beginCell, BitString, Cell, toNano } from "@ton/core";
import { paddedBufferToBits } from "@ton/core/dist/boc/utils/paddedBits";
import * as crc32 from "crc-32";
import { evalConstantExpression } from "./constEval";
import { CompilerContext } from "./context";
import { CompilerContext } from "../context";
import {
TactCompilationError,
TactConstEvalError,
idTextErr,
throwConstEvalError,
throwInternalCompilerError,
} from "./error/errors";
} from "../error/errors";
import {
AstAddress,
AstBinaryOperation,
Expand Down Expand Up @@ -64,20 +64,20 @@ import {
getAstFactory,
idText,
isSelfId,
} from "./grammar/ast";
import { AstUtil, divFloor, getAstUtil, modFloor } from "./optimizer/util";
} from "../grammar/ast";
import { AstUtil, divFloor, getAstUtil, modFloor } from "./util";
import {
getStaticConstant,
getStaticFunction,
getType,
hasStaticConstant,
hasStaticFunction,
} from "./types/resolveDescriptors";
import { getExpType } from "./types/resolveExpression";
import { TypeRef, showValue } from "./types/types";
} from "../types/resolveDescriptors";
import { getExpType } from "../types/resolveExpression";
import { TypeRef, showValue } from "../types/types";
import { sha256_sync } from "@ton/crypto";
import { defaultParser, getParser, Parser } from "./grammar/grammar";
import { dummySrcInfo, SrcInfo } from "./grammar";
import { defaultParser, getParser, Parser } from "../grammar/grammar";
import { dummySrcInfo, SrcInfo } from "../grammar";

// TVM integers are signed 257-bit integers
const minTvmInt: bigint = -(2n ** 256n);
Expand Down
4 changes: 2 additions & 2 deletions src/optimizer/test/partial-eval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
isLiteral,
} from "../../grammar/ast";
import { AstUtil, getAstUtil } from "../util";
import { getOptimizer } from "../../constEval";
import { getOptimizer } from "../constEval";
import { CompilerContext } from "../../context";
import { ExpressionTransformer, Rule } from "../types";
import { AssociativeRule3 } from "../associative";
import { evalBinaryOp, evalUnaryOp } from "../../interpreter";
import { evalBinaryOp, evalUnaryOp } from "../interpreter";
import { getParser } from "../../grammar";
import { defaultParser } from "../../grammar/grammar";
import { throwInternalCompilerError } from "../../error/errors";
Expand Down
2 changes: 1 addition & 1 deletion src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { getRawAST } from "../grammar/store";
import { cloneNode } from "../grammar/clone";
import { crc16 } from "../utils/crc16";
import { isSubsetOf } from "../utils/isSubsetOf";
import { evalConstantExpression } from "../constEval";
import { evalConstantExpression } from "../optimizer/constEval";
import {
intMapKeyFormats,
intMapValFormats,
Expand Down
4 changes: 2 additions & 2 deletions src/types/resolveErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { sha256_sync } from "@ton/crypto";
import { CompilerContext, createContextStore } from "../context";
import { AstNode, FactoryAst, isRequire } from "../grammar/ast";
import { traverse } from "../grammar/iterators";
import { evalConstantExpression } from "../constEval";
import { evalConstantExpression } from "../optimizer/constEval";
import { throwInternalCompilerError } from "../error/errors";
import {
getAllStaticFunctions,
getAllTypes,
getAllStaticConstants,
} from "./resolveDescriptors";
import { ensureSimplifiedString } from "../interpreter";
import { ensureSimplifiedString } from "../optimizer/interpreter";
import { AstUtil, getAstUtil } from "../optimizer/util";

type Exception = { value: string; id: number };
Expand Down
4 changes: 2 additions & 2 deletions src/types/resolveSignatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { AstNumber, AstReceiver, FactoryAst } from "../grammar/ast";
import { commentPseudoOpcode } from "../generator/writers/writeRouter";
import { sha256_sync } from "@ton/crypto";
import { dummySrcInfo } from "../grammar";
import { ensureInt } from "../interpreter";
import { evalConstantExpression } from "../constEval";
import { ensureInt } from "../optimizer/interpreter";
import { evalConstantExpression } from "../optimizer/constEval";
import { getAstUtil } from "../optimizer/util";

export function resolveSignatures(ctx: CompilerContext, Ast: FactoryAst) {
Expand Down
4 changes: 2 additions & 2 deletions src/types/resolveStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
} from "./resolveDescriptors";
import { getExpType, resolveExpression } from "./resolveExpression";
import { FunctionDescription, printTypeRef, TypeRef } from "./types";
import { evalConstantExpression } from "../constEval";
import { ensureInt } from "../interpreter";
import { evalConstantExpression } from "../optimizer/constEval";
import { ensureInt } from "../optimizer/interpreter";
import { crc16 } from "../utils/crc16";
import { SrcInfo } from "../grammar";
import { AstUtil, getAstUtil } from "../optimizer/util";
Expand Down
Loading