54
Stanley B. Lippman Stanley B. Lippman [email protected] [email protected] Evolving C++ onto the CLI Environment Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model Integrating a Static and Dynamic Programming Model

Stanley B. Lippman [email protected] Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Embed Size (px)

Citation preview

Page 1: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Stanley B. LippmanStanley B. [email protected]@microsoft.com

Evolving C++ onto the CLI EnvironmentEvolving C++ onto the CLI EnvironmentIntegrating a Static and Dynamic Programming ModelIntegrating a Static and Dynamic Programming Model

Page 2: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

How Should We Think aboutHow Should We Think aboutProgramming Languages?Programming Languages?

I use to think about them under the conceit of I use to think about them under the conceit of Aristotle and Copernicus cosmology with these Aristotle and Copernicus cosmology with these forming two poles of a continuum …forming two poles of a continuum …

AristotleanAristotlean languages impose a languages impose a top-down visiontop-down vision independent of the actual machine technology of the day independent of the actual machine technology of the day … … CLU, Scheme, SelfCLU, Scheme, Self

CopernicanCopernican languages emerge from a languages emerge from a bottom-up bottom-up discoverydiscovery of the actual machine technology of the day … of the actual machine technology of the day … FORTRAN, C/C++/JavaFORTRAN, C/C++/Java

And you could then cluster the other languages – And you could then cluster the other languages – CLOS, Smalltalk, Ada – along the continuum … CLOS, Smalltalk, Ada – along the continuum …

Page 3: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

How Should We Expect to be How Should We Expect to be Thought of as Programmers?Thought of as Programmers?

Under this conceit, one would expect Under this conceit, one would expect the Copernican languages to be widely used, butthe Copernican languages to be widely used, but the Aristotlean languages to be widely admired.the Aristotlean languages to be widely admired.

the Copernican languages to come out of industry [labor], the Copernican languages to come out of industry [labor], butbut

the Aristotlean languages to come from the academy the Aristotlean languages to come from the academy [thought].[thought].

the Copernican programmer to be considered less intelligent the Copernican programmer to be considered less intelligent and therefore less worthy of respect, andand therefore less worthy of respect, and

the Aristotlean programmer to be … well, you get the idea …the Aristotlean programmer to be … well, you get the idea …

Admittedly, this only gets us so far … but I see Admittedly, this only gets us so far … but I see nothing in the history of programming to invalidate nothing in the history of programming to invalidate the conceit. the conceit.

Page 4: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Let’s Step Back a MomentLet’s Step Back a Moment

One of the cool things about computer science is One of the cool things about computer science is that it is a very young field – therefore, we can that it is a very young field – therefore, we can hold it in our mind’s eye – that is, if we constrain hold it in our mind’s eye – that is, if we constrain it to the digital era:it to the digital era:

Forget Leibniz, although he was a genius and a visionary, Forget Leibniz, although he was a genius and a visionary, and deserved better from Newton and the Royal Society.and deserved better from Newton and the Royal Society.

Forget Babage, although he came very very close, and Forget Babage, although he came very very close, and Forget Ada Lovelace, although she provides an attractive Forget Ada Lovelace, although she provides an attractive

alternative to the image of the programmer as a soulless alternative to the image of the programmer as a soulless nerd.nerd.

So, what was the language used by the ENIAC So, what was the language used by the ENIAC computer, which I am going to start from? computer, which I am going to start from?

Page 5: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

There Was No LanguageThere Was No Language

Oddly enough, the idea of an independent Oddly enough, the idea of an independent program – let alone the idea of a program – let alone the idea of a programming language – wasn’t part of the programming language – wasn’t part of the original invention of the computer. original invention of the computer.

Rather, cables were plugged into one Rather, cables were plugged into one configuration for configuration for thatthat formulation or formulation or reconfigured for reconfigured for thisthis formulation, and so on. formulation, and so on.

This is where the term tight coupling originated … This is where the term tight coupling originated …

Page 6: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Programs Dwell in aPrograms Dwell in aComputational EnvironmentComputational Environment

The The modernmodern computing era computing era beganbegan without the concept without the concept of either a program or a programming language. of either a program or a programming language.

Of course, a transcription of a mathematical formula Of course, a transcription of a mathematical formula into a format within the computer was necessary, but into a format within the computer was necessary, but

it was not thought of as a symbolic program notation it was not thought of as a symbolic program notation there was no concept of inventing a language … there was no concept of inventing a language … there was no such entity thought of as a computer programmer there was no such entity thought of as a computer programmer

……

The immediately intractable problems were hardware – The immediately intractable problems were hardware – could the vacuum tubes persist long enough to maintain a could the vacuum tubes persist long enough to maintain a

computation.computation. the math was done in base 10 …the math was done in base 10 …

Page 7: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Von Neumann Hijacked the ENVACVon Neumann Hijacked the ENVAC

The successor to ENIAC was the EDVAC.The successor to ENIAC was the EDVAC.

