Welcome to CS215 Introduction to 'NET C - PowerPoint PPT Presentation

About This Presentation
Title:

Welcome to CS215 Introduction to 'NET C

Description:

Topic 0: Introduction to .NET. What do ... incorporate it in the specific topic later. Discuss in the newsgroup. 9 ... Topic 1: Memory Management. Garbage ... – PowerPoint PPT presentation

Number of Views:239
Avg rating:3.0/5.0
Slides: 135
Provided by: kam77
Category:

less

Transcript and Presenter's Notes

Title: Welcome to CS215 Introduction to 'NET C


1
Welcome to CS215Introduction to .NET / C
  • Kamen Yotov
  • kyotov_at_cs.cornell.edu
  • Department of Computer Science
  • Cornell University

2
About the course
  • .NET / C interesting features
  • CLR
  • CTS
  • C
  • Prerequisites
  • CS211 (OO programming)

3
Administrative
  • 1 Credit
  • Website http//www.cs.cornell.edu/Courses/cs215
  • Newsgroup cornell.class.cs215
  • When MWF, 1220-1310, 09/02/02 - 09/27/02
  • Add/Drop Deadline 09/09/02
  • Instructor Kamen Yotov
  • Consultants/Graders
  • TBA
  • Office Hours
  • TBA

4
Assignments Grading
  • 4 assignments
  • Only programming (write-up inside)
  • Graded Pass/Fail
  • Demonstrations
  • Workarounds
  • Extra projects
  • More interesting stuff

5
Resources
  • Visual Studio.NET Academic
  • May be Ill use Academic features
  • Distributions will be available
  • Books (optional)
  • Introduction to C Using .NETRobert J.
    ObergPrentice Hall, ISBN 0-13-041801-3
  • Other
  • C Language Specificationhttp//msdn.microsoft.co
    m/vstudio/techinfo/articles/upgrade/Csharpdownload
    .asp
  • Websites
  • Rotor
  • Search for it in Google!

6
Topic 0Introduction to .NET
  • 09/02/02

7
Topic 0 Introduction to .NETChallenges
  • Development Which OS / Language?
  • Delivery Media, Licenses,
  • Stability DLLs mess, Security
  • Maintenance Who? What?
  • Services The new model

8
Topic 0 Introduction to .NETWhat is .NET
  • The combination of
  • Framework
  • Common language runtime (CLR)
  • Class libraries
  • ADO.NET, ASP.NET, ...
  • Web Services
  • .NET Enterprise Servers

9
Topic 0 Introduction to .NETWhat is .NET (cont.)
10
Topic 0 Introduction to .NETWhat is .NET (cont.)
11
Topic 0 Introduction to .NETWhat is the CLR?
  • Common type system
  • Mapping of data types. Programming language ?
    Framework
  • Just-in-time (JIT) compilers
  • JIT compiles intermediary language (MSIL) into
    native code
  • Highly optimized for platform or device
  • Garbage collector
  • Permission and policy-based security
  • Exceptions
  • Threading
  • Diagnostics and profiling

12
Topic 0 Introduction to .NETWhat is the CLR?
13
Topic 0 Introduction to .NETMore .NET
  • Namespaces and Classes
  • Hierarchical, unified class libraries
  • Unified and extensible provide system and base
    functionality and services
  • Object-oriented everything is an object!
  • The systems uses the same classes offered to you
  • Interfaces
  • The .NET (Service) contracts
  • Types
  • Byte, Sbyte, Single, Double, String, Int16, etc.
  • These all map to the common type system

14
Topic 0 Introduction to .NETWhat do you want to
know?
  • Tell me what you want to know about .NET
  • Ill try to incorporate it in the specific topic
    later
  • Discuss in the newsgroup

15
Topic 1Memory Management
  • 09/04/02

16
Topic 1 Memory ManagementDoing it manually
  • Allocate memory for a resource
  • Initialize the resource
  • Use the resource
  • Clean up the state of the resource
  • Free the allocated memory

17
Topic 1 Memory ManagementGarbage Collection
(Why?)
  • C/C applications often leak memory
  • Manual memory management
  • No clear rules for ownership of objects
  • COM fixes this partly through ref-counts
  • AddRef/Release calls must be balanced
  • SmartPointers don't catch all problems
  • Server applications must be leak free
  • Must run for months and years
  • Solution automatic memory management

18
Topic 1 Memory ManagementGarbage Collection
(Why not?)
  • Garbage Collection downside
  • Destructors called at some random future time
  • Finalization code is never synchronous
  • Breaks some established design patterns from C
  • Beware Code should never depend on destructors
    to free external resources.
  • Instead Implement IDisposable with using
  • using (File f new File("c\\xyz.xml"))
    ...
  • IDisposable.Dispose() called on object when scope
    is exited.

19
Topic 1 Memory ManagementGarbage Collection
(How?)
  • Developer creates new objects and data arrays
  • Everything created/allocated using new keyword
  • The .NET runtime tracks all memory usage
    automatically
  • GC automatically removes all unused objects
  • More efficient memory management
  • Ease of use and "zero memory leaks"

20
Topic 1 Memory ManagementGarbage Collection
(What?)
  • Garbage collection
  • Generations
  • Resurrection
  • Disposability (Unmanaged Resources)
  • Finalization
  • Weak References
  • Assignment 1
  • Study the GarbageCollection demoltFrameworkSDKgt\S
    amples\Technologies\GarbageCollection
  • Extra Credit
  • Discover the objects reachable from a given
    object

21
Topic 1 Memory ManagementGarbage Collection
(Details)
  • GC Heap
  • Allocated Address Space
  • Allocation
  • Comparison C vs. C/C

