Class 27: Translating variables and data
Held Friday, November 6, 1998
- Makeup exams will be held on Monday and Wednesday next week. The policies at the
top of each exam will read (approximately)
- This examination is closed book, closed notes, closed computer, closed
answer all questions on paper. You are expected to finish the exam
within the fifty minutes allocated. [Those of you with special
needs should let me know about such needs.]
- I will give partial credit for partially correct answers. You ensure
the best possible grade for yourself by highlighting your answer and
including a clear set of work that you used to derive the
- I will not be available when you take the exam. I have done my best
to write answerable and unambiguous questions. If you feel a question
is ``incorrect'' please correct it and note your corrections.
- In some questions, you may be asked to write code. You should feel free
to write pseudocode in any reasonable form (i.e., similar to any common
language). Your pseudocode need not be syntactically or semantically
correct. However, it should be clear and should indicate to me that you
would be able to write correct and working code (in some language) if
you had sufficient time and inclination.
- Each exam will be approximately the same difficulty as one of the problems
on the take-home midterm. At least one of the exams will be quite similar
to one of those problems. As on the midterm, a problem may have multiple
- I will entertain questions on the exam at the beginning of class.
- As you may know, arrays are handled quite differently in different languages.
- Is it possible to describe a ``constant'' array? (Not in Tiger, although
when one builds an array one describes an initial contents.)
- Can you copy the contents of an array with assignment or by function calls?
(Not in Tiger, in which all arrays are handled by reference.)
- What bounds are permissible? (in Tiger, the bounds are 0 to length-1.)
- Do we check whether accesses are legal? If so, when? (In Tiger, you'll
need to make sure that the array is allocated (non-null) and that the
access is legal. If not, the program should crash, but ``elegantly''.)
- Where are the bounds specified? As part of the type, or only when the
array is created? (In Tiger, the bounds are only specified during creation,
which can complicate error checkng).
- In Tiger, all arrays are handled by reference. This means that we need to
do some indirection to get the contents of an array.
- How shoud we store our arrays? If the bounds of an array are determined at
run time and we need to do error checking, then we should store the bounds
as part of the array.
- If there are k things in the array, each of which requires
b bytes, and an integer requires i bytes, then the
array requires i + k*b bytes.
- If the array begins at memory location m, then the size is at
memory location m, the zeroth element is at memory location
m+i, the first element at m+i+b and the nth
element at m+i+n*b.
- Suppose array reference A is at offset
the current frame. How do we get A? We'll assume that everything
takes four bytes.
MEM(BINOP(PLUS, (TEMP(FP), CONST(a)))) // Reference to array
- How do we get A[exp]?
MEM(BINOP(PLUS, (TEMP(FP), CONST(a)))),
CONST(4) // Offset for start of array
BINOP(MULT, exp, CONST(4)))))
- How do we get A assuming that array reference A is stored
a in the parent frame, and the static link is at
s in the current frame?
MEM(BINOP(PLUS, TEMP(FP), CONST(s))) // Parent frame
CONST(a))) // Reference to array
CONST(4))) // Offset for start of array
- Once again, we'd prefer to have all of this handled automatically.
- However ,this time you'll be responsible for writing the code to do
the ``automatic'' handling.
- Is this it? Nope. You also need to do error checking. For error checking,
it will be useful to store the reference in a temporary. We'll also need
some labels. Assume that all of these are generated during translation.
Note that I haven't written the
Note also that the array is given by aexp and the index is given
MOVE(TEMP(aref), aexp) // E.g., MEM(BINOP(PLUS,TEMP(FP),CONST(a)))
CJUMP(EQUAL, TEMP(aref), CONST(0), NullArray, OKREFERENCE)
CJUMP(GE, TEMP(index), TEMP(SIZE), BadIndex, BIGENUF)
CJUMP(LT, TEMP(index), CONST(0), BadIndex, OKINDEX)
MEM(BINOP(PLUS, TEMP(aref), // Base of the array
BINOP(PLUS, CONST(4), // Offset for size
BINOP(TIMES, TEMP(index), CONST(4))))) // Offset in array
- Note that we don't need code to check whether we actually reference an array.
Why not? Because that check is done at compile time, as part of the type
checking of the system.
- Can we get rid of that
CONST(4) that clogs up our code?
(Alternately, why doesn't Appel have it?)
- When allocating memory, we can allocate the appropriate amount but
have the reference point to the initial element, rather than to the size.
To get the size, we need to offset by a negative amount.
- Records will be handled similarly to arrays (in that the are also accessed
by reference), differing in how one offsets. Instead of offsetting by a
multiple determined by the index, the offsetting is done with precomputed
values, one for each field.
- You no longer need to check bounds.
- You should still check for null references.
- Things are somewhat easier in Tiger because everything is the same size.
- Believe it or not, most of what we've been looking at is fairly simple
compared to the complex nesting of structured types that are possible.
For example, we might store arrays in arrays, necessitating additional
- Make sure that you can handle things like
- It should be rather natural, using recursive translation and an
appropriate abstract syntax.
- Created Monday, November 2, 1998 as a blank outline.
- On Wednesday, November 4, 1998, added the main body (taken from
previous outlines and some other places).
- On Friday, November 13, 1998, moved some uncovered sections to the
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Fri Nov 13 12:32:10 1998.
This page generated on Fri Nov 13 12:32:34 1998 by SiteWeaver.
Contact our webmaster at firstname.lastname@example.org