简体   繁体   English

C / C ++编译器如何工作?

[英]How do C/C++ compilers work?

After over a decade of C/C++ coding, I've noticed the following pattern - very good programmers tend to have detailed knowledge of the innards of the compiler. 经过十多年的C / C ++编码,我注意到了以下模式 - 非常优秀的程序员倾向于详细了解编译器的内部结构。

I'm a reasonably good programmer, and I have an ad-hoc collection of compiler "superstitions", so I'd like to reboot my knowledge and start from the basics. 我是一个相当优秀的程序员,我有一个特殊的编译器“迷信”集合,所以我想重新启动我的知识并从基础开始。

Can anyone recommend links to online resources or favorite books? 有人可以推荐在线资源或喜欢的书籍链接吗? I'm particularly interested in C/C++ compiling, optimization, GCC and LLVM. 我对C / C ++编译,优化,GCC和LLVM特别感兴趣。

Start with the dragon book....(stress more on code optimization and code generation) 从龙书开始....(更多关注代码优化和代码生成)

Go onto write a toy compiler for an educational programming language like Decaf or Cool.., you may use parser generators (lex and yacc) for your front end(to make life easier and focus on more imp stuff).... 继续编写一个玩具编译器,用于教育编程语言,如Decaf或Cool ..,你可以使用解析器生成器(lex和yacc)作为你的前端(让生活更轻松,专注于更多的东西)....

Then read gcc internals book along with browsing gcc source code. 然后阅读gcc internals book以及浏览gcc源代码。

