CS453 Colorado State University ============================================== Code Gen for Arrays ============================================== ------------------------ Outline - finish code gen for objects - overview of PA7 - type errors relevant to PA7 - implementing arrays - memory layout for an example - dynamically allocating arrays - the length expression - uses and defines of array elements ------------------------- Overview of PA7 (show grammar) - array allocation, array access, array length, and array element assign - Meggy.setAuxLEDs - show game of life demo ------------------------- Possible Type Errors in PA7 - see slide 2 ------------------------- Array example (bring up in editor) import meggy.Meggy; class PA7rainbow { public static void main(String[] whatever){ // display a rainbow on row 5 new Rainbow().run((byte)5); } } class Rainbow { Meggy.Color [] p; public void run(byte row) { p = new Meggy.Color [(byte)8]; p[(byte)0] = Meggy.Color.RED; p[(byte)1] = Meggy.Color.ORANGE; p[(byte)2] = Meggy.Color.YELLOW; p[(byte)3] = Meggy.Color.GREEN; p[(byte)4] = Meggy.Color.BLUE; p[(byte)5] = Meggy.Color.VIOLET; p[(byte)6] = Meggy.Color.WHITE; p[(byte)7] = Meggy.Color.DARK; Meggy.setPixel((byte)2, (byte)3, p[(byte)0]); Meggy.setPixel((byte)2, (byte)4, p[(byte)4]); this.displayRow(row, p); } public void displayRow(byte row, Meggy.Color [] a) { int i; i=0; while (i<8) { Meggy.setPixel((byte)i, row, a[(byte)i]); i = i+1; } } } - show run-time stack and heap after displayRow prologue ------------------------- dynamically allocating arrays NewArrayExp 1) assume size of array in elements is on stack, its type is int. Generate code to pop numelem off the stack. 2) gen code to alculate how many bytes needed for array numelem * sizeof(elem) 3) gen code to add 2 bytes for length int to size of array 4) gen code to call malloc to allocate space on the heap 5) gen code to set the first two bytes to numelem 6) gen code to push array's address onto stack NOTE: you could generate code that initializes the values in the array, but none of our test cases will depend on that ------------------------- the length expression LengthExp 0) assume code for array reference has already been generated and the array reference will be on the top of the stack 1) generate code that pops array reference off stack into two registers 2) generate code that loads the integer value being pointed at by the array into two registers 3) generate code to push the value/length onto the stack ------------------------- uses and defines of array elements ArrayExp 0) assume the integer index is at the top of the stack and the array reference is in the slots under the index 1) generate code that calculates the array element address pop the index pop the array reference bump reference 2 bytes to skip length field multiply index by 2 if elements are ints and store result in index add index to base reference to get address of element 2) generate code that loads the array element and pushes it onto stack ArrayAssignStatement 0) assume the rhs expression is on the top of the stack the index is next and the array reference is under the index 1) generate code that calculates the array element address 2) generate code that stores the rhs expression into the array element memory location ------------------------ Memory Layout Examples 1) Show run-time stack and heap as the expression x[0] + x[x[1]] is evaluated. import meggy.Meggy; class ArrayExp { public static void main(String[] a){ Meggy.setPixel((byte)(new MyClass().testing()), (byte)4, Meggy.Color.GREEN); } } class MyClass { public int testing() { int [] x; x = new int [7]; x[0] = 1; x[1] = 6; x[6] = 3; return x[0] + x[x[1]]; } } ------------------------ mstrout@cs.colostate.edu, 4/13/11