The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is...

13
The Cn Language over view The Cn Language over view The Cn language strongly on ANSI C . So if you are The Cn language strongly on ANSI C . So if you are familiar with ANCI it is not so tough to deal with Cn familiar with ANCI it is not so tough to deal with Cn language . language . Basic Data types: Basic Data types: char, unsigned char, signed char. char, unsigned char, signed char. short, unsigned short, signed short. short, unsigned short, signed short. int, unsigned int, signed int. int, unsigned int, signed int. long, unsigned long, signed long. long, unsigned long, signed long. float, double float, double Cn also supports the following aggregate types: Cn also supports the following aggregate types: structute structute union union pointer pointer arrays arrays Note : These are exactly same as for ANSI C. Note : These are exactly same as for ANSI C.

Transcript of The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is...

Page 1: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

The Cn Language over viewThe Cn Language over view

The Cn language strongly on ANSI C . So if you are familiar The Cn language strongly on ANSI C . So if you are familiar with ANCI it is not so tough to deal with Cn language .with ANCI it is not so tough to deal with Cn language .

Basic Data types: Basic Data types: char, unsigned char, signed char.char, unsigned char, signed char. short, unsigned short, signed short.short, unsigned short, signed short. int, unsigned int, signed int.int, unsigned int, signed int. long, unsigned long, signed long.long, unsigned long, signed long. float, doublefloat, doubleCn also supports the following aggregate types:Cn also supports the following aggregate types: structutestructute union union pointerpointer arraysarraysNote : These are exactly same as for ANSI C.Note : These are exactly same as for ANSI C.

Page 2: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Mono and poly specifiersMono and poly specifiers

These are the added two features which we will found in Cn language. These are the added two features which we will found in Cn language. These keywords are called multiplicity specifiers.These keywords are called multiplicity specifiers.

mono:mono: The declaration exists in mono domain (i.e. one instance ) The declaration exists in mono domain (i.e. one instance ) e.g. mono variable/memory .e.g. mono variable/memory .

poly: poly: The declaration exists in poly domain (i.e. many instance ) The declaration exists in poly domain (i.e. many instance ) e.g. poly variable/memory.e.g. poly variable/memory.

Note default multiplicity is mono.Note default multiplicity is mono.

Basic types.Basic types.

The basic types can always be used in conjunction with multiplicity specifiers. For instance:

poly int counter; //A different instance of ’counter’ exists on each PE mono unsigned char initial; // A single instance of

’initial’ exists

Page 3: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

ExampleExample

Page 4: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Pointers TypePointers Type

As we have done pointers in traditional C languageAs we have done pointers in traditional C language const int * const foo; /* const pointer to const int */ int * const bar; /* const pointer to non-const int */ const int * bing; /* non-const pointer to const int */

Multiplicity specifiers work in similar way

poly int * poly foo; /* poly pointer to poly int */ int * poly bar; /* poly pointer to mono int (equiv to

mono int * poly bar) */ poly int * bing; /* mono pointer to poly int (equiv to poly

int * mono bing) */ mono int * mono p; ; /* /* mono pointer to mono int */

Page 5: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Pointers to mono dataPointers to mono data

Mono pointer to mono data :Mono pointer to mono data : Poly pointer to mono data :Poly pointer to mono data :

Page 6: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Pointer to poly dataPointer to poly data

For poly data, the same two types of pointers exist: mono pointer points to the same address in every PE’s memory. poly pointer can hold a different address on each PE (pointing to

data on the same PE). Mono pointer to poly data:Mono pointer to poly data:

A mono ptr to poly memory A mono ptr to poly memory (poly*mono)(poly*mono)

Example :Example :

poly char buffer[BUFFER_SIZE] poly char * mono ptr; poly int count; int i; ptr = buffer; //// Initialize Initializecount = 0;

for (i = 0; i < BUFFER_SIZE; i++) { if (*ptr == 0) ////check value pointed check value pointed

to each PEto each PE{ count++; // // increment the counterincrement the counter

} } ptr++; //// move the pointer to next move the pointer to next bytebyte

}}

Page 7: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Continue……Continue……

Mono pointer to poly data :Mono pointer to poly data :The type is (poly * poly)The type is (poly * poly)