Compiler Text are good, but they are a bit heavy for teaching yourself. 编译器文本很好,但是对于自己教学来说它们有点沉重。 Jack Crenshaw has a "Book" that was a series of articles you can download and read call "Lets Build a Compiler." Jack Crenshaw有一本“书”,这是一系列文章,你可以下载并阅读“Lets Build a Compiler”。 It follows a "Learn By Doing" methodology that is great if you didn't get anything out of taking formal classes on the subject, or it's been WAY too many years since took it (that's my case). 它遵循“边做边学”的方法,如果你没有从这个主题的正式课程中得到任何东西,或者它已经花了太多年了(这是我的情况)。 It holds your hand and leads you through writting a compiler instead of smacking you around with Lambda Calculus and deep theoretical issues that only academia cares about. 它牵着你的手,引导你通过编写编译器,而不是用Lambda微积分和只有学术界关心的深层理论问题来打扰你。 It was a good way to stir up those brain cells that only had a fuzzy memory of writting something on the Vax (YEAH, that right a VAX!) many many moons ago at school. 这是一个很好的方式来激发那些只有在Vax(YEAH,那是一个VAX!)许多月前在学校写东西的模糊记忆的脑细胞。 It's written very conversationally and easy to just sit down and read, unlike most text books which require several pots of coffee just to get past the first chapter. 它的编写非常简单,易于坐下来阅读,不像大多数教科书需要几罐咖啡才能超越第一章。 Once you have a basis for understanding then more traditional text such as the Dragon book are great references to expand on your understanding. 一旦你有了理解的基础,那么更多的传统文本,如龙书,是扩展你的理解的伟大参考。 (And personal I like the Dead Tree versions, I printed out Jack's, it's much easier to read in a comfortable position than on a laptop. And the Ebook readers are too expensive for something that doesn't actually feel like you're reading a real book yet.) (而且我个人喜欢Dead Tree版本,我打印出Jack's,在舒适的位置阅读比在笔记本电脑上阅读更容易。而电子书阅读器太昂贵了,因为某些东西实际上并不像你在阅读真实的书。)

What some might call a "downside" is that it's written in Pascal, but I thought that just made me think about it more than if someone had given me a working C program to start with. 有些人可能称之为“缺点”是它是用Pascal编写的,但我认为这让我更多地考虑它,而不是有人给我一个有效的C程序开始。 Appart from that it was written with the 68000 in mind, which is only being used in embedded systems at this point time. 从那时起,它就以68000编写,仅在此时才用于嵌入式系统。 Again for me this wasn't a problem, I knew 68000 asm and 68000 asm is easier to read than some other asm. 对我而言,这不是一个问题,我知道68000 asm和68000 asm比其他一些asm更容易阅读。

如果你想要死树版,试试编译器设计的艺术:理论与实践

As noted by Pete Eddy, Jack Crenshaw's tutorial is excellent for newbies. 正如Pete Eddy所说,Jack Crenshaw的教程非常适合新手。 But if you want to see how to a real, production C compiler works —one which was designed by brilliant engineers instead of created by throwing code at the wall until something stuck—get yourself a copy of Fraser and Hanson's A Retargetable C Compiler: Design and Implementation , which contains the source code to the very clean lcc compiler. 但是,如果你想看看如何将一个真实的,生产的C编译器的工作原理 -酮这是由聪明的工程师设计的 ,而不是通过在墙投掷代码中创建,直到东西卡,让自己弗雷泽和汉森的副本一个可重定向C编译器:设计和实现 ,其中包含非常干净的lcc编译器的源代码。 Explanations of the design and implementation are mixed in with the code. 设计和实现的解释与代码混合在一起。 It is not a first book for a beginner, but it will repay careful study, and you can get a used copy for $35. 它不是初学者的第一本书,但它会报销仔细研究,你可以花35美元得到一份二手书。

For a longer blurb about lcc , see Compile C Faster on Linux . 有关lcc的更长lcc ,请参阅在Linux上编译C更快

The lcc web page also has links to a number of good textbooks. lcc网页还包含许多优秀教科书的链接。 I don't know of an intro text that I really like, however. 但是,我不知道我真正喜欢的介绍文本。

PS Sorry you got ripped off at Uni. PS对不起你在Uni被扯掉了。

see Fabrice Bellard's otcc source code 请参阅Fabrice Bellard的otcc源代码

http://bellard.org/otcc/ http://bellard.org/otcc/

Depending on what you exactly want to know, you should have a look at pipes&filter pattern, because as far as I know this (or something similar) is used in a lot of compilers in the last years. 根据您确切想知道的内容,您应该查看管道和过滤器模式,因为据我所知,在过去几年中,许多编译器都使用了这种(或类似的东西)。

When my compiler knowledge is not too outdated it works like this: 当我的编译器知识不是太过时它的工作原理如下:

Parse sourcecode into symbolic representation 将源代码解析为符号表示

Clean up symbolic representation, do some normalization 清理符号表示,做一些规范化

Optimization of the symbolic tree based on certain rules 基于特定规则的符号树优化

write out executable code based on symbolic tree 写出基于符号树的可执行代码

Of course dependencies etc. have to be resolved too. 当然,依赖性等也必须得到解决。

And of course having a look at gcc or javac sourcecode may help in getting more detailed understanding. 当然,查看gcc或javac源代码可能有助于获得更详细的理解。

It may also be valuable to pick up and read the source code to a compiler. 获取和读取编译器的源代码也可能很有价值。 I doubt that GCC is the best first choice, since it is burdened with full compatibility to more than 20 years of evolution of the language. 我怀疑海湾合作委员会是最好的首选,因为它充分兼容20多年的语言发展。 But I'm also sure that a reading of its source, guided by one of the internal reference manuals, would be educational. 但我也确信,在其中一本内部参考手册的指导下阅读其来源将具有教育意义。

I'd seriously consider looking at the source to a scripting language that is internally compiled to a bytecode for a virtual machine. 我认真考虑查看内部编译为虚拟机字节码的脚本语言的源代码。 Several languages fit that description, but I would start with Lua . 有几种语言符合这种描述,但我会从Lua开始。 The language is small, and the VM is novel. 语言很小,VM很新颖。 The source code is also small and the bits I've looked at have been very clear although lightly commented. 源代码也很小,我看过的内容非常清楚,虽然轻微评论。

have a look on Kaleidoscope . 看看万花筒 You can write your own compiler in just a few days with LLVM. 您可以使用LLVM在几天内编写自己的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM