38
Programming A Historic Perspective on Code Reuse Yingcai Xiao

Programming A Historic Perspective on Code Reuse Yingcai Xiao

  • View
    226

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Programming A Historic Perspective on Code Reuse Yingcai Xiao

ProgrammingA Historic Perspective on Code Reuse

Yingcai Xiao

Page 2: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Want to know?

Why we have to write programs to run a computer?

Why an error in a program is called a bug?

Why there are so many programming languages?

What is a computer? (From a programmer’s point of view).

How do those languages support code reuse?

Page 3: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Programming a Computer

Page 4: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Programming a Computer

Types of ComputersAnalog: Analog Device, 1.2345678Digital: Binary Device, 0 or 1

Programming a ComputerWiring: Hardware, Bug, AdaCoding: Software

Modern Computers: von Neumann architecture• Run stored programs (code reuse) to process

stored data.• Components: Memory, IO, CPU, Secondary

Storage.

Page 5: Programming A Historic Perspective on Code Reuse Yingcai Xiao

What is a program and what is programming?

Programs: stored instructions for data processing.

Programming

= Data Structures + Algorithms

Professor Donald E. Knuth

http://www-cs-faculty.stanford.edu/~knuth/

Page 6: Programming A Historic Perspective on Code Reuse Yingcai Xiao

What is a program from a computer’s point of view?

Programs: • Stored binary opcodes• Different types of computers have

different opcodes • Opcodes are not reusable on different

types computers • Programs in binary codes are not reusable

on different types of computers

Page 7: Programming A Historic Perspective on Code Reuse Yingcai Xiao

How data are stored on a computer?

Bits (0/1) and bytes (0-255):

Short Int (2 bytes):

0 0 0 0 0 0 1 0

0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

Endian (byte ordering): little (Intel), big (Moterola, Sun), bi (DEC Alpha, MIPS), big-to-bi (Sun SPARK v9)

Page 8: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Is data reusable?

0 1 0 0 0 0 0 1

No, in general.

Is data saved on one type of computers reusable on another type of computers?

Yes, for ASCII text

‘A’ (65)

ASCII text (ISO/IEC 8859-1) is platform-independent.

Page 9: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Programming on punch cards. Anyone?

Programs: stored binary opcodes

Punch Card Programming:

punch card machines

converts instructions typed into binary codes (0 no hole, 1 hole) on a stack of cards.

Page 10: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Programming Languages

Page 11: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Assembly Languages

• English-like: load, add, save

• Assembler: a program that translates code written in an assembly language into opcodes.

• Assembly languages are machine-dependent. An assembly language is only valid for a specific CPU architecture.

• Programs written in an assembly language are machine-dependent and not reusable on a different types of CPU architectures.

Page 12: Programming A Historic Perspective on Code Reuse Yingcai Xiao

High-level Programming Languages

• English-like: if, for, switch, …

• Compiler: a program that translates code written in a high-level programming language into opcodes. The input is called the source code and output is called the object code (.obj).

• Object-codes and executables are machine-dependent.• High-level languages are machine-independent.• Examples: A, B, Basic, Fortran, C, C++, C#, Java

(Why using such names?)

• Linker: a program that links object codes together to make an executable (.exe).

Page 13: Programming A Historic Perspective on Code Reuse Yingcai Xiao

High-level Programming Languages

• Binary codes are reusable as libraries on computers of the same architecture. (compile-time sharing).

• Object codes (from different high-level programming languages) can be put together to make a library (.lib).

• Libraries (.lib and .dll) are machine-dependent.

• A dynamically-linked library (.dll) can be shared by all programs on the same computer and by all the running processes on the same computer (run-time sharing).

• Libraries and object files on a computer are linked together to form an executable. (compile-time sharing of binary code).

Page 14: Programming A Historic Perspective on Code Reuse Yingcai Xiao

High-level Programming Languages

• The header files contain the header (not the implementation) of user defined data types and related methods (functions), i.e., describe what’s in the library.

