archives

Closures without function pointers

I am working on compiling a dynamic, collection-oriented, (mostly) first-order language onto hardware which does not efficiently support function pointers (or much indirection of any sort). This would be easy if my language were fully first-order, but the language has a limited set of primitive higher-order operators (such as map, reduce, and scan). I avoid the creation of function values by specializing these higher order operators on each distinct function argument they receive. So, map(sqrt, ...) is compiled separately from map(cos, ...) and so forth. In my current translation/compilation scheme I have a template for each operator, such as:
map_template(f, arg_type, result_type) =
   emit_fn(arg)
     z = allocate result_type[size(arg)]
     for i = 1 to n:
        z[i] = INLINE(f, arg[i]) /* copy body of f above this, replacing its input variable with arg[i] */
   return z
I run into trouble, however, when I try to pass a closure to a higher order operator. I can't simply inline the function portion of the closure, since it will be missing its closed-over arguments. Do the closure parameters need to be passed into the emitted function? It seems that I need to distinguish ordinary naked functions from closures in some way-- though I'm not sure what a rigorous way to do this might be. Can someone suggest some reading which will either give me ideas worth copying or help me clarify my thinking?
edit: For clarification, compilation happens at runtime, so the set of possible first-order functions is unbounded.
Thanks,
Alex