JOVANA
Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
Back to the library
程式語言 1957

FORTRAN 自動編碼系統

約翰·巴科斯 等(IBM)

寫數學,而不是機器的位址——讓一個程式把它編譯成不輸於人手的程式碼。

Choose your version
In depth · the introduction

1957 年以前,告訴電腦做什麼,意味著用它原始的數字語言,一個位址一個位址地親手去寫。FORTRAN 讓你改寫數學——剩下的,交給機器。

核心想法

電腦只懂得一長串細小的、帶編號的命令——把這個數取來、把那個數加上、把結果存到這裡。親手寫這些清單又慢、又枯燥、又容易出錯。FORTRAN 一併帶來了兩樣東西:一種高階語言,讓你寫下貼近普通代數的式子;以及一個編譯器——一個會讀你的公式、替你寫出機器所有細小命令的程式。

於是,你不必再寫幾十條晦澀的指令,只需寫一行——Y = A*X**2 + B*X + C——編譯器就會自動把它翻譯成快速的機器碼。最大膽之處在於:當時沒人相信機器能把這種翻譯做得足夠好。FORTRAN 的編譯器證明了它能:它產出的程式碼,跑得幾乎和人類專家手寫的一樣快。

它是如何誕生的

專案於 1954 年在 IBM 啟動,由一位名叫約翰·巴科斯的年輕研究者領導,為 IBM 強大的新機器 704 而作。那時,僅僅為一個問題做準備——寫程式、除錯——的花費,就超過了計算本身。巴科斯的主張,是用自動編碼把這筆花費砍下來。

他遇到了深深的懷疑。專家程式設計師以親手雕琢緊湊的機器碼為榮,不信任何自動系統能與他們比肩。於是巴科斯的團隊把「效率」當成執念:編譯器不只翻譯,還要最佳化,竭力把機器稀缺的快速暫存器用在刀刃上。這花了約三年,以及一場浩大的寫程式苦工。當 1957 年編譯器交付時,它成了——懷疑者,也被折服了。

它為何重要

FORTRAN 把寫程式,向真正手握問題的人——科學家與工程師——敞開,而不再把它鎖在一小撮機器碼祭司手中。它讓軟體的編寫大幅變便宜、變快,並在此過程中,幫助催生了軟體產業本身。我們今天掛在嘴邊的詞——編譯器、高階語言——正是經由它,走入了尋常用語。

一個可以想像的畫面

想想樂譜。作曲家用乾淨、可讀的記法寫下一段旋律——紙上寥寥幾行。技藝嫻熟的演奏者,再把每一個音符,化作真正發出聲音的、精確而飛快的指間動作。FORTRAN 就是那位演奏者:你寫下可讀的記法,編譯器便一音一音地,把它演繹成機器必須做出的成千上萬個精確的小動作——而且,演繹得幾乎不輸於最高明的那雙手。

輸入一條算術公式,元件會把它編譯成一份帶編號的清單,列出電腦必須按正確順序執行的那些小步驟,顯示一行可讀的公式如何展開成許多步。

它的位置

FORTRAN 並非自動寫程式的最初嘗試——葛麗絲·霍普早已造出過早期編譯器,也有人試過代數系統——但它是第一個既高效、又被廣泛採用的,正因如此才扎下了根。它之後,語言如潮水般湧來:ALGOL、COBOL、BASIC、C,以至後來的 Python。對它那纏繞的 GO TO 風格的反彈,則把艾茲格·戴克斯特拉等人引向了結構化程式設計——這條線索,你可以在本館別處接著追下去。

The original document
Original source text
J. W. Backus, R. J. Beeber, S. Best, R. Goldberg, L. M. Haibt, H. L. Herrick, R. A. Nelson, D. Sayre, P. B. Sheridan, H. Stern, I. Ziller, R. A. Hughes, R. Nutt · Proc. Western Joint Computer Conference (1957): 188–198
Why it was built
By the mid-1950s, writing and debugging a program by hand — in the IBM 704's raw numeric code, juggling memory addresses and index registers — had become the dominant cost of using a computer. The FORTRAN team's aim was to let a programmer state a numerical procedure in a concise notation close to mathematics, and have the machine produce automatically an efficient 704 program to carry it out.
The language
FORTRAN gave the programmer arithmetic assignment statements written like algebra (with the precedence ** before × and ÷, before + and −), variable and array names of up to six characters declared by DIMENSION, formatted input/output, the GO TO branch, the counting DO loop, and the three-way arithmetic IF. Two of its most characteristic lines:
DO 5 I = 1, 10
The DO statement repeats every statement down to the one labelled 5, for I running 1, 2, … 10 — the ancestor of the counting loop in every language since. The other:
IF (X) 10, 20, 30
The arithmetic IF jumps to statement 10 if X is negative, to 20 if it is zero, and to 30 if it is positive — branching directly on the sign of an expression.
The translator
The compiler ran in six sections. Its celebrated, hardest work was optimization: it analysed the flow through nested DO loops, treated array subscripts as functions of the loop indices, and allocated the 704's three index registers across each region of the program using a frequency analysis of how often each path would run — keeping the busiest indices in registers. This was register allocation and loop optimization, decades before those names existed.
What it set out to do
The project's overriding aim was that the object program should be nearly as efficient as one a good programmer would write by hand — otherwise no one would trust a high-level language — while cutting the labour of coding and debugging to a fraction of what it had been. Meeting both goals at once was the achievement.
[ … ]
IBM · 1957