How local variable usage infomation is maintained in .net clr source code

Mouhong Lin

This great answer explains how GC is able to collect local variables before the method finishes executing:

The jitter performs two important duties when it compiles the IL for a method into machine code. ... It also generates a table that describes how the local variables inside the method body are used. That table has an entry for each method argument and local variable with two addresses. The address where the variable will first store an object reference. And the address of the machine code instruction where that variable is no longer used. ... The "no longer used" address in the table is very important. It makes the garbage collector very efficient. It can collect an object reference, even if it is used inside a method and that method hasn't finished executing yet.

I'm curious about how JIT created internal tables look like, and how "no longer used" addresses are maintained in the real clr source code. Can anybody show me related code snippets in the recently open sourced coreclr source code?

Martin Törnwall

Disclaimer: I'm no expert on the CLR or RyuJIT. I may be completely wrong about all of this.

I came across the following section in the RyuJIT chapter of the Book of the Runtime:

For lvlVars with tracked lifetimes, or for expression involving GC references, we report the range over which the reference is live. This is done by the emitter, which adds this information to the instruction group, and which terminates instruction groups when the GC info changes.

The structure that appears to store this information can be found in jit/jitgcinfo.h and looks like this:

struct varPtrDsc
{
    varPtrDsc   *   vpdNext;

    unsigned        vpdVarNum;         // which variable is this about?

    unsigned        vpdBegOfs ;        // the offset where life starts
    unsigned        vpdEndOfs;         // the offset where life starts
};

The paragraph I quoted above suggests that these fields are filled by "the emitter", by which I believe they mean jit/emit.cpp.

The start of the lifetime interval is set in emitter::emitGCvarLiveSet(); the relevant excerpt is (blanks eliminated for brevity):

/* Allocate a lifetime record */
desc = new (emitComp, CMK_GC) varPtrDsc;
desc->vpdBegOfs = emitCurCodeOffs(addr);
#ifdef DEBUG
desc->vpdEndOfs = 0xFACEDEAD;
#endif
desc->vpdVarNum = offs;
desc->vpdNext = NULL;

The end of the lifetime is set in a similar manner, in emitter::emitGCvarDeadSet():

/* Record the death code offset */
assert(desc->vpdEndOfs == 0xFACEDEAD);
       desc->vpdEndOfs  = emitCurCodeOffs(addr);

Finally, the tables appear to be written in jit/gcencode.cpp, specifically in GCInfo::gcMakeVarPtrTable().

Hopefully this will serve as a starting point if you want to explore further.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is the source code to Common Interpreted Language compiler located within the CLR in .NET

From Dev

Code Verification done by CLR in .NET

From Dev

Usage of Microsoft .NET Source Code in my own program

From Dev

codeigniter how to get infomation of a row

From Dev

.NET local variable optimization

From Dev

Update local gem source code

From Dev

How to store typed infomation in a method from Scanner

From Dev

How to get user infomation with uid in firebase?

From Dev

How can I declare local variable in Razor vb.net?

From Dev

.NET CLI: How is a local variable popped off the stack if not on top?

From Dev

usage of antlr for generating source code for another language

From Dev

How to clear/garbage collect a local variable value after its usage is done

From Dev

How to track a Java app's usage without rewriting its source code?

From Dev

Returned variable with no indication in source code

From Dev

How to "source" a specific variable

From Dev

Dumping Source Code into a local file using CasperJS

From Dev

Run gem from local source code

From Dev

How the order for dictionary in python is maintained?

From Dev

How are dependencies maintained during uninstallation?

From Dev

How RPM and dnf databases are maintained?

From Dev

Is it possible to access source code or a config file of a terminal command, to read how it works and even customize it on local computer?

From Dev

Repository variable usage - How to track?

From Dev

How refresh to refresh C# CLR code (assembly) in SQL Server?

From Dev

How to connect to a Postgresql database using .NET Core CLR

From Dev

How to make Windows use always the latest CLR for .Net assemblies?

From Dev

How to connect to a Postgresql database using .NET Core CLR

From Dev

.NET CLR: How does runtime calculate size of object?

From Dev

java framework source has a pattern that assigns instance variable to local variable

From Dev

java framework source has a pattern that assigns instance variable to local variable

Related Related

  1. 1

    Is the source code to Common Interpreted Language compiler located within the CLR in .NET

  2. 2

    Code Verification done by CLR in .NET

  3. 3

    Usage of Microsoft .NET Source Code in my own program

  4. 4

    codeigniter how to get infomation of a row

  5. 5

    .NET local variable optimization

  6. 6

    Update local gem source code

  7. 7

    How to store typed infomation in a method from Scanner

  8. 8

    How to get user infomation with uid in firebase?

  9. 9

    How can I declare local variable in Razor vb.net?

  10. 10

    .NET CLI: How is a local variable popped off the stack if not on top?

  11. 11

    usage of antlr for generating source code for another language

  12. 12

    How to clear/garbage collect a local variable value after its usage is done

  13. 13

    How to track a Java app's usage without rewriting its source code?

  14. 14

    Returned variable with no indication in source code

  15. 15

    How to "source" a specific variable

  16. 16

    Dumping Source Code into a local file using CasperJS

  17. 17

    Run gem from local source code

  18. 18

    How the order for dictionary in python is maintained?

  19. 19

    How are dependencies maintained during uninstallation?

  20. 20

    How RPM and dnf databases are maintained?

  21. 21

    Is it possible to access source code or a config file of a terminal command, to read how it works and even customize it on local computer?

  22. 22

    Repository variable usage - How to track?

  23. 23

    How refresh to refresh C# CLR code (assembly) in SQL Server?

  24. 24

    How to connect to a Postgresql database using .NET Core CLR

  25. 25

    How to make Windows use always the latest CLR for .Net assemblies?

  26. 26

    How to connect to a Postgresql database using .NET Core CLR

  27. 27

    .NET CLR: How does runtime calculate size of object?

  28. 28

    java framework source has a pattern that assigns instance variable to local variable

  29. 29

    java framework source has a pattern that assigns instance variable to local variable

HotTag

Archive