• No results found

CS440: Programming Languages and Translators

N/A
N/A
Protected

Academic year: 2021

Share "CS440: Programming Languages and Translators"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

CS440: Programming

Languages and Translators

Lecture 0

Spring 2021

(2)

Outline

1. Programming Languages

2. Translators: Compilers and Interpreters

3. Types of Programming Languages

4. Syllabus

5. OCaml?

(3)

You can program without programming

languages… if you really want

Lecture 0

3

(4)

Computer Architecture in One Slide

Lecture 0

4

Memory

Registers

42

84

35

1

7

0

987

1024

2

Program Counter

01100111

(5)

You can program without programming

languages… if you really want

Lecture 0

5

Altair 8800

1974

(6)

You can program without programming

languages… if you really want

Lecture 0

6

Instruction tape for Harvard Mark I

~1944

(7)

Assembly code makes instructions more

human-readable

Lecture 0

7

Assembler

Binary

1010101010010001000100

1111001010100100010000

0111110110000110000…

(8)

If we can turn text into binaries, why not

easier-to-write text?

Lecture 0

8

Grace Murray Hopper

(1906-1992)

Rear Admiral

Compiler

Binary/Assembly

COBOL

(1959)

(9)

Compiler-writing can be lucrative

Lecture 0

9

Bill Gates

1984

2021

Net worth:

$120,700,000,000

Altair BASIC (1975-76)

(10)

Outline

1. Programming Languages

2. Translators: Compilers and Interpreters

3. Types of Programming Languages

4. Syllabus

5. OCaml?

(11)

There are different ways of translating a

programming language

Lecture 0

11

Compiler

Source Code

Binary/Assembly

Interpreter

Source Code

Compiler

Source Code

Bytecode

VM

(12)

Compilers translate code in phases

Lecture 0

12

Source

Code

Lexical

Analyzer

Tokens

Parser

Abstract

Syntax

Lowering

Intermed.

Rep.

Code

Gen.

Target

Code

Analysis

Optimization

a = b + c - 1

VAR a

EQUAL

VAR b

OP +

VAR C

OP

-CONST 1

Assign

a

+

b

-c

1

temp = c – 1

a = b + temp

subl %rax, 1

addl %rax, %rbx

(13)

May have many more phases, several

intermediate representations

Lecture 0

13

(14)

Front End is language specific

Back End is machine specific

Lecture 0

14

Source

Code

Lexical

Analyzer

Tokens

Parser

Abstract

Syntax

Lowering

Intermed.

Rep.

Code

Gen.

Target

Code

Analysis

Optimization

a = b + c - 1

VAR a

EQUAL

VAR b

OP +

VAR C

OP

-CONST 1

Assign

a

+

b

-c

1

temp = c – 1

a = b + temp

subl %rax, 1

addl %rax, %rbx

(15)

Can (and usually do) swap out back ends to

target different machines

Lecture 0

15

Intermediate

Representation

Machine-Independent Optimizations

x86

ARM

PowerPC

(16)

Compiler collections also swap out front ends

for different languages

Lecture 0

16

Intermediate

Representation

Machine-Independent Optimizations

x86

ARM

PowerPC

C

C++

Java

(17)

Outline

1. Programming Languages

2. Translators: Compilers and Interpreters

3. Types of Programming Languages

4. Syllabus

5. OCaml?

(18)

Programming Language =

Syntax

+

Semantics

Lecture 0

18

What programs look like

(19)

Compilers translate code in phases

Lecture 0

19

Source

Code

Lexical

Analyzer

Tokens

Parser

Abstract

Syntax

Lowering

Intermed.

Rep.

Code

Gen.

Target

Code

Analysis

Optimization

Syntax

Semantics

Needs to preserve semantics

def func ();

return 5 + “hello”

File "main.py", line 1

def func ();

^

SyntaxError: invalid syntax

def func ():

return 5 + “hello”

File "main.py", line 2, in func

return 5 + "hello"

TypeError: unsupported operand

type(s) for +: 'int' and 'str'

(20)

Compilers translate code in phases

Lecture 0

20

Source

Code

Lexical

Analyzer

Tokens

Parser

Abstract

Syntax

Lowering

Intermed.

Rep.

Code

Gen.

Target

Code

Analysis

Optimization

Syntax

Semantics

Needs to preserve semantics

let func () = 5 + “hello”

Line 5, characters 18-25:

Error: This expression has

type string but an

expression was expected of

type int

(21)

Semantics =

Static

+

Dynamic

Lecture 0

21

Analyzed at compile time

Happens at run time

(22)

We can divide programming languages by

whether they have static or dynamic types

• Static languages: types checked at compile time: no type errors at

runtime

• Dynamic languages: types checked at run time, can have type errors

• (Weakly typed languages): types checked at compile time, but can be

avoided, resulting in unexpected behavior or type errors at run time

(23)

We can also divide programming languages based

on paradigm (how you think about programming)

• Imperative: tell computer what to do

• Functional: describe the computation mathematically

• Object-oriented: objects perform computation and carry data

• Scripting

• Relational

• Domain-specific

• Logic

(24)

Lecture 0

24

Static

Dynamic

Imperative

C Pascal

