- Previously,I use OCaml's include to use auto-generated code. But there are some problem around lexical-context.
Because of ignorant of context,cpp doesn't have these problem.
*.bytes
-src/match_core.ml
+src/match.ml
src/opcode.ml
src/opcode.mli
_build
$(OCAMLBUILD) main.byte
test: generate camlp4/TestCaseCollector.cmo
- rm -f _build/src/match.cm[oi]
+ rm -f _build/src/asm.cm[oi]
$(OCAMLBUILD) runner.byte --
-generate: src/match_core.ml src/opcode.ml src/opcode.mli
+generate: src/match.ml src/opcode.ml
-src/match_core.ml : util/instruction.txt
+src/match.ml : util/instruction.txt
$(OCAMLBUILD) -quiet util/instruction.byte -- -m < util/instruction.txt > $@
-src/opcode.mli : util/instruction.txt
- $(OCAMLBUILD) -quiet util/instruction.byte -- -i < util/instruction.txt > $@
-
src/opcode.ml : util/instruction.txt
$(OCAMLBUILD) -quiet util/instruction.byte -- -t < util/instruction.txt > $@
clean:
ocamlbuild -clean
- rm -f *~ */*~ *.abc *.cm[io] */*.cm[io] src/match_core.ml src/opcode.{ml,mli}
+ rm -f *~ */*~ *.abc *.cm[io] */*.cm[io] src/match.ml src/opcode.{ml,mli}
%.cmo:
$(OCAMLBUILD) $@
-"match.ml": pp(cpp -I../src)
+<asm.*>: pp(cpp -I../src)
<*.ml> and not "debug.ml":pp(camlp4o -parser OCaml -filter Camlp4ExceptionTracer -printer Auto),pkg_extlib
<*.byte>:pkg_extlib
open Base
open Cpool
+open Bytes
-include Opcode.B
-include Match
+type instruction =
+#include "opcode.ml"
+ and meth = {
+ name: string;
+ params: int list;
+ return: int;
+ flags:int;
+ instructions:instruction list;
+ traits: int list;
+ exceptions: int list;
+}
+
+type mmap = meth Pool.map
+type config = {
+ op: int;
+ args: Cpool.cmap * mmap -> Bytes.t list;
+ prefix: Cpool.cmap * mmap -> Bytes.t list;
+ const: Cpool.t;
+ meth: meth option;
+ stack: int;
+ scope: int;
+ count: int;
+}
+
+let const x _ = x
+let default = {
+ op=0;
+ args=const [];
+ prefix=const [];
+ const= Cpool.empty;
+ meth = None;
+ stack=0;
+ scope=0;
+ count=0;
+}
+
+#include "match.ml"
(* convert instruction *)
let add (max,current) n =
-include Opcode.S
+
+type instruction =
+#include "opcode.ml"
+ and meth = {
+ name: string;
+ params: int list;
+ return: int;
+ flags:int;
+ instructions:instruction list;
+ traits: int list;
+ exceptions: int list;
+}
val assemble : meth -> Abc.cpool * Abc.method_info list * Abc.method_body list
+++ /dev/null
-open Opcode.B
-open Cpool
-open Bytes
-
-type mmap = meth Pool.map
-type config = {
- op: int;
- args: Cpool.cmap * mmap -> Bytes.t list;
- prefix: Cpool.cmap * mmap -> Bytes.t list;
- const: Cpool.t;
- meth: meth option;
- stack: int;
- scope: int;
- count: int;
-}
-
-let const x _ = x
-let default = {
- op=0;
- args=const [];
- prefix=const [];
- const= Cpool.empty;
- meth = None;
- stack=0;
- scope=0;
- count=0;
-}
-
-#include <match_core.ml>
Printf.sprintf "| %s %s -> {default with %s}" name args' body
let output_types decls =
- let inst =
- Printf.sprintf "type instruction =\n%s and meth = {
- name: string;
- params: int list;
- return: int;
- flags:int;
- instructions:instruction list;
- traits: int list;
- exceptions: int list;
-}" (String.concat "\n" (List.map type_of_decl decls)) in
- Printf.printf "module type S = sig\n %s \nend\n\n module B = struct\n %s \nend\n" inst inst
-
-let output_inf decls =
- let inst =
- Printf.sprintf "type instruction =\n%s and meth = {
- name: string;
- params: int list;
- return: int;
- flags:int;
- instructions:instruction list;
- traits: int list;
- exceptions: int list;
-}" (String.concat "\n" (List.map type_of_decl decls)) in
- Printf.printf "module type S = sig\n %s \nend\n\nmodule B : S\n" inst
+ print_endline (String.concat "\n" (List.map type_of_decl decls))
let output_match decls =
let func =
done
with End_of_file ->
let decls' =
- List.sort (fun {name=a} {name=b} -> compare a b) !decls in
+ !decls in
if Sys.argv.(1) = "-t" then
output_types decls'
- else if Sys.argv.(1) = "-i" then
- output_inf decls'
else
output_match decls'
GetLocal_1: op=0xD1; stack=1;count=2
GetLocal_2: op=0xD2; stack=1;count=3
GetLocal_3: op=0xD3; stack=1;count=4
-GetLocal of int: op=0x62; stack=1; args=const [u30 arg0];count=(arg0+1)
+GetLocal of int: op=0x62; stack=1; args=const [Bytes.u30 arg0];count=(arg0+1)
SetLocal_0: op=0xD4; stack=1
SetLocal_1: op=0xD5; stack=1
SetLocal_2: op=0xD6; stack=1
SetLocal_3: op=0xD7; stack=1
-SetLocal of int: op=0x63; stack=1; args=const [u30 arg0]
+SetLocal of int: op=0x63; stack=1; args=const [Bytes.u30 arg0]
GetGlobalScope:op=0x64; stack=1
-GetScopeObject of int:op=0x65; stack=1; args=const[u8 arg0]
-GetSlot of int:op=0x6c; args=const [u30 arg0]
-SetSlot of int:op=0x6d; stack= ~-2; args=const [u30 arg0]
-GetGlobalSlot of int:op=0x6e; stack=1; args=const [u30 arg0]
-SetGlobalSlot of int:op=0x6f; stack= ~-1; args=const [u30 arg0]
+GetScopeObject of int:op=0x65; stack=1; args=const[Bytes.u8 arg0]
+GetSlot of int:op=0x6c; args=const [Bytes.u30 arg0]
+SetSlot of int:op=0x6d; stack= ~-2; args=const [Bytes.u30 arg0]
+GetGlobalSlot of int:op=0x6e; stack=1; args=const [Bytes.u30 arg0]
+SetGlobalSlot of int:op=0x6f; stack= ~-1; args=const [Bytes.u30 arg0]
GetProperty of Cpool.multiname: op=0x66; const=multiname arg0; args=fun (cmap,_) ->[multiname_get arg0 cmap]
Swap:op=0x2b
PopScope:op=0x1d; scope= ~-1
-NewObject of int:op=0x55; args=const [u30 arg0]
-NewArray of int:op=0x56; args=const [u30 arg0]
+NewObject of int:op=0x55; args=const [Bytes.u30 arg0]
+NewArray of int:op=0x56; args=const [Bytes.u30 arg0]
NewFunction of meth: op=0x40; stack=1; meth=Some arg0; args=fun (_,mmap)->[Bytes.u30 (Pool.get arg0 mmap)]