Von Neumann arranged to be taken on as a consultant to the Von Neumann arranged to be taken on as a consultant to the ENIAC project…. He now become an avid supporter of the Moore ENIAC project…. He now become an avid supporter of the Moore School’s work, legitimizing it in the eyes of the scientific School’s work, legitimizing it in the eyes of the scientific establishment, and he was helpful in its getting the EDVAC establishment, and he was helpful in its getting the EDVAC contract.contract.

On June 30, 1945, a 101-page document arrived at the Moore On June 30, 1945, a 101-page document arrived at the Moore School from von Neumann….Von Neumann’s paper on EDVAC School from von Neumann….Von Neumann’s paper on EDVAC was replete with references to neurons and other parts of the was replete with references to neurons and other parts of the human nervous system, comparing them to the automatic human nervous system, comparing them to the automatic computer… The machine he described had a stored, computer… The machine he described had a stored, programmable memory.programmable memory.

Again, the ENIAC computer had no concept of a stored program, Again, the ENIAC computer had no concept of a stored program, let alone a programming language. It cost $486,804.22. We’re let alone a programming language. It cost $486,804.22. We’re told ittold it

solved problems in 15 seconds that would have required several solved problems in 15 seconds that would have required several weeks’ work by a trained main. weeks’ work by a trained main.

solved in 2 hours a problem which would have taken 100 trained men solved in 2 hours a problem which would have taken 100 trained men a year to solve manually.a year to solve manually.

Page 8: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Programs and Program Languages Programs and Program Languages A Dialectic with the Computational A Dialectic with the Computational

EnvironmentEnvironment The introduction of the program solved The introduction of the program solved logisticallogistical

bottlenecks of the pre-existing computational bottlenecks of the pre-existing computational environment … environment …

Trade-off of decoupling the processing from the Trade-off of decoupling the processing from the program:program:

Faster, `automatic’ loading of the programFaster, `automatic’ loading of the program the need to invent and implement a software abstraction the need to invent and implement a software abstraction

layer … in this case, a loader …layer … in this case, a loader …

This decoupling has been accelerating …This decoupling has been accelerating …

Page 9: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The Evolution of Complex Structure …The Evolution of Complex Structure …

Software did not begin as software – it was a hard-wired Software did not begin as software – it was a hard-wired configuration – a dance without a separate dance notation. configuration – a dance without a separate dance notation.

This evolved into a reproducible program This evolved into a reproducible program bitbit mapmap that could that could be loaded and flushed from memory. A purely numeric be loaded and flushed from memory. A purely numeric representation.representation.

The first abstraction level, in a sense, was the use of The first abstraction level, in a sense, was the use of hexidecimal over binary … hexidecimal over binary …

The assembler formed a nucleus of a symbolic The assembler formed a nucleus of a symbolic representation of a program but still at the level of representation of a program but still at the level of individual instructions that could be grouped by function. A individual instructions that could be grouped by function. A mnemonic representation.mnemonic representation.

This was controversial and Grace Hopper reports that it was This was controversial and Grace Hopper reports that it was resisted by a portion of the very small number of programmers resisted by a portion of the very small number of programmers who felt it was getting too far from the machine.who felt it was getting too far from the machine.

At each stage, more software complexity is introduced between At each stage, more software complexity is introduced between the program representation and the machine.the program representation and the machine.

Page 10: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The Invention of Programming Languages The Invention of Programming Languages Created a Tradition of Uncivility …Created a Tradition of Uncivility …

FORTRAN, of course, was a proof of concept that we could FORTRAN, of course, was a proof of concept that we could program as a higher level of abstraction and still generate program as a higher level of abstraction and still generate efficient code … efficient code …

They eliminated aspects of the design if it proved too They eliminated aspects of the design if it proved too difficult to compile … this in itself was proof against its difficult to compile … this in itself was proof against its purity of design …purity of design …

FORTRAN also began the language wars …FORTRAN also began the language wars … The disappointed ALGOL team described it as graffitti The disappointed ALGOL team described it as graffitti

written on a bathroom wallwritten on a bathroom wall They attributed its success to the 800-lb guerilla that was They attributed its success to the 800-lb guerilla that was

IBMIBM

These kinds of battles have never ceased – These kinds of battles have never ceased – they’ve just changed as the languages have. they’ve just changed as the languages have. Why?Why?

Page 11: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

A Darwinian Way to Think about A Darwinian Way to Think about Programming Languages Programming Languages

Programming languages are a response to Programming languages are a response to a particular computational environment:a particular computational environment:

facilitates expression within a current facilitates expression within a current environment …environment …

improves on one or a set of existing program improves on one or a set of existing program solutions …solutions …

provides a vocabulary and shared point of view provides a vocabulary and shared point of view … a community… a community

Page 12: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

All Languages Become Extinct …All Languages Become Extinct …

As the computational environment changes, the As the computational environment changes, the more specialized the language to the previous more specialized the language to the previous computational environment, the less adaptive it computational environment, the less adaptive it proves in the new environment …proves in the new environment …

But the historical accumulation of structure But the historical accumulation of structure seems to overwhelm these efforts.seems to overwhelm these efforts.

