Java and C CSE 351 Spring

Similar documents
Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Java and C. CSE 351 Autumn 2018

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Implementing Higher-Level Languages. Quick tour of programming language implementation techniques. From the Java level to the C level.

Typical Compiler. Ahead- of- time compiler. Compilers... that target interpreters. Interpreter 12/9/15. compile time. run time

Structs and Alignment CSE 351 Spring

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Data III & Integers I

Data III & Integers I

Data III & Integers I

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

Hardware: Logical View

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Memory, Data, & Addressing I

CSE351: Memory, Data, & Addressing I

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Structs and Alignment

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Structs and Alignment

We made it! Java: Assembly language: OS: Machine code: Computer system:

Arrays. CSE 351 Autumn Instructor: Justin Hsia

UW CSE 351, Winter 2013 Final Exam

x86-64 Programming III & The Stack

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

CSE 401/M501 Compilers

Arrays. CSE 351 Autumn Instructor: Justin Hsia

x86 Programming I CSE 351 Winter

University of Washington

Building an Executable

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Assembly Programming IV

CS527 Software Security

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Computer Systems CSE 410 Autumn Memory Organiza:on and Caches

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Introduction to Programming Using Java (98-388)

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

Lectures 5-6: Introduction to C

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

CSE 431S Type Checking. Washington University Spring 2013

CSCI 355 LAB #2 Spring 2004

In Java we have the keyword null, which is the value of an uninitialized reference type

Cache Example, System Control Flow

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

The Hardware/Software Interface CSE351 Spring 2015

Lectures 5-6: Introduction to C

EL2310 Scientific Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Motivation was to facilitate development of systems software, especially OS development.

CSE 351 Spring 2017 Final Exam (7 June 2017)

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

However, in C we can group related variables together into something called a struct.

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

Pointers (continued), arrays and strings

Motivation was to facilitate development of systems software, especially OS development.

The Hardware/Software Interface CSE351 Spring 2015

University*of*Washington*

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

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

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Pointers (continued), arrays and strings

Recitation #11 Malloc Lab. November 7th, 2017

Announcements* Hardware:*Logical*View* Hardware:*SemiVLogical*View* Hardware:*Physical*View*

CSE351 Spring 2010 Final Exam (9 June 2010)

VARIABLES AND TYPES CITS1001

C++ Programming: Polymorphism

Inheritance, Polymorphism and the Object Memory Model

CSE351 Spring 2010 Final Exam (9 June 2010)

C++ Inheritance I. CSE 333 Spring Instructor: Justin Hsia

The Hardware/Software Interface CSE351 Spring 2013 (spring has sprung!)

EMBEDDED SYSTEMS PROGRAMMING Language Basics

CSE351 Autumn 2013 Final Exam (11 Dec 2013)

Floating Point II, x86 64 Intro

G52CPP C++ Programming Lecture 12

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

CSCI 355 Lab #2 Spring 2007

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Assembly Programming IV

Derived and abstract data types. TDT4205 Lecture 15

2. The object-oriented paradigm

Pointers, Dynamic Data, and Reference Types

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

x86-64 Programming III

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

Memory, Arrays & Pointers

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Transcription:

Java and C CSE 351 Spring 2018 https://xkcd.com/801/

Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp %rbp 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Java: Car c = new Car(); c.setmiles(100); c.setgals(17); float mpg = c.getmpg(); OS: Memory & data Integers & floats x86 assembly Procedures & stacks Executables Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C Computer system: 2

Java vs. C Reconnecting to Java (hello CSE143!) But now you know a lot more about what really happens when we execute programs We ve learned about the following items in C; now we ll see what they look like for Java: Representation of data Pointers / references Casting Function / method calls including dynamic dispatch Runtime environment Translation from high-level code to low-level code 3

Worlds Colliding CSE351 has given you a really different feeling about what computers do and how programs execute We have occasionally contrasted to Java, but CSE143 may still feel like a different world It s not it s just a higher-level of abstraction Connect these levels via how-one-could-implement-java in 351 terms 4

Meta-point to this lecture None of the data representations we are going to talk about are guaranteed by Java In fact, the language simply provides an abstraction (Java language specification) Tells us how code should behave for different language constructs, but we can't easily tell how things are really represented But it is important to understand an implementation of the lower levels useful in thinking about your program 5

