Material Type: Exam;
Professor: Plane;
Class: INTRO TO LOW-LEVEL PROG;
Subject: Computer Science;
University: University of Maryland;
Term: Spring 2005;
Download Steps in Compiling a Program - Lecture Slides | CMSC 212 and more Exams Computer Science in PDF only on Docsity!
Announcements
l^ Exam #1– re-grade requests due by 1:45 on Thursday l^ Program #1b– will return hand-grading portion today– re-grade requests due by 1:45 one week from today l^ Program #2– Due March 15
Steps in compiling a program l Converting .c to .o files– Pre-processor– Compiler (or Translator)• Scanner/Parser• Type Checker• Optimizer• Code Generator– Assembler• Converts assembly code to machine code l Converting .o's to an executable– Linker• resolves symbols– function use and definitions– global variable declarations and use
Pre-defined Symbols
l^ FILE– the name of the source file being compiled l^ LINE– line number of the current line in the file l^ DATE– Date the file was compiled l^ TIME– Time the file was compiled l^ STDC– 1 if the compiler conforms to ANSI C l^ Note: these start with two underscores andend with two underscores
#define
l^ #define name stuff l^ Symbolic constants– Primary Use– #define MAX_SIZE_TABLE 1024 l^ Macros– #define name(parameter-list) stuff– Example:• #define SUM(a,b) a + b l^ Note: all caps is a convention–^ not required by the preprocessor– makes it easier for us l^ Macros are a single line– Can define a "continuation" by ending line with \– #define macro1 a very long macro \–^ that runs onto another line
Macro Substitution
l^ Items are textually replaced so you must be careful!! l^ Consider:– #define SQUARE(x)
x * x
foo = SQUARE(a + 1)– This is replaced to:• foo = a + 1 * a + 1– #define DOUBLE(x)
x + x
foo = 10 * DOUBLE(3)– This is replaced to:• foo = 10 * 3 + 3
#define substitution
1.^ Check macro invocation arguments for macros•^ If present, replace with macro values2.^ Replace macros invocations by defined macro3.^ Scan for any defined macros used in program•^ If found, repeat steps 1 & 2String literals are not scanned for macro replacement
Other Issues with Macros l Side Effects of Macro Parameters– Replace each use of parameter– Consider:• #define DOUBLE(x)
((x) + (x))
• y = DOUBLE(++j);• y = ((++j) + (++j)) l Can be hard to tell macros from functions– Solution, use all upper case for macro names l #undef name– un-define the macro named “name” l Compiler Command Line Definition– gcc -DFOO=3– defines FOO just as though #define FOO 3 appeared in program
Conditional Compilation
l^ #if const-ex1– statements done iff “const-ex1” is true l^ #elif const-ex2– statements done iff “const-ex1” is false and “const-ex2” is true l^ #else– statements done iff “const-ex1” is false and “const-ex2” is false l^ #endif l^ defined(macro-name)– one example of a constant expression– 1 if macro-name is defined, 0 if not l^ #error– stops the compilation at this point l^ Useful if code must be different on different systems l^ Tends to make code hard to read