All excellent computer science schools offer compiler courses, but relatively few schools make it a required part of their undergraduate courses. This article answers the question: Why do you need to learn compiler knowledge? Even if you never plan to write a compiler.
One of the reasons I wrote this post is that although I loved the compiler course as an undergrad, I could barely see it being used in practice. Most of the material seemed either straightforward or esoteric (in fact, most of the compiler material I found was boring.) Either way, I spent a few years summarizing why this type of course is so useful. actual reasons. Here’s why.
Analyzers and parsers are everywhere
Serious PHP programmers have to understand analyzers and parsers, because we will not always just write some small programs. Every time you write an extensible program or process a new type of input file, you need to know about them (analyzers and parsers). Greenspan’s Tenth Law:
“Any C or Fortran program that is complex to a certain level will contain a temporary Common that has only half functionality, does not fully meet specifications, is full of bugs, and runs very slowly. Lisp implementation."
We spend so much time doing each of these things that we can do each one of them with a one-off trick, or we use 60 years of theoretical and practical knowledge to solve the problem, and Get it right. The important thing is: when should you borrow existing code or use existing tools? When can theoretical knowledge help? What language design principles can be applied to the small languages we use every day?
You are better able to write correct code
The compiler should correctly translate the input language into a legal program. To achieve this goal, compiler developers must understand the entire input language, including some rare problems that ordinary programmers never see. This understanding process is an important step towards the essence of programming language, which is different from what we usually see when writing code. For example, my understanding of the C language completely changed after I learned the details of sequence points, undefined behaviors, and common arithmetic conversions. C compiler writers have mastered these concepts, but most junior and intermediate programmers are unaware of them. It is no exaggeration to say that once you know the nature of a language, you will think about it differently and more accurately. This applies to any language, especially languages with unclear semantics like C and C++.
You can write faster code
By understanding the compiler, you can clearly know which optimizations the compiler can optimize and which ones the compiler cannot optimize, regardless of How reasonable and simple they seem. You'll learn which types of code typically build the best blocks of code, why they do this, and what they do. You will know why some codes can have better performance, such as an FIR filter that uses half a register bank to cache filter coefficients and half a register bank to cache samples, which is different from using a normal optimizer. You and your favorite compiler are a team that creates fast code that you can use to work efficiently or modify it using pointless tricks like lookahead optimization.
Secondly, the compiler backend is closely related to the target architecture. Of course, modern architectures try to provide assembly programmer-friendly platforms, and they are not far away. By understanding the compiler backend and why it generates code the way it does, you will have a better understanding of the operation of computer architecture.
Summary
The compiler (theoretically) has three parts:
1.Language-related front-end (analysis, type detection)
2.Language and target-agnostic mid-end (optimized))
3.Target-dependent backend (generating machine code)
here In this article, I've tried to convince you that there is value in understanding every part of a compiler - even if you don't implement or modify them in the future.
Receive LAMP Brothers original PHP video tutorial CD/"Essential PHP in Details" for free. For details, please contact the official customer service:
http://www.lampbrother.net
|