Compiler: How to implement Reference Counting (in a simple VM)

Ive written a very simple Compiler that translates my source language to bytecode, this code gets processed by the VM (as a simple stack machine, so 3 + 3 will get translated into

push 3
push 3
add

right now I struggle at the garbage collection (I want to use reference counting). I know the basic concept of it, if a reference gets assigned, the reference counter of that object is incremented, and if it leaves scope, it gets decremented, but the thing thats not clear to me is how the GC can free objects that get passed to functions...

here some more concrete examples of what i mean

string a = "im a string" //ok, assignment, refcount + 1 at declare time and - 1 when it leaves scope    
print(new Object()) //how is a parameter solved? is the reference incremented before calling the function?

string b = "a" + "b" + "c" //dont know how to solve this, because 2 strings get pushed, then concanated, then the last gets pushed and concanated again, but should the push operation increase the ref count too or what, and where to decrease them then?

I would be glad if anyone could give me links to tutorials for implementing reference counting or help me with this very specific problem if someone had this problem before (my problem is that i dont understand when to inc, dec the references or where the count is stored)


I think a couple of things can happen with literals. You can treat them like literal numbers, and they are constants and there forever, or you can have an implicit variable that has retrain count of 1 before print , and releases it after.

In response to your edit: You can use the implicit variable solution, or you can use the "autorelease" concept from Objective-C. You have a an object that is placed in the autorelease pool that will be released in a small amount of time, in which the receiver of the object can retain it.


First, what types of objects does your language allow to be put on the heap? Strings? Do you have mutable or immutable strings?

Check out this post about Strings in Java. So in a Java like language strings get copied every time you concatenate them because they are immutable. Also "this is a string" is actually a call to the constructor of the string class.

If the argument to print() is a call to a constructor (new Object()) , there is no reference to the object in the scope calling the function, thus the object lives in the scope of the function and the counters should be incremented and decremented accordingly to entering and leaving the scope of the print() function. If the constructor is called in the calling scope and assigned to a variable, it lives in the calling scope.

While reading about the stuff, Wikipedia is a good start, but Andrew Appel's compiler book would be handy to have (there should be a 2nd edition out there and there is a C and ML version of the book available too). Lambda-the-Ultimate is the place where many of the programming language researchers discuss things, so definitely a place worth looking at.

链接地址: http://www.djcxy.com/p/20890.html

上一篇: String如何在Java中作为Object使用

下一篇: 编译器:如何实现引用计数(在简单的VM中)