Steps in Compiling a Program - Lecture Slides | CMSC 212, Exams of Computer Science

Material Type: Exam; Professor: Plane; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Spring 2005;

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-5pd-1
koofers-user-5pd-1 🇺🇸

8 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 212 – S05 (lect 11)
Announcements
Exam #1
re-grade requests due by 1:45 on Thursday
Program #1b
will return hand-grading portion today
re-grade requests due by 1:45 one week from today
Program #2
Due March 15th at 8:00 pm
Reading
Chapter 14 (Today)
Chapter 12, 10.2 (Thursday)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

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

th^ at 8:00 pm

l^ Reading– Chapter 14 (Today)– Chapter 12, 10.2 (Thursday)

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