gencomp 確認 (5)

色々メモ。

scmlib.c

cgen.scm なコメントの一部を以下に。

;;   c file:
;;     - (ref unit 'preamble)     ;; for "generated by" message etc.
;;     - (ref unit 'pre-decl)     ;; #includes or #defines that are
;;                                ;;   needed before #include <gauche.h>
;;     - #include <gauche.h>
;;     - declarations of static array ;; scm__staticObjs etc.  not yet
;;                                ;; defined, but decls are put here
;;                                ;; to allow foward reference.
;;     - <decl part>
;;     - definitions of static array
;;     - <body part>
;;     - (ref unit 'init-prologue) ;; void Scm__InitFoobar etc.
;;     - <init part>
;;     - (ref unit 'init-epilogue) ;; closing brace etc.

基本出てくる .c なソレはこうなる、というナニ。gencomp に吸わせ対象な方々は Makefile によれば

  • objlib
  • compile
  • scmlib

の三つな模様。これらを見てみるに上記の はざっくり以下なカンジ?

  • scm__scRec 構造体型の scm__sc の定義と初期化
static SCM_CGEN_CONST struct scm__scRec {
  ScmString d199[162];
} scm__sc = {
  {   /* ScmString d199 */
      SCM_STRING_CONST_INITIALIZER("gauche", 6, 6),
  • scm__rcRec 構造体型の scm__rc の定義と初期化
static struct scm__rcRec {
  ScmCompiledCode d201[155];
  ScmWord d200[3019];
  ScmObj d198[1236];
} scm__rc = {
  {   /* ScmCompiledCode d201 */
        SCM_COMPILED_CODE_CONST_INITIALIZER(
            (ScmWord*)(SCM_OBJ(&scm__rc.d200[0])), 4,
            0, 1, 0, SCM_FALSE, SCM_NIL, SCM_FALSE,
            SCM_OBJ(&scm__rc.d201[2]), SCM_FALSE),
// 中略
  },
  {   /* ScmWord d200 */
    /* caaar */
    0x0000003a    /*   0 (LREF0) */,
    0x00000067    /*   1 (CAAR) */,
    0x00000063    /*   2 (CAR) */,
    0x00000014    /*   3 (RET) */,
// 中略
  },
  {   /* ScmObj d198 */
    SCM_UNBOUND,
  },
};
  • ScmCompiledCodel * の配列の toplevels の定義
static ScmCompiledCode *toplevels[] = {
  SCM_COMPILED_CODE(SCM_OBJ(&scm__rc.d201[2])),
// 中略
 NULL /*termination*/
};

中で使われているマクロをチェックしときます。

SCM_STRING_CONST_INITIALIZER
#define SCM_STRING_CONST_INITIALIZER(str, len, siz)             \
    { { SCM_CLASS2TAG(SCM_CLASS_STRING) }, NULL, \
      { SCM_STRING_IMMUTABLE|SCM_STRING_TERMINATED, (len), (siz), (str) } }

これは ScmString を初期化するナニですな。

CM_COMPILED_CODE_CONST_INITIALIZER
#define SCM_COMPILED_CODE_CONST_INITIALIZER(code, codesize, maxstack, reqargs, optargs, name, info, arginfo, parent, iform) \
    { { SCM_CLASS2TAG(SCM_CLASS_COMPILED_CODE) },        \
      (code), NULL, (codesize), 0, (maxstack),           \
      (reqargs), (optargs), (name), (info), (arginfo),   \
      (parent), (iform) }

ここまで来ると初期化するマクロを引用する位では済んでないカンジ。目視で .c を追い掛けると微妙なんだけど、これを生成しているナニはどこなのか。

cond

write-ext-module という手続きの中身を見るに以下。

(define (write-ext-module form)
  (cond ((ext-module-file)
         => (lambda (p) (write form p) (newline p)))))

これもあまり使ったコトない cond です。上記の例だと (ext-module-file) を評価したナニが #f でなければ => の先にある手続きの引数になる模様。
read/write という手続きも興味深い。

うーん

compile-toplevel-form 手続き、その名前の通りなんですが gencomp に吸わせる対象になってるナニについて、toplevel でどんな手続きが使われてるかを確認してみた方が良さげ。

$ grep '^(' compile.scm objlib.scm scmlib.scm

みたいな?
以下のソレ以外で

  • define-module
  • select-module
  • use
  • export
  • export-all
  • provide
  • define-macro
  • define-syntax
  • define

compile したナニが toplevel-constu-ret-code? だったら omit してる模様。
# 本当かいな

申し送り

  • 上記のソレをチェック
  • wrap してる emit-toplevel-executor チェック
  • cgen-emit-c も要確認