Skip to content

Commit 3da4b12

Browse files
authored
Merge pull request #335 from esa/issue334
Issue334
2 parents bdf019a + 59996ba commit 3da4b12

File tree

12 files changed

+1052
-972
lines changed

12 files changed

+1052
-972
lines changed

BackendAst/DAstACN.fs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,54 @@ let adaptArgumentValue = DAstUPer.adaptArgumentValue
225225

226226
let joinedOrAsIdentifier = DAstUPer.joinedOrAsIdentifier
227227

228+
(*
229+
If the type assignment has acnParameters, then no function is generated. This function can only be inlined by the calling function
230+
(i.e. by the parent type encoding function).
231+
Now, we have to make this rule recursive:
232+
A composite type (e.g SEQUENCE, choice etc ) may have references (i.e. reference types) to a type assignment that has acnParameters.
233+
In this case, the reference must have arguments in the acn in the form <arg1,arg2, ...>
234+
These argument can either ACN inserted fields or acnParameters.
235+
If the reference type is written explicitly in the acn, by the user, then the arguments must be checked to be inline with the acnParameters.
236+
If they are not, the user gets an error.
237+
238+
However, there are cases where the reference type is not written explicitly by the user in the acn grammar,
239+
but is infered by the compiler. For example,
240+
241+
The following asn1 grammar define two types:
242+
CfdpPDU ::= SEQUENCE {
243+
pdu-header PDUHeader,
244+
payload OCTET STRING (CONTAINING PayloadData)
245+
}
246+
PayloadData ::= CHOICE {
247+
file-directive FileDirectiveType,
248+
file-data FileDataType
249+
}
250+
FileDataType ::= SEQUENCE {
251+
file-data-pdu FileDataPDU
252+
}
253+
254+
However the acn grammar provides defintions only for CfdpPDU and FileDataType, not PayloadData. In fact, the PayloadData acn spec
255+
is provided inline in the CfdpPDU acn spec, not at the PayloadData Type Assignment Level.
256+
CfdpPDU [] {
257+
pdu-header [] {
258+
pdu-type PDUType [encoding pos-int, size 1],
259+
pdu-data-field-length PDUDataFieldLength [encoding pos-int, size 16]
260+
},
261+
payload [size pdu-header.pdu-data-field-length] {
262+
file-directive [present-when pdu-header.pdu-type==0],
263+
file-data <pdu-header.pdu-data-field-length> [present-when pdu-header.pdu-type==1]
264+
}
265+
}
266+
FileDataType <PDUDataFieldLength:pdu-data-field-length> [] {
267+
file-data-pdu <pdu-data-field-length> []
268+
}
269+
270+
Therefore, the compiler uses a defult acn specs for the PayloadData type assignment, which is not provided by the user.
271+
In this case the file-data reference type has no acnArgs. This means that no acn function must be generated for the FileDataType type assignment.
272+
273+
*)
274+
275+
228276
let private createAcnFunction (r: Asn1AcnAst.AstRoot)
229277
(deps: Asn1AcnAst.AcnInsertedFieldDependencies)
230278
(lm: LanguageMacros)
@@ -257,7 +305,11 @@ let private createAcnFunction (r: Asn1AcnAst.AstRoot)
257305
let EmitTypeAssignment_primitive = lm.acn.EmitTypeAssignment_primitive
258306
let EmitTypeAssignment_primitive_def = lm.acn.EmitTypeAssignment_primitive_def
259307
let EmitTypeAssignment_def_err_code = lm.acn.EmitTypeAssignment_def_err_code
308+
let EmitEncodingSizeConstants = lm.acn.EmitEncodingSizeConstants
260309

310+
let typeDefinitionName = typeDefinition.longTypedefName2 lm.lg.hasModules
311+
let sEncodingSizeConstant = EmitEncodingSizeConstants typeDefinitionName nMaxBytesInACN t.acnMaxSizeInBits
312+
261313
let funcBodyAsSeqComp (st: State)
262314
(prms: (RelativePath * AcnParameter) list)
263315
(nestingScope: NestingScope)
@@ -327,14 +379,14 @@ let private createAcnFunction (r: Asn1AcnAst.AstRoot)
327379
let lvars = bodyResult_localVariables |> List.map(fun (lv:LocalVariable) -> lm.lg.getLocalVariableDeclaration lv) |> Seq.distinct
328380
let prms = t.acnParameters |> List.map handleAcnParameter
329381
let prmNames = t.acnParameters |> List.map (fun p -> p.c_name)
330-
let func = Some(EmitTypeAssignment_primitive varName sStar funcName isValidFuncName (typeDefinition.longTypedefName2 lm.lg.hasModules) lvars bodyResult_funcBody soSparkAnnotations sInitialExp prms prmNames (t.acnMaxSizeInBits = 0I) bBsIsUnreferenced bVarNameIsUnreferenced soInitFuncName funcDefAnnots precondAnnots postcondAnnots codec)
382+
let func = Some(EmitTypeAssignment_primitive varName sStar funcName isValidFuncName typeDefinitionName lvars bodyResult_funcBody soSparkAnnotations sInitialExp prms prmNames (t.acnMaxSizeInBits = 0I) bBsIsUnreferenced bVarNameIsUnreferenced soInitFuncName funcDefAnnots precondAnnots postcondAnnots codec)
331383

332384
let errCodStr =
333385
errCodes |>
334386
List.groupBy (fun x -> x.errCodeName) |>
335387
List.map (fun (k, v) -> {errCodeName = k; errCodeValue = v.Head.errCodeValue; comment = v.Head.comment}) |>
336388
List.map(fun x -> EmitTypeAssignment_def_err_code x.errCodeName (BigInteger x.errCodeValue) x.comment) |> List.distinct
337-
let funcDef = Some(EmitTypeAssignment_primitive_def varName sStar funcName (typeDefinition.longTypedefName2 lm.lg.hasModules) errCodStr (t.acnMaxSizeInBits = 0I) nMaxBytesInACN ( t.acnMaxSizeInBits) prms soSparkAnnotations codec)
389+
let funcDef = Some(EmitTypeAssignment_primitive_def varName sStar funcName typeDefinitionName errCodStr (t.acnMaxSizeInBits = 0I) nMaxBytesInACN ( t.acnMaxSizeInBits) prms soSparkAnnotations codec)
338390
let ns2a =
339391
match t.id.topLevelTas with
340392
| None -> ns1a
@@ -367,6 +419,7 @@ let private createAcnFunction (r: Asn1AcnAst.AstRoot)
367419
funcBodyAsSeqComp = funcBodyAsSeqComp
368420
isTestVaseValid = isTestVaseValid
369421
icdTas = icdAux
422+
encodingSizeConstant = sEncodingSizeConstant
370423
}
371424
ret, ns3
372425

@@ -866,7 +919,12 @@ let createNullTypeFunction (r:Asn1AcnAst.AstRoot) (deps: Asn1AcnAst.AcnInsertedF
866919

867920

868921
let getExternalField0 (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.AcnInsertedFieldDependencies) asn1TypeIdWithDependency func1 =
869-
let dependency = deps.acnDependencies |> List.find(fun d -> d.asn1Type = asn1TypeIdWithDependency && func1 d )
922+
let dependency =
923+
match deps.acnDependencies |> List.tryFind (fun d -> d.asn1Type = asn1TypeIdWithDependency && func1 d ) with
924+
| Some d -> d
925+
| None ->
926+
failwithf "getExternalField0: No dependency found for %A" asn1TypeIdWithDependency
927+
870928
let rec resolveParam (prmId:ReferenceToType) =
871929
let nodes = match prmId with ReferenceToType nodes -> nodes
872930
let lastNode = nodes |> List.rev |> List.head
@@ -1448,7 +1506,7 @@ let rec handleSingleUpdateDependency (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.Acn
14481506
| Some f ->
14491507
let fncBdRes, ns = f.funcBody us [] (NestingScope.init asn1TypeD.acnMaxSizeInBits asn1TypeD.uperMaxSizeInBits []) {CallerScope.modName = ""; arg = Selection.valueEmptyPath "dummy"}
14501508
match fncBdRes with
1451-
| Some x -> x.errCodes, x.localVariables, ns
1509+
| Some x -> x.errCodes, [], ns
14521510
| None -> [], [], us
14531511
| None -> [], [], us
14541512

BackendAst/DAstConstruction.fs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ let private createAcnChild (r:Asn1AcnAst.AstRoot) (icdStgFileName:string) (deps:
8484
let rec dealiasDeps (dep: Asn1AcnAst.AcnDependency): Asn1AcnAst.AcnDependency =
8585
match dep.dependencyKind with
8686
| Asn1AcnAst.AcnDepRefTypeArgument param ->
87-
let dealiased = dealiasDeps (deps.acnDependencies |> List.find (fun dep -> dep.determinant.id = param.id))
88-
{dep with dependencyKind = dealiased.dependencyKind}
87+
match deps.acnDependencies |> List.tryFind (fun dep -> dep.determinant.id = param.id) with
88+
| Some dep2 ->
89+
let dealiased = dealiasDeps dep2
90+
{dep with dependencyKind = dealiased.dependencyKind}
91+
| None ->
92+
raise (Exception(sprintf "Could not find dependency for parameter %s" param.id.AsString))
8993
| _ -> dep
9094

9195
let dealiasedDeps = deps.acnDependencies |> List.filter(fun d -> d.determinant.id = ch.id) |> List.map dealiasDeps

BackendAst/GenerateFiles.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ let private printUnit (r:DAst.AstRoot) (lm:LanguageMacros) (encodings: CommonTy
143143
let xerDecFunc = match tas.Type.xerDecFunction with XerFunction z -> z.funcDef | XerFunctionDummy -> None
144144

145145
let hasAcnEncDec = r.callersSet |> Set.contains (f AcnEncDecFunctionType)
146-
let acnEncFunc =
146+
let acnEncFunc, sEncodingSizeConstant =
147147
match hasAcnEncDec && requiresAcn, tas.Type.acnEncFunction with
148-
| true, Some x -> x.funcDef
149-
| _ -> None
148+
| true, Some x -> x.funcDef, Some x.encodingSizeConstant
149+
| _ -> None, None
150150
let acnDecFunc =
151151
match hasAcnEncDec && requiresAcn, tas.Type.acnDecFunction with
152152
| true, Some x -> x.funcDef
153153
| _ -> None
154154

155-
let allProcs = equal_defs@isValidFuncs@special_init_funcs@([init_globals;init_def;uPerEncFunc;uPerDecFunc;acnEncFunc; acnDecFunc;xerEncFunc;xerDecFunc] |> List.choose id)
155+
let allProcs = equal_defs@isValidFuncs@special_init_funcs@([init_globals;init_def;uPerEncFunc;uPerDecFunc;sEncodingSizeConstant; acnEncFunc; acnDecFunc;xerEncFunc;xerDecFunc] |> List.choose id)
156156
lm.typeDef.Define_TAS type_definition allProcs
157157
)
158158
let arrsValues =

CommonTypes/AbstractMacros.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ Generated by the C stg macros with the following command
366366
abstract member getSizeableSize : p:string -> sAcc:string -> bIsUnsigned:bool -> string;
367367
abstract member EmitTypeAssignment_def_err_code : sErrCode:string -> nErrValue:BigInteger -> soErrorCodeComment:string option -> string;
368368
abstract member EmitAcnParameter : sName:string -> sType:string -> string;
369+
abstract member EmitEncodingSizeConstants : sTypeDefName:string -> nMaxBytesInACN:BigInteger -> nMaxBitsInACN:BigInteger -> string;
369370
abstract member EmitTypeAssignment_primitive_def : sVarName:string -> sStar:string -> sFuncName:string -> sTypeDefName:string -> arrsErrcodes:seq<string> -> bEmptyEncodingSpace:bool -> nMaxBytesInACN:BigInteger -> nMaxBitsInACN:BigInteger -> arrsAcnPrms:seq<string> -> soSparkAnnotations:string option -> codec:Codec -> string;
370371
abstract member EmitTypeAssignment_primitive : sVarName:string -> sStar:string -> sFuncName:string -> soIValidFuncName:string option -> sTypeDefName:string -> arrsLocalVariables:seq<string> -> sContent:string -> soSparkAnnotations:string option -> sInitialExp:string -> arrsAcnPrms:seq<string> -> arrsAcnParamNames:seq<string> -> bEmptyEncodingSpace:bool -> bBsIsUnreferenced:bool -> bVarNameIsUnreferenced:bool -> soInitFuncName:string option -> arrsAnnots:seq<string> -> arrsPrecond:seq<string> -> soPostcond:string option -> codec:Codec -> string;
371372
abstract member alignToNext : sMainBody:string -> sAlignmentValue:string -> nAlignmentValue:BigInteger -> nAbsOffset:BigInteger -> nRemainingMinBits:BigInteger -> nLevel:BigInteger -> nIx:BigInteger -> nOffset:BigInteger -> codec:Codec -> string;

0 commit comments

Comments
 (0)