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

Add code:yytarget_elem to syntax files. #537

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

caffe3
Copy link
Contributor

@caffe3 caffe3 commented Mar 1, 2025

code:yytarget_elem specifies the type used for the element types in the computed goto tables.

@caffe3 caffe3 force-pushed the cgoto-yytarget-elem branch from b5c7d5b to eb98c66 Compare March 2, 2025 10:40
@caffe3
Copy link
Contributor Author

caffe3 commented Mar 2, 2025

Should I make another syntax variable for relative gotos as well i.e. gen_gocp_goto ?

code:yytarget_elem specifies the how the table elements for computed
goto tables are generated.  If cgoto:relative = 1, then the table
entries contain an offset from a calculated base address.

Bootstrap code has been regenerate.
@caffe3 caffe3 force-pushed the cgoto-yytarget-elem branch from eb98c66 to dfe47cc Compare March 2, 2025 10:49
@skvadrik
Copy link
Owner

skvadrik commented Mar 2, 2025

Should I make another syntax variable for relative gotos as well i.e. gen_gocp_goto ?

You mean another syntax configuration for the goto statement that uses table elements? Yes please. Something like code:yytarget_goto = topindent <here goes the definition...> nl;

Copy link
Owner

@skvadrik skvadrik left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good overall, just a few suggestions how to polish it.

@@ -441,3 +441,5 @@ code:yylessthan =
code:yybm_filter = yych " & ~0xFF";

code:yybm_match = yybm "[" offset "+" yych "] & " mask;

code:yytarget_elem = (.cgoto.relative ? "(int)((char*)&&" : "&&") label (.cgoto.relative ? " - (char*)&&" base ")");
Copy link
Owner

Choose a reason for hiding this comment

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

I think it will be a bit cleaner if you totally split the two cases, like this:

code:yytarget_elem = (.cgoto.relative
    ? "(int)((char*)&&" label " - (char*)&&" base ")"
    : "&&" label);

@@ -177,6 +177,27 @@ class GenEnumElem : public RenderCallback {
FORBID_COPY(GenEnumElem);
};

class GenYytargetElem : public RenderCallback {
Copy link
Owner

Choose a reason for hiding this comment

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

Just GenTargetElem to match the rest of the names that don't have YY.

Comment on lines +192 to +193
case StxVarId::BASE: os << base; break;
case StxVarId::LABEL: os << label; break;
Copy link
Owner

Choose a reason for hiding this comment

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

Formatting: in a simple switch where each case fits on one line we do indent cases.

Comment on lines +783 to +784
const std::string base = buf.str(prefix).label(base_label).flush();
const std::string lbl = buf.str(prefix).label(label).flush();
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of creating these temporary strings with unnecessarily long lifetime, can you save options and label references in GenTargetElem and output them directly?

@caffe3
Copy link
Contributor Author

caffe3 commented Mar 2, 2025

Should I make another syntax variable for relative gotos as well i.e. gen_gocp_goto ?

You mean another syntax configuration for the goto statement that uses table elements? Yes please. Something like code:yytarget_goto = topindent <here goes the definition...> nl;

Do you mean to have something like:

code:yytarget_goto = topindent "goto " (.cgoto.relative ? "*((char*)&&" base " + " array "[" index "])" : "*" array "[" index "]") nl ";";

@skvadrik
Copy link
Owner

skvadrik commented Mar 2, 2025

Do you mean to have something like [...]

Yes, but please use yytarget variable instead of array like we do for yybm and other primitives.

@caffe3
Copy link
Contributor Author

caffe3 commented Mar 3, 2025

Do you mean to have something like [...]

Yes, but please use yytarget variable instead of array like we do for yybm and other primitives.

I chose array because the code currently overlaps condition table generation with yytarget. I guess then we should consider make a separate set of syntax for yyctable (which would be useful for relative gotos anyway so we don't have to use the same types for both of them). Do you have any naming preferences for conditions?

@skvadrik
Copy link
Owner

skvadrik commented Mar 3, 2025

I chose array because the code currently overlaps condition table generation with yytarget. I guess then we should consider make a separate set of syntax for yyctable (which would be useful for relative gotos anyway so we don't have to use the same types for both of them). Do you have any naming preferences for conditions?

Ah, I see why you chose array. But if we'd have to use something more generic than code:yytarget_elem, since it would be strange to use yytarget in configuration name, but not in the variable name. I see two options.

First, sticking with yytarget in both places. As for yyctable, making another set of configurations for it seems like a waste: it's a very niche use case, and only for one language. We can reuse yytarget configurations for yyctable (and substitute yyctable for yytarget variable) with a comment explaining this in the codegen code. This way we leave the options open for later if we want to add separate configs for yyctable.

Second, we can rename code:yytarget_goto and code:yytarget_elem to code:cgoto and code:cgoto_target respectively, move them in the syntax file under code:goto definition and use array variable name. This is more generic and can be reused for any APIs.

What do you think?

@caffe3
Copy link
Contributor Author

caffe3 commented Mar 3, 2025

First, sticking with yytarget [...]
Second, [...]

What do you think?

I think the first option could be useful - there's a high chance that when conditions are used, there will be very few conditions, one could in theory set the elem type for conditions as uint8_t.

@skvadrik
Copy link
Owner

skvadrik commented Mar 3, 2025

I think the first option could be useful - there's a high chance that when conditions are used, there will be very few conditions, one could in theory set the elem type for conditions as uint8_t.

Ok, let's go this way. Should we also add code:type_yyctable then? Could be done in a follow-up commit / PR.

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