Loci Programming Language. Stephen Cross

Similar documents
CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

COMP 2355 Introduction to Systems Programming

A brief introduction to C++

C++ Important Questions with Answers

Program template-smart-pointers-again.cc

Fast Introduction to Object Oriented Programming and C++

G52CPP C++ Programming Lecture 13

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

Advanced Systems Programming

04-19 Discussion Notes

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

G52CPP C++ Programming Lecture 9

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Tokens, Expressions and Control Structures

2 2

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Short Notes of CS201

AN OVERVIEW OF C++ 1

CS201 - Introduction to Programming Glossary By

C:\Temp\Templates. Download This PDF From The Web Site

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Program template-smart-pointers.cc

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Lecture 15a Persistent Memory & Shared Pointers

C++ (Non for C Programmer) (BT307) 40 Hours

CE221 Programming in C++ Part 1 Introduction

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Pointers. Developed By Ms. K.M.Sanghavi

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

A Tour of the C++ Programming Language

CS

EL2310 Scientific Programming

Instantiation of Template class

Stream Computing using Brook+

CS 376b Computer Vision

Object-Oriented Principles and Practice / C++

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

COEN244: Class & function templates

QUIZ. What is wrong with this code that uses default arguments?

eingebetteter Systeme

use static size for this buffer

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

6.096 Introduction to C++ January (IAP) 2009

PIC 10A Objects/Classes

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Introduce C# as Object Oriented programming language. Explain, tokens,

UEE1302 (1102) F10: Introduction to Computers and Programming

Programmazione. Prof. Marco Bertini

Part I: Short Answer (12 questions, 65 points total)

1d: tests knowing about bitwise fields and union/struct differences.

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Final CSE 131B Spring 2004

A Tour of the C++ Programming Language

Advanced C++ Topics. Alexander Warg, 2017

PHY4321 Summary Notes

Variables. Data Types.

AIMS Embedded Systems Programming MT 2017

C The new standard

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

G52CPP C++ Programming Lecture 20

C++_ MARKS 40 MIN

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Subtyping (Dynamic Polymorphism)

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Object Oriented Programming. Solved MCQs - Part 2

Chapter 2. Procedural Programming

Review&Preview 1/23/15, 4:08:07 PM 1. 3rd edition - standardized, and standard library allows programmer to start from a higher level

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

C++ Inheritance and Encapsulation

CSCE 110 PROGRAMMING FUNDAMENTALS

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

C++ Programming: Polymorphism

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Database Systems on Modern CPU Architectures

Lecture 10: building large projects, beginning C++, C++ and structs

Smart Pointers. Some slides from Internet

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 333 Midterm Exam 7/22/12

A Fast Review of C Essentials Part I

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Pointers, Dynamic Data, and Reference Types

C++ for Java Programmers

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Programming in C and C++

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Midterm Review. PIC 10B Spring 2018

Intermediate C++ 1/83

Transcription:

Programming Language Stephen Cross

