[gambit-list] [MSLUG] Significance of 'C' in "Gambit-C"

Marc Feeley feeley at iro.umontreal.ca
Sun Dec 9 22:59:42 EST 2007


On 9-Dec-07, at 8:04 PM, Mathieu Bouchard wrote:

> On Sun, 9 Dec 2007, Marc Feeley wrote:
>
>> I think that's what most users want.  Gambit-C is very portable, it  
>> works on any system with a decent C compiler (all the major  
>> workstations are covered of course, but Gambit-C has also been used  
>> on unusual target platforms such as the embedded PowerPC processor  
>> in Xilinx FPGAs and the ARM processor on the Nintendo DS).
>
> If a function definition happens at run time, is Gambit-C able to  
> invoke the C compiler and get it to compile something that will be  
> usable at runtime? (e.g. using dlopen)

Yes.  Compiled code and interpreted code can interact seamlessly in a  
program.

If you want to compile Scheme code on the fly, it is best to use  
"gsc" (Gambit Scheme compiler) which provides the function "compile- 
file", which compiles a file of Scheme code to an "object file" that  
can be loaded at run time using Scheme's "load" procedure.  Here is a  
complete example which shows that it takes about 0.14 seconds to  
generate, compile and load a small Scheme function on a 2GHz MacBook  
Pro (over 90% of the time is spent in the C compiler, which in this  
case is gcc).

;;; File: "compeval.scm"

;;; The "compeval" procedure behaves like "eval" but always evaluates
;;; in the global environment.

(define compeval
   (let ((counter 0))

     (define (definition? expr)
       (and (pair? expr)
            (eq? (car expr) 'define)))

     (lambda (expr)
       (set! counter (+ counter 1))
       (let* ((fn
               (string-append "_compeval" (number->string counter)))
              (fn-scm
               (string-append fn ".scm")))
         (with-output-to-file fn-scm
           (lambda ()
             (pretty-print
              (if (definition? expr)
                  `(begin
                     ,expr
                     (define _compeval-result (void)))
                  `(define _compeval-result ,expr)))))
         (compile-file fn-scm)
         (load fn)
         _compeval-result))))

(define _compeval-result (void))

;;; Tests.

(define (test n)
   (let loop ((i 0))
     (if (< i n)
         (let ((result
                (compeval `(define (f x) (list ,i (* x ,i))))))
           (loop (+ i 1))))))

(time (test 1000))

(pp (f 1000000))

(compeval '(define g (c-lambda (int) int "___result = ___arg1 *  
___arg1;")))

(pp (g 20))

;; The output is:
;;
;; % gsc -i compeval.scm
;; (time (test 1000))
;;     142113 ms real time
;;     10656 ms cpu time (6273 user, 4383 system)
;;     743 collections accounting for 2162 ms real time (1736 user,  
374 system)
;;     498876368 bytes allocated
;;     no minor faults
;;     no major faults
;; (999 999000000)
;; 400




More information about the Gambit-list mailing list