From 2df81dd6450d324ea42274c6ffef713a0223c59a Mon Sep 17 00:00:00 2001 From: MIZUNO Hiroki Date: Fri, 15 Aug 2008 09:42:30 +0900 Subject: [PATCH] Support class compile. - We can generate valid abc with class. But cannot run it. --- src/abc.ml | 4 ++-- src/asm.ml | 10 ++++++---- src/codegen.ml | 2 +- src/instruction.ml | 3 +-- test/test_asm.ml | 19 ++++++++++--------- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/abc.ml b/src/abc.ml index 23184cd..f117d18 100644 --- a/src/abc.ml +++ b/src/abc.ml @@ -232,8 +232,8 @@ let bytes_of_abc { cpool=cpool; array bytes_of_method_info info; bytes_of_list metadata; (* todo: instances *) - array bytes_of_class classes; - HList.concat_map bytes_of_instance instances; + array bytes_of_instance instances; + HList.concat_map bytes_of_class classes; array bytes_of_script script; array bytes_of_method_body body ] diff --git a/src/asm.ml b/src/asm.ml index 67f66a9..3f0a71a 100644 --- a/src/asm.ml +++ b/src/asm.ml @@ -45,10 +45,12 @@ let fold_instruction f init = (**{6 Collecting some information}*) (** [collect_const meth] returns all constant value which contained by [meth]. *) -let collect_const = - fold_instruction +let collect_const meth= + Cpool.append + (method_const meth) + @@ fold_instruction (fun cpool i-> Cpool.append cpool (get_config i).const) - Cpool.empty + Cpool.empty meth (** [collect_klass meth] returns all class which contained by [meth]. *) @@ -95,7 +97,7 @@ let asm_method map index m = let info = { Abc.params=m.params; Abc.return=m.return; - Abc.name=index; + Abc.name=Cpool.multiname_nget m.name map.cpool; Abc.flags=m.flags } in let body = { Abc.method_sig=index; diff --git a/src/codegen.ml b/src/codegen.ml index aa34cf8..0001209 100644 --- a/src/codegen.ml +++ b/src/codegen.ml @@ -104,7 +104,7 @@ let rec generate_expr expr env = cinit = make_meth "cinit" []; iinit = init; interface = []; - methods = List.map snd methods; + methods = List.map snd @@ List.remove_assoc "init" methods; } in [NewClass klass] | Var name -> diff --git a/src/instruction.ml b/src/instruction.ml index 5454440..13c37c6 100644 --- a/src/instruction.ml +++ b/src/instruction.ml @@ -75,7 +75,6 @@ let klass_const {cname=cname; | _ -> failwith "must not happen" with Not_found -> Cpool.empty]; List.map multiname [cname;sname]; - List.map method_const methods] + List.map method_const (cinit::iinit::methods)] - #include "match.ml" diff --git a/test/test_asm.ml b/test/test_asm.ml index 73c18be..f1b07d1 100644 --- a/test/test_asm.ml +++ b/test/test_asm.ml @@ -25,21 +25,21 @@ test asm = assert_equal {Abc.empty_cpool with Abc.int = [~-42]; Abc.uint = [42]; - Abc.string=["";"print"]; + Abc.string=["";"main";"print"]; Abc.namespace = [{Abc.kind=0x08; Abc.ns_name=1}]; - Abc.multiname = [Abc.QName (1,2)]} cpool; - assert_equal [{Abc.params=[]; Abc.return=0; Abc.name=0; Abc.flags=0}] info; - assert_equal [{Abc.method_sig=0; + Abc.multiname = [Abc.QName (1,2);Abc.QName (1,3)]} cpool; + assert_equal [{Abc.params=[]; Abc.return=0; Abc.name=1; Abc.flags=0}] info; + assert_equal [{Abc.method_sig=0; Abc.max_stack=3; Abc.local_count=1; Abc.init_scope_depth=0; Abc.max_scope_depth=1; Abc.code=[u8 208; u8 48; - u8 93; u30 1; + u8 93; u30 2; u8 45; u30 1; (* pushint *) u8 46; u30 1; (* pushuint *) - u8 76; u30 1; u30 1; + u8 76; u30 2; u30 1; u8 41; u8 71]; Abc.exceptions=[]; @@ -50,12 +50,13 @@ test collect_const = List.fold_left Cpool.append Cpool.empty [string "hoge"; int 42; - multiname @@ make_qname "f"] in + multiname @@ make_qname "f"; + multiname @@ make_qname "g"] in let meth = - make_meth "" [ + make_meth "g" [ PushInt 42; NewFunction (make_meth "f" [PushString "hoge"])] in - OUnit.assert_equal ~printer:to_string cpool (collect_const meth) + assert_equal (Cpool.to_abc cpool) (Cpool.to_abc (collect_const meth)) module Set = Core.Std.Set -- 2.11.0