Compilers and Assemblers

Similar documents
Compilers and Assemblers

Introduction to Compiler Construction

Introduction to Compiler Construction

Memory Management. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Compiler, Assembler, and Linker

Lec 13: Linking and Memory. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Introduction to Java Programming

Modern Instruc?on Sets

Formal Languages and Compilers Lecture I: Introduction to Compilers

CISC 124: Introduction To Computing Science II

Programming Languages FILS Andrei Vasilateanu

CS399 New Beginnings. Jonathan Walpole

Review. Disassembly is simple and starts by decoding opcode field. Lecture #13 Compiling, Assembly, Linking, Loader I

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

CSCI341. Lecture 22, MIPS Programming: Directives, Linkers, Loaders, Memory

Modern Instruc?on Sets

CS426 Compiler Construction Fall 2006

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18


About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Assemblers and Compilers

Programming 1. Lecture 1 COP 3014 Fall August 28, 2017

CSE 2021: Computer Organization

Instruction Set Architecture

Compiling Techniques

Computer Systems and Networks. ECPE 170 Jeff Shafer University of the Pacific $$$ $$$ Cache Memory $$$

Processor Architectures

CS 536. Class Meets. Introduction to Programming Languages and Compilers. Instructor. Key Dates. Teaching Assistant. Charles N. Fischer.

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Compilers and Interpreters

Assembly Language Fundamentals

Lecture 1 - Introduction (Class Notes)

Programming 1 - Honors

Introduction. CS 2210 Compiler Design Wonsun Ahn

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Principles of Compiler Design

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Instructors: Randy H. Katz David A. PaKerson hkp://inst.eecs.berkeley.edu/~cs61c/fa10. Fall Lecture #11. Agenda

Compiler Design. Lecture 1

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

Operating Systems (2INC0) 2017/18

Assemblers and Linkers

Notes of the course - Advanced Programming. Barbara Russo

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

CPSC 411: Introduction to Compiler Construction

Course: Operating Systems Instructor: M Umair. M Umair

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Operating System: Chap2 OS Structure. National Tsing-Hua University 2016, Fall Semester

Course introduction. Advanced Compiler Construction Michel Schinz

Memory Management. Before We Begin. Process s Memory Address Space. Process Memory. CSE 120: Principles of Operating Systems.

Software Concepts. It is a translator that converts high level language to machine level language.

Compiling Regular Expressions COMP360

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

COMPILER DESIGN LEXICAL ANALYSIS, PARSING

Chapter 3: Operating-System Structures

Lecture #6 Intro MIPS; Load & Store

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

CS/SE 153 Concepts of Compiler Design

CSC488S/CSC2107S - Compilers and Interpreters. CSC 488S/CSC 2107S Lecture Notes

Computers in Engineering COMP 208. Computer Structure. Computer Architecture. Computer Structure Michael A. Hawker

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

JavaOS. David Burhans 2/3/2003 CS384 Dr. Taylor

Compiler Construction D7011E

AP Computer Science A: Java Programming

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Computer Basics 1/24/13. Computer Organization. Computer systems consist of hardware and software.

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Chapter 2: Operating-System Structures

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

Evolution of Virtual Machine Technologies for Portability and Application Capture. Bob Vandette Java Hotspot VM Engineering Sept 2004

Compilers and Code Optimization EDOARDO FUSELLA

Dynamic Memory Allocation

Introduction. Lecture 1 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

MIPS (SPIM) Assembler Syntax

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Compiler Design (40-414)

ECE 15B COMPUTER ORGANIZATION

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

CS61C Machine Structures. Lecture 18 - Running a Program I. 3/1/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

An Introduction to Python (TEJ3M & TEJ4M)

CHAPTER 16 - VIRTUAL MACHINES

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

COSC3330 Computer Architecture Lecture 20. Virtual Memory

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT I - LEXICAL ANALYSIS PART - A

PSD1C SYSTEM SOFTWAE UNIT: I - V PSD1C SYSTEM SOFTWARE

Introduction to Computer Systems

How to create a process? What does process look like?

OS and Computer Architecture. Chapter 3: Operating-System Structures. Common System Components. Process Management

Transcription:

ECPE 170 Jeff Shafer University of the Pacific Compilers and Assemblers

