CS 536 - Compilers

Notes for Lecture 23 - Tuesday 4/21

<patrickm@cs.wisc.edu>

Virtual Functions

class c{ int a;
    int sum(in b) { return a+b; }
}
c var;
var.sum(10);
                       ^^^^-- invisible parameter 'this' pointer

Examples of virtual functions (members)

class shape{
    color my_color;
    float cost(){ return const* aread() ;
    virtual float area() {}     // We will declare area() for each
                                // type of shape
 
 
Shape 
  • color
  • null
Square 
  • color (A)
  • side
Circle 
  • color (B)
  • radius
In the table, [A] and [B] are the pointers to get to the object
 

class square extends shape{
    float side;
    float area(){ return side*side; }
}

class circle extends shape{
    float raduis;
    float area() { return 3.14*radius*radius; }
}


Unions and Records

union { int A;
        double D;
} U;

U.D = 12.34;
U.A = 1234;
cout << U.D;

(Pascal)
RECORD
    CASE T:INT OF  // tag; set 1 for ints, 2 for doubles
    1: A: INT
    2: D: DOUBLE
END



 
 

Arrays

One-dimensional arrays (vectors)

int a[10];

size of a = number of elements * size per element (and must be word-aligned)

We now need a formula to compute the address of  a[i] :

adress = address of A + i * sizeOf(int)
             =address of A + 4 * i
             = 100 + 4 * i

ex.) for (i=0; i<100000; i++){
    a[i]=0;
    *(addr(a) + i*4) = 0
}

means

LOC=ADR(A)
for(i=0; i<100000; i++){
    *LOC = 0;
   LOC = LOC + 4;
}

means

for (LOC=ADR A; LOC < ADR(A) + 4 million; LOC = LOC +4){ *LOC = 0 }

Example in Pascal (array indexing)

B: ARRAY[1900...1988] OF INTEGER
    ADR(B[I])
    ADR(B) + SIZE*I - 1900*SIZE
    1000 + 4*I - 7600
    -6600 + 4*I             // where I starts at 1900
 



 

Multi-Dimensional Arrays

Row major or column major?
 
 
1 2 3
4 5 6
7 8 9

row major =
1 2 3 4 5 6 7 8 9

column major =
1 4 7 2 5 8 3 6 9