Advanced Programming & C++ Language ~1~ Introduction. Compilation Process Ariel University 2018 Dr. Miri (Kopel) Ben-Nissan
2 Administration Stuff Final Exam: 70% Exercises: 30% Both lectures and exercises material are for the test. My Email: miribn@gmail.com
3 Little History... 1957 1960 1965 1967 1969 FORTRAN COBOL PL/1 SIMULA PASCAL 1972 1972 1978 1984 1993 2001 SmallTalk C ADA C++ JAVA C#
4 C++ Evolution 1979 1984 1998 2003 2011 2014 1984: The Annotated C++ Reference Manual 1998: First ISO Standard C++03: Technical corrigendum of c++98 C++11: Current ISO Standard C++1y: Future ISO Standard C with classes C++84 C++98 C++03 More Details: http://en.cppreference.com/w/cpp/language/history C++11 C++14
C++ Evolution (Cont ) 5 With the new C++11 Standard, C++ faces the first time the challenges of multicore architectures. The 2011 published standard defines how a C++ program has to behave in the presence of multiple threads.
C++ Evolution (Cont ) 6
7 The Process of Language Translation
What is a compiler? 8 Compiler = Software. Translates source code into machine instructions. Compiler Machine Language Reports Errors.
9 Little History... First Computer ENIAC (1946). First Compiler FORTRAN (1957). Miri Kopel, Bar-Ilan University
10 Pre-Processing Phase The pre-processor is a simple program that replaces patterns in the source code with other patterns the programmer has defined.
11 The C++ pre-processor has 4 major functions: File Inclusion #include <iostream> Symbolic Constants #define SCREEN_SIZE 80 Parameterized Macros #define SQUARE(A) ((A)*(A)) Conditional Compilation. #ifndef #endif
12 Phases of Compiler lexical analyzer screening (tokenizing) Symbol Table syntax analyzer semantic analyzer intermediate code generator Error Handler code optimizer code generator
13 lexical analysis lexical analyzing: int icount = iinit + irate * 60 ; creening (also called tokenizing);: int icount = iinit + irate * 60 ; token All redundant blanks are ignored.
14 The Symbol Table Stores variable names, type, size. Stores any defined variable, function or other data. Type Size int 4 Identifier Type Value Location x 3 0x77232 f
The symbol table (cont ) 15
16 Syntax Analysis Goal: Recognize program structure. Often called parsing. Groups the tokens into grammatical phrases. Generates syntax errors.
DECL_LIST syntax analysis TYPE ASSIGN ASSIGN int id1 ass. Int( 8 ) com. id2 ass. id1 screening id( int ) sep. id( x ) ass. Int( 8 ) com. id( y ) ass. id( x ) lexical analysis int x=8, y=x,z=7+y; 17
Semantic Analysis 18 Type checking. Check the source for semantic errors: Match the number of actual parameters to formal parameters. Functions without return. Scope rules. Uses the symbol table. Compiler generated casting.
Code Optimization 19 Attempts to improve the code. Faster program. Smaller program. Less resources.
20 Optimization Example void f() { int a = calculate_a(); int b = calculate_b(); int c = 5; int d = a + b*c; cout<<c<<endl; int e = a + b*c; cout<<d<<endl; int f=4; } void f() { int a = calculate_a(); int b = calculate_b(); int c = 5; int d = a + b*5; cout<<c<<endl; int e = d; cout<<d<<endl; }
Code Generator 21 Walks through the parser output and generates either assembly language code or machine code. The end result is an object module (a file that typically has an extension of.o or.obj).
C++ Source File Organization 22 Organize your program as a collection of header files (.h) and implementation files (.C or.cpp or.cc). The.h file contains: #include of other.h files. pre-processor macros (#define). constant declarations (consts). type definitions (typedefs). enumerated types (enums). procedure declarations (prototypes).
C++ Source File Organization (cont ) 23 The.cpp file contains: #include of the.h files. function definitions. class methods implementation. Good programming practice is to pair the name of a.h file with a.cpp file. For example: XMixDrix.h = defines common constants, types and functions. XMixDrix.cpp = definitions of the declared functions.
Linker 24 Combines a list of object modules into an executable program that can be loaded and run by the operating system. f1.h #include f1.cpp compiler f1.obj f2.h project.exe f3.h #include f3.cpp compiler f3.obj
25
Linker (cont...) 26 When a function in one object module makes a reference to a function or variable in another object module, the linker resolves these references; it makes sure that all the external functions and data you claimed existed during compilation do exist. The linker also adds a special object module to perform start-up activities.
27 Language Processing System (Summery) Original Source Code Pre-processor Source Assembly Compiler Assembler object code Absolute Machine Code Linker / Loader object libraries
28
From Source to Execution 29 / 99 Source Program Preprocessor cpp / gcc -E Compiler cc1 / gcc -S Assembly Code Assembler as / gcc -c dynamic -linking Object Program staticlinking Loader Executables Linker ld / gcc -l CSU0027 - System Programming, NTNU CSIE
From Source to Execution 30 / 99 Source Program Preprocessor cpp / gcc -E Compiler cc1 / gcc -S Assembly Code Assembler as / gcc -c Object Program Loader Executables Linker ld / gcc -l CSU0027 - System Programming, NTNU CSIE