Changes

Jump to navigation Jump to search
5,484 bytes added ,  02:31, April 28, 2021
m
Added link
Line 3: Line 3:  
There are compilers for the more common programming languages available on a wide variety of different computer architectures.  This means that a program written in one of these languages may be ''portable'' and can often be used on any of the different machines just by using the appropriate compiler; note that there may be occasional problems with machine-dependencies, especially if there are substantial differences in the accuracy and size of numbers on the different machines.
 
There are compilers for the more common programming languages available on a wide variety of different computer architectures.  This means that a program written in one of these languages may be ''portable'' and can often be used on any of the different machines just by using the appropriate compiler; note that there may be occasional problems with machine-dependencies, especially if there are substantial differences in the accuracy and size of numbers on the different machines.
   −
Some compilers just perform a fast simple-minded translation from high-level source code to low-level machine language, while other compilers may optionally take (much) longer to perform the compilation by trying to improve or optimise the resulting machine code.
+
When first written many programs contain errors. Some errors can be detected by the compiler while other errors might only be detected when a program is running.<ref>{{cite web
 +
| url = http://www.cs.bu.edu/teaching/cpp/debugging/errors/
 +
| title = Compiler, Linker and Run-Time Errors
 +
| author = Robert I. Pitts
 +
| accessdate = 2012-04-02}}</ref> Some errors only occur under very specific circumstances and may never be detected.
   −
Many programs, when first written, contain errors, so it is important that compilers detect these errors and provide a clear report as to what the error is and where it was detected (unfortunately the point of detection can be in some cases be many lines away from the actual source of the error).
+
Some compilers just perform a fast simple-minded translation from high-level source code to low-level machine language, while other compilers may optionally take (much) longer to perform the compilation by trying to improve or optimise the resulting machine code.  
    
==History==
 
==History==
Line 11: Line 15:  
| url = http://www.computerhistory.org/events/lectures/cobol_06121997/index.shtml
 
| url = http://www.computerhistory.org/events/lectures/cobol_06121997/index.shtml
 
| title = The World's First COBOL Compilers
 
| title = The World's First COBOL Compilers
 +
| author = Harold "Bud" Lawson and Howard Bromberg
 
| accessdate = 2012-03-24}}</ref> based on the legal meaning of [[compilation]], since her translator for the A-0 language merely put together various snippets of pre-built code.
 
| accessdate = 2012-03-24}}</ref> based on the legal meaning of [[compilation]], since her translator for the A-0 language merely put together various snippets of pre-built code.
   Line 16: Line 21:  
| url = http://eprints.cs.vt.edu/archive/00000875/01/CS82010-R.pdf
 
| url = http://eprints.cs.vt.edu/archive/00000875/01/CS82010-R.pdf
 
| title = Twenty five years of Fortran
 
| title = Twenty five years of Fortran
 +
| author = J A N Lee
 
| format = pdf
 
| format = pdf
 
| accessdate = 2012-03-27}}</ref>
 
| accessdate = 2012-03-27}}</ref>
   −
Computer users nowadays may not realise the severe technical constraints which applied to programming in the mid 1950's. Modern personal computers have the entire central processing unit on a single chip (integrated circuit), have main memories (RAM) containing many hundred of millions of bytes, and have processing speeds of several million instructions per second. The IBM 704 computer (fairly advanced for its time) had a central processor made up of many thousands of electronic valves (each the size of a light bulb), had a main memory of only four thousand 36-bit words, and had a processing speed of perhaps forty thousand instructions per second (modern mobile phones are more powerful than this).
+
Computer users nowadays may not realise the severe technical constraints which applied to programming in the mid 1950s. Modern personal computers have the entire central processing unit on a single chip (integrated circuit), have main memories (RAM) containing many hundred of millions of bytes, and have processing speeds of several million instructions per second. The IBM 704 computer (fairly advanced for its time) had a central processor made up of many thousands of electronic valves (each the size of a light bulb), had a main memory of only four thousand 36-bit words, and had a processing speed of perhaps forty thousand instructions per second (modern mobile phones are more powerful than this).
   −