22
Topic 1 Memory ManagementGarbage Collection
  • Trace the roots all the way down
  • Mark all objects that are still alive
  • Remove all dead objects (sweep)
  • Compact the heap to remove holes

23
Topic 1 Memory ManagementGarbage Collection
(Finalization)
  • Similar to C destructors
  • public class BaseObj
  • public BaseObj()
  • protected override void Finalize()
  • // Perform resource cleanup code here...
  • // Example Close file/Close network
    connection
  • Console.WriteLine("In Finalize.")
  • base.Finalize()
  • Drawbacks in comparison to C
  • Slower to allocate finalizable objects
  • Slower to collect dead finalizable objects
  • Promotion
  • Actual method call
  • Ordering and time of finalization uncontrollable
  • Finalize not always called

24
Topic 1 Memory ManagementGarbage Collection
(Finalization)
  • Finalization Queue
  • Freachable Queue
  • Resurrection
  • Forced clean-up
  • GC.ReRegisterForFinalize(o)
  • GC.SupressFinalize(o)

25
Topic 1 Memory ManagementWeak References
  • Types w.r.t. ressurection
  • Short week references
  • Long week references
  • Two different tables in the GC

26
Topic 1 Memory ManagementGenerations
27
Topic 1 Memory ManagementLarge Objects Heap
  • Separate GC Heap
  • Marked
  • Sweeped
  • Never collected
  • Too costly to move that much data
  • Monitoring performance under NT

28
Topic 2Common Type System
  • 09/06/02

29
Topic 2 Common Type SystemLooking Back
  • Why have types at all?
  • Type-less vs. typed language
  • Type vs. instance
  • Each language brings its own type system
  • Different types on different platforms
  • Need for common types for interoperation
  • IDL uses smallest common denominator

30
Topic 2 Common Type SystemThe Unknown Type
  • What is a Type in the Object Oriented World?
  • Allowable values
  • Operations defined
  • Notions in the Common Type System
  • Types are different
  • ...if they have different representations
  • Types implement the same interface
  • ...if they responds to the same set of messages
  • Types are identical
  • ...if they have the same representation
  • ...and respond to the same messages

31
Topic 2 Common Type SystemLocations
  • Values are stored in locations
  • Local variables
  • Parameters
  • A storage place with a name
  • A location can hold a single value at a time
  • Locations have a well defined type
  • Type defines which values can be stored
  • The value must be assignment compatible to the
    location
  • Type defines usage of value

32
Topic 2 Common Type SystemContracts
  • Specify requirements on implementation of a type
  • Type states which contracts must be supported
  • Contracts are shared assumptions on set of
    signatures
  • Signatures define constraints on...
  • Types, locations, parameters, methods, etc
  • Define which values can be stored
  • Enumerate allowed operations
  • Signatures are the enforceable part of a
    contract
  • Implementation of a type can be verified

33
Topic 2 Common Type SystemCoercion
  • Assignment incompatible value must be converted
  • Widening coercion
  • Never loses information
  • Usually implemented as implicit conversion
  • Narrowing coercion
  • Information might be lost
  • Usually implemented as explicit conversion
  • Coercion changes the value

34
Topic 2 Common Type SystemDefining Conversions
  • class Point
  • public int x, y
  • public static implicit operator Rectangle
    (Point p)
  • Rectangle r new Rectangle()
  • r.UL r.LR p
  • return r
  • class Rectangle
  • public Point UL, LR

35
Topic 2 Common Type SystemCasting
  • A value has the type of its location
  • Casting allows use of value as if it had
    different type
  • Usually casting is a compile time operation
  • Unless exact type of value is unknown
  • Runtime check might result in exception
  • Casting does not change the value or its type

36
Topic 2 Common Type SystemCLS
  • Set of rules for interoperability
  • Subset of the Common Type System
  • Large enough to be expressive
  • Small enough that all languages can accommodate
    it
  • Guideline was only to include verifiable
    constructs
  • Only applies to types that are externally
    visible
  • Assumes assemblies as subjects of
    interoperability
  • Identifiers must follow certain rules
  • Case sensitivity

37
Topic 2 Common Type SystemTypology
  • Value types
  • Represented by sequence of bits in a location
  • Common built-in data types
  • Reference types
  • Represented by a location and a sequence of bits
  • Objects, Interfaces, Pointers

38
Topic 2 Common Type SystemBuild-in Value Types
  • Integer values
  • int8, int16, int32, int64
  • unsigned counterparts
  • Super long decimal value
  • decimal (28 digits)
  • Floating point values
  • float32, float64
  • Unicode character value
  • char
  • Boolean value
  • bool

39
Topic 2 Common Type SystemBuild-in Reference
Types
  • Object
  • Base class for all types
  • Allows for polymorphism across all types
  • String
  • Can hold up to 231 characters
  • Is immutable
  • Changing creates new modified string

40
Topic 2 Common Type SystemBoxing and Unboxing
  • Value types can be boxed and unboxed
  • Boxing allows value types to travel by-ref
  • Based on object-ness of all types

double Value // Boxing object BoxedValue
Value// UnboxingValue (double)BoxedValue
41
Topic 2 (continued)Common Type System
  • 09/09/02

42
Topic 2 Common Type SystemExceptions
  • When exception occurs an object is created
  • Subclass of System.Exception
  • Protected block for exception handling try
  • Protected blocks may be nested
  • If exception occurs CLR searches for handler
  • Four kinds of handlers
  • Catch handler filters type of exception object
  • Filter handler determines if exception can be
    handled
  • Fault handler executes when exception occurs
  • Finally handler executes whenever protected block
    is left

