cse322, programming languages and compilers 1 6/19/2015 lecture #1, april 3, 2007 course mechanics...

34
Cse322, Programming Languages and Compilers 1 03/27/22 Lecture #1, April 3, 2007 Course Mechanics Text Book Down-loading SML Themes Back-end tasks Language design issues I.R. Machine Code Optimization Interpretation This weeks assignment

Post on 20-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Cse322, Programming Languages and Compilers

104/18/23

Lecture #1, April 3, 2007•Course Mechanics•Text Book•Down-loading SML•Themes•Back-end tasks•Language design issues•I.R.•Machine Code•Optimization•Interpretation•This weeks assignment

Cse322, Programming Languages and Compilers

204/18/23

Acknowledgements

The material taught in this course was made possible by many people. Here is a partial list:

• Andrew Tolmach• Nathan Linger• Harry Porter• Jinke Lee

Cse322, Programming Languages and Compilers

304/18/23

Class Web Page• The CS322 class web page can be

found at:–www.cs.pdx.edu/~sheard/course/Cs322

• Contents of the page– Link to the department Course Syllabus

– Link to the ML home page

– Copies of the PowerPoint slides used in lectures

– Copies of the assignments

– Project Description

– Copies of the SML code illustrated in the lectures

Cse322, Programming Languages and Compilers

404/18/23

Today’s AssignmentsReading• Engineering a Compiler

– Available In the PSU bookstore– In CS321 we covered chapters 1-6 (You might want to review this material)

– Chapter 7.1 – 7.3, pp 207-222– There will be a 5 minute quiz on the reading Wednesday.

Search• Find the class webpage

1 page programming Assignment • Due Wednesday, April 5, 2005• Login to some SML system. See how the system

operates. Type in solutions (in a file) to some problems. Get them running, and print them out then turn them in on Wednesday. What matters here is that you try out the SML system, not that you get them perfect.

Cse322, Programming Languages and Compilers

504/18/23

Course Information• CS322 - Languages and Compiler Design

– Time: Tuesday & Thursday 16:00-17:50 pm– Place: PCAT 28– Instructor: Tim Sheard – office: room 115, CS Dept, 4th Ave Building, Portland State Univ.– phone: 503-725-2410 (work) 503-649-7242 (home)– office hours: Before class in my office, or by Appt.

• Assignments– Reading from text and handouts (quizzes on reading)– Daily, 1 page programming assignments– 3 part programming project

• Grading: – midterm exam (25%) . Tuesday, May 1st, 2007– 3 parts of project (30%)– Daily 1 page assignments and quizzes (15%)– Final exam (30 %)

Cse322, Programming Languages and Compilers

604/18/23

Text Book• Text: Engineering a Compiler

– Keith D. Cooper, and Linda Torczon

• Other Reference Materials– Auxilliary Material

» Notes by Riccardo Pucella, about programming in ML • http://www.cs.cornell.edu/riccardo/smlnj.html

» Elements of Functional Programming (SML book) by Chris Reade, Addison Wesley, ISBN 0-201-12915-9

» Using the SML/NJ Systemhttp://www.cs.cmu.edu/~petel/smlguide/smlnj.htm

• Class Handouts– In each class, a copy of that day’s slides will be available as a

handout.– I will post files that contain the example programs used in each

lecture on the class web page www.cs.pdx.edu/~sheard/course/Cs322

– I will post Assignments there as well.

Cse322, Programming Languages and Compilers

704/18/23

Copurse Schedule

• Guest Lecturer.– There will be a guest Lecturer on Thursday May 24, as I will be out

of town.

Cse322, Programming Languages and Compilers

804/18/23

Academic Integrity

Students are expected to be honest in their academic dealings. Dishonesty is dealt with severely.

• Homework. Pass in only your own work.• Program assignments. Program independently.• Examinations. Notes and such, only as each

instructor allows.

OK to discuss how to solve

problems with other students,

but each student should

write up, debug, and turn in his

own solution.

Cse322, Programming Languages and Compilers

904/18/23

Standard ML• In this course we will use an

implementation of the language Standard ML

• The SML/NJ Homepage has lots of useful information: http://www.smlnj.org//

• You can get a version to install on your own machine there.

I will use the version 110.57 of SML. Earlier versions probably will work as well. I don’t foresee any problems with other versions, but if you want to use the identical version that I use in class then this is the one.

Cse322, Programming Languages and Compilers

1004/18/23

Course Thesis• This course is about programming

languages. We study languages in two ways.

– From the perspective of the user– From the perspective of the implementer (compiler writer)

• We will learn about some languages you may never have heard of. We will learn to program in one of them (Standard ML). Its good to learn a new language in depth.

• This course is also about programming. There will be extensive programming assignments in SML. If you don’t do them - you won’t learn

– You’re deluding yourself if you think you can learn the material without doing the exercises!

• We will write a comiler for a Java subset. Its good to understand the implementation details of a language you already know.

