Java Reference
In-Depth Information
Constant strings may be allocated in the data segment, which is to say, we may generate
constant strings in the data segment; strings in the data segment will then look exactly like
those on the heap (the dynamic segment). For example, the constant "HelloWorld!" might
be represented at the label Constant..String1: .
Constant..String1:
.word 2 #tag2indicatesastring.
.word 28 #sizeofobjectinbytes.
.word 12 #stringlength(notincludingnullterminator).
.asciiz "HelloWorld!"#stringterminatedbynullcharacter0.
.align2 #nextobjectisonawordboundary.
The reader will have noticed that all objects on the heap share three words (12 bytes)
of information at the start of each object. This complicates the addressing of components
in the object. For example, the first element of an array a is at address a + 12; the first
character of a string s is at s+ 12; and the first field of an object o is at address o+ 12. That
12 must be added to an object's address each time we want an array's element, a string's
character or an object's eld can be expensive. We can do away with this expense by having
our pointer, which we use to reference the object, always point into the object 12 bytes as
illustrated in Figure 6.19.
FIGURE 6.19 An alternative addressing scheme for objects on the heap.
Dynamic Allocation
The logic for allocating free space on the heap is pretty simple in our (oversimplified) model.
obj=heapPointer;
heapPointer+=<objectsize>;
if(heapPointer>=stackPointer)gotofreakOut;
<copyobjecttemplatetoobj>;
When allocating a new object, we simply increment a free pointer by the appropriate size
and if we have run out of space (that is, when the heap pointer meets the stack pointer),
we freak out. A more robust heap management system might perform garbage collection.
Once we have allocated the space for the object, we copy its template onto the heap
at the object's location. Proper initialization is left to the SPIM routines that model the
constructors.
SPIM Class for IO
SPIM provides a set of built-in system calls for performing simple IO tasks. Our run-time
environment includes a class SPIM , which is a wrapper that gives us access to these calls as
a set of static methods. The wrapper is defined as follows:
packagespim;
/**
*ThisisaJavawrapperclassfortheSPIMrun-timeobjectSPIM.s.
*Anyj--programthat'scompiledfortheSPIMtargetmustimport
*thisclassfor(fileandconsole)IOoperations.Notethatthe
*functionshavenoimplementationsherewhichmeansthatifthe
 
Search WWH ::




Custom Search