43
Topic 2 Common Type SystemDelegates Events
  • Delegates
  • Similar to function pointers found in C, C
  • Use is type safe
  • Multicast delegates call more than one method
  • Derive from System.MulticastDelegate
  • Result of Combine method is MulticastDelegate to
    call
  • Events
  • Change of state of an object
  • Observer registers delegate with event as handler
  • Event triggers MulticastDelegate when it is fired

44
Topic 2 Common Type SystemDelegates Example
  • public delegate int operation (int x, int y)
  • int Add (int x, int y)
  • return x y
  • void PrintResult (int x, int y, operation o)
  • Console.WriteLine(o(x, y))
  • void Run ()
  • PrintResult(4, 7, new operation(Add))

45
Topic 2 Common Type SystemEvents Example
  • delegate void Listener (object o)
  • event Listener OnSomethingHeard
  • void SomethingHeard (object o)
  • OnSomethingHeard(o)
  • void SimpleListener (object o)
  • Console.WriteLine(o)
  • void AddListener ()
  • OnSomethingHeard new Listener(SimpleListener)

46
Topic 2 Common Type SystemAttributes
  • User-defined annotations to the metadata
  • An instance of a type is stored along with
    metadata
  • Declarative access to functionality
  • Can be of any type
  • Mostly derived from System.Attribute
  • CLR predefines some attribute types
  • Control of runtime behavior

47
Topic 2 (continued)Common Type System
  • 09/11/02

48
Topic 2 Common Type SystemPointers
  • Pointer contains address of memory location
  • Pointers are typed
  • Defined by specifying location signature
  • Type safe operations defined
  • Loading the value from the location
  • Storing assignment compatible value to the
    location
  • Address arithmetic is considered type unsafe
  • Such unmanaged pointers are not part of the CLS

49
Topic 2 Common Type SystemType Members
  • Allowable values of type may have substructure
  • Representation may be subdivided
  • Named sub-values are called fields
  • Type is called compound type
  • Indexed sub-values are called array elements
  • Type is called array
  • Each field or element is typed
  • Array elements all have same type
  • These types do never change
  • Type may declare static fields
  • Locations associated with type not with instance

50
Topic 2 Common Type SystemType Members (cont.)
  • Methods are operations
  • Defined to operate on the type static method
  • Defined operate on an instance
  • Method is passed the instance to operate on
    this
  • Instance vs. virtual method
  • Both methods may be overridden
  • Instance method is called on declared type
  • Determined at compile time
  • Virtual method is called on exact type
  • Determined at runtime

51
Topic 2 Common Type SystemVisibility
Accessibility
  • Type must be visible to be referenced
  • Access to type member requires three conditions
  • Type must be visible
  • The member must be accessible
  • Security demands are granted
  • Six degrees of accessibility
  • Private, family, assembly, family and assembly
  • Family or assembly, public
  • Interface members are always public
  • Overriding methods may only widen accessibility
  • Not under CLS!

52
Topic 2 Common Type SystemType Inheritance
  • Inheriting type supports all the contracts of
    base type
  • May support additional or modified functionality
  • All object types inherit explicitly or
    implicitly
  • Except System.Object which is the root
  • Objects have a single base class
  • Objects can implement several interfaces

53
Topic 2 Common Type SystemMember Inheritance
  • All non-static fields are inherited
  • Visibility and accessibility determine access
  • All instance and virtual methods are inherited
  • Constructors and static methods are not
  • Non-virtual and static methods may be hidden
  • Virtual methods may be marked as final
  • Properties and events are not inherited

54
Topic 3Unsafe Mode
  • 09/11/02

55
Topic 3 Unsafe ModeUnsafe Contexts
  • unsafe modifier keyword
  • unsafe blocks
  • public unsafe struct Node
  • public int Value
  • public Node Left
  • public Node Right
  • public struct Node
  • public int Value
  • public unsafe Node Left
  • public unsafe Node Right
  • Be careful when you need unsafe

56
Topic 3 Unsafe ModeUnsafe Contexts (cont)
  • Does not automatically propagate
  • public class A
  • public unsafe virtual void F()
  • char p
  • ...
  • public class B A
  • public override void F()
  • base.F()
  • ...
  • When we have a pointer parameter

57
Topic 3 Unsafe ModePointer Types
  • Almost identical to C/C
  • Operators for manipulating pointers (in unsafe
    context)
  • The operator may be used to perform pointer
    indirection
  • The -gt operator may be used to access a member of
    a struct through a pointer
  • The operator may be used to index a pointer
  • The operator may be used to obtain the address
    of a variable
  • The and -- operators may be used to increment
    and decrement pointers
  • The and - operators may be used to perform
    pointer arithmetic
  • The , !, lt, gt, lt, and gt operators may be
    used to compare pointers
  • The stackalloc operator may be used to allocate
    memory from the call stack
  • The fixed statement may be used to temporarily
    fix a variable so its address can be obtained

58
Topic 3 Unsafe ModePointer Type Conversions
  • Implicit
  • To void
  • From null
  • Explicit
  • All pointer types From/To all pointer types
  • Primitive integer types From/To all pointer types

59
Topic 3 Unsafe ModeC-like Pointer Example
  • unsafe static int Find (int pi, int size, int
    value)
  • for (int i 0 i lt size i)
  • if (pi value)
  • return pi
  • pi
  • return null

60
Topic 3 Unsafe ModeUnmanaged Types
  • Not a reference
  • Does not contain references inside
  • Formal definition
  • sbyte, byte, short, ushort, int, uint, long,
    ulong, char, float, double, decimal, or bool
  • Any enum-type
  • Any pointer-type
  • Any user-defined struct-type that contains fields
    of unmanaged-types only

