DOC PREVIEW
Berkeley COMPSCI 164 - Pyth Run-Time Structures

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Restrictions in PythValuesObjects, Classes, and Type DescriptorsBuilt-in TypesFunctions, Methods, and Function DescriptorsNative MethodsThe Main ProgramFront-end Modifications to the ASTRun-Time Support FunctionsExampleCS 164, Spring 2005 Pyth Run-Time Structures P. N. HilfingerVersion 13, 1 May 2005This document describes the assumptions made by the Pyth run-time about the rep resentationof objects, values of variables, functions, virtual tables, and stack frames. User code generated bythe compiler must conform to these assumptions, on pain of extremely obscure errors.1 Restrictions in PythPyth is a dialect of Python, and does not support all of Python’s semantics. Here are the mostrelevant changes, some of which have changed since Project #2.1. Pyth has a different set of built-in types, methods, functions, and global variables. You needn’tgenerally worry about these specifically, since you get them automatically in the s tandardprelude.2. Where Python uses a special boolean type, Pyth uses Int (integer), and the values True andFalse are simply variables defined in the standard prelude.3. Classes and types are not valid as values. The assignmenta = Intis not valid. This is enforced in the front end, and your code generator can assume th at noprogram will attempt to do it.4. The only constructor for a u ser-defined class is the default (parameterless) constructor. Thebuilt-in classes do not have constructors (e.g., the syntax Int() is illegal). This is enforced inthe front end.5. Variables in Pyth are all initialized (in P ython, they have an “uninitialized” state). Variableswhose static type is Int are initialized to 0, and all others are initialized to None.6. Methods and fu nctions defined by def are constants, and may not be m odified.7. The only attributes in a class (or built-in type) are those defined in its definition. UnlikePython, Pyth does not permit defining attributes dynamically by assignment.8. Pyth allows only “downward funargs” for function values that are not nested immediately insidethe global environment. T he indicated statements below, legal in Python, are illegal in Pyth:a = ...def q (x):global adef r (y): return x + ya = r # ILLE GALb = r # OKb = [r] # ILLE GAL (enforced by run-time code)1Pyth Run-Time Structures 2b = (r, ) # ILLEGAL (enforced by run-time code)b = { 1:r } # ILLEGAL (enforced by ru n-time code)b = { r:1 } # ILLEGAL (enforced by ru n-time code)h (r) # OK (h is some function)a = q # OKb = [q]; b = (q, ); b = {1 :q}; b = {q:1} # OKreturn r # ILLEG ALreturn q # OKThe indicated statements are illegal because they attempt (or may attempt) to store or returna function value that contains a non-null environment pointer in a place that outlives thatenvironment. The test is unduly conservative, but easy to implement. The lines that say“enforced by run-time co de” above are checks that we make for you. For the others, whenassigning to a global variable, or to a variable attribute, your generated cod e must check thatthe assigned value is not a function with a non-null environment pointer (that is, th e tag fieldof the assigned value in this case must be ≤ 3).The r eason for this restriction is that (as we covered in lecture some time ago) the need toprovide full function closures requires allocating (at least some) local variables on the heap:both a considerable complication in code generation and a considerable expense at execution.It’s tr ue that additional static analysis can determine when the expense is unnecessary, but wefigure you already have enough to do.9. Pyth allows bound methods only in calls. A bound method is essentially a pair consisting of anobject and a metho d. The following program is legal in Python, but not Pyth:class A:y = 3def f (s elf, x): return x + self.yg = lamb da self, x: x + self.y # Same thing, but g is variableh1 = A.f # OKh2 = A.g # OKr = A()print r.f (2), r.g (2), h1 (r, 2), h2 (r, 2) # All OKh3 = r.f # ERROR in Pythh4 = r.g # ERROR in PythThe front end will pr event the abuses in the assignment to h3, but not h4 (since it does notknow that r.g is a function). Your code must check attribute references that don’t immediatelyresult in calls.10. In Pyth, it is (now) illegal to define a variable attribute in a subclass if there is any attributewith the same name in a superclass. It is illegal to d efi ne an attribute in a subclass via defif there is a variable attribute (i.e., defined by assignment) of that name in a superclass. Itis illegal to override a method (defined via def) with a method h aving a different number ofarguments, a different return type, or different parameters types, aside from the type of thefirst p arameter. For example, th e indicated lines below are illegal:Pyth Run-Time Structures 3class A:def h (s elf, x): ...h: (A, Int) -> Intx = 3class B(A):x = 3 # ILLEGA Lh = 2 # ILLEGA Lclass C(A):def x (s elf): ... # ILLEGALdef h (a , b): .. . # OKh: (C, Int) -> Intclass D(A)def h (s elf): ... # ILLEGALclass E(A)def h (s elf, x): ... # ILLEGAL without type d eclaratio nThese checks are all perf ormed in the front end; you may assume that the program your codegenerator r eceives is legal.11. Pyth Int values are 32-bit integers. Operations on Ints have the same semantics the corre-sponding operations in Java (in particular, arithmetic is modulo 232).12. Pyth methods always return a value. The front end will insert an explicit return to insure thatthis happens. If the static type returned by the m ethod is Int, the default return value is 0;otherwise it is None.2 ValuesValues (and thus variables) in P yth are 8 bytes long and consist of a tag and data1:typedef struct Pyth Value PythValue;typedef PythValue (*PythFunctionPtr)(void* staticLink, ...);#define INT_TAG 1#define OBJECT_TAG 3#define GLOBAL_FUNCTIO N_TAG 0struct PythValue {union {int intValue;PythObject* objectPntr;PythFunctionPtr funcPn tr;} data;int tag;};1If you don’t know C, now would be an excellent time to learn!Pyth Run-Time Structures 4The tag field indicates what general class of value a PythValue contains, and thus which member ofthe union is valid:INT TAG indicates that the value is an integer, and the data.intValue field is valid.OBJECT TAG indicates that the value is some kind of object pointer either to an obj ect havinga built-in type or a user-defined class, and the data.objec tPntr field is valid.GLOBAL FUNCTION TAG indicates that the value is a pointer to a global fun ction, and thatthe data.funcPntr field


View Full Document

Berkeley COMPSCI 164 - Pyth Run-Time Structures

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Pyth Run-Time Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pyth Run-Time Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pyth Run-Time Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?