(module saw_jvmgen
   (import type_type
	   ast_var		; local/global
	   ast_node		; atom
	   object_class		; tclass
	   backend_backend
	   jvm_env
	   saw_woodcutter
	   saw_defs
	   )
   (export (saw-jvmgen b::backend var::global env::env)
	   )
   )

(define (saw-jvmgen b::backend var::global env::env) ;()
   (with-access::global var (type module import value)
      (with-access::sfun value (args body)
	 (env-current-method-set! env var)
	 (env-vcount-set! env 0)
	 (env-code-set! env '())
	 (env-locals-set! env '())
	 (env-freelocals-set! env '())
	 (env-maplocals-set! env '())
	 (env-stack-set! env '())
	 (let ( (l (global->blocks b var)) )
	    (for-each (lambda (ins) (gen-ins ins env)) l) )
	 `(method ,(get-global-name env var)
		  ,(compile-formals args env)
		  (reg1 reg2 ,@(reverse! (env-locals env)))
		  ,@(reverse! (env-code env)) ))))

(define (compile-formals l env) ;()
   (if (null? l)
       l
       (let ( (v (car l)) (rest (compile-formals (cdr l) env)) )
	  (let ( (type (compile-type v env))  (name (local-name v)) )
	     (if (memq type '(double long))
		 (cons* name '_ rest)
		 (cons name rest) )))))

;;
(define (gen-ins ins::rtl_ins env) ;()
   (with-access::rtl_ins ins (fun)
      '(if label (_label env label))
      '(gen fun env) ))

(define-generic (gen ins::rtl_fun env)) ;()

;     (class rtl_entry::rtl_fun)
;     (class rtl_nop::rtl_fun)
;     (class rtl_mov::rtl_fun)
(define-method  (gen ins::rtl_return env) ;()
   '() )
   
;     (class rtl_return::rtl_notseq)
;     (class rtl_loadi::rtl_fun constant::atom)
;     (class rtl_loadg::rtl_fun var::global)
;     (class rtl_loadfun::rtl_fun var::global)
;     (class rtl_storeg::rtl_fun var::global)
;     (class rtl_globalref::rtl_fun var::global)
;;     (class rtl_if::rtl_notseq)
;;     (class rtl_select::rtl_notseq type::type patterns)
;     (class rtl_call::rtl_fun var::global)
;     (class rtl_apply::rtl_fun)
;     (class rtl_lightfuncall::rtl_fun)
;     (class rtl_funcall::rtl_fun)
;;     (class rtl_pragma::rtl_fun format::bstring)
;     (class rtl_getfield::rtl_fun name::bstring type::type)
;     (class rtl_setfield::rtl_fun name::bstring type::type)
;     (class rtl_new::rtl_fun type::type)
;     (class rtl_valloc::rtl_fun type::type)
;     (class rtl_vref::rtl_fun type::type)
;     (class rtl_vset::rtl_fun type::type)
;     (class rtl_vlength::rtl_fun type::type)
;     (class rtl_isa::rtl_fun type::type)
;     (class rtl_cast::rtl_fun type::type)
;     (class rtl_protect::rtl_fun)
;     (class rtl_jumpexit::rtl_notseq)
;     (class rtl_fail::rtl_notseq)
;     (class rtl_makebox::rtl_fun)
;     (class rtl_boxref::rtl_fun)
;     (class rtl_boxset::rtl_fun)
;    (class rtl_ifeq::rtl_notseq then::int)
;    (class rtl_ifne::rtl_notseq then::int)
;    (class rtl_go::rtl_notseq to::int)
;    (class rtl_switch::rtl_notseq type::type patterns labels)
;    (class stl_loadr::rtl_fun reg::rtl_reg)
;    (class stl_storer::rtl_fun reg::rtl_reg)
;    (class stl_pop::rtl_fun)