61
Topic 3 Unsafe ModeFixed and Moveable
  • Fixed
  • Unaffected by the garbage collector
  • Examples
  • Locals variables
  • Value parameters
  • Pointer dereferences
  • Moveable
  • Can be relocated
  • Examples
  • Fields in objects
  • Elements of arrays
  • Static fields
  • ref and out parameters

62
Topic 3 Unsafe Modefixed Example
  • unsafe class Test
  • static int x
  • int y
  • static void F(int p)
  • p 1
  • static void Main()
  • Test t new Test()
  • int arr new int10
  • fixed (int p x) F(p)
  • fixed (int p t.y) F(p)
  • fixed (int p arr0) F(p)
  • fixed (int p arr) F(p)

63
Topic 3Unsafe Mode (cont.)
  • 09/16/02

64
Topic 3 Unsafe Modefixed Example
  • unsafe class Test
  • static int x
  • int y
  • static void F(int p)
  • p 1
  • static void Main()
  • Test t new Test()
  • int arr new int10
  • fixed (int p x) F(p)
  • fixed (int p t.y) F(p)
  • fixed (int p arr0) F(p)
  • fixed (int p arr) F(p)

65
Topic 3 Unsafe Modestackalloc Example
  • class Test
  • unsafe static string IntToString(int value)
  • char buffer stackalloc char16
  • char p buffer 16
  • int n value gt 0? value -value
  • do
  • --p (char)(n 10 '0')
  • n / 10
  • while (n ! 0)
  • if (value lt 0) --p '-'
  • return new string(p, 0, (int)(buffer 16 -
    p))
  • static void Main()
  • Console.WriteLine(IntToString(12345))
  • Console.WriteLine(IntToString(-999))

66
Topic 3 Unsafe ModeDynamic Memory Management
  • Not using the Garbage Collector?
  • Want something like?
  • class Test
  • unsafe static void Main()
  • byte buffer (byte)Memory.Alloc(256)
  • for (int i 0 i lt 256 i) bufferi
    (byte)i
  • byte array new byte256
  • fixed (byte p array) Memory.Copy(buffer,
    p, 256)
  • Memory.Free(buffer)
  • for (int i 0 i lt 256 i)
    Console.WriteLine(arrayi)
  • What is behind the scenes?

67
Topic 3 Unsafe ModeDynamic Memory Example
  • using System.Runtime.InteropServices
  • public unsafe class Memory
  • const int HEAP_ZERO_MEMORY 0x00000008
  • DllImport("kernel32")
  • static extern int GetProcessHeap()
  • DllImport("kernel32")
  • static extern void HeapAlloc(int hHeap, int
    flags, int size)
  • DllImport("kernel32")
  • static extern bool HeapFree(int hHeap, int
    flags, void block)
  • DllImport("kernel32")
  • static extern void HeapReAlloc(int hHeap, int
    flags, void block, int size)
  • DllImport("kernel32")

68
Topic 3 Unsafe ModeDynamic Memory Example (cont)
  • using System.Runtime.InteropServices
  • public unsafe class Memory
  • public static void Alloc(int size)
  • void result HeapAlloc(ph,
    HEAP_ZERO_MEMORY, size)
  • if (result null) throw new
    OutOfMemoryException()
  • return result
  • public static void Free(void block)
  • if (!HeapFree(ph, 0, block)) throw new
    InvalidOperationException()
  • public static void ReAlloc(void block, int
    size)
  • void result HeapReAlloc(ph,
    HEAP_ZERO_MEMORY, block, size)
  • if (result null) throw new
    OutOfMemoryException()

69
Topic 3 Unsafe ModeDynamic Memory Example (cont)
  • using System.Runtime.InteropServices
  • public unsafe class Memory
  • public static void Copy(void src, void dst,
    int count)
  • byte ps (byte)src
  • byte pd (byte)dst
  • if (ps gt pd)
  • for ( count ! 0 count--) pd
    ps
  • else if (ps lt pd)
  • for (ps count, pd count count !
    0 count--) --pd --ps

70
Topic 4Interoperability
  • 09/16/02

71
Topic 4 InteroperabilityIntroduction
  • Introduction to interoperation between
  • .NET and COM
  • COM and .NET
  • .NET and platform services

72
Topic 4 InteroperabilityExisting Stuff Must
Coexist
  • Transition to .NET will be a lasting process
  • Portions of systems will remain as they are now
  • ...and even maintained in their current form
  • Interoperability increases the momentum of .NET
  • Goal is to achieve total transparency
  • Mere presence of .NET must not interfere with COM
  • Performance should not be hit

73
Topic 4 InteroperabilityDifferences in the two
Worlds?
  • Managed Code vs. Unmanaged Code
  • Lifetime management
  • Metadata
  • Common Type System
  • Inheritance concept
  • Threading model
  • Error handling mechanisms
  • Registration

74
Topic 4 InteroperabilityCalling COM Services
from .NET
  • Metadata for COM class must be available
  • Can be generated from Type Library
  • Can declare Custom Wrapper
  • Use COM class like .NET class
  • Early and late bound activation
  • COM component must be registered locally

75
Topic 4 InteroperabilityConverting Type Library
to Metadata
  • Use SDK tool TlbImp
  • Or just add reference to server in Visual Studio
  • Type library can be imported from executable
  • Reference resulting metadata file in project
  • Custom IDL attributes are preserved

76
Topic 4 InteroperabilityCalling an COM Server
  • namespace CallingCOMServer
  • using System
  • using COMServerLib
  • public class DotNET_COMClient
  • ...
  • public static int Main(string args)
  • MyCOMServer myS new MyCOMServer()
  • return (myS.Add (17,4))

