Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
382aabf
Initial SRM impl commit
TIHan Jan 2, 2020
538ef81
Fixing up generic arg
TIHan Jan 2, 2020
5c0c719
TODOs are finished, minus the original reader TODOs
TIHan Jan 3, 2020
960fef7
No asserts but still does not work
TIHan Jan 3, 2020
57cebd5
Fixed some crashes. Still crashing on parameters
TIHan Jan 3, 2020
6808a97
Still not working
TIHan Jan 3, 2020
0a54fa7
Fixed getter/setter
TIHan Jan 3, 2020
8b20a8c
Added isCachingEnabled
TIHan Jan 3, 2020
ecf7f29
Use GenericParameterCount for sanity
TIHan Jan 3, 2020
ed9a03e
make error msg better
TIHan Jan 3, 2020
2b3cb2a
Hopefully making a better decoder
TIHan Jan 3, 2020
87696ec
Fixing build
TIHan Jan 3, 2020
5eb0bac
Fixing build again
TIHan Jan 3, 2020
d89ae96
Resovling signature type kind for typeref
TIHan Jan 3, 2020
44d95c1
Assume boxed is an enum for decoding
TIHan Jan 3, 2020
1bfa452
Fixed resources
TIHan Jan 3, 2020
90dae83
Fixing up resource reading
TIHan Jan 3, 2020
f5ab26a
Fixing variance
TIHan Jan 3, 2020
813f986
Trying to fix generics
TIHan Jan 3, 2020
34287c0
Use mkILCallingConv
TIHan Jan 3, 2020
b47f250
Use scope local
TIHan Jan 3, 2020
1a80436
Determine typar count
TIHan Jan 3, 2020
4d42ab0
Using rawTypeKind
TIHan Jan 3, 2020
b15bf73
We have a type var offset issue
TIHan Jan 3, 2020
87e9322
Still trying to fix some generics
TIHan Jan 3, 2020
046e5dd
Still trying to fix type params
TIHan Jan 4, 2020
abd6387
Fixed native type
TIHan Jan 4, 2020
03cb359
Type params not working
TIHan Jan 4, 2020
1fe4ede
Some refactoring
TIHan Jan 5, 2020
c6e0c91
Fixed more issues with generic params
TIHan Jan 5, 2020
de65691
Fixed type ref enclosing name
TIHan Jan 5, 2020
daa3601
Fixed calling convention
TIHan Jan 5, 2020
f451c6f
Remove LRU cache
TIHan Jan 5, 2020
be0c4c1
Added copyright
TIHan Jan 5, 2020
41f38f8
Fixed property conv
TIHan Jan 5, 2020
aa42a48
Fixing value type in sigs
TIHan Jan 5, 2020
e1ed686
Caching more and reducing some allocations
TIHan Jan 5, 2020
b2fd7df
Merge remote-tracking branch 'upstream/master' into refactor/srm
TIHan Jan 5, 2020
085944e
Trying to fix labels
TIHan Jan 5, 2020
8a24885
Trying to fix stuff
TIHan Jan 5, 2020
33e7589
Hopefully fixes build
TIHan Jan 5, 2020
4d9e65d
Fixes on member reference
TIHan Jan 6, 2020
a3db32e
Fixed methspec reading
TIHan Jan 6, 2020
84a8012
Merge remote-tracking branch 'upstream/master' into refactor/srm
TIHan Jan 6, 2020
7b59f3f
Merge branch 'refactor/srm' of github.com:tihan/visualfsharp into ref…
TIHan Jan 6, 2020
9583050
Trying to get the mapping right for code labels
TIHan Jan 7, 2020
9dbf866
Merge remote-tracking branch 'upstream/master' into refactor/srm
TIHan Jan 14, 2020
12f34b4
Cleaning up diff
TIHan Jan 14, 2020
3da029f
Fixed bug where we read box instructions as unbox instructions
TIHan Jan 14, 2020
d50ff30
We need to recreate a type spec
TIHan Jan 14, 2020
7854df2
Merge remote-tracking branch 'upstream/master' into refactor/srm
TIHan Jan 15, 2020
6e09637
Merging with master
TIHan Jan 15, 2020
3b3cfcc
Merged with main
TIHan Oct 9, 2020
5dddde2
Fixing type parameters
TIHan Oct 9, 2020
0db824e
Fixed type parameters part 2
TIHan Oct 9, 2020
df33012
Remove cache
TIHan Oct 9, 2020
dab14ab
Trying to fix it
TIHan Oct 10, 2020
f991b73
Temporarily turn off caching
TIHan Oct 10, 2020
6e9b04d
Trying to get tests to pass
TIHan Oct 11, 2020
76ad9a3
Trying to get tests to pass
TIHan Oct 11, 2020
a9ed47a
Trying to get tests to pass
TIHan Oct 11, 2020
a916c30
Trying to get tests to pass
TIHan Oct 11, 2020
8d12107
Trying to get tests to pass
TIHan Oct 11, 2020
a5a91ab
remove comment
TIHan Oct 11, 2020
c074515
enabling cache
TIHan Oct 11, 2020
33be155
Trying to pass tests
TIHan Oct 11, 2020
98cb388
Fixing a few things
TIHan Oct 12, 2020
2a4c49a
better
TIHan Oct 12, 2020
a7ac632
Fixing stuff
TIHan Oct 12, 2020
67bbf6a
Revert
TIHan Oct 12, 2020
e45ec06
Fixed a minor issue
TIHan Oct 12, 2020
1ed43f4
Trying to fix it
TIHan Oct 13, 2020
982385f
Prefetching image
TIHan Oct 13, 2020
9d0758d
Remove that
TIHan Oct 13, 2020
204486b
Fixing tests
TIHan Oct 13, 2020
a49d436
Faster
TIHan Oct 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixing a few things
  • Loading branch information