Data in Java Integers, floats, doubles, pointers same as C Pointers are called references in Java, but are much more constrained than C s general pointers Java s portability-guarantee fixes the sizes of all types Example: int is 4 bytes in Java regardless of machine No unsigned types to avoid conversion pitfalls Added some useful methods in Java 8 (also use bigger signed types) null is typically represented as 0 but you can t tell Much more interesting: Arrays Characters and strings Objects 6

Data in Java: Arrays Every element initialized to 0 or null Length specified in immutable field at start of array (int 4 bytes) array.length returns value of this field Since it has this info, what can it do? C: Java: int array[5];?????????? 0 4 20 int[] array = new int[5]; 5 00 00 00 00 00 0 4 20 24 7

Data in Java: Arrays Every element initialized to 0 or null Length specified in immutable field at start of array (int 4 bytes) array.length returns value of this field Every access triggers a bounds-check Code is added to ensure the index is within bounds Exception if out-of-bounds C: Java: int array[5];?????????? 0 4 20 int[] array = new int[5]; 5 00 00 00 00 00 0 4 20 24 To speed up bounds-checking: Length field is likely in cache Compiler may store length field in register for loops Compiler may prove that some checks are redundant 8

Data in Java: Characters & Strings Two-byte Unicode instead of ASCII Represents most of the world s alphabets String not bounded by a \0 (null character) Bounded by hidden length field at beginning of string All String objects read-only (vs. StringBuffer) Example: the string CSE351 C: (ASCII) Java: (Unicode) 43 53 45 33 35 31 \0 0 1 4 7 6 43 00 53 00 45 00 33 00 35 00 31 00 0 4 8 16 9

