iptv techs

IPTV Techs


Into CPS, never to return


Into CPS, never to return


CPS, or continuation-passing style, is an interarbitrate recontransientation for
programs, particularly functional programs. It’s engaged in compilers for
languages such as SML and Scheme.

In CPS, there are two rules: first, that function/operator arguments must
always be unpresentant; second, that function calls do not return. From this, a
lot drops out.

In this post, we’ll present CPS by produceing a straightforward (Plotkin) CPS
convert from a petite Scheme-enjoy language. We’ll sketch some chooseimizations on
the IR. Then we’ll see at a couple of the common ways to actuassociate compile the
IR for execution.

Mini-Scheme

We have integers: 5

We have some operations on the integers: (+ 1 2), (< 3 4) (returns 1 or 0)

We can secure variables: (let ((x 1)) x) / (letrec ...) ?

We can produce one-parameter functions: (lambda (x) (+ x 1))
and they can seal over variables

We can call functions: (f x)

We can branch: (if (< x y) x y) (where we have choosed to engage 0 and 1 as
booleans)

How do I…?

We’re going to carry out a recursive function called cps incremenhighy,
commenceing with the straightforward establishs of the language and laboring up from there. Many
people enjoy carry outing the compiler both in Scheme and for Scheme but I discover
that all the quasiquoting produces everyleang fussier than it should be and
unaccomprehendledgeds the lesson, so we’re doing it in Python.

This uncomfervents we have a pleasant evident separation of code and data. Our Python code is
the compiler and we’ll lean on Python catalogs for S-conveyions. Here’s what some
sample Scheme programs might see enjoy as Python catalogs:

5

["+", 1, 2]

["let", [["x", 1]], "x"]

["lambda", ["x"], ["+", "x", 1]]

Our cps function will apshow two arguments. The first argument, exp, is the
conveyion to compile. The second argument, k, is a continuation. We have
to do someleang with our appreciates, but CPS needs that functions never
returns. So what do we do? Call another function, of course.

This uncomfervents that the top-level invocation of cps will be passed some advantageous
top-level continuation enjoy print-to-screen or author-to-file. All child
invocations of cps will be passed either that continuation, a manufactured
continuation, or a continuation variable.

cps(["+", 1, 2], "$print-to-screen")
# ...or...
cps(["+", 1, 2], ["cont", ["v"], ...])

So a continuation is fair another function. Kind of.

While you tohighy can produce authentic first-class functions for engage as
continuations, it can standardly be advantageous to partition your CPS IR by separating
them. All authentic (engager) functions will apshow a continuation as a last
parameter—for handing off their return appreciates— and can arbitrarily escape,
whereas all continuations are produced and allotd/freed in a stack-enjoy
manner. (We could even carry out them using a native stack if we wanted. See
“Partitioned CPS” and “Recovering the stack” from Might’s page.)

For this reason we syntacticassociate discern IR function establishs ["fun", ["x",
"k"], ...]
from IR continuation establishs ["cont", ["x"], ...]. Similarly, we
discern function calls ["f", "x"] from continuation calls ["$call-cont",
"k", "x"]
(where $call-cont is a exceptional establish understandn to the compiler).

Let’s see at how we compile integers into CPS:

def cps(exp, k):
    align exp:
        case int(_):
            return ["$call-cont", k, exp]
    lift NotImplementedError(type(exp))  # TODO

cps(5, "k")  # ["$call-cont", "k", 5]

Integers phire the unpresentant needment. So does all constant data (if we
had strings, floats, etc), variable references, and even lambda conveyions.
None of these need recursive evaluation, which is the core of the unpresentantity
needment. All of this needs that our nested AST get liproximateized into a
sequence of petite operations.

Variables are analogously straightforward to compile. We exit the variable names
as-is for now in our IR, so we need not grasp an environment parameter around.

def cps(exp, k):
    align exp:
        case int(_) | str(_):
            return ["$call-cont", k, exp]
    lift NotImplementedError(type(exp))  # TODO

cps("x", "k")  # ["$call-cont", "k", "x"]

Now let’s see at function calls. Function calls are the first type of
conveyion that needs recursively evaluating subconveyions. To assess (f
x)
, for example, we assess f, then x, then do a function call. The order
of evaluation is not meaningful to this post; it is a semantic property of the
language being compiled.

To convert to CPS, we have to both do recursive compilation of the arguments
and also synthesize our first continuations!

To assess a subconveyion, which could be arbitrarily complicated, we have to
produce a recursive call to cps. Unenjoy standard compilers, this doesn’t return a
appreciate. Instead, you pass it a continuation (does the word “callback” help
here?) to do future labor when that appreciate has a name. To produce
compiler-inside names, we have a gensym function that isn’t engaging and
returns distinct strings.

The only leang that separateentiates it from, for example, a JavaScript callback,
is that it’s not a Python function but instead a function in the produced
code.

def cps(exp, k):
    align exp:
        case [func, arg]:
            vfunc = gensym()
            varg = gensym()
            return cps(func, ["cont", [vfunc],
                              cps(arg, ["cont", [varg],
                                        [vfunc, varg, k]])])
    # ...

cps(["f", 1], "k")
# ["$call-cont", ["cont", ["v0"],
#            ["$call-cont", ["cont", ["v1"],
#                       ["v0", "v1", "k"]],
#                     1]],
#          "f"]

Note that our produced function call from (f x) now also has a continuation
argument that was not there before. This is becaengage (f x) does not return
anyleang, but instead passes the appreciate to the donaten continuation.

Calls to primitive operators enjoy + are our other engaging case. Like
function calls, we assess the operands recursively and pass in an compriseitional
continuation argument. Note that not all CPS carry outations do this for straightforward
math operators; some pick to permit straightforward arithmetic to actuassociate “return”
appreciates.

def gensym(): ...

def cps(exp, k):
    align exp:
        case [op, x, y] if op in ["+", "-"]:
            vx = gensym()
            vy = gensym()
            return cps(x, ["cont", [vx],
                           cps(y, ["cont", [vy],
                                   [f"${op}", vx, vy, k]])])
    # ...

cps(["+", 1, 2], "k")
# ["$call-cont", ["cont", ["v0"],
#            ["$call-cont", ["cont", ["v1"],
#                       ["$+", "v0", "v1", "k"]],
#                     2]],
#          1]

We also produce a exceptional establish for the operator in our CPS IR that commences with
$. So + gets turned into $+ and so on. This helps discern operator
invocations from function calls.

Now let’s see at creating functions. Lambda conveyions such as (lambda (x)
(+ x 1))
need to produce a function at run-time and that function body comprises
some code. To “return”, we engage $call-cont as common, but we have to also
reaccumulate to produce a novel fun establish with a continuation parameter (and then
thread that thcdisadmireful to the function body).

def cps(exp, k):
    align exp:
        case ["lambda", [arg], body]:
            vk = gensym("k")
            return ["$call-cont", k,
                        ["fun", [arg, vk], cps(body, vk)]]
    # ...

cps(["lambda", ["x"], "x"], "k")
# ["$call-cont", "k",
#                ["fun", ["x", "k0"],
#                        ["$call-cont", "k0", "x"]]]

Alright, last in this mini language is our if conveyion: (if cond ifgenuine
ifdeceptive)
where all of cond, ifgenuine, and ifdeceptive can be arbitrarily
nested conveyions. This fair uncomfervents we call cps recursively three times.

We also comprise this novel compiler builtin called ($if cond ifgenuine ifdeceptive)
that apshows one unpresentant conveyion—the condition—and chooses which of the
branches to carry out. This is cdisadmirewholey equivalent to a machine code conditional
jump.

The straightforward carry outation labors fair fine, but can you see what might
go wrong?

def cps(exp, k):
    align exp:
        case ["if", cond, ifgenuine, ifdeceptive]:
            vcond = gensym()
            return cps(cond, ["cont", [vcond],
                                ["$if", vcond,
                                   cps(ifgenuine, k),
                                   cps(ifdeceptive, k)]])
        # ...

cps(["if", 1, 2, 3], "k")
# ["$call-cont", ["cont", ["v0"],
#                  ["$if", "v0",
#                              ["$call-cont", "k", 2],
#                              ["$call-cont", "k", 3]]],
#                1]

The problem is that our continuation, k, need not be a continuation
variable—it could be an arbitrary complicated conveyion. Our carry outation
copies it into the compiled code twice, which in the worst case could direct to
exponential program prolongth. Instead, let’s secure it to a name and then engage the
name twice.

def cps(exp, k):
    align exp:
        case ["if", cond, ifgenuine, ifdeceptive]:
            vcond = gensym()
            vk = gensym("k")
            return ["$call-cont", ["cont", [vk],
                        cps(cond, ["cont", [vcond],
                                      ["$if", vcond,
                                        cps(ifgenuine, vk),
                                        cps(ifdeceptive, vk)]])],
                        k]
        # ...

cps(["if", 1, 2, 3], "k")
# ["$call-cont", ["cont", ["k1"],
#                  ["$call-cont", ["cont", ["v0"],
#                                   ["$if", "v0",
#                                     ["$call-cont", "k1", 2],
#                                     ["$call-cont", "k1", 3]]],
#                                   1]],
#                  "k"]

Last, let can be handled by using a continuation, as we’ve bound transient
variables in previous examples. You could also handle it by desugaring it into
((lambda (x) body) appreciate) but that will produce a lot more administrative
overhead for your upgrader to get rid of procrastinateedr.

def cps(exp, k):
    align exp:
        case ["let", [name, appreciate], body]:
            return cps(appreciate, ["cont", [name],
                               cps(body, k)])
        # ...

cps(["let", ["x", 1], ["+", "x", 2]], "k")
# ['$call-cont', ['cont', ['x'],
#                 ['$call-cont', ['cont', ['v0'],
#                                 ['$call-cont', ['cont', ['v1'],
#                                                 ['$+', 'v0', 'v1', 'k']],
#                                  2]],
#                  'x']],
#  1]

There you have it. A laboring Mini-Scheme to CPS converter. My carry outation is
~30 lines of Python code. It’s foolishinutive and pleasant but you might have watchd some
foolishinutivecomings…

Now, you might have watchd that we’re giving names to a lot of unpresentant
conveyions—unessential cont establishs engaged enjoy let secureings. Why name the
integer 3 if it’s unpresentant?

One approach people apshow to dodge this is meta-continuations, which I leank
many people call the “higher-order convert”. Instead of always generating
conts, we can sometimes pass in a compiler-level (in this case, Python)
function instead.

See Matt Might’s article and what I leank is a laboring Python
carry outation
.

This approach, though occasionassociate difficulter to reason about and more complicated,
cuts down on a meaningful amount of code before it is ever rehireted. For
scant-pass compilers, for resource-constrained environments, for enormous
programs, … this produces a lot of sense.

Another approach, potentiassociate easier to reason about, is to lean on your
upgrader. You’ll probably want an upgrader anyway, so you might as well engage
it to cut down your interarbitrate code too.

Optimizations

Just enjoy any IR, it’s possible to upgrade by doing recursive reauthors. We
won’t carry out any here (for now… maybe I’ll come back to this) but will
sketch out a scant common ones.

The straightforwardst one is probably constant felderlying, enjoy turning (+ 3 4) into 7.
The CPS equivalent sees comfervent of enjoy this:

["$+", "3", "4", "k"]  # => ["$call-cont", "k", 7]
["$if", 1, "t", "f"]  # => t
["$if", 0, "t", "f"]  # => f

An especiassociate meaningful chooseimization, particularly if using the straightforward CPS
convertation that we’ve been using, is beta reduction. This is the process of
turning conveyions enjoy ((lambda (x) (+ x 1)) 2) into (+ 2 1) by
substituting the 2 for x. For example, in CPS:

["$call-cont", ["cont", ["k1"],
                 ["$call-cont", ["cont", ["v0"],
                                  ["$if", "v0",
                                    ["$call-cont", "k1", 2],
                                    ["$call-cont", "k1", 3]]],
                                  1]],
                 "k"]

# into
["$call-cont", ["cont", ["v0"],
                 ["$if", "v0",
                   ["$call-cont", "k", 2],
                   ["$call-cont", "k", 3]]],
                 1]

# into
["$if", 1,
  ["$call-cont", "k", 2],
  ["$call-cont", "k", 3]]

# into (via constant felderlying)
["$call-cont", "k", 2]

Substitution has to be scope-conscious, and therefore needs threading an
environment parameter thcdisadmireful your upgrader.

As an aside: even if you “alphatise” your conveyions to produce them have
distinct variable secureings and names, you might accidenhighy produce second
secureings of the same names when substituting. For example:

# swap(haystack, name, swapment)
swap(["+", "x", "x"], "x", ["let", ["x0", 1], "x0"])

This would produce two secureings of x0, which vioprocrastinateeds the global distinctness
property.

You may not always want to carry out this reauthor. To dodge code blowup, you may
only want to swap if the function or continuation’s parameter name
materializes zero or one times in the body. Or, if it occurs more than once,
swap only if the conveyion being swapd is an integer/variable.
This is a straightforward heuristic that will dodge some of the worst-case scenarios but
may not be maximassociate advantageous—it’s a local chooseimum.

Another leang to be conscious of is that substitution may alter evaluation order.
So even if you only have one parameter reference, you may not want to
swap:

((lambda (f) (commence (g) f))
 (do-a-side-effect))

As the program is right now, do-a-side-effect will be called before g and
the result will become f. If you swap do-a-side-effect for f in
your upgrader, g will be called before do-a-side-effect. You can be more
opposing if your checkr inestablishs you what functions are side-effect free, but
otherdirectd… be pinsolentnt with function calls.

There are also more progressd chooseimizations but they go beyond an introduction
to CPS and I don’t sense brave enough to sketch them out.

Alright, we’ve done a bunch of CPS→CPS convertations. Now we would enjoy
to carry out the upgraded code. To do that, we have to convert out of CPS into
someleang set uped to be carry outd.

To C, perchance to dream

In this section we’ll catalog a couple of approaches to generating executable code
from CPS but we won’t carry out any.

You can produce innocent C code pretty honestly from CPS. The fun and cont
establishs become top-level C functions. In order to aid clocertains, you need to
do free variable analysis and allot clocertain set ups for each.
(See also the approach in scrapscript in the section called
“functions”.) Then you can do a very generic calling convention where you pass
clocertains around. Unblessedly, this is not very efficient and doesn’t
secure tail-call elimination.

To aid tail-call elimination, you can engage trampolines. This
mostly comprises allocating a structure-enjoy clocertain on the heap with each
tail-call. If you have a garbage accumulateor, this isn’t too horrible; the structures do
not inhabit very lengthy. In fact, if you instrument the factorial
example
in Eli’s blog post, you can see that the
trampoline structures inhabit only until the next one gets allotd.

This observation led to the prolongment of the Cheney on the MTA
algorithm, which engages the C call stack as a youthfuler generation for the garbage
accumulateor. It engages setjmp and lengthyjmp to unthrived the stack. This approach is
engaged in CHICKEN Scheme and Cyclone Scheme. Take a see at
Baker’s 1994 carry outation.

If you don’t want to do any of this trampoline stuff, you can also do the One
Big Switch approach which stuffs each of the funs and conts into a case
in a massive switch statement. Calls become gotos. You can handle your
stack roots pretty easily in one contiguous array. However, as you might
imagine, huger Scheme programs might caengage trouble with some C compilers.

Last, you need not produce C. You can also do your own droping from CPS into
a drop-level IR and then to some comfervent of assembly language.

Wrapping up

You have seen how to produce CPS, how to upgrade it, and how to omit it.
There’s much more to lget, if you are interested. Plrelieve sfinish me material if
you discover it advantageous.

I had originassociate reckond to author a CPS-based upgrader and code generator for
scrapscript but I got stuck on the finer details of compiling pattern aligning
to CPS. Maybe I will return to this in the future by desugaring it to nested
ifs or someleang.

Check out the code.

Acunderstandledgements

Thanks to Vaibhav Sagar and Kartik
Agaram
for giving feedback on this post. Thanks to
Olin Shivers for an excellent
course on compiling functional programming languages.

See also

Source connect


Leave a Reply

Your email address will not be published. Required fields are marked *

Thank You For The Order

Please check your email we sent the process how you can get your account

Select Your Plan