Cse322, Programming Languages and Compilers

1104/18/23

Course topics

• Semantics of programming languages• Interpreters• Run-time organizations and systems• Intermediate code generation• Backend analysis• Transformations and optimizations for a

number of different kinds of languages• Machine code generation

Cse322, Programming Languages and Compilers

1204/18/23

Project

• Write a miniJava interpreter• Build a complete MiniJava Compiler

Cse322, Programming Languages and Compilers

1304/18/23

Themes

• Mapping from High-level to Low-level• Implementing resource management• Integration with OS and hardware

environment• Syntax directed techniques

Cse322, Programming Languages and Compilers

1404/18/23

Compiler Backend Tasks

• Simplify expressions and statement into flat goto/label form

• Fix location of variables and temporaies in memory and registers

• Generate machine instructions• Manage machine resources• Interact with O/S, runtime systems

Machine code for specific target machine

Type-checked Abstract Syntax for Source Language

Or, build interpreter for H.L.L. features

Cse322, Programming Languages and Compilers

1504/18/23

Language Design Issues

Lexical Analysis

Parsing

Type-Checking

Intermediate code generation

Interpreter

Optimization

Machine code generation

Cs321

Cs322

Source Code

Tokens

Abstract Syntax

Intermediate Code

Machine Code

Control Structures

Machine independent

Machine-dependent

Syntax

Data Types

Cse322, Programming Languages and Compilers

1604/18/23

Translation to Abstract Syntax (CS321)

text:

tokens:

syntax tree:

*

+

Id(z)

float(12.0)

Id(x) Id(pi)

=

Id(z)

id(z) eql id(x) plus id(pi) times float(12..0)

z = x + pi * 12.0

Cse322, Programming Languages and Compilers

1704/18/23

Intermediate Code Generation

• Emit I.C. (or “I.R.”) from abstract syntax or directly from parser

• Advantages:• Keeps more of compiler machine-independent

• Facilitates some optimizations

• Typical examples:• Postfix

• Trees or DAGs

• Three-address code (quadruples, triples, etc.)

• Abstracts key features of machine architectures• E.g., sequential execution, explicit jumps

• But hides details• E.g., # of registers, style of conditionals, etc.

• Many possible levels

Cse322, Programming Languages and Compilers

1804/18/23

Three-address-code – a typical linear I.R.• Generate list of “instructions”• Each has an operator, up to 2 args, and up to 1

result• Instructions can be labeled• Operands are names for locations in some

abstract memory e.g., symbol table entries)

• Examples of instructions:A := B copyA = B op C binary opsA = op B unary opsgoto L jumpsif A relop B goto L conditional jumpsparam A procedure call setupcall P,N procedure callreturn N procedure returnA[I] array dereference

Cse322, Programming Languages and Compilers

1904/18/23

3-Address Code Example

if c > 7 goto L1goto L2L1: if b = e goto L3L2: t1 := d + 2t2 := -ct3 := t1 * t2goto L4L3: t1 := d + et3 := 3 * t1L4: a := t3

• Linearized

• Nested conditionals expanded (badly)

• Temporaries for all intermediate results

and * *

:=

a if

> = 3 + - +

c 7 b e d e c d d 2

Cse322, Programming Languages and Compilers

2004/18/23

Machine-code Generation

• “Read” I.R.; generate assembly language (symbol or binary).

• Must cooperate with I.R. to define and “enforce” runtime environment.

• Must deal with idiosyncrasies of target machine,• e.g., instruction selection

• Must perform resource management,• e.g., register assignment.

• Lots of case analysis, especially for complex target architectures.

• It can be done by hand, but it is hard.• Tools limited, but sometimes useful

– mainly based on pattern matching

Cse322, Programming Languages and Compilers

2104/18/23

Sample machine code

• Assumes – a global;

– b,c args;

– d,e locals.

• Illustrates register conventions, delay slots, etc.

sethi %hi(_a),%o2cmp %i1,7ble L2or %o2,%lo(_a),%l1cmp %i0,%l2bne L4sub %g0,%i1,%o0add %l0,%i0,%o1

sll %o1,1,%o0 add %o0,%o1,%o0 b L3 st %o0,[%o2+%lo(_a)]L2: sub %g0,%i1,%o0L4: call .umul,0 add %l0,2,%o1 st %o0,[%l1]L3:

Cse322, Programming Languages and Compilers

2204/18/23

