Title: Programming Language Concepts CIS 280
1Programming Language Concepts (CIS 280)
- Elsa L Gunter
- NJIT
- Fall 2001
2WWW Addresses for SML
- http//www.cis.njit.edu/elsa/280/110-smlnj.exe
- ftp//ftp.research.bell-labs.com/dist/smlnj/releas
e/110/110-smlnj.exe - http//cm.bell-labs.com/cm/cs/what/smlnj/index.htm
l - http//cm.bell-labs.com/cm/cs/what/smlnj/doc/basis
/pages/sml-std-basis.html
3Language Paradigms
- Imperative languages
- Main focus machine state the set of values
stored in memory locations - Command-driven Each statement uses current
state to compute a new state - Syntax S1 S2 S3 ...
- Example languages C, Pascal, FORTRAN, COBOL
4Language Paradigms (continued)
- Applicative (functional) languages
- Programs as functions that take arguments and
return values arguments and returned values may
be functions - Programming consists of building the function
that computes the answer function application
and composition main method of computation - Syntax P1(P2(P3 X))
- Example languages ML, LISP, Scheme
5Language Paradigms (continued)
- Rule-based languages
- Programs as sets of basic rules for decomposing
problem - Computation by deduction search, unification and
backtracking main components - Syntax Answer - specification rule
- Example languages (Prolog, Datalog,BNF Parsing)
6Language Paradigms (continued)
- Object-oriented languages
- Classes are complex data types grouped with
operations (methods) for creating, examining, and
modifying elements (objects) subclasses include
(inherit) the objects and methods from
superclasses - Computation is based on objects sending messages
(methods applied to arguments) to other objects - Syntax Varies, object lt- method(args)
- Example languages Java, C, Smalltalk
7Programming Language Implementation
- Develop layers of machines, each more primitive
than the previous - Translate between successive layers
- End at basic layer
- Ultimately hardware machine at bottom
8Basic Machine Components
- Data basic data types and elements of those
types - Primitive operations for examining, altering,
and combining data - Sequence control order of execution of primitive
operations
9Basic Machine Components
- Data access control of supply of data to
operations - Storage management storage and update of program
and data - External I/O access to data and programs from
external sources, and output results
10Basic Computer Architecture
External files
Main memory
Cache memory
CPU
11Typical Hardware Pipeline
- Fetch next instruction in program
- Decode op-code to determine next operation to
perform - Load data from specified registers into operator
buses - Execute operations
- Store results in specified registers
- One or more steps performed each clock cycle
12Hardware Machine Components Data
- Bits 0, 1
- Bytes 8 bits
- Words 16 bits, 32 bits, 64 bits
- Op-codes typically one or two words long, not
all words are codes (usually)
13Operations
- Correspond to (named by) op-codes
- Copy data between main registers and main memory
(or cache) - Perform arithmetic and logic operations on data
in registers, putting results in registers - Alter flow of execution, conditionally or
unconditionally - Copy data between I/O busses and memory
14Sequence Control
- Most instructions cause the program counter to
increment by one as a side effect - Goto, aka jump, reset program counter
- Gosub, aka, jump-save-return, same as goto,
except save previous program counter to register
first - Conditional operations allow a single step to be
skipped - Conditional gotos, and gosubs
15Data Access
- Instructions specify data directly (as part of
the word) or indirectly - Indirect access
- Specify a register that contains data
- Specify a memory location that contains data
- Specify a register that contains a memory
location that contains data
16Storage Management
- Basically all computes need to move data between
registers and main memory - Increasingly, most computers have cache in
between registers and main memory for temporary
storage - Secondary caches becoming more common
- Multiple processors accessing same memory, but
with separate caches need coherent view of memory
17External Environment (I/O)
- Computer (CPU) must control access to external
devises - Extended memory
- Hard drives
- CD-ROM, floppies, etc.
- Input devices, eg, keyboards, mice,
analog-to-digital converters - Output devices, eg, consoles, sound boards
18Virtual (Software) Machines
- At first, programs written in assembly language
(or at very first, machine language) - Hand-coded to be very efficient
- Use layers of software (eg operating system)
- Now, no longer write in native assembly language
- Each layer makes a virtual machine in which the
next layer is defined
19Example Layers of Virtual Computers for a C
Program
Input data
Output results
20Virtual Machines Within Compilers
- Compilers often define layers of virtual machines
- Functional languages Untyped lambda calculus -gt
continuations -gt generic pseudo-assembly -gt
machine specific code - May compile to intermediate language that is
interpreted or compiled separately - Java virtual machine, CAML byte code
21To Class
- Name some examples of virtual machines
- Name some examples of things that arent virtual
machines
22Interpretation Versus Compilation
- A compiler from language L1 to language L2 is a
program that takes an L1 program and for each
piece of code in L1 generates a piece of code in
L2 of same meaning - An interpreter of L1 in L2 is an L2 program that
executes the meaning of a given L1 program - Compiler would examine the body of a loop once
an interpreter would examine it every time the
loop was executed
23Bindings of Program Elements
- A binding of a program element is the selection
of a particular value for it from a set of
possible values - The binding time is when that selection is made
- Binding time can depend on whether we use a
compiler or an interpreter
24Binding Times
- Language definition time language syntax and
semantics - Language implementation time interpreter versus
compiler, aspects left flexible in definition,
set of available libraries
25Binding Times
- Compile time data layout, internal data
structures - Link time (load time) binding of values to
identifiers across program modules - Run time (execution time) actual values assigned
to non-constant identifiers
26To class
- In expression x y 5 what bindings can you
identify, and when are they most likely made?
27Program Aspects
- Syntax what valid programs look like
- Semantics what valid programs mean what they
should compute - Compiler must contain both information
28 Major Phases of a Compiler
- Lex
- Break the source into separate tokens
- Parse
- Analyze phrase structure and apply semantic
actions, usually to build an abstract syntax tree - Semantic analysis
- Determine what each phrase means, connect
variable name to definition (typically with
symbol tables), check types
29Major Phases of a Compiler
- Translate to intermediate representation
- Instruction selection
- Optimize
- Emit final machine code
30Major Phases of a Compiler
Source Program
Lex
Relocatable Object Code
Instruction Selection
Tokens
Linker
Parse
Unoptimized Machine-Specific Assembly Language
Abstract Syntax
Machine Code
Semantic Analysis
Optimize
Optimized Machine-Specific Assembly Language
Symbol Table
Translate
Emit code
Intermediate Representation
Assembly Langague
Assembler
Modified from Modern Compiler Implementation in
ML, by Andrew Appel
31Example of Optimization
- Program code X Y Z W
- Load reg1 with Y
- Load reg2 with Z
- Add reg1 and reg2, saving to reg1
- Store reg1 to tmp
- Load reg1 with tmp
- Load reg2 with W
- Add reg1 and reg2, saving to reg1
- Store reg1 to X
- Eliminate two steps marked