2 Schedule Quiz 6 Tuesday, Nov 22 nd Input / Output (HW #15) OperaOng Systems (HW #16) Compilers & Assemblers (HW #16) Review the lecture notes before the quiz (not just the homework!) Bring a Calculator

3 Quiz 5 Return and Review Quiz 5

4 Homework #13 Average score was a bit low Decided to drop the lowest homework assignment (since we have plenty in this semester!)

5 Compilers & Assemblers

6 Assembler You used the MARIE assembler this semester What was the input? Mnemonic instrucoons (assembly code) What was the output? Machine code (binary)

7 Assembler Most assemblers do this translaoon in two passes over the source code Pass #1: ParOally assemble the code and build symbol table Pass #2: Complete the instrucoons by replacing labels with the memory addresses stored in the symbol table You can do this by hand it s that easy! See Exam 2 problem

8 Loader The assembler produces the binary machine code The loader (part of the operaong system) copies the machine code from disk and places it in main memory Are we ready to execute it? Not quite - there s a challenge!

9 Memory Addresses Imagine a system without virtual memory The operaung system wants to load and run two programs at once: Program A will be placed at address 1000+ Program B will be placed at address 5000+ What if the assembly code for program B was hardwired to assume it started at address 0? The program would fail we d have to get the programmer to send us a new version wricen to run at address 5000

10 Relocatable Binary Code Obviously, that would be a huge pain SoluOon? Relocatable Code MARIE doesn t use this, but real systems do The assembler treats your program as if it started at memory address 0 But, when the operaong system loader copies the binary code from disk into main memory (to execute it), it modifies all your memory addresses New Mem Addr = Old Addr + Start Addr of Program

11 Binary Code Three different types of binary code Absolute code operand addresses are fixed This is how MARIE works Suitable for device and operaong system code only Relocatable code code that can be copied to any memory address, but must be modified before execuong Operand addresses are relaove to where the operaong system chooses to load the program (i.e. offset from a base address) The loader must adjust operands when loading the program Or, special registers in CPU provide base address PosiUon Independent Code code that can be copied to any memory address and run without modificauon

12 Linker Real programs are typically wricen with muluple source files and many subrouones Each file is assembled separately But we need some way to join everything together into a single executable file This is the job of the linker (aka link editor ) Input many files with binary machine code Output single file with all of the necessary binary machine code Linker also uses two passes: Pass #1: Creates a symbol table Pass #2: Resolve references to the values in the symbol table

13 Linker + Loader

14 Dynamic Linking Regular linking happens at compile Ome (last step to produce executable file) Dynamic linking is when the linker runs when the program is loaded (or even later when the program is running!) External modules are loaded from from dynamic link libraries (DLLs) Dynamic linking makes program modules smaller, but carries the risk that the programmer may not have control over the DLL

15 Language Levels Remember that the computer can understand only the 1 st GL!

16 Language Levels Each language generaoon presents problem solving tools that are: Closer to how people think Farther away from how the machine implements the soluoon Assembly code Why would I want (or need) to use assembly code? Why would I not want to use assembly code? Compilers bridge the semanoc gap between the higher level language and the machine s binary instrucoons

17 Compiler Operation Compilers are much more complicated than assemblers/ linkers TranslaOon process takes 6 steps The first three steps are source code analysis: 1. Lexical analysis extracts tokens, e.g., reserved words and variables 2. Syntax analysis (parsing) checks statement construcoon 3. SemanUc analysis checks data types and the validity of operators

18 Compiler Operation The last three compiler steps are synthesis phases: 4. Intermediate code generauon creates three address code to facilitate opomizaoon and translaoon 5. OpUmizaUon creates assembly code while taking into account architectural features that can make the code efficient 6. Code generauon creates binary code from the opomized assembly code We write these steps as separate modules Benefit: Compilers can be wricen for various CPU architectures by rewriong only the last two modules

19 Compiler Operation

20 Interpreter A compiler processes all the source code and produces a binary executable first. Then, the executable is run. Interpreters produce executable code from source code in real Ome (i.e. while the program is running) Pros? Don t have to wait for enore program to compile to test a part of it Portability the program is distributed as the source code, and can run on any machine architecture that has an interpreter Cons? Performance the compiler runs once, but the interpreter runs every Ome the program is executed

21 Java: All of the Above Java exemplifies many of the concepts that we have discussed in this chapter Java programs (classes) execute within a virtual machine, the Java Virtual Machine (JVM) This allows Java programs to run on any plarorm for which a virtual machine environment has been wricen Java is both a compiled and an interpreted language The output of the compilaoon process is an assembly- like intermediate code (bytecode) This bytecode is interpreted by the JVM

22 Java: All of the Above The JVM is an operaong system in miniature. It loads programs, links them, starts execuoon threads, manages program resources, and deallocates resources when the programs terminate. Because the JVM performs so many tasks at run Ome, its performance cannot match the performance of a tradioonal compiled language

23 Java: All of the Above At execuoon Ome, a Java Virtual Machine must be running on the host system It loads and executes the bytecode class file While loading the class file, the JVM verifies the integrity of the bytecode. The loader then performs a number of run- Ome checks as it places the bytecode in memory The loader invokes the bytecode interpreter 23

24 Java: All of the Above The bytecode interpreter: Run a linker over the bytecode instrucoons and asks the loader to supply all referenced classes and system binaries if they are not already loaded Creates and inioalizes the main stack frame and local variables Creates and starts execuoon thread(s) Manages heap storage by deallocaong unused storage while the threads are execuong (garbage collecuon) Deallocates resources of terminated threads Upon program terminaoon, kills any remaining threads and terminates the JVM 24

25 Java: All of the Above Because the JVM does so much as it loads and executes its bytecode, it can't match the performance of a compiled language A Just- In- Time (JIT) compiler can help Compiles blocks of to naove machine code, and saves it for future reuse Benefits of using an interpreter? Class files can be created on one machine architecture and executed on a completely different machine architecture Write once, run anywhere model

26 Evaluation Time! Instructor EvaluaOon Course EvaluaOon Need 1 volunteer to collect/return forms