Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6064489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:17:09+00:00 2026-05-23T09:17:09+00:00

Suppose I sometime would like to write a compiler that generates C code for

  • 0

Suppose I sometime would like to write a compiler that generates C code for a language that:

  • has purely reference counting GC,
  • lets you return to a point multiple frames up the stack (stealing Common Lisp’s (return-from) form),
  • and lets you define a destructor for each data type (to be called immediately before the memory for the data structure is freed).

How would you go about implementing this? Could you do it (well) with C’s setjmp() and longjmp() and a global list of points to return to for clean up while unwinding the stack?

Another possibility is to just generate C++ code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:17:10+00:00Added an answer on May 23, 2026 at 9:17 am

    There are a couple of ways I’d consider first off.

    One is similar to exception frames. Create a per-thread linked list of structures. Before calling a function, create a structure on the stack and add it to the end of the list. The structure contains a jmp_buf. Call setjmp and if it returns 0 continue with this function, otherwise (somehow) check to see whether you are the target of the return-from — if so then continue with the function, otherwise clean up your locals and longjmp the previous frame. I think this might be overkill, it depends how flexible this return-from needs to be. Is it necessary only to return from a function that in a surrounding lexical scope, or are we actually searching up the call stack for a named function that may or may not even be present? If the former, I doubt that we need this.

    Another possibility is to make all reference-counted types implement a common interface (internally to the language implementation, this is – it needn’t be visible to users). Then you can just create a stack of (pointers to) objects that require clean up, together with the ability to create index points in that stack corresponding to call-stack levels. On function exit, you clean up all variables below the stack level to which you are returning, just by looping through the list dereffing each one rather than needing to jump to cleanup code segments in the routines above us on the call stack. Then you could longjmp straight to the target, or you could come up with a “calling convention” in which the return value as far as your language is concerned is actually stored in a location determined by a pointer parameter (an out-param), while the return value as far as C is concerned indicates where on the stack you’re returning to. Callers therefore check whether the return value matches their own level, and if not return immediately, and there’s no need for longjmp. It may or may not be more efficient to longjmp, depending how many stack levels you’re skipping, and hence how many repetitions of check-and-return.

    This scheme is a bit like the cleanup stack in Symbian/C++. In fact that goes a bit further – it’s not the resource to be cleaned up that has to implement the common interface, what goes on the stack is a TCleanupItem consisting of a function that knows how to free the resource, and some data to feed to that function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose user has opened my web application in many different browser windows. After sometime
I would like to write a program to extract the URLs of websites visited
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose you're working in a language with variable length arrays (e.g. with A[i] for
Suppose I have have a program P which has a filename as argument. For
I would like to use libev for a streaming server I am writing. This
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose we have a table A: itemid mark 1 5 2 3 and table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.