The conditions that give rise to a language leads The conditions that give rise to a language leads to its eventual extinction …to its eventual extinction …

There are many more extinct There are many more extinct

than active programming languagesthan active programming languages

Page 13: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

All Languages Compete for Scarce Resources …All Languages Compete for Scarce Resources …

Although a language is not an organism, there is a Although a language is not an organism, there is a continual struggle for survival among its continual struggle for survival among its population …population …

There is a competition for finite budgetary resources to There is a competition for finite budgetary resources to feed new projects and sustain existing one …feed new projects and sustain existing one …

There is competition to reproduce in the minds of a new There is competition to reproduce in the minds of a new generation of programmers.generation of programmers.

Language wars are virtually bloody both in tooth and clawLanguage wars are virtually bloody both in tooth and claw

Page 14: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

All Languages Resist Extinction …All Languages Resist Extinction …

Typically, the language leaders at some point Typically, the language leaders at some point cease resisting and attempt to readapt the cease resisting and attempt to readapt the language to the changing environment … language to the changing environment …

this however may backfire … emphasizing its this however may backfire … emphasizing its current maladaption to the new environment …current maladaption to the new environment …

The population of a language constricts when it fails to The population of a language constricts when it fails to reproduce in the minds of the new members of the reproduce in the minds of the new members of the community community

Dropping below a certain threshold, it no longer has the Dropping below a certain threshold, it no longer has the critical mass to command finite budgetary resourcescritical mass to command finite budgetary resources

It is relegated to unique niche environments – the deserts It is relegated to unique niche environments – the deserts and swamps of the software development landscape …and swamps of the software development landscape …

Page 15: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

So, Where Is This Leading Us?So, Where Is This Leading Us?

1.1. The Common Language Infrastructure (CLI) is a The Common Language Infrastructure (CLI) is a major consolidation of thoughts about a software major consolidation of thoughts about a software abstraction layer between the program and the abstraction layer between the program and the Operating System. Operating System.

This is not in itself new – Smalltalk carried its own This is not in itself new – Smalltalk carried its own environment, and Java targets its own virtual machine. environment, and Java targets its own virtual machine.

What is new is its inclusiveness: it supports over 30 What is new is its inclusiveness: it supports over 30 languages ... It rightly diminishes the focus on languages languages ... It rightly diminishes the focus on languages ……

This is what we look at briefly in the next section.This is what we look at briefly in the next section.

Page 16: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

So, Where Is This Leading Us?So, Where Is This Leading Us?

2.2. C++/CLI is an adaptation of ISO-C++ to the C++/CLI is an adaptation of ISO-C++ to the dynamic programming object model of thedynamic programming object model of the CLI CLI. It . It follows a tradition of C++ adaptations:follows a tradition of C++ adaptations:

C with Classes (~1979) (ADT)C with Classes (~1979) (ADT) Object-Oriented Programming (~1984) (OO)Object-Oriented Programming (~1984) (OO) Generic Programming (~1991) (Templates)Generic Programming (~1991) (Templates) Dynamic Programming (~2005) (CLI)Dynamic Programming (~2005) (CLI)

This is what we look at in the last section of this talkThis is what we look at in the last section of this talk

Page 17: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Therefore, it puts existing languages at risk, and Therefore, it puts existing languages at risk, and provides an opportunity for new languages to thrive.provides an opportunity for new languages to thrive.

The CLI Changes The CLI Changes

the Computational Environmentthe Computational Environment

Page 18: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

A Note on TerminologyA Note on Terminology

I will be speaking of both the CLI and CLR as kind of I will be speaking of both the CLI and CLR as kind of counterpoints of one another. Here is their formal counterpoints of one another. Here is their formal relationship …relationship …

1.1. Common Language Infrastructure (CLI)Common Language Infrastructure (CLI)

This is an ECMA/ISO platform-independent standard. It This is an ECMA/ISO platform-independent standard. It represents the abstract facilities/architecture.represents the abstract facilities/architecture.

2.2. Common Language Runtime (CLR)Common Language Runtime (CLR)

This is the Windows Operating System implementation This is the Windows Operating System implementation of the CLI. This is what we mean when we speak of the CLI. This is what we mean when we speak of .NET …of .NET …

Page 19: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The CLI/CLR Provides a VESThe CLI/CLR Provides a VES A Virtual Execution System (VES) provides an environment for A Virtual Execution System (VES) provides an environment for

executing managed codeexecuting managed code

It provides a software layer between the managed code and It provides a software layer between the managed code and the native operating system.the native operating system.

It is responsible for loading and running programs …It is responsible for loading and running programs …

It provides the services needed to execute managed code It provides the services needed to execute managed code and data …and data …

Garbage collection, for example, is an aspect of the VES, Garbage collection, for example, is an aspect of the VES, not of a particular language …not of a particular language …

Page 20: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Architectural OverviewArchitectural Overview

Co

mm

on

Lan

gu

age

Ru

nti

me

Co

mm

on

Lan

