Skip to content

Commit

Permalink
Merge branch 'master' into debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jul 4, 2023
2 parents f6c99e7 + fef81e5 commit a0f1b6c
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 62 deletions.
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
### 5.4.0-prerelease0001
* Initial prerelease version for 5.4

### 5.3.6
* fshadeaot using resources instead of literal strings

### 5.3.5
* merged v52 (containing improved AOT)

### 5.3.4
* Added intrinsics for dynamically accessing matrix rows and columns (matrix.Row() / matrix.Column())
* Added intrinsics for dynamically accessing matrix elements (matrix.[x, y])
Expand Down
121 changes: 101 additions & 20 deletions src/FShade.Preprocessor/Interpreter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,51 @@ open System.Runtime.Loader


let rec getType (ctx : AssemblyLoadContext) (t : TypeReference) : System.Type =

let d = t.Resolve()
let mm = d.Module
let aName = mm.Assembly.FullName
let mName = mm.Name
if d.IsPrimitive then
match d.FullName with
| "System.Byte" -> typeof<System.Byte>
| "System.SByte" -> typeof<System.SByte>
| "System.UInt16" -> typeof<System.UInt16>
| "System.Int16" -> typeof<System.Int16>
| "System.UInt32" -> typeof<System.UInt32>
| "System.Int32" -> typeof<System.Int32>
| "System.UInt64" -> typeof<System.UInt64>
| "System.Int64" -> typeof<System.Int64>
| "System.Single" -> typeof<System.Single>
| "System.Double" -> typeof<System.Double>
| "System.Decimal" -> typeof<System.Decimal>
| "System.String" -> typeof<System.String>
| "System.Boolean" -> typeof<System.Boolean>
| "System.IntPtr" -> typeof<System.IntPtr>
| "System.UIntPtr" -> typeof<System.UIntPtr>
| _ -> failwithf "bad primitive: %A" d.FullName
else
let mm = d.Module

let aName, mName =
if mm.Assembly.Name.Name = "System.Runtime" then
"System.Runtime", "System.Runtime.dll"
else
mm.Assembly.FullName, mm.Name
//let aName = assName
//let mName = mm.Name

let ass = ctx.LoadFromAssemblyName(AssemblyName aName)
let mToken = d.MetadataToken.ToInt32()
let m = ass.GetModule(mName)
let ass = ctx.LoadFromAssemblyName(AssemblyName aName)
let mToken = d.MetadataToken.ToInt32()
let m = ass.GetModule(mName)

let targs =
match t with
| :? GenericInstanceType as t ->
t.GenericArguments |> Seq.map (getType ctx) |> Seq.toArray
| _ ->
[||]
let targs =
match t with
| :? GenericInstanceType as t ->
t.GenericArguments |> Seq.map (getType ctx) |> Seq.toArray
| _ ->
[||]

let r = m.ResolveType(mToken, targs, [||])
if targs.Length > 0 then r.MakeGenericType targs
else r
let r = m.ResolveType(mToken, targs, [||])
if targs.Length > 0 then r.MakeGenericType targs
else r

let getMethodBase (ctx : AssemblyLoadContext) (m : MethodReference) =
let d = m.Resolve()
Expand Down Expand Up @@ -116,6 +142,30 @@ module Patterns =
if i.OpCode = OpCodes.Pop then Some ()
else None

let (|Nop|_|) (i : Instruction) =
if i.OpCode = OpCodes.Nop then Some ()
else None

let (|Ceq|_|) (i : Instruction) =
if i.OpCode = OpCodes.Ceq then Some ()
else None
let (|Clt|_|) (i : Instruction) =
if i.OpCode = OpCodes.Clt then Some ()
else None
let (|Cgt|_|) (i : Instruction) =
if i.OpCode = OpCodes.Cgt then Some ()
else None
let (|CltUn|_|) (i : Instruction) =
if i.OpCode = OpCodes.Clt_Un then Some ()
else None
let (|CgtUn|_|) (i : Instruction) =
if i.OpCode = OpCodes.Cgt_Un then Some ()
else None

