Assignment 9 Solutions

Part of the homework for 22C:60, Fall 2010
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Homework

  1. Background: In your job as master of graphical computations, you have discovered that you need to manipulate a number of data points, but that in different contexts, you need different representations. You therefore determine that you will use an abstract or polymorphic class, point, with subclasses for cartesian points and polar points. Each object of any subclass of point, will begin with a pointer to the class descriptor for that subclass, where that class descriptor contains pointers to the methods for that class. The remainder of the object has a format that depends on the particular subclass.

    Given that p is some kind of point, p.setx(i) and p.sety(i) p.getx(i) and p.gety(i) access the x and y coordinates of p. These will be fast if p is a cartesian point, but slow if it is a polar points.

    Similarly, p.setr(i) and p.settheta(i) p.getr(i) and p.gettheta(i) access the r and theta coordinates of p. These will be fast if p is a polar point, but slow if it is a cartesian point.

    Note that the implementation of the cartesian point class would go in the file cartesianpoint.a and that the class descriptor for each class is a constant and therefore, the descriptor data structure for cartesian points can be part of cartesianpoint.a.

    a) Give an appropriate header file, point.h, that defines the SMAL interface to any object of the polymorphic class point. (0.5 points)

    ; point.h -- interface to the class point
    
    ; every point begins with a word pointing to the method table
    ; for the class that implements that kind of point
    ;DESCRIPTOR     =      0       ; pointer to the method table
    
    ; structure of class descriptors (method table) for all subclasses
    SETX    =       0
    GETX    =       4
    SETY    =       8
    GETY    =       12      ; the order of these does not matter
    SETR    =       16      ; but it should not be random
    GETR    =       20      ; values should be consecutive multiples of 4
    SETTHETA=       24
    GETTHETA=       28
    
    ; all methods take the object as a parameter in R3
    ; all set methods take a second parameter in R4, the value to set
    ; all get methods return R3 equal to the requested attribute
    

    Note in the above that the definition of DESCRIPTOR is commented out because users should always use LOADS to access this field, so they will never use this displacement into the structure.

    b) Using your answer to part a) as a framework, and given that the local variable P in the current subroutine points to a point, write un-optimized SMAL Hawk code to set the x coordinate of that point to 55. (0.5 points)

            LOAD    R3,R2,P         ; get pointer to object
            LIS     R4,55           ; get parameter
            ADDI    R2,R2,ARSIZE
            LOADS   R1,R3           ; get pointer to method table
            LOAD    R1,R1,SETX      ; get pointer to setx method
            JSRS    R1,R1           ; call method
            ADDI    R2,R2,-ARSIZE
    

    c) Give appropriate SMAL code for the class descriptor shared by all cartesian points that should be part of cartesianpoint.a. (0.5 points)

    	; export the address of the class descriptor (method table)
            INT     CARTESIANPOINT  ; so that points can be initialized
    
    	; the class descriptor (method table)
    	; the first word of each instance is a pointer to this table
    	; this table conforms to the structure documented in point.h
    	ALIGN	4
    CARTESIANPOINT:
            W       CARTESIANSETX
            W       CARTESIANGETX
            W       CARTESIANSETY   ; the order must be the same as part a
            W       CARTESIANGETY
            W       CARTESIANSETR
            W       CARTESIANGETR
            W       CARTESIANSETTHETA
            W       CARTESIANGETTHETA
    

    d) Using your anwers above as a framework, give appropriate SMAL code to define the structure of a cartesian point object as seen by an outsider, appropriate to include in cartesianpoint.h, the public interface to cartesian points. (0.5 points)

    ; structure of cartesian points
    ; the following is the redacted version what is in cartesianpoint.a
    ;DESCRIPTOR     =      0       ; pointer to the method table
    ; -- hidden --  =      4       ; probably X, but hidden from users
    ; -- hidden --  =      8       ; probably Y, but hidden from users
    CARTESIANPOINTSIZE=    12      ; 3 words, users need to know how big it is
    

    Note in the above that users must know how big a cartesian point is so that they can allocate storage for cartesian points and initialize them, but they do not get any information about the internal representation except that the first word of each point is the descriptor.

    e) Using your anwers above as a framework, give appropriate SMAL code to define the complete structure of a cartesian point object as seen by an the implementation of the cartesian point subclass, appropriate to include in cartesianpoint.a. (0.5 points)

    ; structure of cartesian points
    ; the following is the complete version what was in cartesianpoint.h
    ;DESCRIPTOR     =      0       ; pointer to the method table
    X               =      4       ; the X coordinate
    Y               =      8       ; the Y coordinate
    CARTESIANPOINTSIZE=    12
    

    f) Using your answers above) as a framework, write SMAL Hawk code for the setx method that would be part of cartesianpoint.a. (Writing settheta would be hard because of the need for trigonometry, but this one requires no computation. (0.5 points)

            INT     CARTESIANSETX  ; so that direct calls are possible
    CARTESIANSETX:
    	; conforms to the SETX interface in point.h, that is:
            ; expects R3 points to a cartesian point object
            ; expects R4 is the new X coordinate
            STORE   R4,R3,X
            JUMPS   R1