Gauche0.9 CiSE example

CiSE usage

simple example cise

;save as cise-simple-example.scm

(define-cise-stmt (mywhen test . body)
  `(if ,test (begin ,@body)))

(define-cfn test1 (aa::int  ) ::void
  (mywhen (> aa  50 )  (test2)) 
)

(define-cfn test2 (argc::int argv::char**) ::int
  (dotimes [i (strlen (aref argv 0))] (printf "%02x" (aref (aref argv 0) i)))
)

(define-cfn main (argc::int argv::char**) ::int
  (dotimes [i (strlen (aref argv 0))] (printf "%02x" (aref (aref argv 0) i)))
)
;compile cise-simple-example.scm
(use gauche.cgen)
(use gauche.cgen.cise)

(call-with-output-file "cise-simple-example.c"
    (lambda (po)
      (cise-translate (open-input-file "cise-simple-example.scm ") po)))

generated C code

#line 5 "cise-simple-example.scm"
 void test1(int aa){{
#line 6 "cise-simple-example.scm"
if ((aa)>(50)){{test2();}}}}
#line 9 "cise-simple-example.scm"
 int test2(int argc,char** argv){{
#line 10 "cise-simple-example.scm"
{int i=0;int cise__628=strlen((argv)[0]);for (; (i)<(cise__628); (i)++){printf("%02x",((argv)[0])[i]);}}}}
#line 13 "cise-simple-example.scm"
 int main(int argc,char** argv){{
#line 14 "cise-simple-example.scm"
{int i=0;int cise__629=strlen((argv)[0]);for (; (i)<(cise__629); (i)++){printf("%02x",((argv)[0])[i]);}}}}

simple example cise.stub

;save as stub-simple-example.scm
(declcode (.include <stdio.h>)
 "#ifndef M_PI
#define M_PI 3.1415926535897932384
#endif"
)

(define-cise-stmt (mywhen test . body)
  `(if ,test (begin ,@body)))

(define-cfn test1 (aa::int  ) ::void
  (mywhen (> aa  50 )  (test2)) 
)

(define-cfn test2 (argc::int argv::char**) ::int
  (dotimes [i (strlen (aref argv 0))] (printf "%02x" (aref (aref argv 0) i)))
)
;compile stub-simple-example.scm
(use gauche.parseopt)
(use gauche.parameter)
(use gauche.cgen)
(use gauche.cgen.stub)
(use file.util)
(use util.match)

(cgen-genstub "stub-simple-example.scm")

generated C code

#include <gauche.h>

#line 2 "stub-simple-example.scm"
#include <stdio.h>

#ifndef M_PI
#define M_PI 3.1415926535897932384
#endif
 void test1(int aa){{
#line 12 "stub-simple-example.scm"
if ((aa)>(50)){{test2();}}}}
 int test2(int argc,char** argv){{
#line 16 "stub-simple-example.scm"
{int i=0;int cise__871=strlen((argv)[0]);for (; (i)<(cise__871); (i)++){printf("%02x",((argv)[0])[i]);}}}}
void Scm_Init_stub_simple_example(ScmModule *mod){

}

more example cise.stub

;compile stub-simple-example.scm
;generate stub-simple-example.c and  stub-simple-example.h
(use gauche.parseopt)
(use gauche.parameter)
(use gauche.cgen)
(use gauche.cgen.stub)
(use file.util)
(use util.match)

(parameterize ([cgen-current-unit
                    (make <cgen-stub-unit>
                      :name "stub-simple-example"
		      :c-file  "stub-simple-example.c"
		      :h-file    "stub-simple-example.h"
                      :preamble '("/* Generated by my-genstub.  Do not edit. */")
                      :pre-decl '("#define AAAA 3.14")
                      :init-prologue "void test111(int aaaaa);\n"
		      )
		    ]
	       )
	      (with-input-from-file "stub-simple-example.scm"
		(cut port-fold
		     (lambda (form decl-strings?)
		       (cond [(and decl-strings? (string? form)) (cgen-decl form) #t]
			     [else (cgen-stub-parse-form form) #f]))
		     #t read))
     
	      (cgen-emit-c (cgen-current-unit))
	      (cgen-emit-h (cgen-current-unit))
) 

generated code

stub-simple-example.c

/* Generated by my-genstub.  Do not edit. */
#define AAAA 3.14
#include <gauche.h>

#line 2 "stub-simple-example.scm"
#include <stdio.h>

#ifndef M_PI
#define M_PI 3.1415926535897932384
#endif
 void test1(int aa){{
#line 12 "stub-simple-example.scm"
if ((aa)>(50)){{test2();}}}}
 int test2(int argc,char** argv){{
#line 16 "stub-simple-example.scm"
{int i=0;int cise__899=strlen((argv)[0]);for (; (i)<(cise__899); (i)++){printf("%02x",((argv)[0])[i]);}}}}
void test1(int aa);

}

stub-simple-example.h

/* Generated by my-genstub.  Do not edit. */

cgen code in Gauche-0.9

Gauche-0.9/lib/gauche/cgen.scm
Gauche-0.9/lib/gauche/cgen/*.scm

example in Gauche 0.9

in Gauche-0.9/src/

  • builtin-syms.scm

usage

mkdir gauche
gosh builtin-syms.scm

then generate builtin-syms.c gauche/builtin-syms.c

  • geninsn
mkdir -p ../lib/gauche/vm/
mkdir gosh geninsn

then generate vminsn.c gauche/vminsn.h ../lib/gauche/vm/insn.scm

  • vminsn.scm
  • genstub and *.stub

関連link