Download call

Document related concepts

Go (programming language) wikipedia , lookup

Programming language wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Reactive programming wikipedia , lookup

Subroutine wikipedia , lookup

Functional programming wikipedia , lookup

Object-oriented programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Structured programming wikipedia , lookup

Transcript
Programming Languages
5/24/2017
Che-Rung Lee
5/24/2017
CS135601 Introduction to Information Engineering
1
Why is machine code not good?
assembly
enough?
Machine code
156C
166D
5056
30CE
C000
Assembly language
Op-code+Operand
(registers, memory
locations, values)
Assembler
What does this mean?
LD
LD
ADDI
ST
HLT
$5, Price
$6, ShippingCharge
$0, $5, $6 Registers
$0, TotalCost
Mnemonic names for op-codes
One-to-one correspondence
Identifiers:
Descriptive names
for memory
locations
• Still need to think like a machine
• Inherently machine-dependent
5/24/2017
CS135601 Introduction to Information Engineering
2
Third generation languages
• Uses high-level primitives
– Similar to the pseudocode in Algorithm lesson
– Each primitive corresponds to a sequence of
machine language instructions
• Machine independent (mostly)
– Ex: little endian(Intel) and big endian(HP,IBM)
Memory address
0x0000
0x0001
0x0002
0x0003
Big endian
12
34
56
78
Little endian
78
56
34
12
5/24/2017
CS135601 Introduction to Information Engineering
3
The evolution of programming
languages
• Why do people need/invent so many
different programming languages?
– In fact, there are many more Isn’t C good enough?
5/24/2017
CS135601 Introduction to Information Engineering
4
Programming paradigms
Imperative paradigm
Object-oriented paradigm
A sequence of commands
that manipulate data to
produce the desired results.
A collection of objects that
can perform actions and
interact with other objects.
(C,JavaScript,Fortran,Matlab)
(C++,Java,C#,VisualBasic)
Declarative paradigm
Functional paradigm
Describe the problem to be
A composition of functions
solved rather than the
(in math sense) that accept
algorithms.(Prolog,Verilog,VHDL inputs and produce outputs.
,Lex/Yacc,AMPL,SQL,HTML)
5/24/2017
(LISP, Mathematica)
CS135601 Introduction to Information Engineering
5
Outline
• “Traditional” programming languages
– Imperative programming languages
• Object-oriented programming languages
• Declarative programming languages
• Functional programming languages
• *Parallel programming
5/24/2017
CS135601 Introduction to Information Engineering
6
“Traditional” 3rd generation
Programming Languages
Imperative programming languages
(procedural programming languages)
5/24/2017
CS135601 Introduction to Information Engineering
7
Programming primitives
• Declarative statements: define customized
names to be used later in the program
• Imperative statements: describe the steps
in the underlying algorithms
• Comments: enhance the readability of a
program for handy explanation
• Directives: assist compiler/interpreter to
generate codes, documents
5/24/2017
CS135601 Introduction to Information Engineering
8
Declarative statements
• Variables are identifiers or customized names
defined to refer some memory locations
• Literals are fixed, predetermined values
• Constants are customized names initiated with
fixed values (meaningful literals)
• Data type: for variables or constants
– Primitive data types: integer, real, character,
Boolean. (predefined in languages)
– Derived data types: customized data type
5/24/2017
CS135601 Introduction to Information Engineering
9
Imperative statements
• Assignment statements
– variables = expression
• Control statements: alter the execution
sequence of the program
– Unconditional jump: goto
– Conditional jump: if-then-else, switch-case
– Loop: for, while-loop, do-until
• Function invocation: we will talk this later
5/24/2017
CS135601 Introduction to Information Engineering
10
Directives
• Not part of executable code or declaration,
but statements to assist code generation
– In C/C++, “#include <stdio.h>”
– In shell script, “#!/usr/bin/bash”
• Directives used for another purposes
– Macro preprocessor in C/C++, “#ifdef”
– JavaDoc uses tags and comments to
create document for Java programs
– OpenMP uses them to parallelize programs
5/24/2017
CS135601 Introduction to Information Engineering
Talk
later
11
Procedural unit
• A program unit written independently of
other program units yet associated with
them through a transfer/return process
– Two kinds of procedural units
• Function: a procedural unit that returns values
• Procedure: a procedural
unit that does not
return values
5/24/2017
CS135601 Introduction to Information Engineering
12
Procedure declaration
• Header(interface): type name (parameters)
– Type: procedure or function
• For a function, define the data type of returned value
• Body: variable declarations + commands
Header
Variable declaration
Imperative statements
5/24/2017
CS135601 Introduction to Information Engineering
13
Procedure invocation
Call by value
5/24/2017
Call by reference
CS135601 Introduction to Information Engineering
14
Variable scope
• What is the output?
– 3 or 5 or 7 or else?
• Global and local
– The same name
but at different
memory addresses
• What is the output
for this one?
5/24/2017
Global variable
int main(){
int var1=3;
foo();
printf(“var1=%d\n”,var1);
}
Local variable
void foo(){
int var1=5;
bar();
}
Local variable
void
void bar(){
bar(){
int
var1=7;
var1=7;
}
}
CS135601 Introduction to Information Engineering
15
Memory allocation
• Global variables are allocated at heap;
local variables are allocated at stack.
• When a procedure call is
finished, local variables are var1 for main
popped out from the stack.
Heap
– Why uses stack for local
variables?
• Dynamic allocated variables
are in heap, no matter local
or global. (what’s problem?)
5/24/2017
var1 for bar
Stack
var1 for foo
CS135601 Introduction to Information Engineering
16
Language implementations
• Interpreter: interprets and executes a
program statement by statement
• Compiler: translates high level program
primitives into machine codes.
– The translation process
5/24/2017
CS135601 Introduction to Information Engineering
17
Lexical analyzer
• Breakdown a program into a list of tokens
a = b + 32;
Token
a
Variable
=
Assignment operator
b
Variable
+
Addition operator
32
Integer
;
5/24/2017
Type
End of statement
CS135601 Introduction to Information Engineering
18
Parser
• Group tokens into meaningful structures
– “meaningful” is defined by the grammatical
rules of the programming language.
– It can be hard even for human
The man the horse that won the race threw was not hurt.
– Grammar: the rules to define the syntax
– Syntax diagram: flow chart for grammar
– Parse tree (output of parser): the hierarchical
structure of tokens
5/24/2017
CS135601 Introduction to Information Engineering
19
An example x+yz
• Syntax diagram for
algebraic expression
5/24/2017
• Parse tree for x+yz
CS135601 Introduction to Information Engineering
20
Code generation
• Translates parse trees to machine codes
• Why are the compiled programs more
efficient than the interpreted programs?
Source code
Interpreted code
x = y + z;
w = x + z;
Compiled code
Load
Load
Addi
Addi
Store
5/24/2017
$1, y
$2, z
$1, $2, $3
$2, $3, $4
$4, w
Load $1, y
Load $2, z
Addi $1, $2, $3
Store $3, x
Memory I/O
(Load, Store) is
much slower than
computation.
Load $1, x
Load $2, z
Addi $1, $2, $3
Store $3, w
CS135601 Introduction to Information Engineering
21
Object-Oriented
Programming Languages
5/24/2017
CS135601 Introduction to Information Engineering
22
E-pet game
• Suppose you want to design a computer
game about E-pet
– If you call its name, the pet will run to you.
– If you pat the pet, it will be happy.
– If you feed the pet, it will eat happily.
–…
5/24/2017
CS135601 Introduction to Information Engineering
23
How to design?
Puppy
Kitty
Name
Four legs
One tail
Color
Name
Four legs
One tail
Color
Bark
Wag Tail
Bite
Meow
Jump
Bite
Puppy
Chick
Name
Two legs
Two wings
Color
Cheep
Flip wings
Peck
Kitty
Chick
Run
With 4 legs
With 4 legs
With 2 legs, 2 wings
Happy
Bark, wag tail
Meow, jump
Cheep, flip wings
Eat
Bite
Bite
Peck
5/24/2017
CS135601 Introduction to Information Engineering
24
First implementation
• Implement procedures for puppy_run,
puppy_happy, puppy_bite, kitty_run, …
• Implement procedures for callName, pat,
and feed.
Procedure feed(p)
Procedure callName(p)
if p is a puppy
call puppy_run(p)
elseif p is a kitty
call kitty_run(p)
elseif p is a chick
call chick_run(p)
5/24/2017
Procedure pat(p)
if p is a puppy
call puppy_happy(p)
elseif p is a kitty
call kitty_happy(p)
elseif p is a chick
call chick_happy(p)
if p is a puppy
call puppy_eat(p)
call puppy_happy(p)
elseif p is a kitty
call kitty_eat(p)
call kitty_happy(p)
elseif p is a chick
call chick_eat(p)
call chick_happy(p)
CS135601 Introduction to Information Engineering
25
Problems for impl 1
• When adding or removing an E-pet, you
need to change all the procedures (pat,…)
• Want to reuse codes as much as possible
– The code of puppy_run and kitty_run, and
puppy_eat and kitty_eat are the same
• What’s the benefit of code reusing?
– Reduce development cost and maintenance cost
• Think about if you want add a feature to all E-pet
• Reduce code size
5/24/2017
CS135601 Introduction to Information Engineering
26
Second implementation
Procedure callName(p)
call run(p)
Procedure pat(p)
call happy(p)
Procedure happy(p)
if p is a puppy
Procedure run(p)
call puppy_happy(p)
if p is a puppy or kitty
call runWith4Legs(p) elseif p is a kitty
call kitty_happy(p)
elseif p is a chick
call runWith2Legs(p) elseif p is a chick
call chick_happy(p)
Procedure feed(p)
call eat(p)
call happy(p)
Procedure eat(p)
if p is a puppy or kitty
call Bite(p)
elseif p is a chick
call Peck(p)
• We don’t need to change callName, pat,
and feed when adding/remove pets, and
we reuse common codes for puppy&kitty
5/24/2017
CS135601 Introduction to Information Engineering
27
Problems for impl 2
• We didn’t really solve the problems of impl 1.
When adding/removing pets, we still need to
change the code for run, happy, eat.
– We do not want to change existing code when
adding or removing some “objects”.
• We make the code more complicated.
– EX: if we want a puppy running with its tail
wagging, we need to change the procedure run
Code
reusing
5/24/2017
Function
isolation
CS135601 Introduction to Information Engineering
28
Object-oriented programming
• Object: relevant data + procedures to
process the data
– The definition of an object is called a class
• Use the relations of objects to achieve
code reuse, like inheritance.
• Use polymorphism to allow variations, by
which more codes can be reused.
• Use encapsulation to hide information.
5/24/2017
CS135601 Introduction to Information Engineering
29
Inheritance
• Allows new classes to be defined in terms
of previously defined classes
Puppy
Two-leg pet
Pet
Two legs
Name
Color
Run
Run
Eat
MakeSound
Four-leg pet
Four legs
One tail
Run
5/24/2017
Chick
Wings
Bark
Wag tail
FlipWings
Cheep
Kitty
Meou
Jump
Bite-pet
Bite
Chew-pet
Chew
CS135601 Introduction to Information Engineering
Calf
moo
Lamb
Baa
30
Polymorphism
• Allows procedure calls to be interpreted by
the object that receives the call
– Dynamic binding: function binding in runtime
void pat(Pet p) { p.happy(); }
Pet p1 = new Puppy();
pat(p1);
Pet p2 = new Kitty();
pat(p2);
5/24/2017
class Puppy extends Pet {
public void happy()
{ bark(); wagTail();}
}
class Kitty extends Pet
{ public void happy()
{ meou(); jump(); }
}
CS135601 Introduction to Information Engineering
31
Encapsulation
• A way of restricting access to the internal
components of an object
– Private versus public
Puppy p1 = new Puppy();
class Puppy extends Pet
{ private char[]: name;
public char[] getName()
{ return area; }
}
5/24/2017
p1.name = “spot”;
char[] a = p1.name;
char[] b = p1.getName();
CS135601 Introduction to Information Engineering
32
Declarative
Programming Languages
5/24/2017
CS135601 Introduction to Information Engineering
33
What is a declarative language?
• A programming paradigm that expresses
the problem to be solved rather than the
algorithms.
– Imperative languages need to describe
algorithms explicitly.
– Uses backend engine to “solve” problems.
– It is usually domain specific.
• Prolog,HTML,Verilog,VHDL,Lex/Yacc,AMPL,SQL
– Many languages hybrid declarative and
imperative paradigms.
5/24/2017
CS135601 Introduction to Information Engineering
34
Prolog
• PROgramming in LOGic: A logic
programming language for general logic
problems solving.
1: witch(X) <= burns(X) and female(X).
2: burns(X) <= wooden(X).
3: wooden(X) <= floats(X).
4: floats(X) <= sameweight(duck, X).
5:
6: female(girl).
Facts
7: sameweight(duck,girl).
8:
9: ? witch(girl). Query
5/24/2017
Resolution
Rules
--- running --witch(girl) yes
--- finished ---
{\fB After Monty
Python Introduction
(Sir Bedevere).
CS135601
to\fP}
Information Engineering
35
Verilog
• A hardware description language used in
the design, verification, and implementation
of digital logic chips.
1
module addbit (a, b, ci, sum, co)
2
input
3
output sum, co;
4
//Port Data types
5
wire a, b, ci, sum, co;
6
//Code starts here
7
assign {co,sum} = a + b + ci;
8
5/24/2017
a
b
ci
a, b, ci;
addbit
co
sum
endmodule
CS135601 Introduction to Information Engineering
36
VHDL
• VHSIC (Very High Speed Integrated
Circuits) Hardware Description Language:
– a design-entry language for field-programmable gate arrays and application-specific
integrated circuits in electronic design
automation of digital circuits.
5/24/2017
CS135601 Introduction to Information Engineering
37
Lex / Yacc
• Lex is a program that generates lexical
analyzers; yacc (Yet Another Compiler Compiler)
is a parser generator based on grammar.
Token definition:
number [0-9]
identifier [a-z][a-z0-9]*
Grammar definition:
expr : expr '+' expr
{ $$ = node( '+', $1, $3 ); }
lex
Source code
HelloWorld.c
5/24/2017
Lexical analyzer
(lex.yy.c)
yacc
Token
stream
Parser
(y.tab.c)
CS135601 Introduction to Information Engineering
Parse
tree
38
SQL
• Structured Query Language: A database
language designed for managing data in
relational database management systems.
SELECT Title, Authors
FROM Book
WHERE price > 100.00
ORDER BY Authors;
Title
Authors
-------------------------------------------------- ------SQL Examples and Guide
3
The Joy of SQL
1
An Introduction to SQL 2 Pitfalls of SQL 1
We may talk more on this in the database lesson.
5/24/2017
CS135601 Introduction to Information Engineering
39
HTML
• Hyper Text Markup Language: describes
the display and format of text, graphics,
hyperlink to other html files…
5/24/2017
CS135601 Introduction to Information Engineering
40
JavaDoc
• Generates API documentation in HTML
format from comments in Java source code.
/**
* Returns an Image object that can then be painted on the screen.
*
* @param url
an absolute URL giving the base location of the image
* @param name the location of the image, relative to the url argument
* @return the image at the specified URL
* @see Image
*/
public Image getImage(URL url, String name)
getImage
public Image getImage(URL url, String name)
Returns an Image object that can then be painted on the screen.
Parameters:
url - an absolute URL giving the base location of the image
name - the location of the image, relative to the url argument
Returns:
the image at the specified URL
See Also:
5/24/2017
CS135601 Introduction to Information Engineering
Image
Input: a source
code with tagged
comments
Output: an HTML
document for the
function
41
Functional
Programming Languages
5/24/2017
CS135601 Introduction to Information Engineering
42
Functional languages
• Computation=evaluation of math functions.
– The output value of a “function” depends only
on the arguments that are input to the function
• It avoids state and mutable data.
– Imperative programming emphasizes
changes state.
– We will see an example for their differences.
• It uses recursion instead of iteration (loop)
5/24/2017
CS135601 Introduction to Information Engineering
43
LISP: LIst Processing Language
• The first functional programming language
• Represent program code and data as lists
• Every list returns a value, which will be
used as an input of its upper level list.
– The return value of the top level will be output
– Ex: (+ (* 3 (+ 1 (- 4 2 (+ 3 4))))) outputs ?
• Many variations (dialects)
– Common lisp, Scheme, emacs lisp
5/24/2017
CS135601 Introduction to Information Engineering
44
Syntax
• Atom: symbol or number
• List: consists of 0 or more expression
– Ex: (42 69 613)
– The first atom in the list is an “operator”
• Recursion:
– compute n!
5/24/2017
(defun factorial (n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
CS135601 Introduction to Information Engineering
45
List evaluation
• Ex: (+ 1 2 3 (* 4 5) 6)
Step 1. (+ 1 2 3 X 6)
evaluate the list first
Step 7. the operator is *,
multiply all atoms
– X = (* 4 5)
Step 2. the operation is +, Step 8. found 4, integer
add all atoms
Step 9. found 5, integer
Step 3. found 1, integer
Step 10. end of list,
evaluate 4*5=20
Step 4. found 2, integer
Step 11. found 6, integer
Step 5. found 3, integer
Step 12. end of list,
Step 6. found (* 4 5), list
evaluate 1+2+3+20+6=32
5/24/2017
CS135601 Introduction to Information Engineering
46
Functional vs. imperative
• A function for checkbook balancing
constructed from two simpler functions
(Find_diff
(Find_sum Old_balance Credits)
(Find_sum Debits))
Functional program
Total_credits = sum of all credits
Temp_balance = Old_balanace +
Total_credits
Total_debit
= sum of all debits
Balance
= Temp_balance –
Total_debits
Imperative program
5/24/2017
CS135601 Introduction to Information Engineering
47
Parallel Programming
5/24/2017
CS135601 Introduction to Information Engineering
48
Why parallel computing?
• Before:
– Save time and money
– Solve larger problems
• Now:
– Multi-core and many-core processors become
the major architecture of computers
– Devices such as GPU (graphics processing
unit) have large number of cores
5/24/2017
CS135601 Introduction to Information Engineering
49
An example
• Problem: compute f (x,y)=x3+2x2y3+4xy2+3y
• Number of processors: 2 (P1 and P2)
• Problem is divided into small problems x3,
2x2y3, 4xy2, 3y and put them into a queue Q
• Algorithm: give x and y, and set f = 0.
1. P1 and P2 read x and y.
2. P1 and P2 get a small problem from Q,
compute it, and add the result to the
variable f, until Q is empty.
5/24/2017
CS135601 Introduction to Information Engineering
50
Problems
• Can P1 and P2 read x or y simultaneously?
– Most memory cannot be read simultaneously.
• How can f be updated by P1 and P2?
– The read-and-update f must be “atomic”.
– Like the critical region idea in the OS lesson.
• There could be dead lock
– Suppose P1 has x
and P2 has y.
5/24/2017
P1:
P2:
wait(y)
wait(x)
send(x)
send(y)
CS135601 Introduction to Information Engineering
51
Parallel programming methods
• For distributed systems: message passing
– Explicit network communication.
– Tight coupled systems: MPI, PVM
– Loosely coupled systems: RPC, RMI, CORBA
• For multi-core processor: shared memory
– Multithreading: P-thread, OpenMP
– Parallel programming language: HPF, UPC
• For specific multiprocessor
– Hardware specific languages: Cuda, OpenCL
5/24/2017
CS135601 Introduction to Information Engineering
52
MPI
• Message Passing Interface: a library
specification for message-passing
programming model
5/24/2017
CS135601 Introduction to Information Engineering
53
RPC
• Remote Procedure Call: allows a program
to call a remote procedure like to call a
local one
– Remote procedure: the execution of the
procedure is on another machine
Client
RPC
Server
Procedure execution
1
6
stub
3
2
4
skeleton
5
5/24/2017
CS135601 Introduction to Information Engineering
54
P-thread
• POSIX threads: a standard programming
interface for threads programming.
– We have talked about thread in the OS lesson
• Three classes of functions
– Thread management:
– Mutexes:
– Condition variables:
5/24/2017
CS135601 Introduction to Information Engineering
55
OpenMP
• Open Multi-Processing: an Application
Program Interface (API) comprised of
three primary components:
– Compiler Directives
– Runtime Library Routines
– Environment Variables
• Features
– Multi-platform, available in C/C++ and Fortran
– Incrementally parallelizing serial programs
5/24/2017
CS135601 Introduction to Information Engineering
56
OpenMP example
#include <omp.h>
Header file
main () {
int nthreads, tid;
/* Fork a team of threads with each thread having a private tid variable */
#pragma omp parallel private(tid)
Compiler derivative
{ /* Obtain and print thread id */
tid = omp_get_thread_num();
Runtime functions
printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0) {
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and terminate */
}
5/24/2017
CS135601 Introduction to Information Engineering
57
Compiling and running
• Compiling
– For visual c++, using “/openmp” to enable
OpenMP support
• Running
– The number of threads
is decided by the
environment variable
OMP_NUM_THREADS
5/24/2017
CS135601 Introduction to Information Engineering
58
OpenCL
• Open Computing Language: a framework
for writing programs that execute across
heterogeneous platforms consisting of
CPUs, GPUs, and other processors.
– A language for writing kernel functions
– APIs for programming on platforms
• Task parallelism and data parallelism.
5/24/2017
CS135601 Introduction to Information Engineering
59
Related courses
• C, C++, Java, JavaScript
– 計算機程式設計,Web程式設計,物件導向程式設
計,軟體實驗,高等程式設計實作,網路程式設計
• Assembly:
– 軟體實驗,計算機結構,嵌入式系統概論
• Matlab:科學計算,影像處理簡介
• Visual basic:多媒體技術概論 (maybe)
5/24/2017
CS135601 Introduction to Information Engineering
60
Related courses (cont’)
•
•
•
•
•
Parallel programming:平行程式設計
Language translation, lex/yacc:編譯器設計
Language theory:正規語言
Programming languages: 程式語言
Learn how to use them and the
Verilog, VHDL
algorithms in the backend engine.
– 數位邏輯設計,硬體實驗,硬體描述語言與合成
• SQL:資料庫系統概論
• Lisp or prolog:人工智慧概論 (maybe)
5/24/2017
CS135601 Introduction to Information Engineering
61
References
• http://www.allisons.org/ll/Logic/Prolog/(prolog example)
• http://www.asic-world.com/ (verilog example)
• http://www.ee.ccu.edu.tw/~wl/FPGA/VHDL 20training.pdf
(VHDL example)
• http://en.wikipedia.org/wiki/SQL (SQL example)
• https://computing.llnl.gov/tutorials/parallel_comp/
(parallel programming)
• Textbook: chap 6
5/24/2017
CS135601 Introduction to Information Engineering
62