gu

age

Ru

nti

me

FrameworksFrameworks

Class loader and layoutClass loader and layout

IL t

o

IL t

o

nat

ive

cod

e n

ativ

e co

de

com

pile

rsco

mp

ilers

GC, stack walk, code managerGC, stack walk, code manager

Sec

uri

tyS

ecu

rity

Exe

cuti

on

Exe

cuti

on

Su

pp

ort

Su

pp

ort

Base ClassesBase Classes

Page 21: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The CLI/CLR Provides a CILThe CLI/CLR Provides a CIL

Each CLI language is compiled down into a Common Each CLI language is compiled down into a Common Intermediate Language (CIL) based on a stack program model.Intermediate Language (CIL) based on a stack program model.

All tools ideally work off of the CIL, and are therefore All tools ideally work off of the CIL, and are therefore shared across all languges … browsers, debuggers, and so shared across all languges … browsers, debuggers, and so on. on. New tools target the CIL, not a language …New tools target the CIL, not a language …

Metadata is generated in parallel describing both the Metadata is generated in parallel describing both the program and its environment … this allows automation of program and its environment … this allows automation of many previously manual `plumbing’ …many previously manual `plumbing’ …

An extensive object-oriented Base Class Library (BCL) An extensive object-oriented Base Class Library (BCL) framework is shared across all CLI languages …framework is shared across all CLI languages …

Page 22: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

MetadataMetadata The Lifeblood of the CLI The Lifeblood of the CLI

MetadataMetadata(and code)(and code)

DebuggerDebugger

Schema Schema GeneratorGenerator

ProfilerProfiler

OtherOtherCompilerCompiler

Proxy GeneratorProxy Generator

Type BrowserType Browser

CompilerCompiler

SourceSourceCodeCode

XML encodingXML encoding(WSDL)(WSDL)

SerializationSerialization(e.g. SOAP)(e.g. SOAP)

DesignersDesigners

ReflectionReflection

Page 23: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The CLI Provides a CTSThe CLI Provides a CTS

The Common Language Infrastructure (CLI) defines a The Common Language Infrastructure (CLI) defines a Common Type System (CTS) over which all CLI Common Type System (CTS) over which all CLI languages are built.languages are built.

A unified type system rooted in an Object base A unified type system rooted in an Object base class. class. All types and literal values have an underlying All types and literal values have an underlying

class representationclass representation All types are guaranteed to be a kind of Object All types are guaranteed to be a kind of Object

and share a common set of operationsand share a common set of operations All types can be converted to an instance of type All types can be converted to an instance of type

Object.Object. All types have an associated Type class that All types have an associated Type class that

provides runtime reflection support. provides runtime reflection support.

Page 24: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The CLI Provides a CTSThe CLI Provides a CTS

The Common Language Infrastructure (CLI) defines a The Common Language Infrastructure (CLI) defines a Common Type System (CTS) over which all CLI languages Common Type System (CTS) over which all CLI languages are built.are built.

Separation of class types based on Separation of class types based on behavior/design charactertistics:behavior/design charactertistics: Reference class is polymorphic: supports OO Reference class is polymorphic: supports OO

designdesign Value class is blitable: supports small, efficient Value class is blitable: supports small, efficient

independent typesindependent types Interface class is abstract: supports defining Interface class is abstract: supports defining

families of servicesfamilies of services

Page 25: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The CLI Provides a CTSThe CLI Provides a CTS

A secondary set of types including numeric A secondary set of types including numeric types, a delegate type, an event type, an types, a delegate type, an event type, an array type, an enum type … array type, an enum type …

A single class inheritance model with A single class inheritance model with support for multiple interface inheritance support for multiple interface inheritance

Each CLI language generally exposes these Each CLI language generally exposes these to the programmer as built-in language to the programmer as built-in language facilities … this is a first order design facilities … this is a first order design aspect of building a CLI language.aspect of building a CLI language.

Page 26: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The CLI Provides a CLSThe CLI Provides a CLSThe Common Language Specification (CLS) The Common Language Specification (CLS)

defines a set of restrictions on the Common Type defines a set of restrictions on the Common Type System (CTS) to ensure interoperability among System (CTS) to ensure interoperability among CLI languages.CLI languages.

These rules apply to These rules apply to types that are visible in assemblies other than those in types that are visible in assemblies other than those in

which they are defined.which they are defined. Members that are accessible outside the assembly.Members that are accessible outside the assembly.

CLS-compliant code is guaranteed to be both CLS-compliant code is guaranteed to be both consumable and inheritable by all CLI languages. consumable and inheritable by all CLI languages.

The canonical example of a CLS constraint is to prohibit The canonical example of a CLS constraint is to prohibit unsigned integral values as part of the public interface …unsigned integral values as part of the public interface …

Page 27: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Adapting C++ to this Adapting C++ to this New EnvironmentNew Environment

Page 28: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

C++/CLI represents a tuple …C++/CLI represents a tuple …

C++C++

