Skip to content

Commit

Permalink
Merge PR #68 from feature/gr-emit
Browse files Browse the repository at this point in the history
Assembler: replace ILGenerator with GrEmit.
  • Loading branch information
ForNeVeR committed May 8, 2016
2 parents 3f4a665 + aeca04d commit f52671f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 40 deletions.
14 changes: 9 additions & 5 deletions Naggum.Assembler/Assembler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ let private findMethod (signature : MethodSignature) =
``type``.GetMethod (signature.Name, Array.ofList signature.ArgumentTypes)

let private buildMethodBody (m : MethodDefinition) (builder : MethodBuilder) =
let generator = builder.GetILGenerator ()
use generator = new GrEmit.GroboIL (builder)

m.Body
|> List.iter (function
| Call signature ->
let methodInfo = findMethod signature
generator.Emit (OpCodes.Call, methodInfo)
| LdcI4 i -> generator.Emit (OpCodes.Ldc_I4, i)
| Ldstr string -> generator.Emit (OpCodes.Ldstr, string)
| SimpleInstruction r -> generator.Emit r)
generator.Call methodInfo
| LdcI4 i -> generator.Ldc_I4 i
| Ldstr string -> generator.Ldstr string
| Simple Add -> generator.Add ()
| Simple Div -> generator.Div (false) // TODO: Signed division support
| Simple Mul -> generator.Mul ()
| Simple Ret -> generator.Ret ()
| Simple Sub -> generator.Sub ())

let private assembleUnit (assemblyBuilder : AssemblyBuilder) (builder : ModuleBuilder) = function
| Method m ->
Expand Down
51 changes: 27 additions & 24 deletions Naggum.Assembler/Naggum.Assembler.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,6 @@
<DocumentationFile>bin\Release\Naggum.Assembler.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Representation.fs" />
<Compile Include="Processor.fs" />
<Compile Include="Assembler.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Naggum.Backend\Naggum.Backend.fsproj">
<Name>Naggum.Backend</Name>
<Project>{243738f3-d798-4b09-8797-f90b21414b60}</Project>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
Expand All @@ -78,6 +54,33 @@
</Otherwise>
</Choose>
<Import Project="$(FSharpTargetsPath)" />
<ItemGroup>
<None Include="App.config" />
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Representation.fs" />
<Compile Include="Processor.fs" />
<Compile Include="Assembler.fs" />
<Compile Include="Program.fs" />
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="GrEmit">
<HintPath>..\packages\GrEmit.2.1.2\lib\net40\GrEmit.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<ProjectReference Include="..\Naggum.Backend\Naggum.Backend.fsproj">
<Name>Naggum.Backend</Name>
<Project>{243738f3-d798-4b09-8797-f90b21414b60}</Project>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
11 changes: 5 additions & 6 deletions Naggum.Assembler/Processor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
open System
open System.IO
open System.Reflection
open System.Reflection.Emit

open Naggum.Assembler.Representation
open Naggum.Backend
open Naggum.Backend.Matchers

let private (|SimpleOpCode|_|) = function
| Symbol "add" -> Some (SimpleInstruction OpCodes.Add)
| Symbol "div" -> Some (SimpleInstruction OpCodes.Div)
| Symbol "mul" -> Some (SimpleInstruction OpCodes.Mul)
| Symbol "ret" -> Some (SimpleInstruction OpCodes.Ret)
| Symbol "sub" -> Some (SimpleInstruction OpCodes.Sub)
| Symbol "add" -> Some (Simple Add)
| Symbol "div" -> Some (Simple Div)
| Symbol "mul" -> Some (Simple Mul)
| Symbol "ret" -> Some (Simple Ret)
| Symbol "sub" -> Some (Simple Sub)
| _ -> None

let private processMetadataItem = function
Expand Down
10 changes: 8 additions & 2 deletions Naggum.Assembler/Representation.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Naggum.Assembler.Representation

open System.Reflection
open System.Reflection.Emit

type MetadataItem =
| EntryPoint
Expand All @@ -18,11 +17,18 @@ type MethodSignature =
ArgumentTypes : Type list
ReturnType : Type }

type SimpleInstruction =
| Add
| Div
| Mul
| Ret
| Sub

type Instruction =
| Call of MethodSignature
| Ldstr of string
| LdcI4 of int
| SimpleInstruction of OpCode
| Simple of SimpleInstruction

type MethodDefinition =
{ Metadata : Set<MetadataItem>
Expand Down
4 changes: 4 additions & 0 deletions Naggum.Assembler/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="GrEmit" version="2.1.2" targetFramework="net461" />
</packages>
5 changes: 2 additions & 3 deletions Naggum.Test/ProcessorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
open System
open System.IO
open System.Reflection
open System.Reflection.Emit
open System.Text

open Xunit
Expand Down Expand Up @@ -50,7 +49,7 @@ let ``Simplest method should be processed`` () =
let result =
{ Name = "Stub"
Units = [Method { mainMethodDefinition with
Body = [ SimpleInstruction OpCodes.Ret ] } ] }
Body = [ Simple Ret ] } ] }
checkPreparationResult source [result]

[<Fact>]
Expand All @@ -70,5 +69,5 @@ let ``Hello world assembly should be processed`` () =
ReturnType = typeof<Void>
Body = [ Ldstr "Hello, world!"
Call consoleWriteLine
SimpleInstruction OpCodes.Ret ] } ] }
Simple Ret ] } ] }
checkPreparationResult source [result]

0 comments on commit f52671f

Please sign in to comment.