@@ -1000,42 +1000,78 @@ let private mapFile (r:Asn1AcnAst.AstRoot) (newTasMap : Map<TypeAssignmentInfo,
10001000let private reMapFile ( r : Asn1AcnAst.AstRoot ) ( icdStgFileName : string ) ( files0 : Asn1File list ) ( deps : Asn1AcnAst.AcnInsertedFieldDependencies ) ( lm : LanguageMacros ) ( f : Asn1File ) ( us : State ) =
10011001 let newModules , ns = f.Modules |> foldMap ( fun cs m -> reMapModule r icdStgFileName files0 deps lm m cs) us
10021002 { f with Modules = newModules}, ns
1003- (*
1004- //the following functions determines which functions are used by PDU types.
1005- //Only those functions are generated in the generated code.
1006- let determinGeneratedFunctions (r:Asn1AcnAst.AstRoot) (files: Asn1File list) (us:State) =
1007- let getFunctionCalls (tasId:ReferenceToType)=
1003+
1004+ let detectPDUs ( r : Asn1AcnAst.AstRoot ) ( us : State ) =
1005+ let functionTypes = Set.ofList [ AcnEncDecFunctionType]
1006+ let functionCalls = us.functionCalls |> Map.filter( fun z _ -> z.funcType = AcnEncDecFunctionType)
1007+ printfn " Detecting PDUs. Function calls: %d " functionCalls.Count
1008+
1009+ let memo = System.Collections.Generic.Dictionary< Caller, Set< Caller> >()
1010+ let rec getCallees2 bIsTass ( c : Caller ) =
1011+ let getCallees2_aux ( c : Caller ) =
1012+ if memo.ContainsKey( c) then
1013+ memo.[ c]
1014+ else
1015+ let result =
1016+ match functionCalls.ContainsKey( c) with
1017+ | false ->
1018+ match bIsTass with
1019+ | true -> Set.empty
1020+ | false -> Set.singleton c
1021+ | true ->
1022+ let callees = functionCalls.[ c]
1023+ let ret2 =
1024+ callees
1025+ |> List.map ( fun callee -> getCallees2 false { Caller.typeId = callee.typeId; funcType = callee.funcType})
1026+ |> List.fold Set.union Set.empty
1027+ match bIsTass with
1028+ | true -> ret2
1029+ | false -> Set.add c ret2
1030+ memo.[ c] <- result
1031+ result
1032+ getCallees2_ aux c
1033+
1034+ let callesList =
10081035 seq {
1009- for c in us.functionCalls do
1010- if c.Key.typeId.AsString.StartsWith (tasId.AsString) then
1011- yield (c.Key, c.Value)
1036+ for f in r.Files do
1037+ for m in f.Modules do
1038+ for tas in m.TypeAssignments do
1039+ for fncType in functionTypes do
1040+ //let msg = sprintf "Processing %s.%s. " m.Name.Value tas.Name.Value
1041+ //printf "%s" msg
1042+ let tsInfo = { TypeAssignmentInfo.modName = m.Name.Value; tasName = tas.Name.Value}
1043+ let caller = { Caller.typeId = tsInfo; funcType = fncType}
1044+ //let calles = getCallees true caller |> List.map(fun c -> c.typeId) |> List.distinct
1045+ let calles = getCallees2 true caller |> Set.map( fun c -> c.typeId)
1046+ //printfn "Calles detected: %d" calles.Length
1047+ yield calles
10121048 } |> Seq.toList
1049+ printfn " Calles detected: %d " callesList.Length
1050+ //let callesMap = callesList |> List.collect id |> List.distinct |> Set.ofList
1051+ let callesMap = callesList |> List.fold ( fun acc x -> Set.union acc x) Set.empty
10131052
1014- let allTasses =
1053+ //PDUS are the types that are not called by any other types
1054+ let pdus =
10151055 seq {
1016- for f in files do
1056+ for f in r.Files do
10171057 for m in f.Modules do
10181058 for tas in m.TypeAssignments do
1019- match tas.Type.isValidFunction with
1020- | None -> ()
1021- | Some isValidFunction ->
1022- match isValidFunction.funcName with
1023- | None -> ()
1024- | Some fncName ->
1025- let fncCalls = getFunctionCalls tas.Type.id
1026- yield {modName = m.Name.Value; tasName = tas.Name.Value; validationFunName = fncName; validationDependencies = fncCalls}
1027-
1028- } |> List.ofSeq
1029- let ret =
1030- match r.args.icdPdus with
1031- | None -> allTasses |> List.map(fun tas -> tas.modName, tas.tasName)
1032- | Some pdus ->
1033- let pduTas = pdus |> List.choose (fun pdu -> allTasses |> List.tryFind(fun tas -> tas.tasName = pdu))
1034- 0
1035- 0
1036-
1037- *)
1038-
1059+ let tsInfo = { TypeAssignmentInfo.modName = m.Name.Value; tasName = tas.Name.Value}
1060+ if not ( callesMap.Contains tsInfo) then
1061+ //printfn "PDU detected: %s.%s" m.Name.Value tas.Name.Value
1062+ let caller = { Caller.typeId = tsInfo; funcType = AcnEncDecFunctionType}
1063+ //let calles = getCallees true caller |> List.map(fun c -> c.typeId) |> List.distinct
1064+ let calles = getCallees2 true caller |> Set.map( fun c -> c.typeId)
1065+ if ( calles.Count > 0 ) then
1066+ printfn " PDU %s .%s detected. It has %d callees" m.Name.Value tas.Name.Value calles.Count
1067+ yield ( tsInfo, calles)
1068+ } |> Seq.toList
1069+
1070+ let pudsSorted = pdus |> List.sortByDescending( fun ( tas , calles ) -> calles.Count)
1071+ let maxToPrint = min 5 ( pudsSorted.Length)
1072+ printfn " PDUs detected: %d . Printing the first %d " pudsSorted.Length maxToPrint
1073+ for ( tas, calles) in pudsSorted |> List.take maxToPrint do
1074+ printfn " %s .%s references %d types" tas.modName tas.tasName calles.Count
10391075
10401076let calculateFunctionToBeGenerated ( r : Asn1AcnAst.AstRoot ) ( us : State ) =
10411077 let functionCalls = us.functionCalls
@@ -1051,6 +1087,7 @@ let calculateFunctionToBeGenerated (r:Asn1AcnAst.AstRoot) (us:State) =
10511087 if requiresAcn then yield AcnEncDecFunctionType;
10521088 } |> Seq.toList
10531089
1090+ (*
10541091 let rec getCallees (c: Caller)=
10551092 seq {
10561093 yield c
@@ -1061,6 +1098,27 @@ let calculateFunctionToBeGenerated (r:Asn1AcnAst.AstRoot) (us:State) =
10611098 for callee in callees do
10621099 yield! getCallees {Caller.typeId = callee.typeId; funcType = callee.funcType}
10631100 } |> Seq.toList
1101+ *)
1102+ let memo2 = System.Collections.Generic.Dictionary< Caller, Set< Caller> >()
1103+ let rec getCallees ( c : Caller )=
1104+ if memo2.Count% 50 = 0 then printfn " Processing %d " memo2.Count
1105+ match memo2.ContainsKey( c) with
1106+ | true -> memo2.[ c]
1107+ | false ->
1108+ let getCallees_aux ( c : Caller ) =
1109+ let result =
1110+ match functionCalls.ContainsKey c with
1111+ | false -> Set.singleton c
1112+ | true ->
1113+ let callees = functionCalls[ c]
1114+ let ret2 =
1115+ callees
1116+ |> List.map ( fun callee -> getCallees { Caller.typeId = callee.typeId; funcType = callee.funcType})
1117+ |> List.fold Set.union Set.empty
1118+ Set.add c ret2
1119+ memo2.[ c] <- result
1120+ result
1121+ getCallees_ aux c
10641122
10651123 let ret =
10661124 seq {
@@ -1078,6 +1136,7 @@ let calculateFunctionToBeGenerated (r:Asn1AcnAst.AstRoot) (us:State) =
10781136 | true -> yield ! getCallees caller
10791137 | false -> yield caller
10801138 } |> Set.ofSeq
1139+ printfn " Function calls detected: %d " ret.Count
10811140 match r.args.icdPdus with
10821141 | None -> ()
10831142 | Some _ ->
@@ -1130,5 +1189,7 @@ let DoWork (r:Asn1AcnAst.AstRoot) (icdStgFileName:string) (deps:Asn1AcnAst.AcnIn
11301189 acnParseResults = r.acnParseResults
11311190 deps = deps
11321191 icdHashes = ns.icdHashes
1133- callersSet = calculateFunctionToBeGenerated r ns
1192+ callersSet =
1193+ detectPDUs r ns //this function will print the PDUs detected
1194+ calculateFunctionToBeGenerated r ns
11341195 }
0 commit comments