Optimization• Improve (don't perfect) code by removing

inefficiencies:• In original program• Introduced by compiler itself

• Can operate on source, I.R., or object code.• Local Improvements• Example: changing if c > 7 goto L1 goto L2 L1: ... L2: ...• to if c <= 7 goto L2 L1: ... L2: …

Cse322, Programming Languages and Compilers

2304/18/23

Optimization (continued)

“Global” Improvements• Example: changing

for (i := 0; i < 1000; i++)

a[i] := b*c + i;

• to

t1 := b * c;

for (i = 0; i < 1000; i++)

a[i] = t1 + i;

• Inter-procedural improvements• Example: In-lining a function

• Most of a modern compiler is devoted to optimization.

Cse322, Programming Languages and Compilers

2404/18/23

Interpretation

• Simulate execution of program (source, AST, or other IR) on an abstract machine.

• Implement abstract machine on a real machine.• Inputs to interpreter are

• Program to be interpreted

• Input to that program

• Simpler than compiling and takes no time up front, but interpreted code runs (~10X) more slowly than compiled code.

• Much more portable than real machine code (as for Java).

• Helps with semantic definition.

Cse322, Programming Languages and Compilers

2504/18/23

Using ML

• We will use ML to program the projects.– ML page to download the compiler

http://www.smlnj.org//

– A set of notes from CS321. Available from cs321 website

– Notes by Riccardo Pucella, about programming in ML

» http://www.cs.cornell.edu/riccardo/smlnj.html

– Elements of Functional Programming (SML book) by Chris Reade, Addison Wesley, ISBN 0-201-12915-9

– Using the SML/NJ System

http://www.cs.cmu.edu/~petel/smlguide/smlnj.htm

Cse322, Programming Languages and Compilers

2604/18/23

ML modules

• Code in ML is broken into modules.– Another name for a module is a library

• Each module contains a set of type declarations and a set of functions that operate on those types.

• Modules are separately compiled.

• Modules can depend on other modules.

• Modules are tied together by the compile-manager

Cse322, Programming Languages and Compilers

2704/18/23

The Compile-manager

• The compile manager is a “make” like facility for SML.

• It has some documentation found here.– http://www.smlnj.org/doc/CM/index.html

• Peter Lee’s notes also contains a brief (but out of date) introduction – http://www.cs.cmu.edu/~petel/smlguide/smlnj.htm

• The basic approach is that a single file contains a list of all the pieces that comprise a project.

– A complete scan of all the pieces determines a dependency graph of which pieces depend on which other pieces.

– It determines the time-stamp of each piece

– It recompiles all the pieces that are out of date

– It links everything together

Cse322, Programming Languages and Compilers

2804/18/23

Using the Compile manager

• The compile manager compiles whole compilation units called structures or libraries.

• Break program into files where each file contains 1 module or library.

• If a file needs stuff from another module or library, open that library inside of the file.

• Create a compile manager source file that lists all the libraries.

Cse322, Programming Languages and Compilers

2904/18/23

Examplegroup is ProgramTypes.sml Printer.sml

$/basis.cm

structure Printer = struct

open ProgramTypes;

fun showB Int = "int"

| showB Real = "real"

| showB Bool = "boolean";

structure ProgramTypes = struct

type Id = string;

datatype Basic = Bool | Int | Real;

datatype Type

= BasicType of Basic

| ArrayType of Basic

| ObjType of Id

| VoidType;

Printer.sml

Example.cm

ProgramTypes.sml

Cse322, Programming Languages and Compilers

3004/18/23

File: ProgramTypes.sml

Name of the library, can be different from the name of the file.

Libraries are bracketed by“structure name = struct”

and “end”

structure ProgramTypes = struct

type Id = string;

datatype Basic = Bool | Int | Real;

datatype Type

= BasicType of Basic

| ArrayType of Basic

| ObjType of Id

| VoidType;

end;

Cse322, Programming Languages and Compilers

3104/18/23

File: Printer.sml

Opens libraries with needed components

structure Printer = struct

open ProgramTypes;

fun showB Int = "int"

| showB Real = "real"

| showB Bool = "boolean";

Cse322, Programming Languages and Compilers

3204/18/23

The sources file

File: Example.cm

The name of the file where this list

resides

All the user defined pieces

System libraries

group is

ProgramTypes.sml

Printer.sml

$/basis.cm

Cse322, Programming Languages and Compilers

3304/18/23

Putting it all together.- CM.make "Example.cm";

[scanning Example.cm]

[parsing (Example.cm):ProgramTypes.sml]

[parsing (Example.cm):Printer.sml]

[compiling (Example.cm):ProgramTypes.sml]

[code: 76, env: 3383 bytes]

[compiling (Example.cm):Printer.sml]

[code: 11030, data: 446, env: 2700 bytes]

[New bindings added.]

val it = true : bool

- open Printer;

opening Printer

val showB : Basic -> string

val showConstant : Constant -> string

val showExp : Exp -> string

It compiles the “*.sml” files

I open the Printer Library to get at the function

showStmt

Cse322, Programming Languages and Compilers

3404/18/23

Assignment #1

CS322 Prog Lang & Compilers Assignment # 1

Assigned: April 3, 206 Due: Wed. April 5, 2006

• Read Chapters 7.1 - 7.3 of the text book• Find (and install if necessary) a version of

SML• Create a files with some ML program• Load these into SML.• Test them out by typing some examples.• Cut and paste your session, print it out, and

hand it in during class on Wednesday. Don’t forget your name.