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

Constant fold all kinds of constants #2819

Merged
merged 2 commits into from
Aug 4, 2023
Merged

Conversation

emanuele6
Copy link
Member

Also use jv_equal() to constant fold == and != instead of jv_cmp() to match the implementation of _equal and _notequal.

@emanuele6 emanuele6 marked this pull request as draft August 3, 2023 22:36
@emanuele6 emanuele6 force-pushed the lesscmp branch 2 times, most recently from cfa9ab6 to f3bf084 Compare August 4, 2023 00:05
src/parser.y Outdated
}

if (jv_get_kind(res) == JV_KIND_INVALID)
if (!jv_is_valid(res)) {
jv_free(res);
Copy link
Member Author

@emanuele6 emanuele6 Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to generate a constant error block? That would be cool. =)

Copy link
Member Author

@emanuele6 emanuele6 Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a way to do it!

@emanuele6 emanuele6 changed the title Only call jv_cmp() for comparison operators when constant folding Constant fold all kinds of constants Aug 4, 2023
@emanuele6
Copy link
Member Author

I noticed that string comparison, string division, string-number multiplication, etc were not being constant folded, so I decided to finally export the binary operators' implementations from builtin.c, and use those to implement constant folding so now all type combinations are supported and the constant folding behaviour is exactly the same as runtime.

@emanuele6 emanuele6 marked this pull request as ready for review August 4, 2023 00:12
@emanuele6 emanuele6 force-pushed the lesscmp branch 2 times, most recently from 3907aa4 to bfdfff6 Compare August 4, 2023 00:58
@@ -47,3 +47,5 @@ OP(GENLABEL, NONE, 0, 1)

OP(DESTRUCTURE_ALT, BRANCH, 0, 0)
OP(STOREVN, VARIABLE, 1, 0)

OP(ERROR, CONSTANT, 1, 0)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 1, 0 correct here? I think stack_in == 1 is correct, but the second value (stack_out) is not used for anything so I am not sure what it should be.

@emanuele6 emanuele6 force-pushed the lesscmp branch 3 times, most recently from 4e3ddb5 to e18aa67 Compare August 4, 2023 01:30
@emanuele6
Copy link
Member Author

I renamed the opcode to ERRORK. It seems that on windows, ERROR is already used for something else.

This patch exports all the binary operator builtins functions from
builtin.c and uses them for constant folding in the parser, allowing
constant folding to work will all kinds and combinations of constants.

Now string*number,  $ARGS+$ARGS,  string/string,  etc will also be
constant folded and the implementation of constant folded operators and
runtime operators will be the same.

And thanks to the new ERRORK bytecode operation, errors are constant
folded too! (e.g. 1 / 0   [] * {}   etc)
@emanuele6
Copy link
Member Author

I made a mistake when renaming ERROR, now it should work

@nicowilliams
Copy link
Contributor

Well done!

@nicowilliams nicowilliams merged commit 3b6e7dd into jqlang:master Aug 4, 2023
28 checks passed
@nicowilliams
Copy link
Contributor

Thanks!

@emanuele6 emanuele6 deleted the lesscmp branch August 4, 2023 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants