1
0
Fork 0
cl-sites/guile.html_node/Compiling-CPS.html

113 lines
5.3 KiB
HTML
Raw Normal View History

2024-12-17 12:49:28 +01:00
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Compiling CPS (Guile Reference Manual)</title>
<meta name="description" content="Compiling CPS (Guile Reference Manual)">
<meta name="keywords" content="Compiling CPS (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Continuation_002dPassing-Style.html" rel="up" title="Continuation-Passing Style">
<link href="CPS-Soup.html" rel="prev" title="CPS Soup">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
span:hover a.copiable-link {visibility: visible}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsubsection-level-extent" id="Compiling-CPS">
<div class="nav-panel">
<p>
Previous: <a href="CPS-Soup.html" accesskey="p" rel="prev">CPS Soup</a>, Up: <a href="Continuation_002dPassing-Style.html" accesskey="u" rel="up">Continuation-Passing Style</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsubsection" id="Compiling-CPS-1"><span>9.4.4.5 Compiling CPS<a class="copiable-link" href="#Compiling-CPS-1"> &para;</a></span></h4>
<p>Compiling CPS in Guile has three phases: conversion, optimization, and
code generation.
</p>
<p>CPS conversion is the process of taking a higher-level language and
compiling it to CPS. Source languages can do this directly, or they can
convert to Tree-IL (which is probably easier) and let Tree-IL convert to
CPS later. Going through Tree-IL has the advantage of running Tree-IL
optimization passes, like partial evaluation. Also, the compiler from
Tree-IL to CPS handles assignment conversion, in which assigned local
variables (in Tree-IL, locals that are <code class="code">&lt;lexical-set&gt;</code>) are
converted to being boxed values on the heap. See <a class="xref" href="Variables-and-the-VM.html">Variables and the VM</a>.
</p>
<p>After CPS conversion, Guile runs some optimization passes over the CPS.
Most optimization in Guile is done on the CPS language. The one major
exception is partial evaluation, which for historic reasons is done on
Tree-IL.
</p>
<p>The major optimization performed on CPS is contification, in which
functions that are always called with the same continuation are
incorporated directly into a function&rsquo;s body. This opens up space for
more optimizations, and turns procedure calls into <code class="code">goto</code>. It can
also make loops out of recursive function nests. Guile also does dead
code elimination, common subexpression elimination, loop peeling and
invariant code motion, and range and type inference.
</p>
<p>The rest of the optimization passes are really cleanups and
canonicalizations. CPS spans the gap between high-level languages and
low-level bytecodes, which allows much of the compilation process to be
expressed as source-to-source transformations. Such is the case for
closure conversion, in which references to variables that are free in a
function are converted to closure references, and in which functions are
converted to closures. There are a few more passes to ensure that the
only primcalls left in the term are those that have a corresponding
instruction in the virtual machine, and that their continuations expect
the right number of values.
</p>
<p>Finally, the backend of the CPS compiler emits bytecode for each
function, one by one. To do so, it determines the set of live variables
at all points in the function. Using this liveness information, it
allocates stack slots to each variable, such that a variable can live in
one slot for the duration of its lifetime, without shuffling. (Of
course, variables with disjoint lifetimes can share a slot.) Finally
the backend emits code, typically just one VM instruction, for each
continuation in the function.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Previous: <a href="CPS-Soup.html">CPS Soup</a>, Up: <a href="Continuation_002dPassing-Style.html">Continuation-Passing Style</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>