The first term, The first term, C++C++, refers of course to the , refers of course to the C++C++ programming language invented by Bjarne programming language invented by Bjarne Stroustrup at Bell Laboratories. Stroustrup at Bell Laboratories.

It supports a static object model that is It supports a static object model that is optimized for the speed and size of its optimized for the speed and size of its executables. executables.

It does not support run-time modification of the It does not support run-time modification of the program other than, of course, heap allocation. program other than, of course, heap allocation.

It allows unlimited access to the underlying It allows unlimited access to the underlying machine, but very little access to the types machine, but very little access to the types active in the running program and no real access active in the running program and no real access to the associated infrastructure of that program. to the associated infrastructure of that program.

Page 29: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

C++/CLI represents a tuple …C++/CLI represents a tuple …

CLICLI

The third term, The third term, CLICLI, refers to the , refers to the CommonCommon LanguageLanguage InfrastructureInfrastructure, a multi-tiered architecture supporting , a multi-tiered architecture supporting a a dynamicdynamic componentcomponent programming model. programming model.

In many ways, this represents a complete reversal of In many ways, this represents a complete reversal of the C++ object model.the C++ object model.

A runtime software layer, the virtual execution A runtime software layer, the virtual execution system, runs between the program and the system, runs between the program and the underlying operating system. underlying operating system.

Access to the underlying machine is fairly Access to the underlying machine is fairly constrained. constrained.

Access to the types active in the executing program Access to the types active in the executing program and the associated program infrastructure – both as and the associated program infrastructure – both as discovery and construction – is supported.discovery and construction – is supported.

Page 30: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

C++/CLI represents a tuple …C++/CLI represents a tuple …

//

The second term, slash (The second term, slash (/), /), represents a represents a bindingbinding between C++ and the CLI. between C++ and the CLI.

So, a first approximation of an answer as to So, a first approximation of an answer as to what is what is C++/CLIC++/CLI is to say that it is a binding is to say that it is a binding of the static of the static C++C++ object model with the object model with the dynamic component object model of the dynamic component object model of the CLICLI..

The design of this binding is the focus of The design of this binding is the focus of the rest of this talk.the rest of this talk.

Page 31: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The Architectural UnderpinningThe Architectural Underpinning

The Design of C++/CLIThe Design of C++/CLI

Page 32: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The 3 Elements of a CLI LanguageThe 3 Elements of a CLI Language

There are three aspects in the design of a CLI There are three aspects in the design of a CLI language that hold across all languages. language that hold across all languages.

1.1. A mapping of language level syntax to the underlying A mapping of language level syntax to the underlying Common Type System.Common Type System.

2.2. A choice of a level of detail to expose of the underlying A choice of a level of detail to expose of the underlying CLI infrastructure to the direct manipulation of the CLI infrastructure to the direct manipulation of the programmer.programmer.

3.3. A choice of what additional functionality to provide over A choice of what additional functionality to provide over that supported directly by the CLI that supported directly by the CLI

Item #1 is largely the same across all CLI languages. Item #1 is largely the same across all CLI languages. Items #2 and #3 are what distinguish one CLI Items #2 and #3 are what distinguish one CLI language from another.language from another.

I like to think of these three items as representing I like to think of these three items as representing coordinates positioning each language in a three-coordinates positioning each language in a three-dimensional design space supported by the CLI.dimensional design space supported by the CLI.

Page 33: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Mapping to the Common Type Mapping to the Common Type SystemSystem

This design aspect is common to all CLI languages – This design aspect is common to all CLI languages – the syntax of course varies. So, for example, the syntax of course varies. So, for example,

public abstract class Shape {…} // C#public abstract class Shape {…} // C#

public ref class Shape abstract {…}; // C++/CLIpublic ref class Shape abstract {…}; // C++/CLI

Shape s = new Cube(); // C#Shape s = new Cube(); // C#

Shape^ s = gcnew Cube; // C++/CLIShape^ s = gcnew Cube; // C++/CLI

represents the C# and C++/CLI support to define an represents the C# and C++/CLI support to define an abstract CLI reference class and allocate a derived abstract CLI reference class and allocate a derived instance on the CLI heap. instance on the CLI heap.

Our choice of syntax is based on an attempt to Our choice of syntax is based on an attempt to closely integrate the CLI class types with that of ISO-closely integrate the CLI class types with that of ISO-C++. C++.

Page 34: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The C++/CLI TypesThe C++/CLI Types

ref class R abstract {}; ref class R abstract {};

value class V{}; value class V{};

interface class I{}; interface class I{};

ref class R2 : R, I {};ref class R2 : R, I {};

enum class E : short { e1, e2 };enum class E : short { e1, e2 };

delegate void D( signature );delegate void D( signature );

event D handler;event D handler;

array< T, dim > a;array< T, dim > a;

Page 35: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Level of DetailLevel of Detail

The second design aspect reflects the level The second design aspect reflects the level of detail of the underlying CLR of detail of the underlying CLR implementation model to incorporate into implementation model to incorporate into the language. the language.