TIHan committed Oct 12, 2020
commit 98cb388c954c22db4b7408bf7723c180c63f9377
111 changes: 62 additions & 49 deletions src/absil/ilread.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ open Internal.Utilities.Collections

#nowarn "9"

type ILReaderMetadataSnapshot = (obj * nativeint * int)
type ILReaderTryGetMetadataSnapshot = (* path: *) string * (* snapshotTimeStamp: *) System.DateTime -> ILReaderMetadataSnapshot option

[<RequireQualifiedAccess>]
type MetadataOnlyFlag = Yes | No

[<RequireQualifiedAccess>]
type ReduceMemoryFlag = Yes | No

[<AutoOpen>]
module rec ILBinaryReaderImpl =

Expand All @@ -41,11 +50,10 @@ module rec ILBinaryReaderImpl =

[<Sealed>]
type cenv(
peReader: PEReader,
peReaderOpt: PEReader option, // only set when reading full PE including code etc. for static linking
mdReader: MetadataReader,
pdbReaderProviderOpt: PdbReaderProvider option,
entryPointToken: int,
isMetadataOnly: bool,
canReduceMemory: bool,
sigTyProvider: ISignatureTypeProvider<ILType, MethodTypeVarOffset>,
localSigTyProvider: ISignatureTypeProvider<ILLocal, MethodTypeVarOffset>) =
Expand All @@ -60,11 +68,9 @@ module rec ILBinaryReaderImpl =

let isCachingEnabled = not canReduceMemory

member _.IsMetadataOnly = isMetadataOnly

member _.CanReduceMemory = canReduceMemory

member _.PEReader = peReader
member _.TryPEReader = peReaderOpt

member _.MetadataReader = mdReader

Expand Down Expand Up @@ -1858,8 +1864,7 @@ module rec ILBinaryReaderImpl =
Locals = [] //let locals = readMethodDebugInfo cenv methDef raw2nextLab - does not work yet and didn't in the original reader
}

let readILMethodBody (cenv: cenv) typarOffset (methDef: MethodDefinition) : ILMethodBody =
let peReader = cenv.PEReader
let readILMethodBody (cenv: cenv) (peReader: PEReader) typarOffset (methDef: MethodDefinition) : ILMethodBody =
let mdReader = cenv.MetadataReader

let methBodyBlock = peReader.GetMethodBody(methDef.RelativeVirtualAddress)
Expand All @@ -1880,32 +1885,44 @@ module rec ILBinaryReaderImpl =
SourceMarker = None // Note: The original reader never set this.
}