77
Topic 4 InteroperabilityLate Bound Activation
  • Accomplished by Reflection API
  • No difference whether COM or .NET object
  • Type can be obtained from ProgID or ClsID
  • InvokeMember to access methods and properties
  • Metadata is not needed, but useful
  • COM object is wrapped by __ComObject

78
Topic 4 InteroperabilityLate Bound Call to COM
Server
  • namespace LateBoundClient
  • using System.Reflection
  • ...
  • Type typ
  • Object obj
  • Object prms new Object2
  • int r
  • typ Type.GetTypeFromProgID(MyLib.MyServer")
  • obj Activator.CreateInstance(typ)
  • prms0 10
  • prms1 150
  • r (int)typ.InvokeMember(aMethod",
  • BindingFlags.InvokeMethod, null, obj, prms)
  • ...

79
Topic 4 InteroperabilityCalling .NET Services
from COM
  • Use .NET class like COM class
  • Type and methods must be public
  • Early and late bound activation
  • Convert Metadata to Type Library
  • Wrapper of .NET component must be registered
  • Can use RegAsm tool

80
Topic 4Interoperability (cont.)
  • 09/18/02

81
Topic 4 InteroperabilityCalling Platform
Services from .NET
  • Calling static functions in DLLs
  • P/Invoke provides services
  • Locates implementing DLL
  • Loads DLL
  • Finds function address
  • Fuzzy name matching algorithm
  • Pushes arguments on stack
  • Performs marshalling
  • Enables pre-emptive garbage collection
  • Transfers control to unmanaged code

82
Topic 4 InteroperabilityP/Invoke Sample
  • namespace HelloWorld using System class
    MyClass dllimport(user32.dll,
    CharSetCharSet.Ansi) static extern int
    MessageBox(int h, string m, string c,
    int t) public static int Main()
    return MessageBox(0, "Hello World!", "Caption",
    0)

83
Topic 4 InteroperabilityIntroduction
  • Unmanaged code can call back to managed code
  • Unmanaged parameter is function pointer
  • Must supply parameter as delegate
  • P/Invoke creates callback thunk
  • Passes address of thunk as callback parameter

84
Topic 4 InteroperabilityCallback Sample
  • public class EnumReport public bool
    Report(int hwnd, int lParam) // report the
    window handle Console.Write("Window handle is
    ") Console.WriteLine(hwnd) return
    true
  • public class SampleClass delegate bool
    CallBack(int hwnd, int lParam)
  • DllImport("user32") static extern int
    EnumWindows(CallBack x, int y)
  • public static void Main() EnumReport
    er new EnumReport() CallBack myCallBack
    new CallBack(er.Report) EnumWindows(myCallBac
    k, 0)

85
Topic 5C Syntax Features
  • 09/18/02

86
Topic 5 C Syntax Features_at_ Literals
  • Identifiers vs. Keywords
  • _at_ to use keyword as identifier
  • E.g. _at_if, _at_while, _at_throw
  • _at_ String literals
  • Turn off character escaping
  • c\\temp\\test vs. _at_c\temp\test
  • hello\\nworld vs. _at_hello\nworld

87
Topic 5 C Syntax Features_at_ Literals (examples)
  • string a "hello, world" // hello,
    worldstring b _at_"hello, world" // hello,
    world
  • string c "hello \t world" // hello
    worldstring d _at_"hello \t world" // hello \t
    world
  • string e "Joe said \"Hello\" to me" // Joe
    said "Hello" to mestring f _at_"Joe said
    ""Hello"" to me" // Joe said "Hello" to me
  • string g "\\\\server\\share\\file.txt" //
    \\server\share\file.txtstring h
    _at_"\\server\share\file.txt" // \\server\share\file
    .txt
  • string i "one\ntwo\nthree"string j
    _at_"onetwothree"

88
Topic 5 C Syntax FeaturesEnumerations
  • Have underlying type
  • First class value (not int as in C/C)
  • Defines conversions
  • Defines parsing
  • Runtime information

89
Topic 5 C Syntax FeaturesReference and Output
parameters
  • ref keyword
  • Does not accept null
  • Needs to be initialized
  • out keyword
  • Cannot be read inside the function
  • Need not be initialized outside
  • Example
  • Declaration void f (int x, ref int y, out int
    z)
  • Call f(5, ref a, out b)

90
Topic 5 C Syntax FeaturesOperator Overloading
  • Overloadable
  • Unary - ! -- true false
  • Binary - / ltlt gtgt ! lt gt lt gt
  • Non-overloadable
  • . -gt ? new typeof sizeof is as
  • Implicit assignment overloading
  • E.g. when is overloaded
  • Cast overloading
  • Conversions
  • Overloading
  • Indexer properties
  • At least one parameter is the declaring class

91
Topic 5 C Syntax FeaturesOperator Overloading
(cont.)
  • Advanced cases
  • Operators true and false
  • Used in Boolean conditions and
  • is T.false(x) ? x T.(x, y)
  • is T.true(x) ? x T.(x, y)

92
Topic 5 C Syntax FeaturesVariable argument
lists
  • Conventional way
  • void f (int x, int y, params float f)
  • Call f(4, 5, 4.5, 2.3, )
  • Undocumented way
  • More general
  • Slower
  • void f(__arglist)
  • If you are interested see http//www.iunknown.com/
    Weblog/fog0000000098.html

93
Topic 5 C Syntax Featureschecked and unchecked
  • Checked and unchecked contexts
  • unchecked()
  • unchecked
  • Affected
  • -- - /
  • Explicit conversions

94
Topic 5 C Syntax Featuresis and as
  • Dynamic type checking
  • O is T
  • O as T
  • Is can be very powerful
  • Knows about boxing and stuff
  • As in not like a cast
  • Returns null on error (not exception)

95
Topic 5C Syntax Features
  • 09/20/02

96
Topic 5 C Syntax Featuresforeach
  • Use of enumerators in the language
  • IEnumerable
  • Enumeration patter
  • foreach (type identifier in expression)
    embedded-statement

97
Topic 5 C Syntax Featuresforeach (formal)
  • A type C is said to be a collection type if it
    implements the System.IEnumerable interface or
    implements the collection pattern by meeting all
    of the following criteria
  • C contains a public instance method with the
    signature GetEnumerator() that returns a
    struct-type, class-type, or interface-type, which
    is called E in the following text.
  • E contains a public instance method with the
    signature MoveNext() and the return type bool.
  • E contains a public instance property named
    Current that permits reading the current value.
    The type of this property is said to be the
    element type of the collection type.

98
Topic 5 C Syntax FeaturesPreprocessor
  • define
  • undef
  • if
  • else
  • endif
  • region
  • endregion
  • error
  • warning
  • line

99
Topic 5 C Syntax FeaturesPreprocessor
(examples)
  • warning Code review needed before check-in
  • if Debug Retail error A build can't be both
    debug and retail
  • endif
  • class Test ...

100
Topic 5 C Syntax FeaturesOthers
  • readonly keyword
  • Static constructors
  • switch works on strings
  • using keyword
  • using directives
  • nesting
  • aliases
  • lock keyword
  • sealed and new keywords

101
Topic 5 C Syntax FeaturesClasses
  • Special Classes
  • Array, Delegate, Enum, ValueType
  • const vs. readonly fields
  • static readonly fields
  • abstract, virtual, override and sealed
    methods
  • external methods
  • Strange constructor behavior
  • Code for initialized fields executed before the
    constructor in object creation!

102
Topic 5 C Syntax FeaturesClasses (cont.)
  • Constructors
  • Instance
  • Class (static)
  • Overloading
  • this(...)
  • base()
  • Default constructors
  • base()

103
Topic 5 C Syntax FeaturesProperties
  • Look like fields
  • Access pattern
  • Behave like functions
  • Can be virtual,
  • Have get and set methods
  • Can be static
  • Treated as a single member of the class
  • new hiding example

104
Topic 5 C Syntax FeaturesProperties (example)
  • public class Button Control private string
    caption
  • public string Caption
  • get
  • return caption set
  • if (caption ! value)
  • caption value Repaint()
  • public override void Paint(Graphics g,
    Rectangle r)
  • // Painting code goes here

105
Topic 5 C Syntax FeaturesIndexers
  • Essentially overriding the operator
  • Behaves as a property
  • With parameters the index
  • get, set again
  • Can be overloaded

106
Topic 5 C Syntax FeaturesIndexers (example)
  • using System
  • class BitArray int bits int length
  • public BitArray(int length) if (length lt 0)
    throw new ArgumentException() bits new
    int((length - 1) gtgt 5) 1 this.length
    length
  • public int Length get return length
  • public bool thisint index get if
    (index lt 0 index gt length) throw new
    IndexOutOfRangeException() return
    (bitsindex gtgt 5 1 ltlt index) ! 0 set
    if (index lt 0 index gt length)
    throw new IndexOutOfRangeException()
    if (value) bitsindex gtgt 5 1 ltlt
    index else bitsindex gtgt 5 (1
    ltlt index)

107
Topic 5 C Syntax FeaturesArrays
  • Inherit from System.Array
  • Initialization
  • foreach works
  • Jagged arrays
  • Multi-dimensional arrays
  • Array covariance
  • Example to follow
  • Runtime check in assignment

108
Advanced TopicADO.NET
  • 09/23/02-09/25/02
  • Microsoft Slides (separate set)

109
Conclusion
  • 09/27/02

110
What now?
  • Office hours till the end of the semester by
    appointment
  • Bernoulli Compiler
  • ASP.NET extensions
  • Grades
  • Evaluation
  • ACSU programming contest

111
ACSU Programming Contest
  • What is it?
  • A programming contest for individuals organized
    by the ACSU
  • Who can participate?
  • Any undergraduate or first-year graduate at
    Cornell, regardless of major.
  • Why participate?
  • have fun, win prizes
  • get free T-shirtpizzasoda
  • great resume item, practice in programming
  • best programmers represent Cornell at the ACM
    Collegiate Contest (finals in Beverly Hills, CA)
  • When and where is it?
  • Sunday, September 29th, 2002, 915am to 2pm, in
    the CSUG-Lab (Room Upson 315/317)
  • More information
  • http//www.cs.cornell.edu/kempe/contest/default.ht
    ml
  • mpal_at_cs.cornell.edu

112
Bernoulli Compiler
  • 09/27/02

113
Introduction Motivation
  • Why do we need it?
  • Majority of projects we work on require program
    transformations
  • We define them and then we need to implement
    them!
  • Where?
  • Current problems
  • I need a test-bed for implementing different
    memory performance models and testing them
  • Dan needs a cleverer code restructuring
    alternative to PERL
  • Unified approach for transformation and
    translation
  • Transformations
  • Application-level Check-pointing
  • Imperfectly nested loop transformations
  • Blocking
  • Translations
  • Intermediate language (needed for register
    allocation)
  • Assembly
  • Binary
  • Last but not least To have our own compiler
    test-bed!

114
Example
  • Some non-trivial Fortran code
  • Calculate dependence relations (Omega)
  • Transform imperfectly nested loop structure
    (Nawaaz)
  • Perform several other optimizations (SUIF)
  • Perform tiling / unrolling (our stuff)
  • Spill out Fortran or C code

115
Overview
  • High level overview
  • Front-end (lexical and syntax analysis)
  • Custom transformations back-end
  • XML rendering engine

116
Front-end detail
  • Building a configuration
  • XML Signature
  • Token taxonomy
  • Keywords
  • Non-Terminals
  • Lexical analyzer
  • RegEx based
  • Source oriented
  • Syntactic analyzer
  • LALR(k) parsing
  • Source oriented
  • Type-safe definitions
  • Clean implementation
  • Somewhat slow
  • but reliable!

117
Interoperability
  • Expose SUIF-compatible interface to our XML AST
    structure
  • Will be able to run existing SUIF passes
  • Maybe similar approach for OpenC
  • Maybe even ATLAS sort of thing
  • Make it easy for researchers to expose their
    favorite compiler-construction interface to our
    trees
  • Facilitate interoperability with libraries like
    OMEGA
  • This way we can have a clean back-end that we
    understand, while we will be able to leverage
    others work in the field

118
The interesting partCustom XML Transformations
  • Tools
  • XSLT
  • XML, XSD, XPath libraries
  • Transformations vs. Translations
  • A transformation converts XML ASTs preserving the
    schema
  • A translation converts XML ASTs from one schema
    to another (IR)
  • The important part
  • Inter-language XSD Schema

119
XML Rendering
  • Translations
  • To initial source code
  • To some intermediate representation
  • To assembly code
  • To binary

120
What to shoot forFirst feelings huge project!
Can we do it?
  • What we want?
  • Easy to learn and use
  • Short Comprehensible implementation
  • No proprietary tools for transformations
  • As open as possible
  • How do we get it?
  • XML ASTs
  • Well structured code
  • How about a fixed version number at say 2.71828 ?
  • What we dont want
  • Steep learning curve
  • SUIF, OpenC
  • An API to learn
  • A specific library and tools to use
  • Compilation speed compared to industrial
    compilers
  • Better said we are willing to sacrifice this in
    exchange for power!

121
Current Status
  • Working on C (ISO/IEC 98991999(E))
  • Completed lexer
  • Completed parser
  • Inter-language schema in place
  • Error checking is really primitive
  • Need you to tweak Polaris to output the XML ASTs
    we need
  • Need applicationsbut first
  • My memory performance models
  • Nawaaz the thesis
  • Chatterjee counting of cache misses (OMEGA)
  • Ghosh Cache miss equations
  • Dans Application-level Check-pointing

122
References
  • Lex Yacchttp//dinosaur.compilertools.net/
  • XMLhttp//www.w3.org/XML/
  • XSDhttp//www.w3.org/XML/Schema
  • XPathhttp//www.w3.org/TR/xpath
  • Microsoft .NET frameworkhttp//www.microsoft.com/
    net/
  • SUIFhttp//suif.stanford.edu/
  • OpenChttp//www.csg.is.titech.ac.jp/chiba/open
    c.html
  • Programming Language Chttp//webstore.ansi.org/an
    sidocstore/product.asp?skuANSI2FISO2FIEC98992
    D1999
  • (we bought this, so if you want it, let me
    know!)
  • C Language Specificationhttp//msdn.microsoft.co
    m/vstudio/techinfo/articles/upgrade/Csharpdownload
    .asp

123
Backup slides
  • Ask me questions on these

124
Our example
  • S ? X
  • X ? X A
  • A
  • A ? A ? F
  • F
  • F ? ( X )
  • number
  • variable

125
XML Signature
lt?xml version"1.0" encoding"utf-8" ?gt lttnroot
xmlnstn"http//bernoulli.cs.cornell.edu/Token.xs
d" namespace"BC.X" class"X"gt lttnroottoken
identity"token" declaration"false"gt lttntoken
identity"keyword" declaration"false"
/gt lttntoken identity"asterisk" /gt lttntoken
identity"primitive"gt lttntoken
identity"number" /gt lttntoken
identity"variable" /gt lt/tntokengt lttntoken
identity"punctuator" declaration"false"
gt lttntoken identity"operator"
declaration"false"gt lttntoken identity"add"
name"O_add" /gt lttntoken identity"multiply"
name"O_multiply"
bind"//tntokenattributeidentity'asterisk'"
/gt lt/tntokengt lttntoken identity"grouping"
declaration"false"gt lttntoken
identity"left" name"P_left" /gt lttntoken
identity"right" name"P_right"
/gt lt/tntokengt lt/tntokengt lt/tnroottokengt
lttnkeywords /gt lttnnonterminalsgt lttnnontermin
algtSlt/tnnonterminalgt lttnnonterminalgtXlt/tnnont
erminalgt lttnnonterminalgtAlt/tnnonterminalgt ltt
nnonterminalgtFlt/tnnonterminalgt lt/tnnonterminal
sgt lt/tnrootgt
126
Token Taxonomy
  • Hierarchical structure
  • Declarative power
  • Virtual / Physical tokens

127
XML Signaturetranslated
namespace BC.X public class X public
static string keywords new string0 public
static BC.Core.Grammars.Terminal T_asterisk
new BC.Core.Grammars.Terminal("_asterisk") publ
ic static BC.Core.Grammars.Terminal T_primitive
new BC.Core.Grammars.Terminal("_primitive") pub
lic static BC.Core.Grammars.Terminal T_number
new BC.Core.Grammars.Terminal("_primitive_number")
public static BC.Core.Grammars.Terminal
T_variable new BC.Core.Grammars.Terminal("_pr
imitive_variable") public static
BC.Core.Grammars.Terminal TO_add
new BC.Core.Grammars.Terminal("_punctuator_oper
ator_add") public static BC.Core.Grammars.Termi
nal TO_multiply T_asterisk public static
BC.Core.Grammars.Terminal TP_left
new BC.Core.Grammars.Terminal("_punctuator_grou
ping_left") public static BC.Core.Grammars.Term
inal TP_right new BC.Core.Grammars.Terminal("
_punctuator_grouping_right") public static
BC.Core.Grammars.NonTerminal NS new
BC.Core.Grammars.NonTerminal("S") public
static BC.Core.Grammars.NonTerminal NX new
BC.Core.Grammars.NonTerminal("X") public
static BC.Core.Grammars.NonTerminal NA new
BC.Core.Grammars.NonTerminal("A") public
static BC.Core.Grammars.NonTerminal NF new
BC.Core.Grammars.NonTerminal("F") private
static System.Collections.Hashtable hKeywords
new System.Collections.Hashtable() public
static BC.Core.Grammars.Terminal T_keyword(string
name) if ((hKeywords.Contains(name)
false)) hKeywordsname new
BC.Core.Grammars.Terminal(("_keyword_"
name)) return ((BC.Core.Grammars.Terminal)(hKe
ywordsname))
128
Lexical analysis
  • Source
  • sealed class ELexer Lexer
  • protected override void Configure ()
  • thisX.T_number _at_"0-9"
  • thisX.T_variable _at_"a-z"
  • thisX.TO_add _at_"\"
  • thisX.TO_multiply _at_"\"
  • thisX.TP_left _at_"\("
  • thisX.TP_right _at_"\)"
  • thisWhiteSpace _at_" \t\v\r\n"
  • Generic form
  • Tokens t1, t2, t3, tn
  • Patterns r1, r2, r3, rn
  • Result
  • (?lttokengt(?ltt1gtr1)(?ltt2gtr2)(?lttngtrn))
  • Final result
  • (?lttokengt
  • (?lt_primitive_numbergt0-9)
  • (?lt_primitive_variablegta-z)
  • (?lt_asteriskgt\)
  • (?lt_whitespacegt \t\v\r\n)
  • (?lt_punctuator_operator_addgt\)
  • (?lt_punctuator_grouping_rightgt\))
  • (?lt_punctuator_grouping_leftgt\()
  • )
  • Notes
  • This time fast
  • Natively compiled
  • Single string
  • .NET regular expressions grouping
  • _primitive_xxx

129
Syntactic Analysis
  • Sample grammar definition
  • See how close it is to the initial format
  • sealed class EGrammar Grammar
  • . . .
  • public EGrammar ()
  • thisX.NS
  • X.NX new Reduction(_S)
  • thisX.NX
  • X.NX X.TO_add X.NA new Reduction(_X1)
  • X.NA new Reduction(_X2)
  • thisX.NA
  • X.NA X.TO_multiply X.NF new
    Reduction(_A1)
  • X.NF new Reduction(_A2)
  • thisX.NF
  • X.T_primitive new Reduction(_F1)
  • X.TP_left X.NX X.TP_right new
    Reduction(_F2)

130
Syntactic analysis terminology
  • Symbol is a Terminal or Non-Terminal (e.g.
    anything like X.Txxx and X.Nxxx)
  • Sequence is several symbols concatenated with
    (e.g. X.NX X.TO_add X.NA)
  • Alternative is a Sequence Reduction
  • Reduction is a object-member function closure,
    that gets called when the parser performs a
    reduction using the Production whose Alternative
    has the Reduction in question. Using .NET
    terminology, this is a delegate
  • Production is a Non-Terminal ? Alternative
  • Alternatives is a list of Alternative elements
    concatenated with
  • Rule is a Non-Terminal ? Alternatives
  • Grammar is a set of Rule elements.

131
Syntactic AnalysisParsing Phase 1
  • LR(0) NFA construction
  • Nodes
  • Stations, labeled with something like ?
    Non-Terminal (e.g. ? S). We create one of
    these for each non-terminal in the grammar
  • Items, labeled with one of the productions in the
    grammar with a ? somewhere in the middle of the
    RHS (e.g. X ? X ? A). The intuition behind
    the ? is that we want to perform a reduction
    using this production and we have reached that
    far in recognizing the RHS.
  • Edges
  • ?-edges from each station ? N to all items in the
    form N ? ? (i.e. dot in the beginning)
  • ?-edges from all items in the form N ? ? N2
    to the station ? N2
  • edge labeled with ? from N ? ? ? to N ? ? ?

132
Syntactic AnalysisParsing Phase 1 (picture)
133
Syntactic AnalysisParsing Phase 2
  • Firstk(N) construction
  • Non-terminal look-ahead sets
  • Inductive definition
  • F0(T) ltTgt
  • Fi(N) Fi-1(N) Union Fi-1(Y1) ?k Fi-1(Y2) ?k
    ?k Fi-1(Ym) for all N ? Y1Y2Ym
  • X ?k Y Union xy truncated to the first k
    symbols for all x in X and y in Y
  • F0(T) ? ? Fi(N) ? ? Fl(N) Fl1(N)
  • Firstk(N) Fl(N)

134
Syntactic AnalysisParsing Phase 3, 4, 5,
  • Look-ahead set propagation
  • DFA construction
  • Normal algorithm from discrete math
  • Actual LALR(k) parsing
  • Described in detail in the books
Write a Comment
User Comments (0)
About PowerShow.com