let (|Ldnull|_|) (i : Instruction) =
if i.OpCode = OpCodes.Ldnull then Some ()
else None

let (|Ldfld|_|) (ctx : AssemblyLoadContext) (i : Instruction) =
if i.OpCode = OpCodes.Ldsfld || i.OpCode = OpCodes.Ldfld then Some (getFieldInfo ctx (i.Operand :?> FieldReference))
else None
Expand All @@ -125,17 +175,29 @@ module Patterns =
else None

let (|Ldloc|_|) (i : Instruction) =
if i.OpCode = OpCodes.Ldloc then Some (i.Operand :?> VariableDefinition).Index
elif i.OpCode = OpCodes.Ldloc_S then Some (i.Operand :?> VariableDefinition).Index
if i.OpCode = OpCodes.Ldloc then
match i.Operand with
| :? VariableReference as v -> Some v.Index
| _ -> Some (System.Convert.ToInt32 i.Operand)
elif i.OpCode = OpCodes.Ldloc_S then
match i.Operand with
| :? VariableReference as v -> Some v.Index
| _ -> Some (System.Convert.ToInt32 i.Operand)
elif i.OpCode = OpCodes.Ldloc_0 then Some 0
elif i.OpCode = OpCodes.Ldloc_1 then Some 1
elif i.OpCode = OpCodes.Ldloc_2 then Some 2
elif i.OpCode = OpCodes.Ldloc_3 then Some 3
else None

let (|Stloc|_|) (i : Instruction) =
if i.OpCode = OpCodes.Stloc then Some (i.Operand :?> VariableDefinition).Index
elif i.OpCode = OpCodes.Stloc_S then Some (i.Operand :?> VariableDefinition).Index
if i.OpCode = OpCodes.Stloc then
match i.Operand with
| :? VariableReference as v -> Some v.Index
| _ -> Some (System.Convert.ToInt32 i.Operand)
elif i.OpCode = OpCodes.Stloc_S then
match i.Operand with
| :? VariableReference as v -> Some v.Index
| _ -> Some (System.Convert.ToInt32 i.Operand)
elif i.OpCode = OpCodes.Stloc_0 then Some 0
elif i.OpCode = OpCodes.Stloc_1 then Some 1
elif i.OpCode = OpCodes.Stloc_2 then Some 2
Expand Down Expand Up @@ -228,7 +290,7 @@ let rec private tryGetTopOfStackInternal (state : State) (instructions : Instruc
// arguments are unknown
idx-1, None

| Patterns.Tail | Patterns.Volatile ->
| Patterns.Tail | Patterns.Volatile | Patterns.Nop ->
// JIT helpers
tryGetTopOfStackInternal state instructions (idx - 1)

Expand Down Expand Up @@ -369,6 +431,25 @@ let rec private tryGetTopOfStackInternal (state : State) (instructions : Instruc
let idx, _ = tryGetTopOfStackInternal state instructions (idx - 1)
tryGetTopOfStackInternal state instructions idx

| Patterns.Ldnull ->
idx - 1, Some null

| Patterns.Ceq ->
let idx, v1 = tryGetTopOfStackInternal state instructions (idx - 1)
let idx, v2 = tryGetTopOfStackInternal state instructions idx
match v1, v2 with
| Some v1, Some v2 ->
idx, Some (Unchecked.equals v1 v2)
| _ ->
idx, None
//| Patterns.Cgt ->
// let idx, v1 = tryGetTopOfStackInternal state instructions (idx - 1)
// let idx, v2 = tryGetTopOfStackInternal state instructions idx
// match v1, v2 with
// | Some v1, Some v2 ->
// idx, Some (System.Convert.ToInt64 v1 > System.Convert.ToInt64 v2)
// | _ ->
// idx, None

| _ ->
Log.warn "bad instruction: %A" i
Expand Down
Loading

0 comments on commit a0f1b6c

Please sign in to comment.