[Haskell-cafe] Compilers: Why do we need a core language?
Brandon Allbery
allbery.b at gmail.com
Tue Nov 20 17:59:28 CET 2012
On Tue, Nov 20, 2012 at 6:54 AM, <citb at lavabit.com> wrote:
> I know nothing about compilers and interpreters. I checked several
> books, but none of them explained why we have to translate a
> high-level language into a small (core) language. Is it impossible
> (very hard) to directly translate high-level language into machine
> code?
>
You might note that gcc does the same thing internally; its core seems to
be inspired by S-expressions. (It's also somewhat lower level than GHC's
core, but then C is itself lower level.)
As for why:
* It's a way to remove redundancy and simplify implementation.
* Sometimes it's easier to rephrase a language feature in terms of another
similar feature, instead of duplicating code or making that code
sufficiently reusable to apply easily in multiple not-quite-identical
places.
* Sometimes it's because the language definition specifies a translation
that describes the behavior of a language feature (see, for example, do
blocks and automatically derived typeclasses in Haskell) and the simplest
way to ensure compliance with the standard is to do the same translation in
the compiler.
* It can also be easier to apply high level optimization techniques; if you
go straight from the highest level code to the lowest level, you are likely
to miss optimization opportunities that are only revealed (or only sanely
implementable) at intermediate levels.
--
brandon s allbery kf8nh sine nomine associates
allbery.b at gmail.com ballbery at sinenomine.net
unix/linux, openafs, kerberos, infrastructure http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121120/37a956b3/attachment.htm>
More information about the Haskell-Cafe
mailing list