Skip to content

Commit

Permalink
Compiler: replace ILGenerator with GroboIL
Browse files Browse the repository at this point in the history
Close #67.
  • Loading branch information
ForNeVeR committed May 8, 2016
1 parent f52671f commit 1e8b06a
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 232 deletions.
41 changes: 24 additions & 17 deletions Naggum.Compiler/ClrGenerator.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Naggum.Compiler.ClrGenerator

open System
open System.Reflection.Emit

open Naggum.Backend.MaybeMonad
open Naggum.Backend.Reader
Expand Down Expand Up @@ -52,30 +51,38 @@ let nearestOverload (clrType : Type) methodName types =
else
Some (List.head methods)

type ClrCallGenerator(context : Context, typeBuilder : TypeBuilder, clrType : Type, methodName : string, arguments : SExp list,
gf : IGeneratorFactory) =
let args_seq = gf.MakeSequence context arguments
let arg_types = args_seq.ReturnTypes()
type ClrCallGenerator (context : Context,
clrType : Type,
methodName : string,
arguments : SExp list,
gf : IGeneratorFactory) =
let args = gf.MakeSequence context arguments
let arg_types = args.ReturnTypes()
let clrMethod = nearestOverload clrType methodName arg_types
interface IGenerator with
member this.Generate ilGen =
member __.Generate il =
args.Generate il
il.Call (Option.get clrMethod)

args_seq.Generate ilGen
ilGen.Emit(OpCodes.Call, Option.get clrMethod)
member this.ReturnTypes() =
[(Option.get clrMethod).ReturnType]

type InstanceCallGenerator(context : Context, typeBuilder : TypeBuilder, instance : SExp, methodName : string, arguments : SExp list, gf : IGeneratorFactory) =
type InstanceCallGenerator (context : Context,
instance : SExp,
methodName : string,
arguments : SExp list,
gf : IGeneratorFactory) =
interface IGenerator with
member this.Generate ilGen =
let inst_gen = gf.MakeGenerator context instance
let args_gen = gf.MakeSequence context arguments
let methodInfo = nearestOverload (inst_gen.ReturnTypes() |> List.head) methodName (args_gen.ReturnTypes())
member __.Generate il =
let instGen = gf.MakeGenerator context instance
let argsGen = gf.MakeSequence context arguments
let methodInfo = nearestOverload (instGen.ReturnTypes() |> List.head) methodName (argsGen.ReturnTypes())
if Option.isSome methodInfo then
inst_gen.Generate ilGen
args_gen.Generate ilGen
ilGen.Emit(OpCodes.Callvirt,Option.get methodInfo)
else failwithf "No overload found for method %A with types %A" methodName (args_gen.ReturnTypes())
instGen.Generate il
argsGen.Generate il
il.Call (Option.get methodInfo)
else failwithf "No overload found for method %A with types %A" methodName (argsGen.ReturnTypes())

member this.ReturnTypes () =
let inst_gen = gf.MakeGenerator context instance
let args_gen = gf.MakeSequence context arguments
Expand Down
8 changes: 5 additions & 3 deletions Naggum.Compiler/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ open System.Collections.Generic
open System.Reflection
open System.Reflection.Emit

open GrEmit

open Naggum.Runtime

type ContextValue =
|Local of LocalBuilder * Type
|Field of FieldBuilder * Type
|Arg of int * Type
| Local of GroboIL.Local * Type
| Field of FieldBuilder * Type
| Arg of int * Type

type Context =
val types : Dictionary<Symbol,Type>
Expand Down

0 comments on commit 1e8b06a

Please sign in to comment.