Example:Example:// prototype for poly version of

strchr() poly char * poly strchrp(const poly

char * mono s1, poly char c);

poly char str[256]; // variable str is // variable str is actually a poly * mono pointer actually a poly * mono pointer

poly char * poly ptr; // a pointer // a pointer into stringinto string

......... ......... // initialize string on each // initialize string on each PEPE

ptr = strchrp(str, ’z’); // search for // search for first occurrence of ‘z’ different first occurrence of ‘z’ different on each PEon each PE

Page 8: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Illegal CastIllegal Cast

We have to take a note that , because mono and poly data are We have to take a note that , because mono and poly data are in completely separate memory space , it is not legal to in completely separate memory space , it is not legal to cast or assign a mono * pointer, or vice-versa.cast or assign a mono * pointer, or vice-versa.

Problems in doing so:Problems in doing so:1. The mono and poly pointer sizes are not guaranteed to be the

same; poly memory is typically quite small and may use 16-bit pointers, while mono pointers may be 32 or 64 bits .

2. A pointer to data in poly memory, for example, will not necessarily point to anything meaningful if cast to a pointer to mono memory; it could point to arbitrary data or even code.

Page 9: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

ArraysArrays

Array types :Array types :The multiplicity specifier for an array type defines the domain for

the base type. Example : poly char buffer[20] ; The declaration will create space for 20 characters on the poly

stack frame. The address of each of these arrays in poly space will be same .

Note : It is not possible to create a poly char * poly pointer using array notation since an array only specifies the base type multiplicity class ( the implicit pointer multiplicity class will always be mono).

The multi dimension array are supported in Cn language as we have done in ANSI C.

Page 10: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Structure and Union Structure and Union

They are same as ANSI C .They are same as ANSI C . But multiplicity specifiers have strict rules for use inside the structure / But multiplicity specifiers have strict rules for use inside the structure /

union .union . Objects inside a structure /union definition have no multiplicity class. Like :Objects inside a structure /union definition have no multiplicity class. Like : struct _A { int a; char b; // Multiplicity class not defined in struct definition float c; };poly struct _A my_struct; // All objects within the struct are poly mono struct _A my_struct_2; // All objects within the struct are mono

But :

poly struct _B { int a; // Not allowed to declare multiplicity inside

definition int b; // (but statment also declares a poly object)

} my_struct_3;

Page 11: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Structure and Union Cont …….Structure and Union Cont …….

Even the following syntax is also wrong :Even the following syntax is also wrong :union _B { poly int a; // Illegal use of multiplicity

specifier mono char b; // Illegal use of multiplicity

specifier float c; };

mono union _B my_union; // Multiplicity of declaration would conflict with definition

Only at the time of pointer declaration in structure/union the poly and mono can be used.

Because the member itself (the pointer) cannot have a multiplicity specifier, but the object pointed to can

Without this capability it would not be possible to have a pointer to a poly object as a member of a structure or union.

Page 12: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

Structure and Union Cont …….Structure and Union Cont …….

Example :Example :struct _C { mono int *a; // pointer to a mono int poly char *b; // pointer to a poly char }; struct _C my_struct2; // Note: this is an implicit mono object // a is mono pointer to mono int

// b is mono pointer to poly char struct _C poly my_struct3; // Poly object of the same type // a is poly pointer to mono int // b is poly pointer to poly char

The first object my_struct2 contains two members which are mono int * mono a and poly char * mono b. The second object my_struct3 contains two members which are mono int * poly a and poly char * poly b

Page 13: The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.

TypedefsTypedefs

Cn supports typedefs as of ANSI CCn supports typedefs as of ANSI C The typedef statement cannot use multiplicity specifiers to define

the multiplicity of the type. But as with structs and unions, it can define pointers to mono or

poly types.

Like : Like :

typedef poly int p_int; // illegal use of multiplicity specifier

typedef poly int * p_ptr; // ’p_ptr’ is a pointer to poly int typedef mono int * m_ptr; // ’m_ptr’ is a pointer to mono int

p_ptr a; // poly int * mono a poly p_ptr a; // poly int * poly a m_ptr a; // mono int * mono a poly m_ptr a; // mono int * poly a