Thread: Writing an interpreter (for a GC language) in C

  1. #1
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Writing an interpreter (for a GC language) in C

    How can one write an interpreter for a GC (garbage collected) language in C? I have tried the Boehm GC library, but the supplied Makefiles do not work and the documentation is horrid and out of date. Also I have heard that it fails to collect all garbage, thus leaking memory, which is unacceptable.

    I would rather code the whole thing in standard C, and not rely on a 3rd party library. I don't want to write a platform-specific GC, using assembly code to implement.

    I currently have a prototype implementation in Java, and the only thing preventing me from porting to C (for a great gain of efficiency) is the GC aspect.

    I wish hardware had native support for memory GC at least. Now, I can *maybe* implement a GC since I know the root set of variables in the interpreter, but there are additional data structures like environments that ALSO need to be GC'd. Basically everything (user-visible objects and implementation objects) lives on the heap and need to be GC'd.

    I have read that it is impossible to develop a GC library for C in general, but that it IS possible for a subset of C programs, where certain constructs are avoided throughout the code-base (things like pointer aliasing, ect). Does anyone know of such a GC library (that I can link in) and the exact restrictions of this?
    Last edited by MacNilly; 05-13-2017 at 06:15 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    If you're implementing your own language, it makes sense to implement your own GC as well. The GC doesn't necessarily need to apply to the C program itself. Only to the interpreted language program. In the interpretation process, you'll have full access to all of the stack frames, and as such, you'll be able to conclusively know whenever something is no longer referenced. Then it's a simple matter to collect the garbage, using a linked list of all allocations.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by Elkvis View Post
    If you're implementing your own language, it makes sense to implement your own GC as well. The GC doesn't necessarily need to apply to the C program itself. Only to the interpreted language program. In the interpretation process, you'll have full access to all of the stack frames, and as such, you'll be able to conclusively know whenever something is no longer referenced. Then it's a simple matter to collect the garbage, using a linked list of all allocations.
    Yes, unless I snarf properties of the implementation language (Java currently), I have come to the conclusion that, in order to port to C, I will have to at least implement my own GC. I've been thinking about it, and I believe that doing the GC will end up being the most complicated programming I've ever attempted. I understand the principles, but the devils are in the details.

    My current plan:

    I'll maintain a list of all allocated blocks (basically re-implement my own version of GC'd malloc). All the "live" references will be reachable through my root set, which would consist of 1) references from global environment and 2) references from the current environment (or "stack" -- I won't actually be implementing a real stack). At any point in time, if any variables are unreachable from these roots, they can be collected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing an interpreter and parsing
    By Shoone in forum C Programming
    Replies: 3
    Last Post: 04-29-2015, 11:38 PM
  2. Replies: 18
    Last Post: 11-11-2012, 11:06 PM
  3. Creation of a Command Language Interpreter
    By Sicilian_10 in forum C Programming
    Replies: 18
    Last Post: 04-16-2010, 06:36 AM
  4. Replies: 0
    Last Post: 04-06-2007, 04:55 PM
  5. Register tranfer language interpreter in C
    By Nutcasey in forum C Programming
    Replies: 18
    Last Post: 01-06-2004, 01:11 PM

Tags for this Thread