Interfacing to C in 1.0.8 - M. Ernst

To begin with, please note The following applies to the current Sather 1.0 spec / 1.0.x compiler! On the Sather page (http://www.icsi.berkeley.edu/ sather) you will find a link to the Sather 1.1 spec that contains a greatly enhanced C interface (especially for structured C types). ICSI folks want to release a pre-release of the 1.1 compiler that may contain this enhancement.

The external class interface works as follows:

external class C_CLASS is
  routine_a(arg: ARG): RES; -- no body => this is a C function
  routine_b(arg: ARG): RES is
    do_something;
  end;                      -- with body => this is a Sather function
                            -- that can be called from C
end;

The set of allowed types in signatures of external classes' routines is constrained to:

INT
corresponds to C's long/int/32bit; the latest is a safe guess. Poses a portability problem. Will be solved with 1.1
CHAR
char
FLT
float
FLTD
double
X ( a class including AREFT )
The C side will receive a pointer to the object's array portion, i.e. a T *. Especially: STR => char * ('' terminated) EXT_OB: any pointer. Useful if the Sather side need not know about the type of the object but receives it from C and some time passes it back.

           external class TCL is
             Tcl_CreateInterp: EXT_OB;
             Tcl_Eval(interp: EXT_OB, script: STR): INT;
           end;
Sather code may now do:
           
           interpreter ::= TCL::Tcl_CreateInterp;
           return_code ::= TCL::Tcl_Eval(interpreter, "eval {3 + 4}");

Referencing C variables

You currently cannot directly reference external C variables, but 1.1 will permit this. You have to build a getter function.

 
my_c_code.c:
int variable;
int get_variable(void) { return variable; }
 
 
my_c_code.sa:
external class MY_C_CODE is
  get_variable: INT;
end;

use_c_code.sa:
var: INT := MY_C_CODE::get_variable;

Note that there is nothing like an instance or a (shared) attribute of an external class. To get from an implemented routine in an external class (like 'routine_b' above to other Sather code you need to work with shared routines or attributes of another class (since there's no object you can get your hands on).

C structs to Sather

You currently cannot directly interpret structured types in Sather. If you need to access the elements you have to build functions for this. Second, you can't pass them by value. Pass their pointers as EXT_OB's like above. This should be solved in 1.1

Sather objects to C

You also cannot pass complex Sather objects to C nor receive them, either here or in 1.1

Naming conventions

Using the same example as above, C_CLASS::routine_a refers to a C function with name 'routine_a'. i.e. C_CLASS does not give the C functions a separate name space.

  external class A
    foo: INT;
  end;
  
  external class B
    foo: CHAR;
  end;
will clash. This means you may want to prefix them again.

C_CLASS::routine_b is callable from C as C_CLASS_routine_b.

Also look into the GUI sources which interface to TCL/TK.



Benedict A. Gomes
Mon Apr 29 10:12:43 PDT 1996