SCAN: Computer Science - second year Christian Wolf
Plan 1 Introduction 2 Languages 3 Development methodologies 4 Debugging 5 Separated compilation : units christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 2 / 39
Who am I? Contact : Christian Wolf Email : christian.wolf@insa-lyon.fr Tél : 04 72 43 63 08 Teaching : Computer Science 2nd year : lectures in computer science for SCAN 3rd year : department of computer science Computer architecture (micro processor design) Design and implementation of network protocolls Research : Image processing and pattern recognition (Virtual) restoration of document images Optical word Recognition christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 3 / 39
Contents of the second year Advanced concepts in Algorithmics Development methodologies Separated compilation of different files Pointers and dynamic data structures (lists, graphs) Introduction to event based programming and user interface design (UID) Object oriented programming Design and implementation of a major graphical application (project) christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 4 / 39
The means Timetable : One 2h session per week 7 lectures 4 TD 13 TD 6 sessions for the mini project Evaluation - grades for : Mid term exam (interro) Final exam (DS) graded mini project christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 5 / 39
Plan 1 Introduction 2 Languages 3 Development methodologies 4 Debugging 5 Separated compilation : units christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 6 / 39
Interpreted languages A program written in an interpreted language needs an auxiliary program, called interpreter, which translates the instructions line after line into machine code. Only machine code is understood by the processor. Interpretation cycles : Read and analyze an instruction If the syntax of the instruction is correct, execute it ANALYSIS/EXECUTION a = sqrt(b); B = speed_matrix *V(4,:); plot (x, B, k, LineWidth, 3); ANALYSIS/EXECUTION ANALYSIS/EXECUTION ANALYSIS/EXECUTION Examples : Matlab, Maple, Perl, Python, Ruby, Javascript Advantages : rapid development cycle (no compilation) Disadvantages : slow execution christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 7 / 39
Compiled languages A program written in a compiled language is translated once, by a program called compiler, into an executable file different from the source file. Execution of the program does not need a translation anymore. a := sqrt(b); B = mul(transp(speed),v); line_to (x, B); COMPILER 010101010010101010101 001010010101010101010 010100101010100101010 Examples : Pascal, C, C++, Fortran, Advantages : fast execution ; compiler not needed for execution Disadvantages : slow development cycle : each change of the program needs a new compilation christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 8 / 39
Languages Language Domain Comp./Interp. ADA Real time, military compiled BASIC indicated by its name... interpreted C system programming compiled C++ object oriented system programming compiled COBOL Business, reporting compiled Fortran Scientific computing compiled Java Portable computing (e.g. internet) intermediate MATLAB Scientific computing (numeric) interpreted Maple Scientific computing (symbolic) interpreted LISP Artificial intelligence intermediate Pascal Teaching compiled Prolog Artificial intelligence interpreted Perl String processing interpreted christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 9 / 39
Plan 1 Introduction 2 Languages 3 Development methodologies 4 Debugging 5 Separated compilation : units christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 10 / 39
Lifecycles in software development TYPE THE PROGRAM PROBLEM SOLUTION SPECIFICATION ANALYSIS IMPLEMENTATION TEST christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 11 / 39
Syntax maps (1) Phrase Preposition Subject Verb Complement in Article the Subject a Article Noun Verb is are Complement Noun Fox Preposition Article Noun Foxes House christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 12 / 39
Syntax maps (2) Which of these phrases are syntactically correct? The Fox is in the house The Foxes are in the house The Fox are in the house In the House is the Fox The Fox is a Fox The Foxes is in the house A Fox is a fox christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 13 / 39
From the sources to the executable program TOTO.PAS TOTO.O TOTO a := sqrt(b); B = mul(transp(speed),v); line_to (x, B); COMPILER 010101010010101010101 001010010101010101010 010100101010100101010 LINKER 010101010010101010101 001010010101010101010 010100101010100101010 SYSUTILS.PPU 010101010010101010101 001010010101010101010 010100101010100101010 Compilation Lexical analysis Syntaxic analysis Translation of the source statements into machine instructions Linkage : creation of a single executable linking together : One or several object file(s) Eventual third party libraries System libraries (input/output etc.) The executable file is independent!! christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 14 / 39
Example program cycle ; var var1, var2 : integer ; procedure a ; begin var1 := 5; var2 := 10; var1 := var1+var2 ; end ; begin a ; end. (... ) pushl %ebp movl %esp,%ebp movw $5, U_P$CYCLE_VAR1 movw $10, U_P$CYCLE_VAR2 movswl U_P$CYCLE_VAR1,%edx movswl U_P$CYCLE_VAR2,%eax addl %edx,%eax movw %ax, U_P$CYCLE_VAR1 leave r e t christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 15 / 39
Compilation stages Stage Operation Result Analysis Translation Production Lexical analysis Syntactical analysis Semantical analysis Instruction selection Register allocation Assembly Register allocation Program source Lexical elements (tokens) Program structure Intermediate representation Assembly language structureobject code (machine code christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 16 / 39
Compilation : details Analysis and Translation these stages translate the program written in a higher programming language (e.g. Pascal) into a language which is very close to machine language. This part is independent of the target processor, the code is created for an abstract machine. Production this stage depends on the target processor : the abstract code is translated into code for a specific processor. Java : the two phases are separated. Analysis and translation are executed during compilation, production is executed in the target (e.g. webbrowser or mobile phone). christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 17 / 39
Linkage : details Several modules are linked together (object files), libraries, system code... Each module may provide ( export ) a set of functions and/or variables Each module may access to the exported functions and variables The linker connects the access instructions to the correct addresses for function calls and/or read/write operations. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 18 / 39
Integrated Development Environments (IDE) A software which integrates several functionalities specific to software development : An editor designed for the specific programming language A compiler A debugger Also : Configuration files Examples Libraries Documentation and help files... christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 19 / 39
Plan 1 Introduction 2 Languages 3 Development methodologies 4 Debugging 5 Separated compilation : units christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 20 / 39
Error types TOTO.PAS TOTO.O TOTO a := sqrt(b); B = mul(transp(speed),v); line_to (x, B); COMPILER 010101010010101010101 001010010101010101010 010100101010100101010 LINKER 010101010010101010101 001010010101010101010 010100101010100101010 SYSUTILS.PPU 010101010010101010101 001010010101010101010 010100101010100101010 Compilation : Lexical and syntactical errors Linkage : Missing libraries, errors in the interface Execution : Semantical errors christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 21 / 39
How to debug a program A sort routine including a bug : Var i, j : integer ; cnt : integer ; begin For i :=2 to N do begin cnt := T [ i ] ; j := i 1; While ( j >0) and ( cnt <T [ j ] ) do begin T [ j ] :=T [ j +1 ] ; j := j 1 end ; T [ j +1] := cnt end ; end ; Input array : 7 4 2 5 1 3 8 6 Expected array : 1 2 3 4 5 6 7 8 Output array : 1 1 1 1 1 3 6 6 christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 22 / 39
How to debug a program (1 st ) Answer : Normally it is sufficient to watch the contents of some key variables during the execution of the program. 1 st technique : add some output instructions ( writeln ) to the code which displays the variables, and remove them when the problem has been found. Disadvantage : the code needs to be changed. takes time risk of adding further bugs christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 23 / 39
How to debug a program (1 st ) Var i, j, k : integer ; cnt : integer ; T : t _ t a b ; begin For i :=2 to N do begin cnt := T [ i ] ; w r i t e l n ( cnt, at p o s i t i o n, i ) ; j := i 1; While ( j > 0) and ( cnt < T [ j ] ) do begin T [ j ] :=T [ j +1 ] ; j := j 1 end ; T [ j +1] := cnt ; w r i t e l n ( number i s now at p o s i t i o n, j + 1 ) ; for k :=1 to N do w r i t e ( T [ k ], ) ; readln end ; for i :=1 to N do w r i t e ( T [ i ], ) ; readln end. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 24 / 39
How to debug a program (1 st ) Problem with the update of the offset in the array!! christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 25 / 39
How to debug a program (2 nd ) 2 nd technique : conditional compilation { $DEFINE Debug } / / D e f i n i n g whether we debug or not { $IFDEF Debug } / / t h i s code i s executed Writeln ( Debug a c t i v e. ) ; {$ELSE} / / t h i s code i s not executed ( the code w i l l / / not even be compiled!! Writeln ( Debug not a c t i v e. ) ; { $ENDIF } {$UNDEF Debug } / / suppression de l a c o n d i t i o n Compilation instructions are special instructions beginning with $ : they allow to control the compiler. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 26 / 39
How to debug a program (3 rd ) 3 rd technique : usage of a debugger : Set breakpoints into the program Ask to display ( watch ) the contents of key variables Continue to execute the program at different speeds : up to the next breakpoint procedure by procedure instruction by instruction... The code of the program is not changed! christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 27 / 39
Plan 1 Introduction 2 Languages 3 Development methodologies 4 Debugging 5 Separated compilation : units christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 28 / 39
Seperated compilation : units Motivation : Regroup into separated modules the data and processing (procedures and functions) necessary for a specific part of the program, for instance a specific subject. Identifying these modules is an important part of the Analysis step of the software development life cycles. Advantages : Avoids (or makes them less frequent) the situation of multiple people working on the same files. Makes the code more browsable - relevant parts are found easier Makes the code more readable - small units are easier to understand than large sections of code christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 29 / 39
Calculating and drawing an ellipse Program Ellipse ; Type TCoord = Record x : Real ; y : Real ; End ; TCurve =Record NbPts : Integer ; Data : Array [ 1.. 1 0 0 ] of TCoord End ; Function C a l c E l l i p s e ( rx, ry : real ) : TCurve ; Begin... ( Calculate the e l l i p s e ) End ; Procedure DrawCurve ( A : TCurve ) ; Begin... ( Draw the curve ) End ; Var TabPoints : TCurve ; RayonX, RayonY : real ; Begin... ( Enter the dimensions ) TabPoints := CalcEllipse ( RayonX, RayonY ) ; DrawCurve ( TabPoints ) ; End. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 30 / 39
Calculating and drawing an square Program Square ; Type TCoord = Record x : Real ; y : Real ; End ; TSquare =Record NbPts : Integer ; Data : Array [ 1.. 1 0 0 ] of TCoord End ; Function CalcSquare ( rx, ry : real ) : TCurve ; Begin... ( Calculate the square ) End ; Procedure DrawCurve ( A : TCurve ) ; Begin... ( Draw the curve ) End ; Var TabPoints : TCurve ; RayonX, RayonY : real ; Begin... ( Enter the dimensions ) TabPoints := CalcSquare ( RayonX, RayonY ) ; DrawCurve ( TabPoints ) ; End. Inefficient modifications, risk of introducing new errors!!! christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 31 / 39
Solution : separate code into units Unit Curves ; I n t e r f a c e Type TCoord = Record x : Real ; y : Real end ; TCurve = Record NbPts : Integer ; Data : Array [ 1.. 100] of TCoord ; End ; Procedure DrawCurve ( A : TCurve ) ; Implementation Procedure DrawCurve ( A : TCurve ) ; Begin... ( Draw the curve ) End ; End. Program Square ; Uses Curves ; Fonction CalcSquare ( rx : real ) : TCurve ; Begin... ( Calculate the square ) End ; Var TabPoints : TCurve ; coté : real ; Begin... ( Enter the dimensions ) TabPoints := CalcSquare ( Coté ) ; DrawCurve ( TabPoints ) ; End. Program E l l i p s e ; Uses Curves ; Fonction C a l c E l l i p s e ( rx : real ) : TCurve ; Begin... ( Calculate the e l l i p s e ) End ; Var TabPoints : TCurve ; coté : real ; Begin... ( Enter the dimensions ) TabPoints := C a l c E l l i p s e ( Coté ) ; DrawCurve ( TabPoints ) ; End. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 32 / 39
Syntax of a unit D e c l a r a t i o n of the Unit I n t e r f a c e ( Data types and v a r i a b l e s defined here are v i s i b l e to a l l units and programs which i n c l u d e t h i s u n i t with the " Uses " statement. V i s i b l e procedures and f u n c t i o n s must be declared here ( header only ). ) Unit Curves ; I n t e r f a c e Type TCoord = Record x : Real ; y : Real end ; TCurve = Record NbPts : Integer ; Data : Array [ 1.. 100] of TCoord ; End ; Procedure DrawCurve ( A : TCurve ) ; Implementation ( Data types and v a r i a b l e s defined here are v i s i b l e to t h i s u n i t only. V i s i b l e procedures and f u n c t i o n s must be implemented here. ) Implementation Procedure DrawCurve ( A : TCurve ) ; Begin... ( Draw the curve ) End ; End. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 33 / 39
Syntax of a unit : optional functionality Unit Curves ; I n t e r f a c e... Implementation... I n i t i a l i z a t i o n ( Code c a l l e d when the Unit i s loaded ) F i n a l i z a t i o n ( Code c a l l e d when the Unit i s un loaded ) End. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 34 / 39
Visibiltiy of units (1) Unit UnitA ; I n t e r f a c e Procedure X ( A : Integer ) ; Implementation Procedure X ( A : Integer ) ; Begin... End ; End. Unit UnitB ; I n t e r f a c e Procedure X ( A, B : Real ) ; Implementation Procedure X ( A, B : Real ) ; Begin... End ; End. The declaration of X in Unit B overshadows the declaration of X in Unit A. Program Demo; Uses UnitA, UnitB ; Begin X( 1. 5, 2. 5 ) ; ( C o r r e c t ) X ( 1 ) ; ( E r r o r ) End. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 35 / 39
Visibiltiy of units (2) Unit UnitA ; I n t e r f a c e Procedure ProcA ; Implementation... End. Unit UnitB ; I n t e r f a c e Uses UnitA Procedure ProcB ; Implementation... End. Procedure A is visible to Unit B only, not to the main program. Program Test ; Uses UnitB ; Begin ProcB ; ( Correct ) ProcA ; ( e r r o r ) End. christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 36 / 39
From the sources to the executable program (2) MAINPROG.PAS MAINPROG.O a := sqrt(b); B = mul(transp(speed),v); line_to (x, B); COMPILER 010101010010101010101 001010010101010101010 010100101010100101010 UNITFOO.PAS UNITFOO.O a := sqrt(b); B = mul(transp(speed),v); line_to (x, B); COMPILER 010101010010101010101 001010010101010101010 010100101010100101010 MAINPROG UNITBAR.PAS UNITBAR.O LINKER 010101010010101010101 001010010101010101010 010100101010100101010 a := sqrt(b); B = mul(transp(speed),v); line_to (x, B); COMPILER 010101010010101010101 001010010101010101010 010100101010100101010 SYSUTILS.PPU 010101010010101010101 001010010101010101010 010100101010100101010 Only one souce file may contain the main program, all other files must be units! christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 37 / 39
Project management Separate compilation requires the management of several files, in practical applications a very large number of files. Integrated Development Environments (IDE) work in terms of projects. Instead of single source files, the user opens a project : All source files All resource files (graphical user interfaces, images, logos, configuration files of the application) The configuration of the project (editor, compiler, linker and debugger settings,...) Possible support of version control systems Examples : C++ : C++ Builder, Visual C++, Eclipse, Kdevelop Pascal : Delphi, Lazarus Java : J++ Builder, Eclipse christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 38 / 39
IDE at INSA (Scan) During the 2 nd year we will use Lazarus. Advantages : Open source software Freeware Portable and multi-platforme (Windows, Linux) Supports object oriented programming. Allows the design of graphical user interfaces : fully compatible to the widely used commercial product Borland Delphi. Available for download on the website of the first cycle s IT center (cipcnet). christian.wolf@insa-lyon.fr SCAN: Computer Science - second year 39 / 39