let readMethodBody (cenv: cenv) typarOffset (methDef: MethodDefinition) =
let lazyReadMethodBody (cenv: cenv) typarOffset (methDef: MethodDefinition) =
let mdReader = cenv.MetadataReader
let attrs = methDef.Attributes

let isPInvoke = int (attrs &&& MethodAttributes.PinvokeImpl) <> 0
let isAbstract = methDef.RelativeVirtualAddress = 0

if isPInvoke then
let import = methDef.GetImport()
let importAttrs = import.Attributes
let pInvokeMethod : PInvokeMethod =
{
Where = readILModuleRefFromModuleReference cenv (mdReader.GetModuleReference(import.Module))
Name = readString cenv import.Name
CallingConv = mkPInvokeCallingConvention importAttrs
CharEncoding = mkPInvokeCharEncoding importAttrs
NoMangle = int (importAttrs &&& MethodImportAttributes.ExactSpelling) <> 0
LastError = int (importAttrs &&& MethodImportAttributes.SetLastError) <> 0
ThrowOnUnmappableChar = mkPInvokeThrowOnUnmappableChar importAttrs
CharBestFit = mkPInvokeCharBestFit importAttrs
}
MethodBody.PInvoke(pInvokeMethod)
elif isAbstract then
MethodBody.Abstract
let implAttrs = methDef.ImplAttributes

let codeType = implAttrs &&& MethodImplAttributes.CodeTypeMask
let isPinvoke = int (attrs &&& MethodAttributes.PinvokeImpl) <> 0
let isAbstract = int (attrs &&& MethodAttributes.Abstract) <> 0
let isInternalCall = int (implAttrs &&& MethodImplAttributes.InternalCall) <> 0
let isUnmanaged = int (implAttrs &&& MethodImplAttributes.Unmanaged) <> 0

if codeType = MethodImplAttributes.Native && isPinvoke then
methBodyNative
elif isPinvoke then
mkMethBodyLazyAux (
lazy
let import = methDef.GetImport()
let importAttrs = import.Attributes
let pInvokeMethod : PInvokeMethod =
{
Where = readILModuleRefFromModuleReference cenv (mdReader.GetModuleReference(import.Module))
Name = readString cenv import.Name
CallingConv = mkPInvokeCallingConvention importAttrs
CharEncoding = mkPInvokeCharEncoding importAttrs
NoMangle = int (importAttrs &&& MethodImportAttributes.ExactSpelling) <> 0
LastError = int (importAttrs &&& MethodImportAttributes.SetLastError) <> 0
ThrowOnUnmappableChar = mkPInvokeThrowOnUnmappableChar importAttrs
CharBestFit = mkPInvokeCharBestFit importAttrs
}
MethodBody.PInvoke(pInvokeMethod))
elif isInternalCall || isAbstract || isUnmanaged || codeType <> MethodImplAttributes.IL then
methBodyAbstract
else
MethodBody.IL(readILMethodBody cenv typarOffset methDef)
match cenv.TryPEReader with
| Some peReader ->
mkMethBodyLazyAux(lazy MethodBody.IL(readILMethodBody cenv peReader typarOffset methDef))
| _ ->
methBodyNotAvailable

let readILMethodDef (cenv: cenv) (methDefHandle: MethodDefinitionHandle) : ILMethodDef =
match cenv.TryGetCachedILMethodDef methDefHandle with
Expand Down Expand Up @@ -1933,7 +1950,7 @@ module rec ILBinaryReaderImpl =
callingConv = mkILCallingConv si.Header,
parameters = parameters,
ret = ret,
body = mkMethBodyLazyAux (lazy readMethodBody cenv typarOffset methDef),
body = lazyReadMethodBody cenv typarOffset methDef,
isEntryPoint = isEntryPoint,
genericParams = readILGenericParameterDefs cenv typarOffset (methDef.GetGenericParameters()),
securityDeclsStored = readILSecurityDeclsStored cenv (methDef.GetDeclarativeSecurityAttributes()),
Expand All @@ -1948,10 +1965,11 @@ module rec ILBinaryReaderImpl =
let fieldDef = mdReader.GetFieldDefinition(fieldDefHandle)

