Anonymous avatar Anonymous committed 563a477

Start verifying the types of the args for LLVM functions; and call the right version of the function.

Comments (0)

Files changed (4)

       in
       fprintf f "(%a)" pp_pair p
     end
-  | Sjitclosure _ -> fprintf f "<#llvm>"
   | Snull -> fprintf f "<#null>"
   | Svector _ -> fprintf f "<#vector>"
   | Sstring s -> fprintf f "\"%s\"" s
   | Sint x -> const_int lltype x
   | Sfloat x -> const_float lltype x
   | Ssymbol s -> const_array i8_type (convert_string s)
+
+(*
   | Sjitclosure Jitcode (code, t) -> 
       (* TODO unify t and type_ *)
       code
 	  Some fn -> fn
 	| None -> assert false
     end
+*)
 
   | s ->
       let msg = 
 
 
 (* NOTE env probably needs to be passed through gen_llvm *)
-let jit_compile name globals env ast typeinfo =
+let jit_compile globals env ast typeinfo =
   let current_module = m in
   let type_ = get_type typeinfo ast in
 
     | _ -> assert false
   in
 
-  let current_function = define_function name (function_sig type_) 
+  let current_function = define_function "lambda" (function_sig type_) 
     current_module 
   in
   set_function_call_conv CallConv.fast current_function;
   | Spair of spair
   | Strue | Sfalse | Snull | Sunbound
   | Sclosure of sclosure
-  | Sjitclosure of jitcode
   | Spfn of sprimitive
   | Spfe of (env_frame -> environment -> sval array -> stack_frame list -> sval)
   | Sstring of string
 	      printf "Function has %d unsolved type variables\n%!" num_unsolved;
 
 	      if (num_free = 0) && (num_unsolved = 0) then begin
-		let llvm_code = Blub_llvm.jit_compile "foofoo" globals env ast tmap in
-		lambda.lam_jitcode <- ([||], llvm_code) :: lambda.lam_jitcode
+		let llvm_code = Blub_llvm.jit_compile globals env ast tmap in
+		let Fun (argtypes, _) = PMap.find ast tmap in
+
+		let argtypes = Array.map Blub_llvm.simplify_type argtypes in
+		printf "Adding JITcode for argtypes %a\n%!"
+		  (pp_array pp_type) argtypes;
+
+		lambda.lam_jitcode <- 
+		  (argtypes, llvm_code) :: lambda.lam_jitcode
 	      end;
 	      ()
 	    with Not_found ->
 	  let next = if acc == Sfalse then falseb else trueb in
 	  eval acc globals env rib stack next
       | B_abs (abs, next) -> begin
-	  if abs.lam_jitcode = [] then
-	    let closure = Sclosure (env, abs) in
-	    eval closure globals env rib stack next
-	  else
-	    (* choose some jitcode at random :-) *)
-	    let (_, tmp) :: _ = abs.lam_jitcode in
-	    let closure = Sjitclosure tmp in
-	    eval closure globals env rib stack next
+	  let closure = Sclosure (env, abs) in
+	  eval closure globals env rib stack next
 	end
       | B_ret ->
 	  let frame :: stack = stack in
 	  
   and apply acc globals env rib stack =
     match acc with
-	Sclosure (env, abs) ->
+	Sclosure (env, abs) -> begin
 	  let { lam_params = params;
 		lam_varargs = varargs;
 		lam_bytecode = body } = abs in
+
 	  let frame = if varargs then
 	    (* one param (the last) is the varargs, the rest are the
 	       regular args *)
 	    { vars = params; vals = rib }
 	  in
 	  let env = frame :: env in
-	  eval acc globals env rib stack body
 
-      | Sjitclosure closure ->
-	  let result = Blub_llvm.execute_function closure rib in
-	  eval result globals env rib stack B_ret
+	  (* Gather up the types of the arguments *)
+	  let arg_types = Array.map Blub_typecheck.type_of_sval frame.vals in
+	  let (at, _) :: _ = abs.lam_jitcode in
+	  printf "argtypes %a\n%!" (pp_array pp_type) at;
+
+	  (* Look for some LLVM code to save us *)
+	  try
+	    let code = List.assoc arg_types abs.lam_jitcode in
+	    let result = Blub_llvm.execute_function code frame.vals in
+	    eval result globals env rib stack B_ret
+	  with Not_found ->
+	    printf "Failed to find JITcode for argtypes %a (%d options)\n%!" 
+	      (pp_array pp_type) arg_types (List.length abs.lam_jitcode);
+	    eval acc globals env rib stack body
+	end
 
       | Spfn { prim_bc = fn } ->
 	  eval (fn rib) globals env rib stack B_ret
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.