Skip to content

Commit 8b49c07

Browse files
committed
Add function call tracking and conditional function inclusion
Introduce mechanism to track and manage function calls within the state. Add `calculateFunctionToBeGenerated` to determine necessary functions. Update functions to propagate and update state with call info. Handle top-level type assignments and update state accordingly. Introduce `callersSet` in `AstRoot` to conditionally include/exclude functions. Combine related function types in `FunctionType` enumeration. Update `Caller` and `Callee` types to use `TypeAssignmentInfo`. Optimize generated code by excluding unused functions based on `callersSet`.
1 parent 47da3f8 commit 8b49c07

File tree

8 files changed

+201
-56
lines changed

8 files changed

+201
-56
lines changed

BackendAst/DAstACN.fs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,14 @@ let private createAcnFunction (r: Asn1AcnAst.AstRoot)
330330

331331
let errCodStr = errCodes |> List.map(fun x -> EmitTypeAssignment_def_err_code x.errCodeName (BigInteger x.errCodeValue) x.comment) |> List.distinct
332332
let funcDef = Some(EmitTypeAssignment_primitive_def varName sStar funcName (typeDefinition.longTypedefName2 lm.lg.hasModules) errCodStr (t.acnMaxSizeInBits = 0I) nMaxBytesInACN ( t.acnMaxSizeInBits) prms soSparkAnnotations codec)
333-
func, funcDef, auxiliaries, icdResult, ns1a
333+
let ns2a =
334+
match t.id.topLevelTas with
335+
| None -> ns1a
336+
| Some tasInfo ->
337+
let caller = {Caller.typeId = tasInfo; funcType= UperEncDecFunctionType}
338+
let callee = {Callee.typeId = tasInfo; funcType=IsValidFunctionType}
339+
addFunctionCallToState ns1a caller callee
340+
func, funcDef, auxiliaries, icdResult, ns2a
334341

335342
let icdAux, ns3 =
336343
match icdResult with
@@ -2523,9 +2530,16 @@ let createReferenceFunction (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.AcnInsertedF
25232530
let funcBodyContent = callBaseTypeFunc lm pp baseFncName codec
25242531
Some ({AcnFuncBodyResult.funcBody = funcBodyContent; errCodes = [errCode]; localVariables = []; bValIsUnReferenced= false; bBsIsUnReferenced=false; resultExpr=resultExpr; auxiliaries=[]; icdResult = icd}), us)
25252532

2533+
let ns =
2534+
match t.id.topLevelTas with
2535+
| None -> us
2536+
| Some tasInfo ->
2537+
let caller = {Caller.typeId = tasInfo; funcType=AcnEncDecFunctionType}
2538+
let callee = {Callee.typeId = {TypeAssignmentInfo.modName = o.modName.Value; tasName=o.tasName.Value} ; funcType=AcnEncDecFunctionType}
2539+
addFunctionCallToState us caller callee
25262540

25272541
let soSparkAnnotations = Some(sparkAnnotations lm (typeDefinition.longTypedefName2 lm.lg.hasModules) codec)
2528-
let a, ns = createAcnFunction r deps lm codec t typeDefinition isValidFunc funcBody (fun atc -> true) soSparkAnnotations [] us
2542+
let a, ns = createAcnFunction r deps lm codec t typeDefinition isValidFunc funcBody (fun atc -> true) soSparkAnnotations [] ns
25292543
Some a, ns
25302544

25312545
| Some encOptions ->