Data in Java: Objects Data structures (objects) are always stored by reference, never stored inline Include complex data types (arrays, other objects, etc.) using references C: struct rec { int i; int a[3]; struct rec *p; ; a[] stored inline as part of struct Java: class Rec { int i; int[] a = new int[3]; Rec p;... a stored by reference in object i a p 0 4 16 24 i a p 0 4 12 20 3 0 4 16 10

Pointer/reference fields and variables In C, we have -> and. for field selection depending on whether we have a pointer to a struct or a struct (*r).a is so common it becomes r->a In Java, all non-primitive variables are references to objects We always use r.a notation But really follow reference to r with offset to a, just like r->a in C So no Java field needs more than 8 bytes C: Java: struct rec *r = malloc(...); struct rec r2; r->i = val; r->a[2] = val; r->p = &r2; r = new Rec(); r2 = new Rec(); r.i = val; r.a[2] = val; r.p = r2; 11

Pointers/References C: Pointers in C can point to any memory address References in Java can only point to [the starts of] objects Can only be dereferenced to access a field or element of that object struct rec { int i; int a[3]; struct rec *p; ; struct rec* r = malloc( ); some_fn(&(r->a[1])); // ptr r i a p 0 4 16 24 r Java: class Rec { int i; int[] a = new int[3]; Rec p; Rec r = new Rec(); some_fn(r.a, 1); // ref, index i a p 0 4 12 20 3 int[3] 0 4 16 12

Casting in C (example from Lab 5) Can cast any pointer into any other pointer Changes dereference and arithmetic behavior struct BlockInfo { size_t sizeandtags; struct BlockInfo* next; struct BlockInfo* prev; Cast b into char * to ; do unscaled addition typedef struct BlockInfo BlockInfo;... int x; Cast back into BlockInfo *b; BlockInfo * to use BlockInfo *newblock; as BlockInfo struct... newblock = (BlockInfo *) ( (char *) b + x );... s n p 0 8 16 24 x s n p 13

Type-safe casting in Java Can only cast compatible object references Based on class hierarchy class Object {... Vehicle v = new Vehicle(); // super class of Boat and Car Boat b1 = new Boat(); // --> sibling Car c1 = new Car(); // --> sibling Vehicle v1 = new Car(); Vehicle v2 = v1; Car c2 = new Boat(); class Vehicle { int passengers; class Boat extends Vehicle { int propellers; class Car extends Vehicle { int wheels; Car c3 = new Vehicle(); Boat b2 = (Boat) v; Car c4 = (Car) v2; Car c5 = (Car) b1; 14

Type-safe casting in Java Can only cast compatible object references Based on class hierarchy class Object {... class Vehicle { int passengers; class Boat extends Vehicle { int propellers; class Car extends Vehicle { int wheels; Vehicle v = new Vehicle(); // super class of Boat and Car Boat b1 = new Boat(); // --> sibling Car c1 = new Car(); // --> sibling Vehicle v1 = new Car(); Vehicle v2 = v1; Car c2 = new Boat(); Car c3 = new Vehicle(); Boat b2 = (Boat) v; Car c4 = (Car) v2; Car c5 = (Car) b1; Everything needed for Vehicle also in Car v1 is declared as type Vehicle Compiler error: Incompatible type elements in Car that are not in Boat (siblings) Compiler error: Wrong direction elements Car not in Vehicle (wheels) Runtime error: Vehicle does not contain all elements in Boat (propellers) v2 refers to a Car at runtime Compiler error: Unconvertable types b1 is declared as type Boat 15

Java Object Definitions class Point { double x; double y; Point() { x = 0; y = 0; fields constructor boolean sameplace(point p) { return (x == p.x) && (y == p.y);... Point p = new Point();... method(s) creation 16

Java Objects and Method Dispatch p Point object header vtable ptr x y vtable for class Point: q Point object header vtable ptr x code for Point() y code for sameplace() Virtual method table (vtable) Like a jump table for instance ( virtual ) methods plus other class info One table per class Object header : GC info, hashing info, lock info, etc. Why no size? 17

Java Constructors When we call new: allocate space for object (data fields and references), initialize to zero/null, and run constructor method Java: Point p = new Point(); C pseudo-translation: Point* p = calloc(1,sizeof(point)); p->header =...; p->vtable = &Point_vtable; p->vtable[0](p); p Point object header vtable ptr x y vtable for class Point: code for Point() code for sameplace() 18

Java Methods Static methods are just like functions Instance methods: Can refer to this; Have an implicit first parameter for this; and Can be overridden in subclasses The code to run when calling an instance method is chosen at runtime by lookup in the vtable Java: p.sameplace(q); C pseudo-translation: p->vtable[1](p, q); p Point object header vtable ptr x y vtable for class Point: code for Point() code for sameplace() 19

Subclassing class 3DPoint extends Point { double z; boolean sameplace(point p2) { return false; void sayhi() { System.out.println("hello"); Where does z go? At end of fields of Point Point fields are always in the same place, so Point code can run on 3DPoint objects without modification Where does pointer to code for two new methods go? No constructor, so use default Point constructor To override sameplace, use same vtable position Add new pointer at end of vtable for new method sayhi 20

Subclassing class 3DPoint extends Point { double z; boolean sameplace(point p2) { return false; void sayhi() { System.out.println("hello"); 3DPoint object header vtable x y z tacked on at end z vtable for 3DPoint: (not Point) constructor sameplace sayhi tacked on at end sayhi Code for sayhi Old code for constructor New code for sameplace 21

Dynamic Dispatch Point object header vtable ptr x y Point vtable: p??? 3DPoint object code for Point() code for Point s sameplace() header vtable x y z 3DPoint vtable: code for sayhi() code for 3DPoint s sameplace() Java: Point p =???; return p.sameplace(q); C pseudo-translation: // works regardless of what p is return p->vtable[1](p, q); 22

Ta-da! In CSE143, it may have seemed magic that an inherited method could call an overridden method You were tested on this endlessly The trick in the implementation is this part: p->vtable[i](p,q) In the body of the pointed-to code, any calls to (other) methods of this will use p->vtable Dispatch determined by p, not the class that defined a method 23

Practice Question Assume: 64-bit pointers and that a Java object header is 8 B What are the sizes of the things being pointed at by ptr_c and ptr_j? struct c { int i; char s[3]; int a[3]; struct c *p; ; struct c* ptr_c; class jobj { int i; String s = "hi"; int[] a = new int[3]; jobj p; jobj ptr_j = new jobj(); 24