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

fix: very simple constant propagation for obvious cases #1596

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f91d409
feat(const-eval): Very simple constant propagation to detect obvious …
jeshecdom Jan 27, 2025
fb128d2
Merge branch 'main' into issue716v2
jeshecdom Jan 27, 2025
35cb3d4
refactor: imports from ast should use A.*
jeshecdom Jan 29, 2025
0d1cc84
refactor: arrays for known mutating and non-mutating functions for st…
jeshecdom Jan 29, 2025
a834015
fix: Added check for simultaneous case of else and else if in AstStat…
jeshecdom Jan 29, 2025
eeeef4a
fix: Early returns and variable renaming.
jeshecdom Jan 29, 2025
c2d1a0e
Added missing "simulates" in the cases for conditional, and operators…
jeshecdom Jan 29, 2025
db5c7e3
refactor: moved tests out of deprecaed folder.
jeshecdom Jan 29, 2025
9edf51f
Prettier pass.
jeshecdom Jan 29, 2025
9109ea0
Merge branch 'main' into issue716v2
jeshecdom Jan 29, 2025
26b5eb2
refactor: split tests into success and fail specs.
jeshecdom Jan 30, 2025
04c0e6a
Merge branch 'main' into issue716v2
jeshecdom Jan 30, 2025
bb689b8
Linter and prettier changes.
jeshecdom Jan 30, 2025
b6634ac
refactor: I had to separate the constant propagation tests into their…
jeshecdom Jan 30, 2025
a851e68
Merge branch 'main' into issue716v2
jeshecdom Feb 3, 2025
6076890
Merge branch 'main' into issue716v2
jeshecdom Feb 4, 2025
40c0fec
refactor: reverted unrelated changes.
jeshecdom Feb 5, 2025
7525d47
refactor: renamings and comments.
jeshecdom Feb 5, 2025
aa8a193
Merge branch 'main' into issue716v2
jeshecdom Feb 5, 2025
42e40ea
Fixes after merge.
jeshecdom Feb 5, 2025
22fc30e
refactor: more reverts.
jeshecdom Feb 5, 2025
ce93b2c
Merge branch 'main' into issue716v2
jeshecdom Feb 5, 2025
562fc4d
Merge branch 'main' into issue716v2
jeshecdom Feb 5, 2025
ded0d2f
Fixes after merge.
jeshecdom Feb 5, 2025
1f7d3b7
Merge branch 'main' into issue716v2
jeshecdom Feb 17, 2025
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
24 changes: 24 additions & 0 deletions src/ast/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ export function cloneNode<T extends AstNode>(
return cloneNode(src);
} else if (src.kind === "string") {
return cloneNode(src);
} else if (src.kind === "address") {
// The actual address value src.value is immutable (it freezes its fields in its constructor)
// So, there is no need to make a copy of the value
return cloneNode(src);
} else if (src.kind === "cell") {
// The actual address value src.value is immutable (it freezes its fields in its constructor)
// So, there is no need to make a copy of the value
return cloneNode(src);
} else if (src.kind === "simplified_string") {
return cloneNode(src);
} else if (src.kind === "slice") {
// This will make a copy of the slice from the point it is
// currently reading (this is compatible with the way equality works for slices)
return cloneNode({ ...src, value: src.value.clone() });
} else if (src.kind === "struct_value") {
return cloneNode({
...src,
args: src.args.map(recurse),
});
} else if (src.kind === "statement_assign") {
return cloneNode({
...src,
Expand Down Expand Up @@ -54,6 +73,11 @@ export function cloneNode<T extends AstNode>(
...src,
initializer: recurse(src.initializer),
});
} else if (src.kind === "struct_field_value") {
return cloneNode({
...src,
initializer: recurse(src.initializer),
});
} else if (src.kind === "statement_expression") {
return cloneNode({
...src,
Expand Down
116 changes: 115 additions & 1 deletion src/ast/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { isLiteral } from "./ast-helpers";
import type { SrcInfo } from "../grammar";
import { dummySrcInfo } from "../grammar";

export const getAstUtil = ({ createNode }: FactoryAst) => {
export const getAstUtil = (astF: FactoryAst) => {
const createNode = astF.createNode;
const cloneNode = astF.cloneNode;

// FIXME: Deprecate makeUnaryExpression and makeBinaryExpression, since they use dummySrcInfo.

function makeUnaryExpression(
op: A.AstUnaryOperation,
operand: A.AstExpression,
Expand Down Expand Up @@ -34,6 +39,38 @@ export const getAstUtil = ({ createNode }: FactoryAst) => {
return result as A.AstExpression;
}

/* Use this function instead of makeUnaryExpression */
function makeUnaryExpressionLoc(
op: A.AstUnaryOperation,
operand: A.AstExpression,
loc: SrcInfo,
): A.AstOpUnary {
const result = createNode({
kind: "op_unary",
op: op,
operand: operand,
loc,
});
return result as A.AstOpUnary;
i582 marked this conversation as resolved.
Show resolved Hide resolved
}

/* Use this function instead of makeBinaryExpression */
function makeBinaryExpressionLoc(
op: A.AstBinaryOperation,
left: A.AstExpression,
right: A.AstExpression,
loc: SrcInfo,
): A.AstOpBinary {
const result = createNode({
kind: "op_binary",
op: op,
left: left,
right: right,
loc,
});
return result as A.AstOpBinary;
}

function makeNumberLiteral(n: bigint, loc: SrcInfo): A.AstNumber {
const result = createNode({
kind: "number",
Expand Down Expand Up @@ -132,7 +169,80 @@ export const getAstUtil = ({ createNode }: FactoryAst) => {
return result as A.AstStructValue;
}

function makeAssignStatement(
path: A.AstExpression,
expression: A.AstExpression,
loc: SrcInfo,
): A.AstStatementAssign {
const result = createNode({
kind: "statement_assign",
path,
expression,
loc,
});
return result as A.AstStatementAssign;
}

function makeMethodCall(
self: A.AstExpression,
method: A.AstId,
args: A.AstExpression[],
loc: SrcInfo,
): A.AstMethodCall {
const result = createNode({
kind: "method_call",
self,
method,
args,
loc,
});
return result as A.AstMethodCall;
}

function makeStaticCall(
fun: A.AstId,
args: A.AstExpression[],
loc: SrcInfo,
): A.AstStaticCall {
const result = createNode({
kind: "static_call",
function: fun,
args,
loc,
});
return result as A.AstStaticCall;
}

function makeInitOf(
contract: A.AstId,
args: A.AstExpression[],
loc: SrcInfo,
): A.AstInitOf {
const result = createNode({
kind: "init_of",
contract,
args,
loc,
});
return result as A.AstInitOf;
}

function changeLocationOfLiteral(
expr: A.AstLiteral,
loc: SrcInfo,
): A.AstLiteral {
return cloneNode({ ...expr, loc });
}

function getAstFactory(): FactoryAst {
return astF;
}

return {
getAstFactory,
changeLocationOfLiteral,
makeUnaryExpressionLoc,
makeBinaryExpressionLoc,
makeUnaryExpression,
makeBinaryExpression,
makeNumberLiteral,
Expand All @@ -144,6 +254,10 @@ export const getAstUtil = ({ createNode }: FactoryAst) => {
makeAddressLiteral,
makeStructFieldValue,
makeStructValue,
makeAssignStatement,
makeMethodCall,
makeStaticCall,
makeInitOf,
};
};

Expand Down
Loading
Loading