BackendAst/DAstConstruction.fs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,9 @@ let private createChoiceChild (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (m:Asn
729729
let private createReferenceType (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.AcnInsertedFieldDependencies) (lm:LanguageMacros) (m:Asn1AcnAst.Asn1Module) (pi : Asn1Fold.ParentInfo<ParentInfoData> option) (t:Asn1AcnAst.Asn1Type) (o:Asn1AcnAst.ReferenceType) (newResolvedType:Asn1Type, us:State) =
730730
let newPrms, us0 = t.acnParameters |> foldMap(fun ns p -> mapAcnParameter r deps lm m t p ns) us
731731
let defOrRef = lm.lg.definitionOrRef o.definitionOrRef
732-
let equalFunction = DAstEqual.createReferenceTypeEqualFunction r lm t o defOrRef newResolvedType
733-
let initFunction = DAstInitialize.createReferenceType r lm t o defOrRef newResolvedType
734-
let isValidFunction, s1 = DastValidate2.createReferenceTypeFunction r lm t o defOrRef newResolvedType us
732+
let equalFunction, s00 = DAstEqual.createReferenceTypeEqualFunction r lm t o defOrRef newResolvedType us
733+
let initFunction,s0 = DAstInitialize.createReferenceType r lm t o defOrRef newResolvedType s00
734+
let isValidFunction, s1 = DastValidate2.createReferenceTypeFunction r lm t o defOrRef newResolvedType s0
735735
let uperEncFunction, s2 = DAstUPer.createReferenceFunction r lm Codec.Encode t o defOrRef isValidFunction newResolvedType s1
736736
let uperDecFunction, s3 = DAstUPer.createReferenceFunction r lm Codec.Decode t o defOrRef isValidFunction newResolvedType s2
737737
let acnEncFunction, s4 = DAstACN.createReferenceFunction r deps lm Codec.Encode t o defOrRef isValidFunction newResolvedType s3
@@ -1036,6 +1036,64 @@ let determinGeneratedFunctions (r:Asn1AcnAst.AstRoot) (files: Asn1File list) (us
10361036
10371037
*)
10381038

1039+
1040+
let calculateFunctionToBeGenerated (r:Asn1AcnAst.AstRoot) (us:State) =
1041+
let functionCalls = us.functionCalls
1042+
let requiresUPER = r.args.encodings |> Seq.exists ( (=) Asn1Encoding.UPER)
1043+
let requiresAcn = r.args.encodings |> Seq.exists ( (=) Asn1Encoding.ACN)
1044+
1045+
let functionTypes =
1046+
seq {
1047+
yield InitFunctionType;
1048+
yield IsValidFunctionType;
1049+
if r.args.GenerateEqualFunctions then yield EqualFunctionType;
1050+
if requiresUPER then yield UperEncDecFunctionType;
1051+
if requiresAcn then yield AcnEncDecFunctionType;
1052+
} |> Seq.toList
1053+
1054+
let rec getCallees (c: Caller)=
1055+
seq {
1056+
yield c
1057+
match functionCalls.ContainsKey c with
1058+
| false -> ()
1059+
| true ->
1060+
let callees = functionCalls[c]
1061+
for callee in callees do
1062+
yield! getCallees {Caller.typeId = callee.typeId; funcType = callee.funcType}
1063+
} |> Seq.toList
1064+
1065+
let ret =
1066+
seq {
1067+
for f in r.Files do
1068+
for m in f.Modules do
1069+
let tasToGenerate, bFindCalles =
1070+
match r.args.icdPdus with
1071+
| None -> m.TypeAssignments, false
1072+
| Some pdus -> pdus |> List.choose (fun pdu -> m.TypeAssignments |> List.tryFind(fun tas -> tas.Name.Value = pdu)), true
1073+
for tas in tasToGenerate do
1074+
for fncType in functionTypes do
1075+
let tsInfo = {TypeAssignmentInfo.modName = m.Name.Value; tasName = tas.Name.Value}
1076+
let caller = {Caller.typeId = tsInfo; funcType = fncType}
1077+
match bFindCalles with
1078+
| true -> yield! getCallees caller
1079+
| false -> yield caller
1080+
} |> Set.ofSeq
1081+
match r.args.icdPdus with
1082+
| None -> ()
1083+
| Some _ ->
1084+
//let's print in the console the functions that will not be generated
1085+
let s = ret
1086+
for f in r.Files do
1087+
for m in f.Modules do
1088+
for tas in m.TypeAssignments do
1089+
for fncType in functionTypes do
1090+
let tsInfo = {TypeAssignmentInfo.modName = m.Name.Value; tasName = tas.Name.Value}
1091+
let caller = {Caller.typeId = tsInfo; funcType = fncType}
1092+
if not (s.Contains caller) then
1093+
printfn "Function %A will not be generated for %s.%s" fncType tsInfo.modName tsInfo.tasName
1094+
1095+
ret
1096+
10391097
let DoWork (r:Asn1AcnAst.AstRoot) (icdStgFileName:string) (deps:Asn1AcnAst.AcnInsertedFieldDependencies) (lang:CommonTypes.ProgrammingLanguage) (lm:LanguageMacros) (encodings: CommonTypes.Asn1Encoding list) : AstRoot=
10401098
let l = lang
10411099

@@ -1072,4 +1130,5 @@ let DoWork (r:Asn1AcnAst.AstRoot) (icdStgFileName:string) (deps:Asn1AcnAst.AcnIn
10721130
acnParseResults = r.acnParseResults
10731131
deps = deps
10741132
icdHashes = ns.icdHashes
1133+
callersSet = calculateFunctionToBeGenerated r ns
10751134
}

BackendAst/DAstEqual.fs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ let createChoiceEqualFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1
285285
createEqualFunction_any r lm t typeDefinition (EqualBodyStatementList isEqualBody)
286286
//createCompositeEqualFunction r l lm t typeDefinition isEqualBody (stgMacroCompDefFunc l)
287287

288-
let createReferenceTypeEqualFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst.Asn1Type) (o:Asn1AcnAst.ReferenceType) (defOrRef:TypeDefinitionOrReference) (baseType:Asn1Type) =
288+
let createReferenceTypeEqualFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst.Asn1Type) (o:Asn1AcnAst.ReferenceType) (defOrRef:TypeDefinitionOrReference) (baseType:Asn1Type) (us:State) =
289289
//let isEqualFuncName = getEqualFuncName r l lm t.id
290290
let isEqualFuncName = getFuncName r lm defOrRef
291291
let typeDefinitionName = lm.lg.getLongTypedefName defOrRef
@@ -327,7 +327,7 @@ let createReferenceTypeEqualFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros)
327327
| IA5String _
328328
| ReferenceType _ ->
329329
let bs = baseType.equalFunction
330-
createEqualFunction_any r lm t defOrRef bs.isEqualBody
330+
createEqualFunction_any r lm t defOrRef bs.isEqualBody, us
331331
| OctetString _
332332
| BitString _
333333
| ObjectIdentifier _
@@ -343,6 +343,15 @@ let createReferenceTypeEqualFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros)
343343
let val2 = lm.lg.getParamTypeSuffix t "2" CommonTypes.Codec.Encode
344344

