MrSmith33 / vox

Vox language compiler. AOT / JIT / Linker. Zero dependencies
Boost Software License 1.0
327 stars 18 forks source link

`be.reg_alloc.move_solver(82): ICE: Assertion failure: Second write to eax detected` error #27

Closed rempas closed 2 years ago

rempas commented 2 years ago

I'm trying to create a function to covert a string to a float I'm keep getting the error in the title. I have found out where the error occurs and I will post a snippet of the code.

Code:

f64 to_f64(u8* s) {
  f64 a = 0.0;
  i32 c;
  i32 e = 0;

  // HERE: "c = *s++" causes the error
  while ((c = *s++) != '\0' && is_digit(c)) {
    a = a * 10.0 + (c - '0');
  }

  if (c == '.') {
    // However, here it works perfectly fine
    while ((c = *s++) != '\0' && is_digit(c)) {
      a = a * 10.0 + (c - '0');
      e = e - 1;
    }

So yeah, any ideas why is this happening?

EDIT: This error happens on other places too but I haven't find the exact place yet so let's first fix that one and then I will update for the other places as well.

EDIT: Actually it's only one other place, I fixed the others

MrSmith33 commented 2 years ago

Should be fixed with 0e7000148a0d0c76a441bdc9d25ae21dedfc3af5. Also, did you run the latest version of the compiler?

rempas commented 2 years ago

At the time of writing this, the last commit wasn't out so even If I wanted, I couldn't try it, lol!

I downloaded it now but It didn't fixed my problem but actually now I have another problem. So If I keep the code as it is I now get a new error: Cannot compare `void` and `u8. Hmmm... Interesting!

So the expression is: (c = *s++) != '\0'. Shouldn't the leftval return the value of "c" which is an i32 like how it would happen in C? Why does it return it as "void"? Or are the rules different in Vox?

I tried to modify the code to the following:

From:

  while ((c = *s++) != '\0' && is_digit(c)) {
    a = a * 10.0 + (c - '0');
  }

To:

  c = *s++;
  while (c != '\0' && is_digit(c)) {
    a = a * 10.0 + (c - '0');
    c = *s++;
  }

And now, while I don't get this error now, I'm getting the original "illegal hardware instruction" error now. I can't understand what I'm doing wrong really...

MrSmith33 commented 2 years ago

Yes, assignments return void in Vox. Do you get the error during compilation?

MrSmith33 commented 2 years ago

Can you reduce the code and post the whole source code that fails?

rempas commented 2 years ago

Yes, assignments return void in Vox

Oh! Why is that? Is there any advantages to that method? Or did you just decided this makes more sense?

Do you get the error during compilation?

Yes.

I will send you the source code in about 2 hours as I'm unable to do that now. Thanks for the support man!

MrSmith33 commented 2 years ago

Oh! Why is that? Is there any advantages to that method? Or did you just decided this makes more sense?

Decided to make assignment as expression illegal (it was previously an expression though)

rempas commented 2 years ago

Sorry it took some time but I'm finally home and I have created the snippet. You can see it here. Tell me if you need anything more

MrSmith33 commented 2 years ago

Can't access the link

rempas commented 2 years ago

Weird, try this one. Is it ok now?

MrSmith33 commented 2 years ago

yep, got the crash reproduced, thanks!

rempas commented 2 years ago

You're welcome! If I knew you would be active at that time, I would even upload it earlier

MrSmith33 commented 2 years ago

Looks like multiplication of float by constant is triggering, which I haven't implemented yet.

rempas commented 2 years ago

Looks like multiplication of float by constant is triggering, which I haven't implemented yet.

Interesting! Tho still If I take out the expression: c = *s++, it seems to compile normally for me.

MrSmith33 commented 2 years ago

Should be working now

rempas commented 2 years ago

Lol, still not working for me with the new commit. Maybe it's a platform thing idk...

rempas commented 2 years ago

Ok, This is really weird but I played with the code and the behavior doesn't really make sense to me. The same code works in other places and it doesn't work in others. Check the following snippet and focus on the comments:

  /* c = *s++; */ // This expression will give use the "illegal hardware instruction" issue if I un-comment it
  while (c != '\0' && is_digit(c)) {
    a = a * 10.0 + (c - '0');
    /* c = *s++; */ // The same happens if I un-comment this out too.
  }

  if (c == '.') {
    c = *s++; // However, the same expression doesn't give the error here
    while (c != '\0' && is_digit(c)) {
      a = a * 10.0 + (c - '0');
      e = e - 1;
      c = *s++; // And here too!
    }
  }

This is part of the function that I uploaded in the gist. So I played it a little bit with it and found this weird behavior out so here is a report sir!

MrSmith33 commented 2 years ago

I see the error there, that's another issue

rempas commented 2 years ago

I see the error there, that's another issue

Oh, cool. That was the problem I had that from the beginning. I'm glad I could report it. I'm making a system library so I'll be writing some code and probably I will be able to report some bugs. Vox rocks so far man thanks to your amazing work!!!

MrSmith33 commented 2 years ago

there is at least 2 bugs in there

rempas commented 2 years ago

What's the second? You mean separate bugs and not the fact that the expression exists two times right? Also when you saying "bugs" you mean things that have to be fixed/impelemented on Vox on not mistakes I made with my code right?

MrSmith33 commented 2 years ago

Any crash or internal error inside of the compiler is a bug. Compiler should report all the invalid code to the user and compile valid code without crashes.

On my machine your source code makes compiler print:

be.emit_mc_amd64(257): ICE: Assertion failure: reg size mismatch 2 != 3

and the stack trace following that

If I modify the source code slightly, then I get a different error:

be.reg_alloc.move_solver(82): ICE: Assertion failure: Second write to r0<c0 s2> detected
rempas commented 2 years ago

Compiler should report all the invalid code to the user and compile valid code without crashes

Yeah, you are right. Creating a multi-platform compiler is hard for sure but still the current state of Vox is amazing!

On my machine your source code....

If I compile with debug with DMD, I will also get similar (but not the same) assertion failure messages. Keep in mind that I'm using the compilation commands as sown in "README.md"

MrSmith33 commented 2 years ago

are you not using the CI builds?

rempas commented 2 years ago

No. Is there a difference?

MrSmith33 commented 2 years ago

Depends on the flags used to compile. Maybe that is the reason why you did only see Illegal instruction being output. But I'm changing the way internal errors are reported, so it should be more informative and not depend on version switch

rempas commented 2 years ago

But I'm changing the way internal errors are reported, so it should be more informative and not depend on version switch

Thanks! I will use the CI builds in this case. This will also free me of the need to have LDC and Libphobos installed. I may change back in the future especially after I start looking at Vox's code and start contributing. Tell me if you need anything else

rempas commented 2 years ago

Yep! I just used the CI build for Linux (the release one) and the message is exactly the one you posted.

rempas commented 2 years ago

Also the CI build seems slightly faster than the one I made myself with ldc2

MrSmith33 commented 2 years ago

You can see the exact commands used to build in CI config here https://github.com/MrSmith33/vox/blob/master/.github/workflows/ci.yml#L56

rempas commented 2 years ago

Thanks man!

MrSmith33 commented 2 years ago

I found both issues. The reg size mismatch 2 != 3 I already fixed. The other one is in progress. While I'm fixing it, passing --no-dce to the compiler should allow you to continue the work.

rempas commented 2 years ago

Man you are a legend and Vox has a future and I hope you love that! Don't rush, I will wait for you to end the job and then I'll try again after the new commit comes out.

rempas commented 2 years ago

God job, I can now confirm that it compiles perfectly!

rempas commented 2 years ago

Btw, I found another error when trying to return a function. Should I create a new issue? Also how would I know of what kind of bugs you are aware and what to post or not? Should you make a list with known bugs and examples and how to produce them so I will know what to do?

rempas commented 2 years ago

*return a value from a function

MrSmith33 commented 2 years ago

Should I create a new issue?

Please do.

how would I know of what kind of bugs you are aware and what to post or not?

Some of those are in the todo.txt, some in the tests, some in my head. I guess if there is no github issue, then it should be useful to be reported. If it is new I will fix it. If it is known by me, then it will become known by Vox users too.

rempas commented 2 years ago

Ok, it is decided then. We are posting and you are updating!