FORTRAN

BASIC

COBOL

Python

JS

Ruby

Functional

Haskell

Scala

OCaml

LISP

Scheme

Object-oriented

Java

C++

C#

Objective C

(25)

Knowing the right paradigm to use can make

programming easier

Lecture 0

25

Task: Sort a linked list (using merge sort)

(26)

Knowing about the language and how it’s

translated can help you write faster code

0.007

6.43

0.014

0.01

0

1

2

3

4

5

6

7

C

Python

OCaml (bytecode)

OCaml (native)

Ti

me

(

s)

Merge sort, 10,000 elements

(27)

Knowing about the language and how it’s

translated can help you write faster code

7

6430

14

10

1

10

100

1000

10000

C

Python

OCaml (bytecode)

OCaml (native)

Ti

me (

ms

)

Merge sort, 10,000 elements

(28)

Type systems can express different levels of

guarantees

• C

• Takes a pointer to a node and returns a pointer to a node.

• OCaml

• Takes an integer list and returns an integer list.

• Haskell

• Takes an integer list, returns an integer list and performs I/O (e.g., printing).

• Coq

• Takes an integer list and returns a sorted permutation of it.

Lecture 0

28

node *mergesort(node *list)

mergesort : int list -> int list

mergesort :: IO ([int] -> [int])

mergesort : forall (l1 : list int), exists (l2: int list),

Sorted l2 /\ Permutation l1 l2

(29)

Different languages are up to different tasks

Lecture 0

29

C?

?

Rust?

(30)

Languages like Java and OCaml have automatic

memory management (garbage collection)

Lecture 0

30

C

int *bigarray = malloc(N*sizeof(int));

do_something(bigarray);

OCaml

let bigarray = Array.make N None;;

do_something bigarray;;

Let me get

that for you!

(31)

Garbage collection is handled by a runtime

that runs alongside the compiled program

Lecture 0

31

Source

Code

Lexical

Analyzer

Tokens

Parser

Abstract

Syntax

Lowering

Intermed.

Rep.

Code

Gen.

Target

Code

Analysis

Optimization

(32)

Outline

1. Programming Languages

2. Translators: Compilers and Interpreters

3. Types of Programming Languages

4. Syllabus

5. OCaml?

(33)

Course Staff

• Instructor: Stefan Muller

• Office Hours: F, 1-2pm

• TA: Xincheng Yang

• Office Hours: T, W 3-4pm

• Office Hours Location: Online (see Blackboard for links)

(34)

Attendance/Lecture Format/Schedule/…

¯\_(ツ)_/¯

(35)

Websites to know

• Course website:

http://cs.iit.edu/~cs440/

• Blackboard

• Piazza (will be linked from Blackboard/main site)

(36)

(Tentative) Schedule

Lecture 0

36

Source

Code

Lexical

Analyzer

Tokens

Parser

Abstract

Syntax

Lowering

Intermed.

Rep.

Code

Gen.

Target

Code

1. Learn OCaml (~3 weeks)

Runtime

2. Syntax, Lexing and Parsing (4 weeks)

3. Semantics (2 weeks)

4. Types (2 weeks)

5. Intermediate Code Gen.

(~1.5 weeks)

6. Memory Management (~0.5 weeks)

7. Logic Programming in Prolog (1.5 weeks)

(37)

Homework

• ~8 homeworks, 1-2 weeks each

• HW0 Out ~Thursday, Due 2/4

• Written and programming

• Mostly individual, maybe some groups

Late Days:

• 6 per student, extend deadline 24 hours

• No more than 2 per assignment

(38)

Exams

• Midterm (~mid-March)

• Final (finals week)

• Both take-home, open book open notes,

• Details TBA

• (No using late days, sorry)

(39)

Grading

• 50% Homeworks

• 20% Midterm

• 30% Final

(40)

Textbooks

• “Purple Dragon Book”: Aho et al. Compilers:

Principles, Techniques and Tools (2

nd

ed.)

For a different take:

• Appel. Modern Compiler Implementation in ML

For more math-y details:

• Pierce. Types and Programming Languages

• Harper. Practical Foundations for Programming

Languages

References

Related documents

“In the plan of care for the Medicare payment episode for which this assessment will define a case-mix group, what is the indicated need for therapy visits (total of reasonable

There are many augmented reality (AR) applications avail- able that can be used to create educational contents for these mobile devices. This paper surveys the most popular

Use an overhand knot to prevent the end of a rope from untwisting, to form a knob at the end of a rope, or to serve as a part of another knot.. When tied at the end or standing

With respect to the research topics, researchers explored many areas, including needs analysis, program development and evaluation, and specific problems related to the process

This is because, during that epoch, the European and Japanese states sooner or later lost their empires and could therefore no longer rely on colonies to protect and privilege

Before Now Handwriting Signature Digital Signed PDF Party A’s Digital Signature Party B’s Digital Signature PDF417 Original Document with Digest Digital signed QR

This document describes how to insert a popup window of the TimeTrade appointment widget while keeping your own web page shaded in the background.. Using this popup widget

Every time the in-memory data structure for a particular column family is dumped to disk we set its bit in the commit log stating that this column family has been successfully