- 3 - Interfacing C, .NET and other languages
description
Transcript of - 3 - Interfacing C, .NET and other languages
![Page 1: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/1.jpg)
- 3 -
Interfacing C, .NET and other languages
![Page 2: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/2.jpg)
Overview
Interfacing C
Interfacing .NET
![Page 3: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/3.jpg)
Overview
Language interoperability
EiffelStudio executionGarbage collection
Interfacing CCalling C from EiffelCalling Eiffel from C
Eiffel to .NET translation
![Page 4: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/4.jpg)
Interoperability problems
The paradigm (!) The object model
Inheritance“Pure” OO vs. value types
Data abstraction Flow control
ExceptionsThreads
Executional modelMemory allocationGarbage collection
![Page 5: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/5.jpg)
Client / Supplier
Client SupplierCall
Return value
Eiffel CCall
Return valueClient
C EiffelCall
Return valueSupplier
Eiffel C
Call
Return value
Call
Return valueCall-backs
![Page 6: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/6.jpg)
Using Wrappers
Eiffel C
WR
AP
PE
REiffel C
WR
AP
PE
R
![Page 7: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/7.jpg)
Inheritance / Subtyping
Eiffel Class
.NET Class
(More of that later)
.NET Class
Eiffel Class
![Page 8: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/8.jpg)
Runtime Framework
FRAMEWORK
Language A Language B
![Page 9: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/9.jpg)
Runtime Framework (cont.)
Every runtime framework (even the CPU itself)
implements a
CODE MODEL(in the case of OO, this is called “object model”).
The code model defines a program data type and an
operational semantics for values of that type.
All languages have to “compile to” this code model.
![Page 10: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/10.jpg)
Assembler Framework
Program data-type
Sequence of assembler instructions
Labels
Declaration of variables / constants
Operational semantics
Effect of executing instructions onto registers,
memory, IO ports ...
![Page 11: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/11.jpg)
.NET Framework
Program data-type
Byte-code assembly definition
(CIL = Common Intermediate Language)
Operational semantics
Execution model (CLR = Common Language Runtime) Memory Management
Object creation
etc.
Base Class Library (implicit semantics)
![Page 12: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/12.jpg)
.NET Framework
.NET CLR
Eiffel C#
EiffelStudio
Only CodeAdaption
Interface and CodeAdaption
C# compiler
![Page 13: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/13.jpg)
Overview
Interfacing C
Interfacing .NET
![Page 14: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/14.jpg)
Calling C from Eiffel
Eiffel defines syntax to interface “foreign” languages:external keyword (ECMA 8.31.1)alias keyword
External language definitions for C, C++ and DLLs
(ECMA 8.31.10 ff)
![Page 15: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/15.jpg)
Wrapping Functions
NAME
abs - compute the absolute value of an integer
SYNOPSIS
#include <stdlib.h>
int abs(int j);
![Page 16: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/16.jpg)
Wrapping Functions
feature -- Wrappers
abs_wrap (x: INTEGER): INTEGER is-- Wrapper for `abs'
external
"C (int) : int | <stdlib.h>"
alias
"abs"
end
![Page 17: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/17.jpg)
Wrapping Functions (cont.)
feature -- Wrappers
abs_wrap (x: INTEGER): INTEGER is-- Wrapper for `abs'
external
"C [macro <stdlib.h>] (int) : int"
alias
"abs"
end
Can also be used for macros:#define abs(x) ((x >= 0) ? x : (-x))
![Page 18: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/18.jpg)
Inline C
Encapsulating:
A possible solution:
Anything wrong?
Missing cast of $p to (const char *)
Should it really be NATURAL?
Missing cast for return instruction
size_t strlen (const char * s);
c_strlen (p: POINTER): NATURALexternal
“C inline use <string.h>”alias
“return strlen ($p);”end
![Page 19: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/19.jpg)
Sample: encapsulating strlen (2)
A safer solution:
Still something wrong?
c_strlen (p: POINTER): NATURAL_64external
“C inline use <string.h>”alias
“[EIF_NATURAL_64 Result;size_t val = strlen ((const char *)
$p);Result = (EIF_NATURAL_64) val;assert (((size_t) Result) == val);return Result;
]”end
![Page 20: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/20.jpg)
Sample: encapsulating strlen (3)
We forgot to equip the routine with contracts
c_strlen (p: POINTER): NATURAL_64require
p_not_null: p /= default_pointerp_valid: -- p is a valid pointer to a C string
external“C inline use <string.h>”
alias“[
EIF_NATURAL_64 Result;size_t val = strlen ((const char *) $p);Result = (EIF_NATURAL_64) val;assert (((size_t) Result) == val);return Result;
]”ensure
positive_count: Result >= 0end
![Page 21: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/21.jpg)
Mapping Eiffel types
Each Eiffel basic type has a corresponding C type:
INTEGER_8 EIF_INTEGER_8
INTEGER_16 EIF_INTEGER_16
INTEGER_32 EIF_INTEGER_32
INTEGER_64 EIF_INTEGER_64
NATURAL_8 EIF_NATURAL_8
NATURAL_16 EIF_NATURAL_16
NATURAL_32 EIF_NATURAL_32
NATURAL_64 EIF_NATURAL_64
BOOLEAN EIF_BOOLEAN
CHARACTER_8 EIF_CHARACTER
CHARACTER_32 EIF_WIDE_CHAR
REAL_32 EIF_REAL_32
REAL_64 EIF_REAL_64
POINTER EIF_POINTER
![Page 22: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/22.jpg)
Mapping Eiffel types (2)
Unsized types CHARACTER, INTEGER and NATURAL
use the C corresponding type of their mapping
Size of Eiffel types guarantees length of underlying
Eiffel C type
Used to cast values obtained from calling C routines
to expected Eiffel type
Used when calling Eiffel runtime routines from C
code
![Page 23: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/23.jpg)
POINTERs
Any pointer definition of C is mapped to the built-
in data-type POINTER.
Access to elements of type POINTER is opaque.
They cannot be accessed in Eiffel.
You can get the pointer to any Eiffel object using
the $ operator:
my_pointer := $my_string
BEWARE OF THE GARBAGE COLLECTOR
![Page 24: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/24.jpg)
Mark / Sweep Garbage Collection
STACK
Object
Object
Object
ObjectObject
Object
![Page 25: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/25.jpg)
Mark Phase
STACK
Object
Object
Object
ObjectObject
Object
![Page 26: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/26.jpg)
Sweep Phase
STACK
ObjectObject
Object
Object
![Page 27: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/27.jpg)
Moving Garbage Collection
STACK
Object
Object
Object
ObjectObject
Object
![Page 28: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/28.jpg)
Moving Phase
STACK
Object
Object
Object
Object
Object Object
![Page 29: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/29.jpg)
Sweep Phase
STACK
Object
Object
Object Object
![Page 30: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/30.jpg)
Eiffel Hector: Adopt / Access / Wean
EIF_OBJECT eif_adopt (EIF_OBJECT r)
C wants to keep the reference
EIF_REFERENCE eif_access (EIF_OBJECT h)
Access the object given by handle h
void eif_wean (EIF_OBJECT h)
Allow the GC to reclaim the object pointed to
by h
![Page 31: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/31.jpg)
Proxy Objects
EiffelObject
Cstruct
![Page 32: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/32.jpg)
Shared structure
EiffelObject
Cstruct
EiffelObject
![Page 33: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/33.jpg)
Helper classes
EiffelBase provides some classes to facilitate
interfacing with C code:
C_STRING
MANAGED_POINTER
MEMORY_STRUCTURE
![Page 34: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/34.jpg)
C_STRING
Convert C string to Eiffel string and vice versa
c_string: C_STRING
create c_string.make (“TEST”)
io.put_string (c_strlen (c_string.item).out)
c_string: C_STRING
create c_string.make_empty (512)
-- Some code that fills `c_string’
io.put_string (c_string.string)
![Page 35: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/35.jpg)
MANAGED_POINTER
Automatic memory management of memory
Allow random access with read/write of any basic type
values in either little or big endian format:c_area: MANAGED_POINTERval: INTEGER_32
create c_area.make (512)
c_area.put_integer_32 (val, 1)c_area.put_integer_32_le (val, 1)c_area.put_integer_32_be (val, 1)
val := c_area.read_integer_32 (5)val := c_area.read_integer_32_le (5)val := c_area.read_integer_32_be (5)
![Page 36: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/36.jpg)
MEMORY_STRUCTURE
Deferred class: recommended as ancestor to all
classes wrapping a C structurestruct point { int x, y; }
class POINT inherit MEMORY_STRUCTUREcreate makefeature -- Measurement
structure_size: INTEGERexternal “C inline use <point.h>”alias “return sizeof(struct point);”end
feature -- Accessx: INTEGER do Result := c_x (item) end
feature {NONE} -- Implementationc_x (p: POINTER): INTEGER
require p_not_null: p /= default_pointerexternal “C inline use <point.h>”alias “return ((struct point *) $p)->x;”end
end
![Page 37: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/37.jpg)
Callbacks
class CALLBACK feature -- Action
callback (val: INTEGER) do … endfeature -- Callback initialization
callback_setupdo
c_callback_setup (Current, $callback)end
feature {NONE} -- Implementationc_callback_setup (obj: ANY; fnptr: POINTER)
external “C inline use %”eif_eiffel.h%””alias
“[callback_object = eif_adopt ($obj);callback_routine = (CALLBACK_PROC) $fnptr;]”
endend
![Page 38: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/38.jpg)
Callbacks
Do not forget to call eif_wean on callback_object
#include “eif_eiffel.h”
typedef void (* CALLBACK_PROC) (EIF_REFERENCE, EIF_INTEGER);
EIF_OBJECT callback_object = NULL;CALLBACK_PROC callback_routine = NULL;
void perform_callback (int i){callback_routine (eif_access (callback_object), i);
}
![Page 39: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/39.jpg)
CECIL
“C-to-Eiffel Call-In Library”
Library to access features of Eiffel objects
Compile your Eiffel system to an own library
make cecil
builds lib<system>.a / .lib
![Page 40: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/40.jpg)
Using CECIL
#include "eif_setup.h"
#include "eif_eiffel.h"
int main(int argc, char **argv, char **envp)
/* Please respect this signature: `argc', `argv' and `envp' are used * in EIF_INITIALIZE. */
{
/* declarations of variables */
/* Initialize the Eiffel run-time. */
EIF_INITIALIZE(failure)
/* body of your "main" function */
/* Reclaim the memory allocated by the Eiffel run-time. */
EIF_DISPOSE_ALL
}
![Page 41: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/41.jpg)
Using CECIL (cont.)
Eiffel: print(obj)
Print (EIF_OBJECT obj)
{
EIF_PROCEDURE ep;
EIF_TYPE_ID tid;
tid = eif_type_id ("ANY");
ep = eif_procedure ("print", tid);
(ep) (eif_access(obj),eif_access(obj));
}
![Page 42: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/42.jpg)
Overview
Interfacing C
Interfacing .NET
![Page 43: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/43.jpg)
Similarities between .NET and Eiffel
Object-oriented
Imperative code
Strong type system
Type can have many ancestors
Garbage collector
![Page 44: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/44.jpg)
Eiffel vs. .NET object model
Multiple Inheritance
Free Generics
Expanded Types
Clusters
Once
Call by Value
Restricted Exceptions
Single Inheritance
Restricted Generics
Value Types
Packages, Assemblies
Static
Call by Value or
Reference
Free use of try/catch
![Page 45: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/45.jpg)
Mapping Inheritance
A B C
D
![Page 46: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/46.jpg)
Mapping Inheritance
A B C
D
IMPL.A
IMPL.D
IMPL.C
IMPL.B
IMPL.A
A Interface
Class
![Page 47: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/47.jpg)
Browsing .NET assemblies
![Page 48: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/48.jpg)
Open Problems
Eiffel for .NET design is based on .NET 1.1 (work with
2.0)
No link between Eiffel and .NET Genericity
Contracts are compiled into the code
Lots of name mangling
Cumbersome Interface
![Page 49: - 3 - Interfacing C, .NET and other languages](https://reader035.fdocuments.us/reader035/viewer/2022062409/568145b7550346895db2bc2d/html5/thumbnails/49.jpg)
Summary
Compiling to C
Language support integrated into Eiffel
Well-defined interface
Call-back Eiffel features using CECIL
Compiling for .NET
Everything is fine if we only execute Eiffel on .NET
Matching the model works, incl. multiple inheritance
Some Eiffel concepts cannot be mapped to .NET and
vice versa