COP4020 Programming Languages

Slides:



Advertisements
Similar presentations
CSE 105 Structured Programming Language (C)
Advertisements

An Overview Of Virtual Machine Architectures Ross Rosemark.
Chapt.2 Machine Architecture Impact of languages –Support – faster, more secure Primitive Operations –e.g. nested subroutine calls »Subroutines implemented.
Copyright © 2005 Elsevier Imperative languages Group languages as –imperative von Neumann(Fortran, Pascal, Basic, C) object-oriented(Smalltalk, Eiffel,
Chapter 3 Loaders and Linkers
Chapter FourModern Programming Languages1 Language Systems.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Systems Software.
Language Systems Chapter FourModern Programming Languages, 2nd ed.1.
Software Language Levels Machine Language (Binary) Assembly Language –Assembler converts Assembly into machine High Level Languages (C, Perl, Shell)
Data Structure and Algorithm 1 Yingcai Xiao. You Me The Course (
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
Chapter 10 Application Development. Chapter Goals Describe the application development process and the role of methodologies, models and tools Compare.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
“C” Programming Language What is language ? Language is medium of communication. If two persons want to communicate with each other, they have to use.
Language Systems Chapter FourModern Programming Languages 1.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
COP4020 Programming Languages
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Creating your first C++ program
Programming With C.
Introduction to C++ Programming Language
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Replay Compilation: Improving Debuggability of a Just-in Time Complier Presenter: Jun Tao.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
LANGUAGE SYSTEMS Chapter Four Modern Programming Languages 1.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CS-303 Introduction to Programming
By: Cheryl Mok & Sarah Tan. Java is partially interpreted. 1. Programmer writes a program in textual form 2. Runs the compiler, which converts the textual.
Introduction to OOP CPS235: Introduction.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Hello world !!! ASCII representation of hello.c.
Programming Language Theory 2014, 1 Chapter 1 :: Introduction Origin : Michael L. Scott School of Computer & Information Engineering,
Language Translation Compilation vs. interpretation Compilation diagram Step 1: compile Step 2: run program Compiled program compiler input output Compiled.
Programming 2 Intro to Java Machine code Assembly languages Fortran Basic Pascal Scheme CC++ Java LISP Smalltalk Smalltalk-80.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Introduction To Software Development Environment.
CS-140 Dick Steflik Lecture 3. Java C++ Interpreted optimized for the internet Runs on virtual ized machine Derived from C++ Good object model Widely.
Chapter 1 Introduction 2nd Semester H
Advanced Computer Systems
Visit for more Learning Resources
Chapter 1 Introduction.
Language Translation Compilation vs. interpretation.
Programming Language Hierarchy, Phases of a Java Program
CSCI-235 Micro-Computer Applications
Lecture 1: Introduction to JAVA
Microprocessor and Assembly Language
Chapter 1 Introduction.
Lecture 1 Runtime environments.
2.1. Compilers and Interpreters
COP4020 Programming Languages
Chapter 11 Introduction to Programming in C
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
CMP 131 Introduction to Computer Programming
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
PROGRAM AT RUNTIME Subject code: CSCI-620
Programming language translators
SPL – PS1 Introduction to C++.
Presentation transcript:

COP4020 Programming Languages Compilation and Interpretation Prof. Xin Yuan

Overview Compilation and interpretation Virtual machines Static linking and dynamic linking Compiler in action (g++) Integrated development environments 4/19/2017 COP4020 Spring 2014 COP4020 Fall 2006

Compilation and interpretation A program written in a high level language can run in two ways Compiled into a program in the native machine language and then run on the target machine Directly interpreted and the execution is simulated within an interpreter Example: how can the following statement be executed? A[i][j] = 1; 4/19/2017 COP4020 Spring 2014

Compilation and interpretation Example: how can the following statement be executed? A[i][j] = 1; Approach 1: You can create a software environment that understands 2-dimensional array (and the language) To execute the statement, just put 1 in the array entry A[i][j]; This is interpretation since the software environment understands the language and performs the operations specified by interpreting the statements. 4/19/2017 COP4020 Spring 2014

Compilation and interpretation Example: how can the following statement be executed? A[i][j] = 1; Approach 2: Translate the statements into native machine (or assembly language) and then run the program. This is compilation. g++ produces the following assembly for this statement. salq $2, %rax addq %rcx, %rax leaq 0(,%rax,4), %rdx addq %rdx, %rax addq %rsi, %rax movl $1, A(,%rax,4) 4/19/2017 COP4020 Spring 2014

Compilation and interpretation How is a C++ program executed on linprog? g++ try.cpp  compiling the program into machine code ./a.out  running the machine code How is a python program executed? python try.py The program just runs, no compilation phase The program python is the software environment that understands python language. The program try.py is executed (interpreted) within the environment. In general, which approach is more efficient? 4/19/2017 COP4020 Spring 2014

Compilation and interpretation In general, which approach is more efficient? A[i][j] = 1; Compilation: salq $2, %rax addq %rcx, %rax leaq 0(,%rax,4), %rdx addq %rdx, %rax addq %rsi, %rax movl $1, A(,%rax,4) Interpretation: create a software environment that understand the language put 1 in the array entry A[i][j]; 4/19/2017 COP4020 Spring 2014

Compilation and interpretation In general, which approach is more efficient? A[i][j] = 1; Interpretation: create a software environment that understand the language put 1 in the array entry A[i][j]; For the machine to put 1 in the array entry A[i][j], that code sequence still needs to be executed. Most interpreter does a little more than the barebone “real work.” Compilation: salq $2, %rax addq %rcx, %rax leaq 0(,%rax,4), %rdx addq %rdx, %rax addq %rsi, %rax movl $1, A(,%rax,4) Compilation is always more efficient!! Interpretation provides more functionality. E.g. for debugging One can modify the value of a variable during execution. 4/19/2017 COP4020 Spring 2014

Compilation Compilation is the conceptual process of translating source code into a CPU-executable binary target code Compiler runs on the same platform X as the target code Source Program Compiler Debug on X Target Program Compile on X Target Program Input Output Run on X 4/19/2017 COP4020 Spring 2014

Cross Compilation Compiler runs on platform X, target code runs on platform Y Debug on X (= emulate Y) Source Program Cross Compiler Target Program Compile on X Copy to Y Target Program Input Output Run on Y 4/19/2017 COP4020 Spring 2014

Interpretation Interpretation is the conceptual process of running high-level code by an interpreter Source Program Interpreter Output Input 4/19/2017 COP4020 Spring 2014

Compilers versus Interpreters Compilers “try to be as smart as possible” to fix decisions that can be taken at compile time to avoid to generate code that makes this decision at run time Type checking at compile time vs. runtime Static allocation Static linking Code optimization Compilation leads to better performance in general Allocation of variables without variable lookup at run time Aggressive code optimization to exploit hardware features 4/19/2017 COP4020 Spring 2014

Compilers versus Interpreters Benefit of interpretation? Interpretation facilitates interactive debugging and testing Interpretation leads to better diagnostics of a programming problem Procedures can be invoked from command line by a user Variable values can be inspected and modified by a user Some programming languages cannot be purely compiled into machine code alone Some languages allow programs to rewrite/add code to the code base dynamically Some languages allow programs to translate data to code for execution (interpretation) 4/19/2017 COP4020 Spring 2014

Compilers versus Interpreters The compiler versus interpreter implementation is often fuzzy One can view an interpreter as a virtual machine that executes high-level code Java is compiled to bytecode Java bytecode is interpreted by the Java virtual machine (JVM) or translated to machine code by a just-in-time compiler (JIT) A processor (CPU) can be viewed as an implementation in hardware of a virtual machine (e.g. bytecode can be executed in hardware) 4/19/2017 COP4020 Spring 2014

Virtual Machines A virtual machine executes an instruction stream in software Adopted by Pascal, Java, Smalltalk-80, C#, functional and logic languages, and some scripting languages Pascal compilers generate P-code that can be interpreted or compiled into object code Java compilers generate bytecode that is interpreted by the Java virtual machine (JVM) The JVM may translate bytecode into machine code by just-in-time (JIT) compilation 4/19/2017 COP4020 Spring 2014

Compilation and Execution on Virtual Machines Compiler generates intermediate program Virtual machine interprets the intermediate program Source Program Compiler Intermediate Program Compile on X Run on VM Virtual Machine Input Output Run on X, Y, Z, … 4/19/2017 COP4020 Spring 2014

Pure Compilation and Static Linking Adopted by the typical Fortran systems Library routines are separately linked (merged) with the object code of the program Source Program Compiler Incomplete Object Code extern printf(); Linker _printf _fget _fscan … Static Library Object Code Binary Executable 4/19/2017 COP4020 Spring 2014

Compilation, Assembly, and Static Linking Facilitates debugging of the compiler Source Program Compiler Assembly Program extern printf(); Assembler _printf _fget _fscan … Linker Static Library Object Code Binary Executable 4/19/2017 COP4020 Spring 2014

Compilation, Assembly, and Dynamic Linking Dynamic libraries (DLL, .so, .dylib) are linked at run-time by the OS (via stubs in the executable) Source Program Compiler Assembly Program extern printf(); Assembler Shared Dynamic Libraries Incomplete Executable _printf, _fget, _fscan, … Output Input 4/19/2017 COP4020 Spring 2014

Static linking and dynamic linking in action Try ‘g++ –static a1.cpp’ and ‘g++ a1.cpp’ Try to run the program on linprog and cetus What are the relative executable file sizes for static and dynamic linking? Why dynamic linking is now much more common? Which linking mechanism is more portable? 4/19/2017 COP4020 Spring 2014

Preprocessing Most C and C++ compilers use a preprocessor to import header files and expand macros (‘cpp a.cpp’ and ‘cpp a1.cpp’ Source Program Preprocessor Modified Source Program #include <stdio.h> #define N 99 … for (i=0; i<N; i++) for (i=0; i<99; i++) Compiler Assembly or Object Code 4/19/2017 COP4020 Spring 2014

Assembly or Object Code The CPP Preprocessor Early C++ compilers used the CPP preprocessor to generated C code for compilation C++ Source Code C++ Preprocessor C Source Code C Compiler Assembly or Object Code 4/19/2017 COP4020 Spring 2014

g++ phases: When running g++, it invokes preprocessor, compiler, assembler, and linker Try ‘g++ -v a.cpp’ to see the commands executed. Stop after preprocess ‘g++ -v -E a1.cpp’ Stop after compiler (assembly code) ‘ g++ -v –S a1.cpp’ 4/19/2017 COP4020 Spring 2014

Integrated Development Environments Programming tools function together in concert Editors Compilers/preprocessors/interpreters Debuggers Emulators Assemblers Linkers Advantages Tools and compilation stages are hidden Automatic source-code dependency checking Debugging made simpler Editor with search facilities Examples Smalltalk-80, Eclipse, MS VisualStudio, Borland 4/19/2017 COP4020 Spring 2014