Skip to content

native: for in string #24613

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

Merged
merged 4 commits into from
May 31, 2025
Merged

native: for in string #24613

merged 4 commits into from
May 31, 2025

Conversation

Eliyaan
Copy link
Contributor

@Eliyaan Eliyaan commented May 30, 2025

  • implements for ch in my_string
    (skips one of the new tests on windows, passing only with gcc)

Copy link

Connected to Huly®: V_0.6-22974

@@ -95,6 +95,7 @@ mut:
cmp_to_stack_top(r Register)
cmp_var_reg(var Var, reg Register, config VarConfig)
cmp_var(var Var, val i32, config VarConfig)
cmp_reg2(reg Register, reg2 Register)
Copy link
Member

Choose a reason for hiding this comment

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

Why use cmp_reg2 instead of cmp_reg here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because cmp_reg needs Amd64Register and not Register, so cmp_reg2 is a function that casts the Registers to Amd64Register and then calls cmp_reg. I did not change the cmp_reg function as it is already used in a lot of places, to avoid big changes. (same for pop and pop2)

Copy link
Member

Choose a reason for hiding this comment

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

Is there a place, that will need to call the interface version, instead of the CPU specific one?
i.e. why does it need to be in the public interface at all?

Copy link
Member

Choose a reason for hiding this comment

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

hm, there are a lot of calls to it fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {, similar to this:

        g.code_gen.pop2(Amd64Register.rdx)
        g.println('; push address of the len:')
        g.code_gen.push(Amd64Register.rdx) // len
        g.code_gen.mov_deref(Amd64Register.rdx, Amd64Register.rdx, ast.int_type)
        g.code_gen.cmp_reg2(main_reg, Amd64Register.rdx)

That does not seem right to me - vlib/v/gen/native/stmt.c.v is supposed to have platform independent code, not pass the names of Amd64 specific registers around 🤔 .

Copy link
Member

Choose a reason for hiding this comment

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

I think we should have something similar to:
main_reg := g.code_gen.main_reg()
for example:
sreg := g.code_gen.secondary_reg()
and then use that instead of Amd64Register.rdx in places, that are not supposed to be platform dependent.

Copy link
Member

Choose a reason for hiding this comment

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

However, that cleanup is better indeed to be in its own separate PR, not here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, I will clean this up when I'll have a better idea on how it could be organized in a cleaner way.

@@ -131,6 +132,7 @@ mut:
mov64(r Register, val Number)
movabs(reg Register, val i64)
patch_relative_jmp(pos i32, addr i64)
pop2(r Register)
Copy link
Member

Choose a reason for hiding this comment

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

why add pop2, instead of pop here?
There is no other pop method in the public interface, and it only has a single argument, not 2.

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

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

Excellent work.

@spytheman spytheman merged commit f9a4f8f into vlang:master May 31, 2025
66 checks passed
@medvednikov
Copy link
Member

Yay!

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.

3 participants