Most compilers in the 1950's and 1960's were written in low-level assembly languages. As computers became more powerful in the 1970's it became possible for compilers to be written in high-level languages. Some compilers are written in the language which they translate (self-hosting), though some sort of bootstrapping process is needed to get the first version of such a compiler working. The first self-hosting compiler was for Lisp in 1962.<ref>{{Cite web | title=AI Memo 39-The new compiler| author=Tim Hart and Mike Levin | url=ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-039.pdf | accessdate=2012-03-29}}</ref>
+
Most compilers in the 1950s and 1960s were written in low-level assembly languages; there wasn't much choice at that time since the computer hardware wasn't capable of supporting compilers written in high-level languages. The use of high-level languages for writing compilers became more mainstream with the advent of Pascal in 1970<ref>{{Cite web
 +
| title = Pascal-P: The portable Pascal compiler
 +
| url = http://www.moorecad.com/standardpascal/PascalP.html
 +
| accessdate = 2012-03-31}}</ref> and C between 1969 and 1973.<ref>{{Cite web
 +
| title = The Development of the C Language
 +
| author = Dennis M. Ritchie
 +
| url = http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
 +
| accessdate=2012-03-31}}</ref>
 +
 
 +
Some compilers are written in the language which they translate (self-hosting), though some sort of bootstrapping process is needed to get the first version of such a compiler working. The first self-hosting compiler was for Lisp in 1962.<ref>{{Cite web  
 +
| title = AI Memo 39-The new compiler
 +
| author = Tim Hart and Mike Levin  
 +
| url = ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-039.pdf  
 +
| accessdate=2012-03-29}}</ref> Both the Pascal and C compilers mentioned above are self-hosting.
    
==Outline of the compilation process==
 
==Outline of the compilation process==
−
In the freely available online book "Basics of Compiler Design"<ref>{{cite web
+
In the freely available online book "Basics of Compiler Design",<ref>{{cite web
 
| url = http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/
 
| url = http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/
 
| title = Basics of Compiler Design
 
| title = Basics of Compiler Design
−
| accessdate = 2012-03-19}}</ref>, Professor Torben Mogensen identifies seven phases within a compiler, with the various phases being performed in one or more passes over the program.  A diagram illustrating these (and other optional optimisation) phases is shown on the last page of Professor Frank Pfenning's "Lecture Notes on Compiler Design: Overview"<ref>{{cite web
+
| author = Torben Mogensen
 +
| accessdate = 2012-03-19}}</ref> Professor Torben Mogensen identifies seven phases within a compiler, with the various phases being performed in one or more passes over the program.  A diagram illustrating these (and other optional optimisation) phases is shown on the last page of Professor Frank Pfenning's "Lecture Notes on Compiler Design: Overview"<ref>{{cite web
 
| url = http://www.cs.cmu.edu/~fp/courses/15411-f09/lectures/
 
| url = http://www.cs.cmu.edu/~fp/courses/15411-f09/lectures/
 
| format = pdf
 
| format = pdf
 
| title = Lecture Notes on Compiler Design: Overview
 
| title = Lecture Notes on Compiler Design: Overview
 +
| author = Frank Pfenning
 
| accessdate = 2012-03-19}}</ref>
 
| accessdate = 2012-03-19}}</ref>
    
:'''Lexical analysis''': convert the source code into a sequence of tokens, such as variable names, keywords, numbers, and special symbols such as '+'.
 
:'''Lexical analysis''': convert the source code into a sequence of tokens, such as variable names, keywords, numbers, and special symbols such as '+'.
   −
:'''Syntax analysis''' or '''parsing''': arrange the tokens into a parse tree or syntax tree which represents the structure of the program.
+
:'''Syntax analysis''' or '''parsing''': arrange the tokens into a parse tree or syntax tree which represents the structure of the program; check that the structure is valid.
    
:'''Semantic analysis''': annotate the parse tree with semantic actions, and perform various consistency checks; a symbol table is used to keep track of names declared by the programmer.
 
:'''Semantic analysis''': annotate the parse tree with semantic actions, and perform various consistency checks; a symbol table is used to keep track of names declared by the programmer.
Line 46: Line 67:     
:'''Assembly and linking''': translate the assembly language into executable machine code.
 
:'''Assembly and linking''': translate the assembly language into executable machine code.
 +
 +
==Dealing with errors==
 +
Almost all non-trivial programs contain errors. Detecting and fixing all errors is very expensive and time-consuming, so a balance has to be struck between the potential effect of certain errors (e.g. could they be life-threatening?) and the cost of fixing them.  The design of a programming language may also affect the likelihood of certain types of errors and the difficulty or otherwise of fixing them.
 +
 +
Gerald Weinberg reported in 1983 that the three most expensive programming errors of all time each cost hundreds of millions of dollars and that each was a one-line, coding-level mistake.<ref>{{cite web
 +
| url = http://www.stevemcconnell.com/ieeesoftware/bp01.htm
 +
| title = Who Cares About Software Construction?
 +
| author = Steve McConnell
 +
| accessdate = 2012-04-02}}</ref>
 +
 +
