Question

Background: I am programming a .NET compiler (very similar to C#) for a school project. One of the features I am currently trying to add is tailcall recursion within methods.

More info: In CIL, the "this" is passed into instance methods as if it were just another argument. So, accessing the first argument of a static method, you would emit ldarg.0, but accessing the first argument of an instance method, you would emit ldarg.1, and accessing "this" in an instance method you would emit ldarg.0. (Instance methods are even more similar to extension methods than I ever imagined.)

Question: Can you set "this" using starg.0 without any side effects?

Why this is in question: Whether or not a method is an instance method is set with the MethodBuilder, which is a bit of a black box. Although "this" seems just like any other argument, for all I know some JIT compilers keep track of "this" separately and change their behavior depending on this value. If there are side effects when you set "this" in an instance method, then how can I avoid them?

Was it helpful?

Solution

You may want to have a look at how F# implements tail-call.

OTHER TIPS

You can extract this as a local variable. This way you will know that you can set it safely. (I hope I understand your question correctly)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top