download 2021 site

This commit is contained in:
Jana Dönszelmann 2025-09-29 16:03:30 +02:00
commit dd6e4afb13
No known key found for this signature in database
138 changed files with 37730 additions and 0 deletions

394
lab/13.html Normal file
View file

@ -0,0 +1,394 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Eelco Visser">
<meta name="generator" content="Jekyll v3.8.5">
<title>Lab 13: Compiling Objects</title>
<!-- <base href="/2021"> -->
<!--link rel="canonical" href="https://getbootstrap.com/docs/4.3/examples/starter-template/"-->
<link rel="icon" href="../img/logo/pl_ico2_2B3_icon.ico" type="image/x-icon">
<!-- Bootstrap core CSS -->
<!--link href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="../css/main.css" rel="stylesheet">
<link href="../css/borders-responsive.css" rel="stylesheet">
<link rel="stylesheet" href="../css/pl.css">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="../index.html">
TU Delft | CS4200
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="../lectures/index.html" tabindex="-1" aria-disabled="true">Lectures</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../homework/index.html" tabindex="-1" aria-disabled="true">Homework</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../project/index.html" tabindex="-1" aria-disabled="true">Project</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../news/index.html" tabindex="-1" aria-disabled="true">News</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../blog/index.html" tabindex="-1" aria-disabled="true">Blog</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div class="mt-3 mb-3 pt-3 pb-3 text-dark border-top border-bottom border-grey">
<div class="text-dark font-weight-bold">
<h1>
Lab 13: Compiling Objects
</h1>
</div>
<div>
</div>
<div>
Project
</div>
<div>
December 17, 2021
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-9 border-lg-right border-grey">
<p>Develop code generation for ChocoPy classes and methods.</p>
<h3 id="objectives">Objectives</h3>
<ol>
<li>Compiling objects</li>
<li>Compiling lists</li>
<li>Challenges
<ul>
<li>Garbage collection</li>
<li>Register allocation</li>
<li>Peephole optimizations</li>
</ul>
</li>
</ol>
<h3 id="compiling-objects">Compiling Objects</h3>
<p>The main objective for todays lab is to extend your compiler to support the compilation of classes.
See also the slides of <a href="../lecture/15.html">Lecture 15</a> and the <a href="https://chocopy.org/chocopy_implementation_guide.pdf">ChocoPy implementation guide</a>.</p>
<h4 id="objects-in-chocopy">Objects in ChocoPy</h4>
<p>ChocoPy supports object-oriented programming through classes with attributes and methods.
The following example program (from the reference manual) illustrates many of the features of classes in ChocoPy:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class animal(object):
makes_noise:bool = False
def make_noise(self: "animal") -&gt; object:
if (self.makes_noise):
print(self.sound())
def sound(self: "animal") -&gt; str:
return "???"
class cow(animal):
def __init__(self: "cow"):
self.makes_noise = True
def sound(self: "cow") -&gt; str:
return "moo"
c:animal = None
c = cow()
c.make_noise()
</code></pre></div></div>
<p>A class defines a type and inherits from a superclass of which it is a subtype.</p>
<p>A class has attributes that are initialized with a literal value, which is <code class="language-plaintext highlighter-rouge">None</code> in the case of object types.</p>
<p>A class has method defintions, which are regular ChocoPy function definitions, except that methods always have a first argument (<code class="language-plaintext highlighter-rouge">self</code>) of the enclosing class type, which represents the objects to which the method is applied.
Methods can have nested functions.</p>
<p>A method call <code class="language-plaintext highlighter-rouge">e0.f(e1, ...)</code> invokes a method on the object. The actual implementation called corresponds to the <em>dynamic</em> type of the object value. That is, when the object is of a subtype <code class="language-plaintext highlighter-rouge">t</code> of the static type of <code class="language-plaintext highlighter-rouge">e0</code>, and the method <code class="language-plaintext highlighter-rouge">f</code> has been overridden in <code class="language-plaintext highlighter-rouge">t</code> or a supertype of <code class="language-plaintext highlighter-rouge">t</code>, the method lowest in the class hierarchy is called. This is known as <em>dynamic dispatch</em>.</p>
<p>A function with the same name as the class (e.g. <code class="language-plaintext highlighter-rouge">cow()</code> in the example above) is used to create objects.
When a class or one of its superclasses has an <code class="language-plaintext highlighter-rouge">__init__</code> method, that method is called implicitly on construction of the object.</p>
<h4 id="object-layout-and-prototypes">Object Layout and Prototypes</h4>
<p>An object value is represented by a chunk of memory in the heap, consisting of a type tag, the size of the object, a pointer to the dispatch table (see below) of the class, and the values of the attributes of the class.</p>
<p>Objects are allocated by the <code class="language-plaintext highlighter-rouge">alloc</code> function in the execution environment, by copying a pointer to a prototype object.
The prototype object for a class is an object following the layout described above with attribute values set to their initial values in the corresponding class.
Thus, there is not need to perform the default initialization programmatically.
After copying the prototype object, the <code class="language-plaintext highlighter-rouge">__init__</code> method is invoked to apply further custom initialization.</p>
<h4 id="dispatch-tables">Dispatch Tables</h4>
<p>Since all object instance of a class share the same set of methods, there is no need to copy the addresses of the corresponding functions into each object.
Instead, an object contains a pointer to a dispatch table, which contains the addresses (labels) of the functions implementing the methods.
Note that the methods of inherited method are also included in the dispatch table.</p>
<h4 id="compiling-methods">Compiling Methods</h4>
<p>Methods are standard top-level functions and can be compiled using the same approach as used for those.
The first argument of a function is a pointer to the object to which the method is applied.
Since methods and attributes are accessed through this <code class="language-plaintext highlighter-rouge">self</code> argument, no special measures are needed.</p>
<h4 id="calling-methods">Calling Methods</h4>
<p>Method calls are similar to top-level function calles, but the first argument is passed as a receiver object.
That is, <code class="language-plaintext highlighter-rouge">e0.f(e1, ...)</code> is essentially a function call <code class="language-plaintext highlighter-rouge">f(e0, e1, ...)</code>.
However, the actual function to be called should be obtained through the dispatch table to which the value of <code class="language-plaintext highlighter-rouge">e0</code> points.
In order to access that object it should not be <code class="language-plaintext highlighter-rouge">None</code>.</p>
<h4 id="accessing-attributes">Accessing Attributes</h4>
<p>An attribute access <code class="language-plaintext highlighter-rouge">e.x</code> is implemented by accessing the object at the offset corresponding to the attribute <code class="language-plaintext highlighter-rouge">x</code>.
Also for attribute accesses, the object should not be <code class="language-plaintext highlighter-rouge">None</code>.</p>
<h3 id="compiling-lists">Compiling Lists</h3>
<p>ChocoPy has lists, which are mutable sequences of a fixed length. So, lists behave like arrays in C. Operations on lists are length <code class="language-plaintext highlighter-rouge">len</code>, indexing <code class="language-plaintext highlighter-rouge">lst[i]</code>, and concatenation <code class="language-plaintext highlighter-rouge">lst1 + lst2</code>.
Lists are represented as objects with <code class="language-plaintext highlighter-rouge">len</code> attributes to represent the elements of the list.
Concatenation constructs a new list copying the original lists.</p>
<h3 id="challenges">Challenges</h3>
<h4 id="garbage-collection">Garbage Collection</h4>
<p>Objects are allocated on the heap. What happens when your program runs out of heap memory to allocate objects? Is compiled code memory safe? Develop a garbage collection algorithm for the execution environment of ChocoPy progams in order to support automatic memory management.</p>
<!-- #### Register Allocation
In [Lecture 15](/2021/lecture/15) we discussed register allocation using graph coloring. Use a separate register allocation phase to assign temporary values to registers in order to avoid saving values to memory. -->
<h4 id="peephole-optimization">Peephole Optimization</h4>
<p>A peephole optimization can be applied to generated code, i.e. to the AST of the generated program. A peephole optimization recognizes a couple of adjacent instructions and replaces them with something more efficient.
Extend your compiler with a peephole optimization phase after code generation that optimizes generated code.
Make sure that optimizations do preserve the semantics of the code.</p>
</div>
<div class="col-sm-12 col-md-12 col-lg-3 col-xl-3 " >
<div class="sticky-top top70 d-none d-lg-block d-xl-block">
<div class="pb4 mb3 border-bottom border-grey ">
<nav>
<ul class="pagination justify-content-left">
<li class="page-item">
<a class="page-link" href="12.html">
&laquo;
</a>
</li>
<li class="page-item">
<a class="page-link" href="../project/index.html">
^
</a>
</li>
<li class="page-item">
<a class="page-link" href="14.html">&raquo;</a>
</li>
</ul>
</nav>
</div>
<ul id="my_toc" class="toc list-group list-group-flush d-none d-lg-block d-xl-block p-0 ml-0 mt-3">
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#objectives">Objectives</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#compiling-objects">Compiling Objects</a>
<ul class="toc-sub pl-0">
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#objects-in-chocopy">Objects in ChocoPy</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#object-layout-and-prototypes">Object Layout and Prototypes</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#dispatch-tables">Dispatch Tables</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#compiling-methods">Compiling Methods</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#calling-methods">Calling Methods</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#accessing-attributes">Accessing Attributes</a></li>
</ul>
</li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#compiling-lists">Compiling Lists</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#challenges">Challenges</a>
<ul class="toc-sub pl-0">
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#garbage-collection">Garbage Collection</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="13.html#peephole-optimization">Peephole Optimization</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="border-top border-bottom border-grey mt-3 pt-3">
<nav>
<ul class="pagination justify-content-center">
<li class="page-item">
<a class="page-link" href="12.html">
Previous
</a>
</li>
<li class="page-item">
<a class="page-link" href="14.html">Next</a>
</li>
<li class="page-item">
<a class="page-link" href="../project/index.html">
Index
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>