Top Banner
T-diagrams “Mommy, where do compilers come from?”
22

T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Mar 06, 2018

Download

Documents

vohanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

T-diagrams

“Mommy, where do compilers come from?”

Page 2: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

T-diagrams

�Different diagrams for different kinds of programs�Visual explanation of interactions involving compilers

and interpreters

Page 3: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Programs

�Program P written in language L

�Example: Sort program written in Java

LP

Javasort

Page 4: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Machines

�Machine executing language M

�Example: Sun workstation executing sparc machine code

M sparc

Page 5: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Executing Programs

�Program implementation language must match machine

sparcsort

sparc

Page 6: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Interpreters

�Interpreter executing language L written in language M

�Example: Lisp interpreter running on sparc

LM

Lispsparc

Page 7: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Interpreting Programs

�Interpreter mediates between program language and machine language

Lispsort

sparc

Lispsparc

Page 8: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Virtual Machines

�Interpreter creates a “virtual machine”

Lispsort

sparc

Lispsparc

Lispsort

Lisp

Page 9: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Compilers

�Compiler translating from source language S to target language T implemented in M

�Example: C compiler for sparc platform

S ��� TM

C ��� sparcsparc

Page 10: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Compiling Programs

�Compiler inputs program in source language, outputs in target language

Csort

C ��� sparcsparc

sparcsort

sparc

sparcsort

sparc

Page 11: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Java Programming Environment

�Javac: Java to Java byte code (JBC) compiler�Java: Java Virtual Machine byte code interpreter

Java ��� JBCM

JavaP

JBCP

JBCP

MM

JBCM“javac” “java”

Page 12: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Where Do Compilers Come From?

1. Write it in machine code

L ��� MM

A lot of work

Page 13: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Where Do Compilers Come From?

1. Write it in machine code2. Write it in a lower level language and compile it

using an existing compiler

L ��� MC C ��� M

M

L ��� MM

But Mom where did the C compiler come from?

Page 14: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Where Do Compilers Come From?

1. Write it in machine code2. Write it in a lower level language and compile it

using an existing compiler3. Write it in the same language that it compiles and

bootstrap

L ��� ML

Page 15: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Bootstrapping a Compiler

�Write the compiler in its own language (#0)�Write a no-frills native compiler (#1)�Use compiler #1 to compile #0 to get native compiler

with more frills (#2)�Repeat as desired

Page 16: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Bootstrapping a Compiler

L ��� ML L --> M

M

L ��� MM#0 (the real

thing)

#1 (no frills)

#2 (real thingcompiled)

Page 17: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Bootstrapping a Compiler, Stage 2

L ��� ML L ��� M

M

L ��� MM#0 (the real

thing)

#2 (the real thing compiled)

#3 (compiled withthe real thing)

Correctness test: #2 = #3 literally

Page 18: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Porting a Compiler

1. Rewrite back end to target new machine

4. Compile on new machine

L ��� MC

L ��� NC

Page 19: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Porting a Compiler

1. Rewrite back end to target new machine

4. Compile on new machine

L ��� NC C ��� N

N

L ��� NN

Page 20: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Porting a Compiler II

�Rewrite back end to target new machine�Compile using native compiler

L ��� NL L ��� M

M

L ��� NM “cross

compiler”

Page 21: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Cross Compilers

�A cross compiler compiles to a target language different from the language of the machine it runs on

Page 22: T-diagrams - Programmiersprachen · PDF fileC ompiler inputs program in source language, outputs in target language C sort ... But Mom where did the C compiler come from? Where Do

Porting a Compiler II

�Rewrite back end to target new machine�Compile using native compiler�Recompile using cross compiler

L ��� NL L ��� N

M

L ��� NN