编译器如何跨平台(硬件)? [英] How can a compiler be cross platform(hardware)?

查看:53
本文介绍了编译器如何跨平台(硬件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到二进制编译器将源代码转换为目标平台的二进制文件.有点明显……但是如果编译器以这种方式工作,那么同一个编译器如何用于x86,ARM,MIPS等不同系统?

I just realized that binary compilers convert source code to the binary of the destination platform. Kind of obvious... but if a compiler works such way, then how can the same compiler be used for different systems like x86, ARM, MIPS, etc?

难道他们不应该知道"硬件平台的机器语言以能够知道如何构建二进制文件吗?像gcc这样的编译器是否知道所支持的每个平台的机器语言?

Shouldn't they be supposed to "know" the machine-language of the hardware platform to be able to know how to build the binary? Does a compiler(like gcc) knows the machine language of every single platform that is supported?

该系统如何实现?如何针对多个平台同时优化编译器?

How is that system possible, and how can a compiler be optimized for that many platforms at the same time?

推荐答案

是的,他们必须知道"他们支持的每个平台的机器语言.这是生成机器代码所必需的.但是,编译是一个多步骤过程.通常,编译的第一步对于大多数体系结构都是通用的.

Yes, they have to "know" the machine language for every single platform they support. This is a required to generate machine code. However, compilation is a multi-step process. Usually, the first steps of the compilation are common to most architectures.

来自维基百科

编译器的结构

编译器在高层桥接源程序语言和底层硬件.

Compilers bridge source programs in high-level languages with the underlying hardware.

编译器需要

  1. 确定程序语法的正确性,

  1. determining the correctness of the syntax of programs,

生成正确且有效的目标代码,

generating correct and efficient object code,

运行时组织和

根据汇编器和/或链接器约定格式化输出.

formatting output according to assembler and/or linker conventions.

A编译器包含三个主要部分:前端,中间端,和后端.

A compiler consists of three main parts: the frontend, the middle-end, and the backend.

前端

检查程序是否正确根据编程语言的语法和语义编写.此处承认合法程序和非法程序.报告错误,如果有的话,以一种有用的方式.类型检查也通过收集来执行类型信息.前端然后生成一个中间源代码的表示形式或IR,供中端.

checks whether the program is correctly written in terms of the programming language syntax and semantics. Here legal and illegal programs are recognized. Errors are reported, if any, in a useful way. Type checking is also performed by collecting type information. The frontend then generates an intermediate representation or IR of the source code for processing by the middle-end.

中间端

是进行优化的地方.典型的优化的转换是去除无用或无法到达的代码,常量值的发现和传播,计算到执行频率较低的地方(例如,循环外),或基于上下文的计算专业化.中端为以下后端生成另一个IR.最优化努力集中在这部分上.

is where optimization takes place. Typical transformations for optimization are removal of useless or unreachable code, discovery and propagation of constant values, relocation of computation to a less frequently executed place (e.g., out of a loop), or specialization of computation based on the context. The middle-end generates another IR for the following backend. Most optimization efforts are focused on this part.

后端

负责将IR从中端转换为汇编代码.目标为每个IR指令选择一条或多条指令.寄存器分配在可能的情况下为程序变量分配处理器寄存器.后端通过弄清楚如何保持并行来利用硬件执行单元忙,填充延迟槽,等等.虽然大多数优化算法在NP中,启发式技术在发达的.

is responsible for translating the IR from the middle-end into assembly code. The target instruction(s) are chosen for each IR instruction. Register allocation assigns processor registers for the program variables where possible. The backend utilizes the hardware by figuring out how to keep parallel execution units busy, filling delay slots, and so on. Although most algorithms for optimization are in NP, heuristic techniques are well-developed.

本文的更多内容描述了编译器的结构,并在此文章中进行了论述.与跨编译器.

More this article which describes the structure of a compiler and on this one which deals with Cross compilers.

这篇关于编译器如何跨平台(硬件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