During translation compilers can usually detect all errors due to lexical or syntactical mistakes (spelling and grammar); they may also be able to detect a few simple semantic errors (meaning). Even when a compiler does detect and report an error, all it can do is report what the error is and where it was detected; unfortunately the point of detection can be in some cases be many lines away from the actual source of the error; also it may occasionally be difficult to explain the error in terms that the programmer can understand.
 +
 +
A semantic/coding error such as using a plus sign '+' instead of a minus sign '-' is almost impossible for any automated system to detect - thorough human checking of both code and calculated results would be needed in this situation.
 +
 +
There are a wide variety of errors which can occur when a program is running.<ref>
 +
{{cite web
 +
| url=http://jjc.public.iastate.edu/Runtime.errors.Paper.March4.2006.pdf
 +
| title=A Survey of Systems for Detecting Serial Run-Time Errors
 +
| author=Glenn R. Luecke et al
 +
| accessdate=2012-04-03}}
 +
</ref> Some compilers can optionally generate extra code to check for many of these errors, and there are some software packages which can provide the same effect. Unfortunately this extra checking code can slow a program down by a factor of 10 or 20 times.
 +
 +
Research is continuing on the problem of proving that a program is correct. This is an extremely difficult problem - it has been shown that proving whether or not a program actually stops is [[undecidable]]. Furthermore, the proofs are often a lot longer than the programs being proved so there is the additional problem of checking that the proof itself is correct. Bruce Schneier reports <ref>
 +
{{cite web
 +
| url=http://www.schneier.com/blog/archives/2009/10/proving_a_compu.html
 +
| title=Proving a Computer Program's Correctness
 +
| author=Bruce Schneier
 +
| accessdate=2012-04-03}}
 +
</ref> that it took six people working for five years to prove the correctness of 7,500 line program (about 250 lines per man-year). Jonathan Aldrich provides some examples of proof methods applied to tiny programs.
 +
<ref>
 +
{{cite web
 +
| url = http://www.cs.cmu.edu/~aldrich/courses/654-sp07/slides/7-hoare.pdf
 +
| title = Hoare Logic: Proving Programs Correct
 +
| author = Jonathan Aldrich
 +
| accessdate = 2012-04-03}}
 +
</ref>
    
==Optimisation==
 
==Optimisation==
 +
In a long-running calculation, the choice of a good [[algorithm]] is essential. Changing to a better algorithm can often provide much better performance gains than is possible with compiler optimisation.
 +
 +
Programs often obey the 80-20 rule, where 20% of the code accounts for 80% of the CPU usage. Programmers usually do a poor job of guessing as to which 20% of the code is causing performance problems, so before doing any hand optimisation it is crucial to (use a tool to) measure the program performance and identify the hot spots.
 +
 
Many compilers have one or more optional optimisation levels.  As the level/amount of optimisation increases the compiler tends to spend more and more time doing the optimisation.  The word 'optimisation' is perhaps misleading - perfect optimisation is a very hard problem ([[NP-complete]] or even [[undecidable]]), so a conceptually better term would be 'improvement'.
 
Many compilers have one or more optional optimisation levels.  As the level/amount of optimisation increases the compiler tends to spend more and more time doing the optimisation.  The word 'optimisation' is perhaps misleading - perfect optimisation is a very hard problem ([[NP-complete]] or even [[undecidable]]), so a conceptually better term would be 'improvement'.
 +
 +
Note that the most important criteria for judging a compiler is that it be correct, i.e. it always produces a correct translation of the high-level language, regardless of the amount of optimisation performed.<ref>{{cite web
 +
| url = http://www.cs.tufts.edu/~nr/cs257/archive/christian-lindig/quest.pdf
 +
| title = Random Testing of C Calling Conventions
 +
| author = Christian Lindig
 +
| accessdate = 2012-04-05}}</ref> The documentation for some compilers warns that results may be slightly different and/or that certain code may be incorrectly translated at high levels of optimisation.<ref>{{cite web
 +
| url = http://www.google.co.uk/url?sa=t&rct=j&q=IBM+fortran+compiler+optimisation+level&source=web&cd=3&ved=0CEgQFjAC&url=http%3A%2F%2Fwww-01.ibm.com%2Fsupport%2Fdocview.wss%3Fuid%3Dswg27005174%26aid%3D1&ei=K-Z9T8XAKMWI8gOx76WYDg&usg=AFQjCNEGkE1JxTZVDkQvuwRK39y037UorA
 +
| title = Code optimization with the IBM XL Compilers
 +
| accessdate = 2012-04-05}}</ref> 
    
Such optimisations generally make the resulting machine code 'better', for some definition of 'better':
 
Such optimisations generally make the resulting machine code 'better', for some definition of 'better':
SkipCaptcha
293

edits

Navigation menu