Overview Systems level language Influenced by C and particularly C++, but incorporates aspects of many languages Very close compatibility with C Carefully designed to match run-time overheads of C++ Hello World (C style) int main(int argc, char** argv) { printf(c Hello world!\n ); return 0; Hello World ( style) int main(int argc, char** argv) { std::print( Hello world!\n ); return 0;

Why? C++ has problems: Overly complex syntax and semantics Easy to trigger undefined/unspecified behaviour Valuable features missing Excessive verbosity End the need to use C for APIs: Implementation specification means binary compatibility between compilers Enhanced encapsulation over C++ Callable from all C-compatible languages

Compilation Strategy Module source file source file Compiler 'module' file Linker Object file Module source file source file Header Generator 'header' file 'module' file

Classes Can generate declaration from definition const only needed in declaration Private variables never mentioned in declaration Destructor never mentioned in declaration Avoid writing class name more than once Declaration class ExampleClass { static default(); Definition class ExampleClass (int value) { static default () { // Constructor. return @(0); ~ { // Destructor. int getvalue() { return @value; int getvalue() const; void setvalue(int value); void setvalue(int value) { @value = value;

Move Operations C++: copy by default, move if supported : move by default, copy if supported Adds move operator: ExampleClass value0 = ; ExampleClass value1 = move value0; move returns r-value; makes l-value empty empty objects are just zeroes No destructor is run for an empty object empty objects can be filled by assignment

Move Operations (1) C++ template <typename T> class auto_ptr { public: auto_ptr() : ptr_(null) { auto_ptr(t* ptr) : ptr_(ptr) { assert(ptr!= NULL); auto_ptr(auto_ptr<t>& p){ ptr_ = p.ptr_; p.ptr_ = NULL; auto_ptr<t>& operator=(const auto_ptr<t>& p) { If (&p!= this) { delete ptr_; ptr_ = p.ptr_; p.ptr_ = NULL; return *this; ~auto_ptr() { delete ptr_; private: T* ptr_; template <typename T> class auto_ptr (T* ptr) { static null() { return @(null); static default(t* ptr) { assert(ptr!= null); return @(ptr); ~ { delete @ptr; ;

Move Operations (2) In C++, objects are tied to their location In, objects are transient; they move between l-values Objects can have the role of l-value lval keyword enables operator overloading for lvalue operations Single responsibility principle lval class int_lvalue { int* opaddress(); void opassign(int newvalue); int opmove(); int& opreference(); void example(lval int_lvalue lvalue) { // Calls lvalue.opassign(1); lvalue = 1; // Calls lvalue.opmove(); int movedval = move lvalue; // etc.

Primitives are Objects Using primitives as objects float minusquarter = -0.25f; float plusquarter = minusquarter.abs(); float zero = plushalf.floor(); float plushalf = plusquarter.sqrt(); Primitives types are object types in Generally this is just syntactic sugar......however it can be used with polymorphism (next) Compiler Internal Declaration primitive float { float abs() const; float floor() const; float sqrt() const; // etc.

Polymorphism - Structural Typing Class class ExampleClass { static default(); int getvalue() const; void setvalue(int value); Interface interface ExampleInterface { int getvalue() const; void setvalue(int value); No need for explicit A implements B If a class has the required methods, then it implements the interface Useful if defining interfaces after classes Works for any type (e.g. int or float) Polymorphism int postincrement(exampleinterface& object) { int value = object.getvalue(); object.setvalue(value + 1); return value; void example() { auto instance = ExampleClass(); int value = postincrement(instance); std::print( Was %0, now %1.\n.format( value, instance.getvalue()));

Polymorphism Implementation Fixed size hash table with stubs to resolve conflicts (using a hidden parameter) Simply load the function pointer at slot HASH(name) % NUM_SLOTS and call it Relative to C++ virtual call: In case of collision: Very low overhead (see x86 code below for an example) No collision: No overhead (other than assigning hidden parameter) vtable Pointer Header Interface Method Table Slots 0 1 2 3 4 5 6 7 8 9... x86 Conflict Resolution resolve_stub_slot_1: cmp eax, <hash value> bne.jump_fn_1.jump_fn_0: jmp firstfunction.jump_fn_1: jmp secondfunction eax used as hidden parameter

Templates Generalise a class or function Not for compile-time evaluation; use forceeval for that Interfaces specify type parameter constraints template <typename T> interface Copyable { T copy() const; template <typename T: Copyable<T>> T copyvalue(const T& value) { return value.copy();

Compile-Time Evaluation Similar to run-time evaluation forceeval forces compile-time evaluation // Admittedly bad way to // calculate fibonacci numbers. int fib(size_t i) { return i == 0? 0 : i == 1? 1 : fib(i 2) + fib(i 1); void example() { int fib30 = forceeval fib(30); std::print( fib(30) = %0.format(fib30));

Algebraic Datatypes Influenced by functional languages Effectively a combination of enum, struct and union template <typename T> datatype Maybe = Something(T value) Nothing; template <typename T> void whatisit(maybe<t> maybe) { switch (maybe) { case Something(_) { std::print( It's something! ); case Nothing { std::print( It's nothing... );

Algebraic Datatypes (1) Can split functions by pattern matching using namespace std; interface stringable { string tostring(); string tostring(nothing) { return Nothing ; template <typename T: stringable> string tostring(something<t> something) { return Something(%0).format(something.value); void example() { print( Got %0.format(Something<int>(42)));

BONUS Virtual Template Parameters Template typename only accepts concrete type parameters virtual typename accepts concrete or abstract parameters interface BasicType { void dosomethings(); interface AdvancedType { void dosomethings(); void doallthings(); template <virtual typename T: BasicType> void calldosomethings(t& object) { object.dosomethings(); void example(advancedtype& object) { calldosomethings(object);

BONUS Virtual Template Parameters (1) Enables covariant and contravariant implicit casts Useful for reference types ( has smart references) template <virtual typename T> class cov { T& get() const; template <virtual typename T> class ctv { void set(t&); // Covariance. cov<basictype> f0(cov<advancedtype> arg) { return arg; // Contravariance. ctv<advancedtype> f1(ctv<basictype> arg) { return arg;

Status Around half of features implemented Compiler currently generates LLVM assembly Clang used for target info (e.g. sizeof(int)) Source File Source File Compiler (Parser, SemAnalysis, CodeGen, etc.) Clang... LLVM Assembly LLVM

Questions?