Chapter 2: Introduction to compilers

How do we know compilers generate correct code?

. Prove that the compiler is correct.

. However, program proving techniques do not exist at a level where large and complex programs like compilers can be proven to be correct

. In practice do a systematic testing to increase confidence level

. Regression testing

- Maintain a suite of test programs

- Expected behavior of each program is documented

- All the test programs are compiled using the compiler and deviations are reported to the compiler writer

. Design of test suite

- Test programs should exercise every statement of the compiler at least once

- Usually requires great ingenuity to design such a test suite

- Exhaustive test suites have been constructed for some languages

Formal methods have been designed for automated testing of correctness of programs. But testing of very large programs like compilers, operating systems etc. is not possible by this method. These methods mainly rely on writing state of a program before and after the execution of a statement. The state consists of the values of the variables of a program at that step. In large programs like compilers, the number of variables is too large and so, defining the state is very difficult. So, formal testing of compilers has not yet been put to practice. The solution is to go for systematic testing i.e., we will not prove that the compiler will work correctly in all situations but instead, we will test the compiler on different programs. Correct results increase the confidence that the compiler is correct.

Test suites generally contain 5000-10000 programs of various kinds and sizes. Such test suites are heavily priced as they are very intelligently designed to test every aspect of the compiler.