{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


16-Simple-Code-Generation - CS107 Spring 2007 Handout 16...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 16 Spring 2007 April 23, 2007 Computer Architecture: Take II Handout written by Julie Zelenski and Nick Parlante Example: Simple variables A variable is a location in memory. When a variable is declared, the stated type determines how many bytes in memory are reserved for that variable. The compiler creates a symbol table to map between the symbolic name of a variable (e.g. " i ") and the location in memory where it will be stored (e.g. address 6210 ). We're not yet going to concern ourselves about exactly where in memory the compiler chooses to place variables. There is no run-time type information available about variables, so when reading or assigning to a location it is not clear whether you are working with an int or a float or a pointer, etc. { int i; i = 6; i++; } In our code generation work, we will always assume int s are 4 bytes, so i has 4 bytes reserved for it, and let's say its address is currently stored in R1 . Generated code: M[R1] = 6 ; assign i the value 6 R2 = M[R1] ; load i's value into a register R2 = R2 + 1 ; do the addition M[R1] = R2 ; store new value into i The compilation translates one statement at a time. There is not a one-to-one correspondence between C statements and machine instructions; usually several low- level instructions are required to express the high-level statement. You can also see why compiled code tends to not run as fast as hand-coded CPU instructions ("assembly language"). A smart optimizing pass of the compiler could shorten the whole sequence to just M[R1] = 7 . When assigning or reading the value of variables that are less than full word size, the alternate form of load or store is used that indicates the number of bytes to move. { char ch; ch = 'A'; } ch has just one byte reserved since it is a char . Assume its address is stored in R1 . Generated code:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 M[R1] =.1 65 ; 65 is ASCII value of 'A' Example: Type conversions The types char , short , int , and long are all in the same family, and use the same binary polynomial representation. C allows you to freely assign between these types. When assigning from a smaller-sized type to a larger, there is no problem. All of the source bytes are copied and the remaining upper bytes in the destination are filled using what is called sign extension — the sign bit is extended across the extra bytes. This makes it so the upper bytes are set to all zero for positive numbers, all ones for negative numbers, which exactly replicates the original number but just in a larger number of bytes. { char ch; int i; ch = 'A'; i = ch; // cast not needed, nor will it change result } Assume address of i is in R1 , ch is at address R1 + 4 . (i.e. just after i , later we'll learn that this is usually the way local variables are laid out). Generated code: M[R1 + 4] =.1 65 ; assign ch ASCII value 'A' R2 =.1 M[R1 + 4] ; load ch into R2 (upper bytes of R2 are zeroed) M[R1] = R2 ; assign value in R2 to i Now i has the value 65 (which is the ASCII decimal representation for 'A '), the three upper bytes are zeros. Assigning from a smaller to a larger data type never introduces
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

16-Simple-Code-Generation - CS107 Spring 2007 Handout 16...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online