How does one go about determining this?How does one go about determining this? What are the kinds of problems the language is What are the kinds of problems the language is

likely to be tasked to solve?likely to be tasked to solve? What are the kinds of programmers the language is What are the kinds of programmers the language is

likely to attract and be used by?likely to attract and be used by?

Let’s look at an example: the issue of value Let’s look at an example: the issue of value types occurring on the managed heap.types occurring on the managed heap.

Page 36: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Value Types on the Managed HeapValue Types on the Managed Heap

Value types can find themselves on the Value types can find themselves on the managed heap in a number of circumstances:managed heap in a number of circumstances:

Implicit boxing Implicit boxing we assign an object of a value type to an Object we assign an object of a value type to an Object we invoke a virtual method through a value type that is we invoke a virtual method through a value type that is

not overriddennot overridden

When a value type serves as a member of a reference When a value type serves as a member of a reference class type.class type.

When a value type is being stored as the element type When a value type is being stored as the element type of a CLI array.of a CLI array.

The design question a CLI language has to ask The design question a CLI language has to ask is,is,

should we allow the programmer to manipulate the should we allow the programmer to manipulate the address of a value type of this sort?address of a value type of this sort?

Page 37: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

What Are the Issues?What Are the Issues?

Any object located on the managed heap is Any object located on the managed heap is subject to relocation during the compaction subject to relocation during the compaction phase of a sweep of the garbage collector. phase of a sweep of the garbage collector.

Any pointers to that object must be tracked Any pointers to that object must be tracked and updated by the runtime; the and updated by the runtime; the programmer has no way to manually track it programmer has no way to manually track it herself. herself.

Therefore, if we were to allow the Therefore, if we were to allow the programmer to take the address of a value programmer to take the address of a value type potentially resident on the managed type potentially resident on the managed heap, we would need to introduce a heap, we would need to introduce a trackingtracking form of pointer in addition to the existing form of pointer in addition to the existing native pointer. native pointer.

Page 38: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

What Are the Trade-Offs?What Are the Trade-Offs?Simplicity and Safety on the One HandSimplicity and Safety on the One Hand

Directly introducing support in the language Directly introducing support in the language for one or a family of tracking pointers for one or a family of tracking pointers makes it a more complicated language. makes it a more complicated language.

By not supporting this, we expand the available By not supporting this, we expand the available pool of programmers to hire from by requiring less pool of programmers to hire from by requiring less sophistication.sophistication.

Allowing the programmer access to these Allowing the programmer access to these ephemeral value types increases the ephemeral value types increases the possibility of programmer error – she may possibility of programmer error – she may purposely or by accident do bad things to purposely or by accident do bad things to the memory. the memory.

By not supporting this, we create a potentially By not supporting this, we create a potentially safer runtime environment.safer runtime environment.

Page 39: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

What Are the Trade-Offs?What Are the Trade-Offs?Efficiency and Flexibility on the Other HandEfficiency and Flexibility on the Other Hand

Each time we assign the same Object with a Each time we assign the same Object with a value type, a new boxing of the value occurs value type, a new boxing of the value occurs … …

Allowing access to the boxed value type allows in-Allowing access to the boxed value type allows in-memory update, which may provide significant memory update, which may provide significant performance …performance …

Without a form of tracking pointer, we Without a form of tracking pointer, we cannot iterate over a CLI array using pointer cannot iterate over a CLI array using pointer arithmetic. This means that the CLI array arithmetic. This means that the CLI array cannot participate in the STL iterator cannot participate in the STL iterator pattern and work with the generic pattern and work with the generic algorithms. algorithms.

Allowing access to the boxed value type allows Allowing access to the boxed value type allows significant design flexibility.significant design flexibility.

Page 40: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The Level of Detail The Level of Detail Reflects the Target ProgrammerReflects the Target Programmer

We chose to provide a collection of We chose to provide a collection of addressing modes that handle value types addressing modes that handle value types on the managed heap.on the managed heap.

int ival = 1024;int ival = 1024; int^int^ boxedi = ival; boxedi = ival;

array<int>^ ia = gcnew array<int>{1,1,2,3,5,8};array<int>^ ia = gcnew array<int>{1,1,2,3,5,8}; interior_ptr<int>interior_ptr<int> begin = &ia[0];begin = &ia[0];

value struct smallInt { int m_ival; … } si;value struct smallInt { int m_ival; … } si; pin_ptr<int> ppipin_ptr<int> ppi = &si.m_ival;= &si.m_ival;

Page 41: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

A Language Layer over the CLIA Language Layer over the CLI

A third design aspect is an language-specific A third design aspect is an language-specific layer of functionality over that directly layer of functionality over that directly supported by the CLI. supported by the CLI.

This requires a mapping between the language-level This requires a mapping between the language-level support and the underlying CLI …support and the underlying CLI …

Or it may be handled by tagging a type with a Or it may be handled by tagging a type with a language-specific attribute discoverable at run-time language-specific attribute discoverable at run-time ……