let data =
if not cenv.IsMetadataOnly && int (fieldDef.Attributes &&& FieldAttributes.HasFieldRVA) <> 0 then
cenv.PEReader.GetSectionData(fieldDef.GetRelativeVirtualAddress()).GetContent().ToArray() // We should just return the immutable array instead of making a copy....
match cenv.TryPEReader with
| Some peReader when int (fieldDef.Attributes &&& FieldAttributes.HasFieldRVA) <> 0 ->
peReader.GetSectionData(fieldDef.GetRelativeVirtualAddress()).GetContent().ToArray() // We should just return the immutable array instead of making a copy....
|> Some
else
| _ ->
None

let literalValue =
Expand Down Expand Up @@ -2251,8 +2269,7 @@ module rec ILBinaryReaderImpl =
if not typeDef.IsNested then
yield readILPreTypeDef cenv typeDefHandle |]

let readILResources (cenv: cenv) =
let peReader = cenv.PEReader
let readILResources (cenv: cenv) (peReader: PEReader) =
let mdReader = cenv.MetadataReader

mdReader.ManifestResources
Expand Down Expand Up @@ -2287,7 +2304,7 @@ module rec ILBinaryReaderImpl =
|> List.ofSeq
|> mkILResources

let readModuleDef (peReader: PEReader) isMetadataOnly canReduceMemory (pdbReaderProviderOpt: PdbReaderProvider option) =
let readModuleDef (peReader: PEReader) (metadataOnly: MetadataOnlyFlag) (reduceMemory: ReduceMemoryFlag) (pdbReaderProviderOpt: PdbReaderProvider option) =
let nativeResources = readILNativeResources peReader

let subsys =
Expand Down Expand Up @@ -2333,11 +2350,16 @@ module rec ILBinaryReaderImpl =
let moduleDef = mdReader.GetModuleDefinition()
let ilModuleName = mdReader.GetString moduleDef.Name
let ilMetadataVersion = mdReader.MetadataVersion

let canReduceMemory = reduceMemory = ReduceMemoryFlag.Yes
let peReaderOpt =
if canReduceMemory && metadataOnly = MetadataOnlyFlag.Yes then None
else Some peReader

let cenv =
let sigTyProvider = SignatureTypeProvider()
let localSigTyProvider = LocalSignatureTypeProvider()
let cenv = cenv(peReader, mdReader, pdbReaderProviderOpt, entryPointToken, isMetadataOnly, canReduceMemory, sigTyProvider, localSigTyProvider)
let cenv = cenv(peReaderOpt, mdReader, pdbReaderProviderOpt, entryPointToken, canReduceMemory, sigTyProvider, localSigTyProvider)
sigTyProvider.cenv <- cenv
localSigTyProvider.cenv <- cenv
cenv
Expand Down Expand Up @@ -2372,18 +2394,9 @@ module rec ILBinaryReaderImpl =
PhysicalAlignment = alignPhys
ImageBase = imageBaseReal
MetadataVersion = ilMetadataVersion
Resources = readILResources cenv
Resources = readILResources cenv peReader
}, ilAsmRefs

type ILReaderMetadataSnapshot = (obj * nativeint * int)
type ILReaderTryGetMetadataSnapshot = (* path: *) string * (* snapshotTimeStamp: *) System.DateTime -> ILReaderMetadataSnapshot option

[<RequireQualifiedAccess>]
type MetadataOnlyFlag = Yes | No

[<RequireQualifiedAccess>]
type ReduceMemoryFlag = Yes | No

type ILReaderOptions =
{ pdbDirPath: string option
reduceMemoryUsage: ReduceMemoryFlag
Expand Down Expand Up @@ -2422,7 +2435,7 @@ let OpenILModuleReaderAux (memory: ReadOnlyByteMemory) (opts: ILReaderOptions) =
match peReader.TryOpenAssociatedPortablePdb(pdbDirPath, streamProvider) with
| true, pdbReaderProvider, pdbPath -> Some(pdbReaderProvider, pdbPath)
| _ -> None)
let ilModuleDef, ilAsmRefs = readModuleDef peReader (opts.metadataOnly = MetadataOnlyFlag.Yes) (opts.reduceMemoryUsage = ReduceMemoryFlag.Yes) pdbReaderProviderOpt
let ilModuleDef, ilAsmRefs = readModuleDef peReader opts.metadataOnly opts.reduceMemoryUsage pdbReaderProviderOpt
{ new Object() with

override _.Finalize() =
Expand Down