[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new alien: bugs & comments



First I think that the function 

os_vm_address_t os_allocate_at(addr,len)
os_vm_address_t addr;
os_vm_size_t len;
{
    return os_validate(&addr, len);
}

should be 

os_vm_address_t os_allocate_at(addr,len)
os_vm_address_t addr;
os_vm_size_t len;
{
    return os_validate(&addr, len);
}

Otherwise load-foreign fails. Im curios about this bug as it is in the
os-common.c file and should bite on mips/mach also.

I mirrored the ../src/16 directory and built a core with this patch
and now load-foreign works.

Any plans to convert the C code to ANSI-C ? Every platform that can
run cmucl should have at least one ANCI-C compiler (gcc). I am willing
to convert it if i the job you want to avoid.

I am still converting the bulk of our alien code to the new aliens so
eventual comments will come later.

I dont think you have had time to test the example in the alien manual
so here is my working and tested (1 time) version.

C code:

struct c_struct
{
  int x;
  char *s;
};
 
struct c_struct *c_function (i, s, r, a)
    int i;
    char *s;
    struct c_struct *r;
    int a[10];
{
  int j;
  struct c_struct *r2;
 
  printf("i = %d\n", i);
  printf("s = %s\n", s);
  printf("r->x = %d\n", r->x);
  printf("r->s = %s\n", r->s);
  for (j = 0; j < 10; j++) printf("a[%d] = %d.\n", j, a[j]);
  r2 = (struct c_struct *) malloc (sizeof(struct c_struct));
  r2->x = i + 5;
  r2->s = "A C string";
  return(r2);
};

LISP code:

;;; -*- Package: test-c-call -*-
(in-package "TEST-C-CALL")
(use-package "ALIEN")
(use-package "C-CALL")

;; This loads in the C object file with the function definition.
(eval-when (compile)
  (load-foreign "test.o"))

;;; Define the record c-struct in Lisp.
(def-alien-type nil
    (struct c-struct
	    (x int)
	    (s c-string)))

;;; Define the Lisp function interface to the C routine.  It returns a
;;; pointer to a record of type c-struct.  It accepts four parameters:
;;; i, an int; s, a pointer to a string; r, a pointer to a c-struct
;;; record; and a, a pointer to the array defined above.

(def-alien-routine c-function
    (* (struct c-struct))
  (i int)
  (s c-string)
  (r (* (struct c-struct)))
  (a (array int 10)))

;;; A function which sets up the parameters to the C function and
;;; actually calls it.
(defun call-cfun ()
  (with-alien ((ar (array int 10))
	       (c-struct (struct c-struct)))
    (dotimes (i 10)                     ; Fill array.
      (setf (deref ar i) i))
    (setf (slot c-struct 'x) 20)
    (setf (slot c-struct 's) "A Lisp String")

    (with-alien ((res (* (struct c-struct))
		      (c-function 5 "Another Lisp String" (addr c-struct) ar)))
      (format t "Returned from C function.~%")
      (multiple-value-prog1
	  (values (slot res 'x)
		  (slot res 's))))))

Test transcript:

davida@pinus$ lisp -version 16m1
;; Loading "/home/davida/init.lisp".
Loading David's init file
;;; Loading "/home/my/w-lisp/global-init.lisp".
CMU Common Lisp 16b, running on pinus
Hemlock 3.5 (16b), Python 1.0(16b), target SPARCstation/Sun 4
Send bug reports and questions to cmucl-bugs@cs.cmu.edu.
* (compile-file "test")

Python version 1.0(16b), VM version SPARCstation/Sun 4 on 15 MAR 92 03:35:39 pm.
Compiling: /home/my/w-lisp/new-rea/test.lisp 15 MAR 92 03:35:23 pm

;;; Running library:load-foreign.csh...
;;; Loading object file...
;;; Parsing symbol table...
Warning:
"_etext" moved from #x0002C8F8 to #x00C00108.

Warning:
"_edata" moved from #x0003DC30 to #x00C00148.

Warning:
"_end" moved from #x000482D0 to #x00C00148.

;;; Done.

File: /home/my/w-lisp/new-rea/test.lisp

In: DEF-ALIEN-ROUTINE C-FUNCTION
  (DEF-ALIEN-ROUTINE C-FUNCTION (* (STRUCT C-STRUCT)) (I INT) (S C-STRING) ...)
--> BLOCK WITH-ALIEN COMPILER-LET SYMBOL-MACROLET VALUES PROG1 LET 
--> ALIEN-FUNCALL ALIEN::NATURALIZE 
==>
  (ALIEN::%SAP-ALIEN ALIEN '#<ALIEN::ALIEN-POINTER-TYPE (* #)>)
Note: Unable to optimize because:
      Could not optimize away %SAP-ALIEN: forced to do runtime 
allocation of alien-value structure.


In: DEFUN CALL-CFUN
  (ADDR C-STRUCT)
--> ALIEN::%LOCAL-ALIEN-ADDR 
==>
  (ALIEN::%SAP-ALIEN C::VAR '#<ALIEN::ALIEN-POINTER-TYPE (* #)>)
Note: Unable to optimize because:
      Could not optimize away %SAP-ALIEN: forced to do runtime 
allocation of alien-value structure.

  (C-FUNCTION 5 "Another Lisp String" (ADDR C-STRUCT) AR)
--> AR ALIEN::LOCAL-ALIEN ALIEN::EXTRACT-ALIEN-VALUE ALIEN::NATURALIZE 
==>
  (ALIEN::%SAP-ALIEN ALIEN '#<ALIEN::ALIEN-ARRAY-TYPE (ARRAY # 10)>)
Note: Unable to optimize because:
      Could not optimize away %SAP-ALIEN: forced to do runtime 
allocation of alien-value structure.


Compilation unit finished.
  3 notes


test.sparcf written.
Compilation finished in 0:00:22.
#p"/home/my/w-lisp/new-rea/test.sparcf"
T
NIL
* (load "test")

; Loading "/home/my/w-lisp/new-rea/test.sparcf".
T
* (test-c-call::call-cfun)
i = 5
s = Another Lisp String
r->x = 20
r->s = A Lisp String
a[0] = 0.
a[1] = 1.
a[2] = 2.
a[3] = 3.
a[4] = 4.
a[5] = 5.
a[6] = 6.
a[7] = 7.
a[8] = 8.
a[9] = 9.
Returned from C function.
10
"A C string"
*

David Axmark
EMAIL: davida@isil.detron.se or davida@emil.csd.uu.se
MAIL:	Detron HB, Petterslundsg 11 A, 753 28, UPPSALA, SWEDEN
PHONE:	+ (46) 18 - 11 07 80

(I can't spell in ANY language ...)