• To use a library, one needs to include the header files (.h) for the library in the source code.

• The compiler use the information in the header files to make type checking.

• Before compilation, the preprocessor of the compiler copies everything in the header files into the source code and generate an intermediate (.I) file.

Page 15: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Traditional Compilation

Source File (.cpp)

Intermediate File (.I)

Object File (.obj)

Binary File (.exe)

Preprocessing

Compilation

Linking

Page 16: Programming A Historic Perspective on Code Reuse Yingcai Xiao

High-level Programming Languages

• Source codes written in a high-level programming language are reusable on different types of computers.

• Binary codes (.obj, .lib, .dll, .exe) compiled from a high-level programming language are reusable on the computers of the same architecture but not reusable on computers of different architecture.

Page 17: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Operating Systems

• OS: software that controls the operation of a computer.

• Each operating system can only run on a specific type of computers (e.g. Mac or PC)

• Multiple operating systems can run on the same type of computers (e.g. Windows and Linux both run on PCs).

• Binary code for one OS will not work on another OS even if both are installed on the same computer (e.g. both Windows and Linux are installed on all lab computers, but (binary) Windows programs will not run in Linux and vice versa.

Page 18: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Common Binary Code?(Binary Code Reuse Cross OS)

Page 19: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Traditional Compilation

Source Code for Language 1

Language 1 Compiler on OS1

Binary Code for OS1

OS1

Source Code for Language 1

Language 1 Compiler on OS2

Binary Code for OS2

OS2

Page 20: Programming A Historic Perspective on Code Reuse Yingcai Xiao

OS-Independent Code: Intermediate Languages

The trend to support machine-independent binary code is to compile the source code into the binary format of an intermediate language.

And to provide an interpreter for the intermediate language on each OS to translate the binary code of the intermediate language into the native binary code of the OS.

Page 21: Programming A Historic Perspective on Code Reuse Yingcai Xiao

OS-Independent Compilation: Intermediate Language

Source Code for Language 1

Language 1 Compiler on OS1

Intermediate Binary Code for Language1

OS1

Intermediate Code Interpreter OS1

OS2

Language 1 Compiler on OS2

Binary Code for OS2Binary Code for OS1

Intermediate Code Interpreter OS2

Page 22: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Java Intermediate Language: Java Bytecode

Java Source Code (.java)

Java Compiler (javac) on OS1

Java Bytecode (.class)

OS1

Java Interpreter on OS1 (java)

OS2

Java Compiler (javac) on OS2

Binary Code for OS2Binary Code for OS1

Java Interpreter on OS2 (java)

Program statements are interpreted one at a time during the run-time.

Page 23: Programming A Historic Perspective on Code Reuse Yingcai Xiao

JIT Compiler

An interpreter interprets intermediate code one line at a time. Slow execution.

A JIT (Just-In-Time) compiler compiles the complete code all at once just into native binary code before execution. Faster execution.

Page 24: Programming A Historic Perspective on Code Reuse Yingcai Xiao

JIT Complier: Java Bite Code Compiler

Java Source Code (.java)

Java Compiler (javac) on OS1

Java Bytecode (.class)

OS1

Java JIT Compiler on OS1

OS2

Java Compiler (javac) on OS2

Binary Code for OS2Binary Code for OS1

Java JIT Compiler on OS2

All programming statements are compiled at compile time.

Page 25: Programming A Historic Perspective on Code Reuse Yingcai Xiao

.NET OS-Platform-Independence

MSIL: Microsoft Intermediate Language (Used by .NET)

Source Code for Language 1

Language 1 Compiler on OS1

MSIL Code

OS1

MSIL JIT Compiler on OS1

OS2

Language 1 Compiler on OS2

Binary Code for OS2Binary Code for OS1

MSIL JIT Compiler on OS2

Page 26: Programming A Historic Perspective on Code Reuse Yingcai Xiao

JIT Compilation in .NET

All MSIL code are JIT-compiled to native binary code before execution. No run-time interpretation, faster execution.

Page 27: Programming A Historic Perspective on Code Reuse Yingcai Xiao

A Common Language?(Source Code Reuse Cross Languages)

.NET CTS/CLR

Page 28: Programming A Historic Perspective on Code Reuse Yingcai Xiao

.NET Common Language Runtime

To make .NET language independent, CLR (Common Language Runtime) is defined as the runtime environment.

CLR defines CTS (Common Type System) which should be followed by all languages to be used in the .NET framework.

The code that follows CTS standard and runs through CLR is called managed code.

Page 29: Programming A Historic Perspective on Code Reuse Yingcai Xiao

.NET Language-Independence

CLR: Common Language Runtime

Source Code for Language 1

Language 1 Compiler on OS1

MSIL Code Confirming CTS (Managed Code)

OS1

CLR on OS1

OS2

Language 2 Compiler on OS2

Binary Code for OS2Binary Code for OS1

CLR on OS2

Source Code for Language 2

Page 30: Programming A Historic Perspective on Code Reuse Yingcai Xiao

.NET Architecture for Language and Platform Independence(fan-in and fan-out on MSIL)

Source Code for Language 1

Language 1 Compiler on OS1

OS1

CLR for OS1

OS2

Language 2 Compiler on OS2

Binary Code for OS2Binary Code for OS1

CLR for OS2

Source Code for Language 2

MSIL Code Confirming CTS (Managed Code)

Page 31: Programming A Historic Perspective on Code Reuse Yingcai Xiao

CLI (Common Language Infrastructure)

CLR/CTS for Everyone?

Page 32: Programming A Historic Perspective on Code Reuse Yingcai Xiao

CLI : Common Language Infrastructure

A specification defines an environment for multiple high-level languages to be used on different computer platforms.

Created by Microsoft based on .NET, standardized by MS, Intel, HP and others, ratified by ECMA and ISO.

.NET is an implementation of CLI for desktop systems.

.NET Compact Framework is an implementation of CLI for portable devices.

Open Source implementations: Mono development platform (Novell), Portable .NET (dotGNU)

Page 33: Programming A Historic Perspective on Code Reuse Yingcai Xiao

CLI (Common Language Infrastructure) SpecificationOpen Architecture for Language and Platform Independent Programming

Source Code for Language 1

Language 1 Compiler on OS1

OS1

CLR for OS1

OS2

Language 2 Compiler on OS2

Binary Code for OS2Binary Code for OS1

CLR for OS2

Source Code for Language 2

CIL (Common Intermediate Language) Code

Confirming CTS (Common Type System)

Page 34: Programming A Historic Perspective on Code Reuse Yingcai Xiao

A Common Language for the Internet?

Page 35: Programming A Historic Perspective on Code Reuse Yingcai Xiao

A Common Language for the Internet

ASCII text (ISO/IEC 8859-1) is platform-independent.

=> HTTP (Hyper Text Transport Protocol)

=> Recognizable by all types of computers. (World Wide Web)

=> Everything is presented as text including data and programs.

Tim Berners-Lee

=> HTML (Hyper Text Markup Language)

Page 36: Programming A Historic Perspective on Code Reuse Yingcai Xiao

A Common Language for the Internet

XML (eXtensible Markup Language), can be used to define data and programs over the Internet.

=> SOAP (Simple Object Access Protocol), XML-based

=> WSDL (Web Service Description Language), XML-based

Page 37: Programming A Historic Perspective on Code Reuse Yingcai Xiao

Web Services

Libraries shared over the Internet at run-time.

Service interfaces specify what the services can do (contracts).

Service interfaces are defined in WSDL (Web Service Description Language)

UDDI Registry: Universal Description, Discovery, and Integration. (yellow page)

Access Standard:

SOAP: Simple Object Access Protocol

Page 38: Programming A Historic Perspective on Code Reuse Yingcai Xiao

What did you learn?

Computer, programming, data structure, algorithm, programming languages, …