In some cases, it just isn’t possible …In some cases, it just isn’t possible … value types are blitted …value types are blitted … virtual function resolution within ctors/dtors …virtual function resolution within ctors/dtors …

So this is a compromise between what we So this is a compromise between what we might wish to do, and what we find ourselves might wish to do, and what we find ourselves able to do. able to do.

Page 42: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Three General Categories of Three General Categories of ExtensionExtension

1.1. a form of a form of Resource Acquisition is InitializationResource Acquisition is Initialization (RAII) for reference types. In particular, to provide (RAII) for reference types. In particular, to provide an automated facility for deterministic finalization an automated facility for deterministic finalization of garage collected types that hold scarce of garage collected types that hold scarce resources.resources.

2.2. a form of deep-copy semantics associated with the a form of deep-copy semantics associated with the C++ copy constructor and copy assignment C++ copy constructor and copy assignment operator – this could not be extended to value operator – this could not be extended to value types. types.

3.3. Direct support C++ templates for CLI types in Direct support C++ templates for CLI types in addition to the CLI generic support.addition to the CLI generic support.

Page 43: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Non-Deterministic FinalizationNon-Deterministic Finalization

Before the memory associated with an object is Before the memory associated with an object is reclaimed by the garbage collector, an reclaimed by the garbage collector, an associated associated Finalize()Finalize() method, if present, is method, if present, is invoked. invoked.

You can think of this method as a kind of super-You can think of this method as a kind of super-destructor since it is not tied to the program destructor since it is not tied to the program lifetime of the object. lifetime of the object.

We refer to this as We refer to this as finalizationfinalization. The timing of . The timing of just when or even whether a just when or even whether a Finalize()Finalize() method method is invoke is undefined. is invoke is undefined.

This is what is meant when we say that garbage This is what is meant when we say that garbage collection exhibits collection exhibits nonnon--deterministicdeterministic finalizationfinalization..

Page 44: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

The Problem of Scarce ResourcesThe Problem of Scarce Resources

Non-deterministic finalization works well with dynamic Non-deterministic finalization works well with dynamic memory management. When available memory gets memory management. When available memory gets sufficiently scarce, the garbage collector kicks in and things sufficiently scarce, the garbage collector kicks in and things pretty much just work. pretty much just work.

Non-deterministic finalization does not work well, however, Non-deterministic finalization does not work well, however, when an object maintains a critical resource such as a when an object maintains a critical resource such as a database connection, a lock of some sort, or perhaps native database connection, a lock of some sort, or perhaps native heap memory.heap memory.

In this case, we would like to release the resource as soon In this case, we would like to release the resource as soon as it is no longer needed. The solution currently supported as it is no longer needed. The solution currently supported by the CLI is for a class to free the resources in its by the CLI is for a class to free the resources in its implementation of the implementation of the Dispose()Dispose() method of the method of the IDisposableIDisposable interface.interface.

The problem here is that The problem here is that Dispose()Dispose() requires an requires an explicitexplicit invocation, invocation, and therefore is liable not to be invoked.and therefore is liable not to be invoked.

Page 45: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Automating Disposal …Automating Disposal …

A fundamental design pattern in C++ is spoken of A fundamental design pattern in C++ is spoken of as resource acquisition is initialization. as resource acquisition is initialization. That is, a class acquires resources within its constructor.That is, a class acquires resources within its constructor. Conversely, a class frees its resources within its Conversely, a class frees its resources within its

destructor.destructor. This is managed automatically within the lifetime of the This is managed automatically within the lifetime of the

class object. class object.

This is what we would like to do with reference This is what we would like to do with reference types in terms of the freeing of scarce resources:types in terms of the freeing of scarce resources: Use the destructor to encapsulate the necessary code for Use the destructor to encapsulate the necessary code for

the freeing of any resources associated with the class.the freeing of any resources associated with the class. Have the destructor invoked automatically tied with the Have the destructor invoked automatically tied with the

lifetime of the class object.lifetime of the class object.

Page 46: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

A Two-Step Solution … Step 1A Two-Step Solution … Step 1

1.1. Mapping the Destructor to Dispose()Mapping the Destructor to Dispose() The CLI has no notion of the class destructor for a The CLI has no notion of the class destructor for a

reference type. So the destructor has to be mapped into reference type. So the destructor has to be mapped into something else in the underlying implementation.something else in the underlying implementation.

Internally, then, the compiler does the following Internally, then, the compiler does the following transformations:transformations: the class has its base class list extended to inherit from the the class has its base class list extended to inherit from the

IDisposable interface. IDisposable interface. the destructor is transformed into the the destructor is transformed into the Dispose()Dispose() method of method of

IDisposableIDisposable. . That get us half the way to our goal. We still need a way That get us half the way to our goal. We still need a way

to automate the invocation of the destructor.to automate the invocation of the destructor.

Page 47: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

A Two-Step Solution … Step 2A Two-Step Solution … Step 2

2.2. Mapping the object to a lifetimeMapping the object to a lifetime A special stack-based notation for a reference type is A special stack-based notation for a reference type is

