﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Just So So-文章分类-Linux</title><link>http://www.cppblog.com/Actionxin/category/13001.html</link><description>贵有恒，何必三更眠五更起</description><language>zh-cn</language><lastBuildDate>Tue, 20 Jul 2010 20:32:10 GMT</lastBuildDate><pubDate>Tue, 20 Jul 2010 20:32:10 GMT</pubDate><ttl>60</ttl><item><title>Gcc 使用</title><link>http://www.cppblog.com/Actionxin/articles/120917.html</link><dc:creator>Action.xin</dc:creator><author>Action.xin</author><pubDate>Tue, 20 Jul 2010 16:43:00 GMT</pubDate><guid>http://www.cppblog.com/Actionxin/articles/120917.html</guid><wfw:comment>http://www.cppblog.com/Actionxin/comments/120917.html</wfw:comment><comments>http://www.cppblog.com/Actionxin/articles/120917.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Actionxin/comments/commentRss/120917.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Actionxin/services/trackbacks/120917.html</trackback:ping><description><![CDATA[<span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　GCC是由理查德&#183;马修&#183;斯托曼在1985年开始的。他首先扩增一个旧有的编译器，使它能编译C，这个编译器一开始是以Pastel语言所写的。Pastel是一个不可移植的Pascal语言特殊版，这个编译器也只能编译Pastel语言。为了让自由软件有一个编译器，后来此编译器由斯托曼和Len Tower在1987年以C语言重写并成为GNU专案的编译器。GCC的建立者由自由软件基金会直接管理。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　在1997年，一群不满GCC缓慢且封闭的创作环境者，组织了一个名为EGCS〈Experimental/Enhanced GNU Compiler System〉的专案，此专案汇整了数项实验性的分支进入某个GCC专案的分支中。EGCS比起GCC的建构环境更有活力，且EGCS最终也在1999年四月成为GCC的官方版本。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　GCC目前由世界各地不同的数个程序设计师小组维护。它是移植到中央处理器架构以及操作系统最多的编译器。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　由于GCC已成为GNU系统的官方编译器（包括GNU/Linux家族），它也成为编译与建立其他操作系统的主要编译器，包括BSD家族、Mac OS X、NeXTSTEP与BeOS。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　GCC通常是跨平台软件的编译器首选。有别于一般局限于特定系统与执行环境的编译器，GCC在所有平台上都使用同一个前端处理程序，产生一样的中介码，因此此中介码在各个其他平台上使用GCC编译，有很大的机会可得到正确无误的输出程序。</span><h3 style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 16px; font-family: Arial; line-height: 22px; "><a name="1_3" style="color: rgb(51, 102, 204); "></a>结构</h3><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　GCC的外部接口长得像一个标准的Unix编译器。使用者在命令列下键入gcc之程序名，以及一些命令参数，以便决定每个输入档案使用的个别语言编译器，并为输出程序码使用适合此硬件平台的组合语言编译器，并且选择性地执行连结器以制造可执行的程序。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　每个语言编译器都是独立程序，此程序可处理输入的原始码，并输出组合语言码。全部的语言编译器都拥有共通的中介架构：一个前端解析符合此语言的原始码，并产生一抽象语法树，以及一翻译此语法树成为GCC的暂存器转换语言〈RTL〉的后端。编译器最佳化与静态程序码解析技术（例如FORTIFY_SOURCE，一个试图发现缓冲区溢位〈buffer overflow〉的编译器）在此阶段应用于程序码上。最后，适用于此硬件架构的组合语言程序码以Jack Davidson与Chris Fraser发明的算法产出。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　几乎全部的GCC都由C写成，除了Ada前端大部分以Ada写成。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　</span><span  style="font-family: arial; font-size: 14px; line-height: 24px; "><strong>前端接口</strong></span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　前端的功能在于产生一个可让后端处理之语法树。此语法解析器是手写之递回语法解析器。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　直到最近，程序的语法树结构尚无法与欲产出的处理器架构脱钩。而语法树的规则有时在不同的语言前端也不一样，有些前端会提供它们特别的语法树规则。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　在2005年，两种与语言脱钩的新型态语法树纳入GCC中。它们称为GENERIC与GIMPLE。语法解析变成产生与语言相关的暂时语法树，再将它们转成GENERIC。之后再使用"gimplifier"技术降低GENERIC的复杂结构，成为一较简单的静态唯一形式（Static Single Assignment form，SSA）基础的GIMPLE形式。此形式是一个与语言和处理器架构脱钩的全域最佳化通用语言，适用于大多数的现代编程语言。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　</span><span  style="font-family: arial; font-size: 14px; line-height: 24px; "><strong>中介接口</strong></span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　一般编译器作者会将语法树的最佳化放在前端，但其实此步骤并不看语言的种类而有不同，且不需要用到语法解析器。因此GCC作者们将此步骤归入通称为中介阶段的部分里。此类的最佳化包括消解死码、消解重复运算与全域数值重编码等。许多最佳化技巧也正在实作中。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　</span><span  style="font-family: arial; font-size: 14px; line-height: 24px; "><strong>后端接口</strong></span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　GCC后端的行为因不同的前处理器宏和特定架构的功能而不同，例如不同的字符尺寸、呼叫方式与大小尾序等。后端接口的前半部利用这些讯息决定其RTL的生成形式，因此虽然GCC的RTL理论上不受处理器影响，但在此阶段其抽象指令已被转换成目标架构的格式。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　GCC的最佳化技巧依其释出版本而有很大不同，但都包含了标准的最佳化算法，例如循环最佳化、执行绪跳跃、共通程序子句消减、指令排程等等。而RTL的最佳化由于可用的情形较少，且缺乏较高阶的资讯，因此比较起近来增加的GIMPLE语法树形式[2]，便显得比较不重要。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　后端经由一重读取步骤后，利用描述目标处理器的指令集时所取得的资讯，将抽象暂存器替换成处理器的真实暂存器。此阶段非常复杂，因为它必须关照所有GCC可移植平台的处理器指令集的规格与技术细节。</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　后端的最后步骤相当公式化，仅仅将前一阶段得到的组合语言码藉由简单的副函式转换其暂存器与内存位置成相对应的机械码。</span><h3 style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 16px; font-family: Arial; line-height: 22px; "><a name="1_4" style="color: rgb(51, 102, 204); "></a>基本规则</h3><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　gcc所遵循的部分约定规则：</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.c为后缀的文件，C语言源代码文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.a为后缀的文件，是由目标文件构成的档案库文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.C，.cc或.cxx 为后缀的文件，是C++源代码文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.h为后缀的文件，是程序所包含的头文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.i 为后缀的文件，是已经预处理过的C源代码文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.ii为后缀的文件，是已经预处理过的C++源代码文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.m为后缀的文件，是Objective-C源代码文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.o为后缀的文件，是编译后的目标文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.s为后缀的文件，是汇编语言源代码文件；</span><div class="spctrl" style="height: 10px; line-height: 10px; font-family: arial; font-size: 14px; "></div><span  style="font-family: arial; font-size: 14px; line-height: 24px; ">　　.S为后缀的文件，是经过预编译的汇编语言源代码文件</span>
<img src ="http://www.cppblog.com/Actionxin/aggbug/120917.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Actionxin/" target="_blank">Action.xin</a> 2010-07-21 00:43 <a href="http://www.cppblog.com/Actionxin/articles/120917.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>