345345
let stgMacroDefFunc = lm.equal.PrintEqualDefinitionComposite
346+
let ns =
347+
match t.id.topLevelTas with
348+
| None ->
349+
printfn "No type assignment info for %A" t.id
350+
us
351+
| Some tasInfo ->
352+
let caller = {Caller.typeId = tasInfo; funcType=EqualFunctionType}
353+
let callee = {Callee.typeId = {TypeAssignmentInfo.modName = o.modName.Value; tasName=o.tasName.Value} ; funcType=EqualFunctionType}
354+
addFunctionCallToState us caller callee
346355

347356
let isEqualFunc, isEqualFuncDef =
348357
match isEqualFuncName with
@@ -358,5 +367,5 @@ let createReferenceTypeEqualFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros)
358367
isEqualBody = EqualBodyStatementList (isEqualBody )
359368
isEqualFunc = isEqualFunc
360369
isEqualFuncDef = isEqualFuncDef
361-
}
370+
}, ns
362371

BackendAst/DAstInitialize.fs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ let createChoiceInitFunc (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAs
12201220
List.head
12211221
createInitFunctionCommon r lm t typeDefinition funcBody initTasFunction testCaseFuncs (constantInitExpression getChildExpression) (constantInitExpression getChildExpressionGlobal) nonEmbeddedChildrenFuncs [] []
12221222

1223-
let createReferenceType (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst.Asn1Type) (o :Asn1AcnAst.ReferenceType) (typeDefinition:TypeDefinitionOrReference) (baseType:Asn1Type) =
1223+
let createReferenceType (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst.Asn1Type) (o :Asn1AcnAst.ReferenceType) (typeDefinition:TypeDefinitionOrReference) (baseType:Asn1Type) (us:State) =
12241224
let initChildWithInitFunc = lm.init.initChildWithInitFunc
12251225
let nonEmbeddedChildrenFuncs = []
12261226

@@ -1246,10 +1246,19 @@ let createReferenceType (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst
12461246
let bs = baseType.initFunction
12471247
match TypesEquivalence.uperEquivalence t1 t1WithExtensions with
12481248
| false ->
1249-
createInitFunctionCommon r lm t typeDefinition bs.initByAsn1Value bs.initTas bs.automaticTestCases bs.initExpressionFnc bs.initExpressionGlobalFnc bs.nonEmbeddedChildrenFuncs [] []
1249+
createInitFunctionCommon r lm t typeDefinition bs.initByAsn1Value bs.initTas bs.automaticTestCases bs.initExpressionFnc bs.initExpressionGlobalFnc bs.nonEmbeddedChildrenFuncs [] [], us
12501250
| true ->
12511251
match t.isComplexType with
12521252
| true ->
1253+
let ns =
1254+
match t.id.topLevelTas with
1255+
| None ->
1256+
printfn "No type assignment info for %A" t.id
1257+
us
1258+
| Some tasInfo ->
1259+
let caller = {Caller.typeId = tasInfo; funcType=InitFunctionType}
1260+
let callee = {Callee.typeId = {TypeAssignmentInfo.modName = o.modName.Value; tasName=o.tasName.Value} ; funcType=InitFunctionType}
1261+
addFunctionCallToState us caller callee
12531262
let baseFncName, baseGlobalName =
12541263
let funcName = typeDefinitionName + (lm.init.methodNameSuffix())
12551264
let globalName = typeDefinitionName + "_constant"
@@ -1265,7 +1274,7 @@ let createReferenceType (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst
12651274
let resVar = p.arg.asIdentifier
12661275
let funcBody = initChildWithInitFunc (lm.lg.getPointer p.arg) baseFncName
12671276
{InitFunctionResult.funcBody = funcBody; resultVar = resVar; localVariables = []}
1268-
createInitFunctionCommon r lm t typeDefinition bs.initByAsn1Value initTasFunction bs.automaticTestCases constantInitExpression constantInitExpressionGlobal nonEmbeddedChildrenFuncs [] []
1277+
createInitFunctionCommon r lm t typeDefinition bs.initByAsn1Value initTasFunction bs.automaticTestCases constantInitExpression constantInitExpressionGlobal nonEmbeddedChildrenFuncs [] [], ns
12691278
| false ->
1270-
createInitFunctionCommon r lm t typeDefinition bs.initByAsn1Value bs.initTas bs.automaticTestCases bs.initExpressionFnc bs.initExpressionGlobalFnc bs.nonEmbeddedChildrenFuncs [] []
1279+
createInitFunctionCommon r lm t typeDefinition bs.initByAsn1Value bs.initTas bs.automaticTestCases bs.initExpressionFnc bs.initExpressionGlobalFnc bs.nonEmbeddedChildrenFuncs [] [], us
12711280

BackendAst/DAstUPer.fs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ let internal createUperFunction (r:Asn1AcnAst.AstRoot)
8888
let sStar = lm.lg.getStar p.arg
8989
let isValidFuncName = match isValidFunc with None -> None | Some f -> f.funcName
9090
let sInitialExp = ""
91-
let func, funcDef, auxiliaries =
91+
let func, funcDef, auxiliaries,ns2 =
9292
match funcName with
93-
| None -> None, None, []
93+
| None -> None, None, [], ns
9494
| Some funcName ->
9595
let content = funcBody (NestingScope.init t.acnMaxSizeInBits t.uperMaxSizeInBits []) p false
9696
let bodyResult_funcBody, errCodes, bodyResult_localVariables, bBsIsUnreferenced, bVarNameIsUnreferenced, auxiliaries =
@@ -106,7 +106,15 @@ let internal createUperFunction (r:Asn1AcnAst.AstRoot)
106106

107107
let errCodStr = errCodes |> List.map(fun x -> (EmitTypeAssignment_def_err_code x.errCodeName) (BigInteger x.errCodeValue))
108108
let funcDef = Some(EmitTypeAssignment_def varName sStar funcName (lm.lg.getLongTypedefName typeDefinition) errCodStr (t.uperMaxSizeInBits = 0I) (BigInteger (ceil ((double t.uperMaxSizeInBits)/8.0))) ( t.uperMaxSizeInBits) soSparkAnnotations (t.uperMaxSizeInBits = 0I) codec)
109-
func, funcDef, auxiliaries
109+
let ns2 =
110+
match t.id.topLevelTas with
111+
| None -> ns
112+
| Some tasInfo ->
113+
let caller = {Caller.typeId = tasInfo; funcType= UperEncDecFunctionType}
114+
let callee = {Callee.typeId = tasInfo; funcType=IsValidFunctionType}
115+
addFunctionCallToState ns caller callee
116+
117+
func, funcDef, auxiliaries, ns2
110118

111119

112120
let ret =
@@ -118,7 +126,7 @@ let internal createUperFunction (r:Asn1AcnAst.AstRoot)
118126
funcBody_e = funcBody_e
119127
auxiliaries = auxiliaries
120128
}
121-
ret, ns
129+
ret, ns2
122130

123131
let getIntDecFuncSuffix (intClass:Asn1AcnAst.IntegerClass) =
124132
match intClass with
@@ -926,6 +934,14 @@ let createReferenceFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (codec:C
926934
match areUperTypesEquivalent with
927935
| true ->
928936
let soSparkAnnotations = TL "UPER_REF_02" (fun () -> Some(sparkAnnotations lm (lm.lg.getLongTypedefName typeDefinition) codec))
937+
let ns =
938+
match t.id.topLevelTas with
939+
| None -> us
940+
| Some tasInfo ->
941+
let caller = {Caller.typeId = tasInfo; funcType=UperEncDecFunctionType}
942+
let callee = {Callee.typeId = {TypeAssignmentInfo.modName = o.modName.Value; tasName=o.tasName.Value} ; funcType=UperEncDecFunctionType}
943+
addFunctionCallToState us caller callee
944+
929945
let funcBody (errCode:ErrorCode) (nestingScope: NestingScope) (p:CallerScope) (fromACN: bool) =
930946
//let funcBodyContent = TL "UPER_REF_03" (fun () -> (baseType.getUperFunction codec).funcBody nestingScope p fromACN)
931947
//match funcBodyContent with
@@ -941,7 +957,7 @@ let createReferenceFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (codec:C
941957
let funcBodyContent = TL "UPER_REF_05" (fun () -> callBaseTypeFunc lm pp baseFncName codec)
942958
Some {UPERFuncBodyResult.funcBody = funcBodyContent; errCodes = [errCode]; localVariables = []; bValIsUnReferenced=false; bBsIsUnReferenced=false; resultExpr=resultExpr; auxiliaries = []}
943959
//| None -> None
944-
TL "UPER_REF_06" (fun () -> createUperFunction r lm codec t typeDefinition None isValidFunc funcBody soSparkAnnotations [] us)
960+
TL "UPER_REF_06" (fun () -> createUperFunction r lm codec t typeDefinition None isValidFunc funcBody soSparkAnnotations [] ns)
945961
| false ->
946962
baseType.getUperFunction codec, us
947963
| Some opts ->

BackendAst/DastValidate2.fs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,16 @@ let createReferenceTypeFunction (r:Asn1AcnAst.AstRoot) (l:LanguageMacros) (t:Asn
10201020
| false -> moduleName + "." + baseTypeDefinitionName + "_IsConstraintValid"
10211021

10221022

1023+
let ns =
1024+
match resolvedType.isValidFunction with
1025+
| Some _ ->
1026+
match t.id.topLevelTas with
1027+
| None -> us
1028+
| Some tasInfo ->
1029+
let caller = {Caller.typeId = tasInfo; funcType=IsValidFunctionType}
1030+
let callee = {Callee.typeId = {TypeAssignmentInfo.modName = o.modName.Value; tasName=o.tasName.Value} ; funcType=IsValidFunctionType}
1031+
addFunctionCallToState us caller callee
1032+
| None -> us
10231033
let funBody (errCode: ErrorCode) (p:CallerScope) =
10241034
let with_component_check, lv2 =
10251035
convertMultipleVCBsToStatementAndSetErrorCode l p errCode vcbs |>
@@ -1038,6 +1048,6 @@ let createReferenceTypeFunction (r:Asn1AcnAst.AstRoot) (l:LanguageMacros) (t:Asn
10381048

10391049
let errorCodeComment = o.refCons |> List.map(fun z -> z.ASN1) |> Seq.StrJoin ""
10401050

1041-
createIsValidFunction r l t funBody typeDefinition [] [] [] [] (Some errorCodeComment) us
1051+
createIsValidFunction r l t funBody typeDefinition [] [] [] [] (Some errorCodeComment) ns
10421052

10431053

0 commit comments

Comments
 (0)