Lecture: April 30, 1998 Notetaker: Elizabeth Markert Reading Assignment: Chap. 13.1-13.4 ----------------------------------------------------------- Refer to Code Generation Routines Part II Arrays Indexing Assignment of an Array to an Array: Array1 = Array2 *Size check *Make a clone of the array *One is actually assigning a pointer Assignment of a string to an Array: Array = "string" *Convert string to a character array *Size check Refer to pg. 10 of Code Gen. part II cg(){//For asgnode computeadr(target); source.cg(); if source is an array clone it //accessible in csxlib else source is a string convert it to an array //accessible in csxlib storename(Target); } cg(){//namenode if name is a scalar put value onto stack else if name is an array push reference to array onto stack else //subscripted name push reference to array evaluate subscript generate ILoad or BALoad or CALoad } computeadr-> computes the address computeadr(namenode){ if name is scalar generate nothing else if name is an array push reference of array onto stack else // subscripted value push reference to array compute subscript } storename(namenode){ if name is a scalar store value at top of stack else if name is an array do a length check store reference on the stack else//Subscripted value generate IAStore or BAStore or CAStore } In Java: char a[5]; // = new char[5]; char b[5]; // = new char[5]; a = b; a = "xyzzz"; a[3] = 'x'; print(b[4]); char a[5]; // = new char[5]; char b[5]; // = new char[5]; .field public static a$ [c .field public static b$ [c ldc 5 //5 indexes in array newarray char //new char array putstatic c/a$ [c ldc 5 newarray char putstatic c/b$ [c a = b; //Reference to cg() of asgnode getstatic c/a$ [c getstatic c/b$ [c invokestatic csxlib/cloneCharArray([c)[c invokestatic csxlib/checkCharArrayLength([c[c)[c putstatic c/a$[c a = "xyzzz"; getstatic c/a$ [c ldc "xyzzz" invokestatic csxlib/convertString(Java path name for string)[c invokestatic csxlib/checkCharArrayLength([c[c)[c putstatic c/a$ [c a[3] = 'x'; getstatic c/a$ [c ldc 3 ldc 120 // character code for x CAStore //char array store //doesn't leave anything on the stack print(b[4]); getstatic c/b$ [c ldc 4 CALoad //Load char onto stack invokestatic csxlib/printchar(c)V Refer To Code Generation Routines Part III cg(){//classnode(pg1 of part III) .class public class .super java/lang/oblect generate fieldDecls .method public static main([string)V generate init for fields call main()V return .limit stack 2 .end method generate methods } eval args \ call subr \ invokestatic c/F(II)I \ //MethodDeclNode \ code generated by MethodDecl \ code for calls compute method \ .method typecode / code for methodDecls args.cg(); / decls.cg(); / stmts.cg(); / gen return; / .end method; / int F(int a){ //a has local index = 0 int b;//b has local index = 1 a = a + b; } //Must keep track of current method F(I)I .method public static F(I)I ILoad 0 //load a ILoad 1 //load b IAdd IStore 0 LDC 0 IReturn .limit stack 25 .limit locals 2 //number of locals used .end metod