05-semantics-post3up

05-semantics-post3up - The semantics of C Readings:...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The semantics of C Readings: Readings from CP, chapters 1-10, as detailed in the document available on the Web site. • The memory model and C • Tail recursion and loops • Stacks for function calls in C and Scheme CS 136 Spring 2011 05: The semantics of C 1 The memory model for C Recall that a C program is typically compiled, or converted to a machine-language program, and that program is later executed. Memory Machine- language translation of user's C program User's data and working storage Processor CS 136 Spring 2011 05: The semantics of C 2 The names of variables in the C program no longer have meaning in the machine language program. But we can still speak of the mapping from name to location. When the compiler generates code, it uses such a mapping. In applying the memory model to C programs, we will show the symbol table of name-location mappings, even though it doesn’t exist at run time. The type tags used by Scheme are now attached to the memory location instead of the value. They can be stored in the symbol table. Like the names, they are not needed at run-time. CS 136 Spring 2011 05: The semantics of C 3 Modelling the execution of a C program Each declaration of an int variable, including the ones in the parameter list, corresponds to a 32-bit word of memory. Other types are allocated the space they require. If a declaration occurs outside of any function, it is visible within all functions (just as with global variables in Scheme). A declaration within a function is only visible within that function. The C89 and C99 standards do not permit nesting of functions. The gcc compiler we are using will permit nested functions, but we will not be using this feature in CS 136. CS 136 Spring 2011 05: The semantics of C 4 int i = 0; int main( void ) { int j = 1; i = i + 1; printf("%d\n", i+j); } In this program, both i and j require one word of storage. ... address contents identifer location 1 1080 ... 1200 type <int> 1200 j <int> 1080 i CS 136 Spring 2011 05: The semantics of C 5 The changes to memory as a result of the execution of a translated C program are simpler than with Scheme. The body of a C function typically consists of a number of statements. In our model, we can execute them one at a time, keeping track of where we are in the code. If a variable is used in an expression, its value is taken from the word associated with that variable. If a variable appears on the left-hand side of the assignment operator = , the value of the expression on the right-hand side is written into that word. CS 136 Spring 2011 05: The semantics of C 6 Iteration Consider the task of summing the natural numbers up to n . This is straightforward using structural recursion....
View Full Document

This note was uploaded on 06/19/2011 for the course CS 136 taught by Professor Becker during the Spring '08 term at Waterloo.

Page1 / 24

05-semantics-post3up - The semantics of C Readings:...

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

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