supported; that is, one in which its lifetime is associated supported; that is, one in which its lifetime is associated within the scope of its declaration. within the scope of its declaration.

Internally, the compiler transforms the notation to Internally, the compiler transforms the notation to allocate the reference object on the managed heap. allocate the reference object on the managed heap.

With the termination of the scope, the compiler inserts a With the termination of the scope, the compiler inserts a invocation of the Dispose() method – the user-defined invocation of the Dispose() method – the user-defined destructor.destructor.

Reclamation of the actual memory associated with the Reclamation of the actual memory associated with the object remains under the control of the garbage collector.object remains under the control of the garbage collector.

Page 48: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

An Example An Example

ref class wrapper {ref class wrapper {

Native *pn;Native *pn;

public:public:

Wrapper( int val ) { pn = new Native( val ); } // RAIIWrapper( int val ) { pn = new Native( val ); } // RAII

~Wrapper(){ delete pn; }~Wrapper(){ delete pn; }

void foo();void foo();

protected:protected:

! Wrapper() { delete pn; }! Wrapper() { delete pn; }

};};

void f1() {void f1() { Wrapper^ w1 = gcnew Wrapper( 1024 );Wrapper^ w1 = gcnew Wrapper( 1024 );

Wrapper w2( 2048 ); // no ^ token !Wrapper w2( 2048 ); // no ^ token !w1->foo(); w2.foo();w1->foo(); w2.foo();

// … // … // w2 is disposed of here// w2 is disposed of here // w1 will be finalized at some point// w1 will be finalized at some point}}

Page 49: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Programming Languages are over-Programming Languages are over-emphasized, much as national identity …emphasized, much as national identity …

The CLI Represents The CLI Represents

a Language Frameworka Language Framework

Page 50: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Language as a Unit of DeploymentLanguage as a Unit of Deployment

A language is often used as a vehicle for the A language is often used as a vehicle for the deployment of a new programming model – deployment of a new programming model – that is, of a new paradigm. that is, of a new paradigm. It tends to demonstratively improve on existing It tends to demonstratively improve on existing

models that have run into some bottleneck of models that have run into some bottleneck of scale. scale.

Or it supports a new model either of technology or Or it supports a new model either of technology or abstraction. abstraction.

These languages tend to be pure – that is, to These languages tend to be pure – that is, to provide support for its program model only. provide support for its program model only. This makes the language both simpler and more This makes the language both simpler and more

elegant.elegant. It requires a relinquishment of the pastIt requires a relinquishment of the past

Page 51: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

In Like a Lion, Out Like a LambIn Like a Lion, Out Like a Lamb

When there is a reinvention of the dominant When there is a reinvention of the dominant program model, there is also a programming program model, there is also a programming language extinction.language extinction. The current generation of languages has no The current generation of languages has no

vocabulary to directly express the new model.vocabulary to directly express the new model. Adding that vocabulary compromises the elegance of Adding that vocabulary compromises the elegance of

the original purity of design.the original purity of design. A pure language moves from a youthful development A pure language moves from a youthful development

community to a acknowledged design influence. community to a acknowledged design influence. This passionate sweeping in and hangdog slinking out This passionate sweeping in and hangdog slinking out

of programming languages has taken its toll socially of programming languages has taken its toll socially on the professional programmer class. on the professional programmer class.

This is not really working, imo. What kind of solutions This is not really working, imo. What kind of solutions suggests themselves?suggests themselves?

Page 52: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Where the CLI Comes inWhere the CLI Comes in

However, there is a possible language However, there is a possible language model we can glean from C++.model we can glean from C++. What has been What has been surprisinglysurprisingly successful for C++ has successful for C++ has

been its ability to support multiple program been its ability to support multiple program models.models.

What has been less successful is the absence of a What has been less successful is the absence of a unifying architecture and crafted boundaries.unifying architecture and crafted boundaries.

Well, perhaps what we need is a conscious Well, perhaps what we need is a conscious design – a deliberate mosaic of component design – a deliberate mosaic of component paradigms using a common type system and paradigms using a common type system and virtual machine model.virtual machine model.

Oh, this is where the CLI comes in …Oh, this is where the CLI comes in …

Page 53: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

A Language FrameworkA Language Framework

The CLI seems to offer the glimmerings of a The CLI seems to offer the glimmerings of a framework for the design of a possible framework for the design of a possible mosaic of component language gems.mosaic of component language gems.

I would like to see you guys come up with a I would like to see you guys come up with a new paradigm of how we should program – new paradigm of how we should program – all two thousand of them. all two thousand of them.

There is so much hard work and invention There is so much hard work and invention that is required of us in the 21that is required of us in the 21stst century. century.

The university must delivery up the science; The university must delivery up the science; industry will deliver up the engineering.industry will deliver up the engineering.

Page 54: Stanley B. Lippman slippman@microsoft.com Evolving C++ onto the CLI Environment Integrating a Static and Dynamic Programming Model

Questions?Questions?

Concerns?Concerns?

Criticisms?Criticisms?

[email protected]@microsoft.com