Skip to content

Commit

Permalink
UPER encode an decode enums withou switches
Browse files Browse the repository at this point in the history
  • Loading branch information
usr3-1415 committed Apr 14, 2024
1 parent 9c16df1 commit 2f26a6f
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 42 deletions.
26 changes: 18 additions & 8 deletions BackendAst/DAstTypeDefinition.fs
Original file line number Diff line number Diff line change
Expand Up @@ -237,29 +237,39 @@ let createEnumerated (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst.A
let define_new_enumerated = lm.typeDef.Define_new_enumerated
let define_subType_enumerated = lm.typeDef.Define_subType_enumerated
let define_new_enumerated_private = lm.typeDef.Define_new_enumerated_private
let define_subType_enumerated_private = lm.typeDef.Define_subType_enumerated_private
let orderedItems = o.items |> List.sortBy(fun i -> i.definitionValue)
let arrsEnumNames = orderedItems |> List.map( fun i -> lm.lg.getNamedItemBackendName None i)
let arrsEnumNamesAndValues = orderedItems |> List.map(fun i -> define_new_enumerated_item td (lm.lg.getNamedItemBackendName None i) i.definitionValue)
let macros = orderedItems |> List.map( fun i -> define_new_enumerated_item_macro td (ToC i.Name.Value) (lm.lg.getNamedItemBackendName None i) )
let nIndexMax = BigInteger ((Seq.length o.items)-1)
let arrsValidEnumNames = o.validItems |> List.map( fun i -> lm.lg.getNamedItemBackendName None i)

let privateDefinition =
match r.args.isEnumEfficientEnabled o.items.Length with
| false -> None
| true ->
let ret = define_new_enumerated_private td arrsValidEnumNames
match System.String.IsNullOrWhiteSpace ret with
| true -> None
| false -> Some ret

match td.kind with
| NonPrimitiveNewTypeDefinition ->
let completeDefinition = define_new_enumerated td arrsEnumNames arrsEnumNamesAndValues nIndexMax macros
let privateDefinition =
match r.args.isEnumEfficientEnabled o.items.Length with
| false -> None
| true ->
let ret = define_new_enumerated_private td arrsValidEnumNames arrsEnumNames
match System.String.IsNullOrWhiteSpace ret with
| true -> None
| false -> Some ret

Some (completeDefinition, privateDefinition)
| NonPrimitiveNewSubTypeDefinition subDef ->
let otherProgramUnit = if td.programUnit = subDef.programUnit then None else (Some subDef.programUnit)
let completeDefinition = define_subType_enumerated td subDef otherProgramUnit
let privateDefinition =
match r.args.isEnumEfficientEnabled o.items.Length with
| false -> None
| true ->
let ret = define_subType_enumerated_private td subDef arrsValidEnumNames arrsEnumNames
match System.String.IsNullOrWhiteSpace ret with
| true -> None
| false -> Some ret
Some (completeDefinition, privateDefinition)
| NonPrimitiveReference2OtherType -> None

Expand Down
31 changes: 21 additions & 10 deletions BackendAst/DAstUPer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,32 @@ let createNullTypeFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (codec:Co
createUperFunction r lm codec t typeDefinition baseTypeUperFunc isValidFunc funcBody soSparkAnnotations [] us

let createEnumeratedFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (codec:CommonTypes.Codec) (t:Asn1AcnAst.Asn1Type) (o:Asn1AcnAst.Enumerated) (typeDefinition:TypeDefinitionOrReference) (baseTypeUperFunc : UPerFunction option) (isValidFunc: IsValidFunction option) (us:State) =
let Enumerated = lm.uper.Enumerated
let Enumerated_item = lm.uper.Enumerated_item
let Enumerated_no_switch = lm.uper.Enumerated_no_switch


let funcBody (errCode:ErrorCode) (p:CallerScope) =
let Enumerated = lm.uper.Enumerated
let Enumerated_item = lm.uper.Enumerated_item
let nMax = BigInteger(Seq.length o.items) - 1I
let nLastItemIndex = nMax
let typeDef0 = lm.lg.getEnumTypeDefinition o.typeDef
let td = typeDef0.longTypedefName2 lm.lg.hasModules (ToC p.modName)
let pp, resultExpr = adaptArgumentValue lm codec p
let nMin = 0I
let nMax = BigInteger(Seq.length o.items) - 1I
let nLastItemIndex = nMax
let items =
o.items |> List.mapi(fun i itm -> Enumerated_item pp (lm.lg.getNamedItemBackendName (Some typeDefinition) itm) (BigInteger i) nLastItemIndex codec)
let nBits = (GetNumberOfBitsForNonNegativeInteger (nMax-nMin))
let sFirstItemName = lm.lg.getNamedItemBackendName (Some typeDefinition) o.items.Head
let funcBodyContent = Enumerated pp td items nMin nMax nBits errCode.errCodeName nLastItemIndex sFirstItemName codec
{UPERFuncBodyResult.funcBody = funcBodyContent; errCodes = [errCode]; localVariables = []; bValIsUnReferenced=false; bBsIsUnReferenced=false; resultExpr=resultExpr}
match r.args.isEnumEfficientEnabled o.items.Length with
| false ->
let nMin = 0I
let items =
o.items |> List.mapi(fun i itm -> Enumerated_item pp (lm.lg.getNamedItemBackendName (Some typeDefinition) itm) (BigInteger i) nLastItemIndex codec)
let nBits = (GetNumberOfBitsForNonNegativeInteger (nMax-nMin))
let funcBodyContent = Enumerated pp td items nMin nMax nBits errCode.errCodeName nLastItemIndex sFirstItemName codec
{UPERFuncBodyResult.funcBody = funcBodyContent; errCodes = [errCode]; localVariables = []; bValIsUnReferenced=false; bBsIsUnReferenced=false; resultExpr=resultExpr}
| true ->
let sEnumIndex = "nEnumIndex"
let enumIndexVar = (Asn1SIntLocalVariable (sEnumIndex, None))
let funcBodyContent = Enumerated_no_switch pp td errCode.errCodeName sEnumIndex nLastItemIndex sFirstItemName codec
{UPERFuncBodyResult.funcBody = funcBodyContent; errCodes = [errCode]; localVariables = [enumIndexVar]; bValIsUnReferenced=false; bBsIsUnReferenced=false; resultExpr=resultExpr}

let soSparkAnnotations = Some(sparkAnnotations lm (lm.lg.getLongTypedefName typeDefinition) codec)
createUperFunction r lm codec t typeDefinition baseTypeUperFunc isValidFunc (fun e p -> Some (funcBody e p)) soSparkAnnotations [] us

Expand Down
14 changes: 10 additions & 4 deletions BackendAst/DastValidate2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,12 @@ let createTimeTypeFunction (r:Asn1AcnAst.AstRoot) (l:LanguageMacros) (t:Asn1AcnA
let createEfficientEnumValidation (r:Asn1AcnAst.AstRoot) (l:LanguageMacros) (o:Asn1AcnAst.Enumerated) (us:State) =
let getEnumIndexByName = l.isvalid.GetEnumIndexByName
let td = (l.lg.getEnumTypeDefinition o.typeDef)

let bSorted =
let sortedItems = o.validItems |> List.map(fun x -> x.definitionValue) |> List.sort
let items = o.validItems |> List.map(fun x -> x.definitionValue)
sortedItems = items
let optimizedValidation (p:CallerScope) =
let ret = getEnumIndexByName td.values_array td.values_array_count (l.lg.getValue p.arg)
let ret = getEnumIndexByName td.values_array td.values_array_count (l.lg.getValue p.arg) bSorted
VCBExpression (ret)
[optimizedValidation], us

Expand Down Expand Up @@ -945,8 +948,11 @@ let rec createReferenceTypeFunction_this_type (r:Asn1AcnAst.AstRoot) (l:Language
| NullType _ ->
[],us
| Enumerated en ->
let cons = refCons |> List.choose(fun c -> match c with Asn1AcnAst.EnumConstraint z -> Some z | _ -> None )
cons |> Asn1Fold.foldMap (fun us c -> enumeratedConstraint2ValidationCodeBlock l en.baseInfo typeDefinition c us) us
match r.args.isEnumEfficientEnabled en.baseInfo.items.Length with
| false ->
let cons = refCons |> List.choose(fun c -> match c with Asn1AcnAst.EnumConstraint z -> Some z | _ -> None )
cons |> Asn1Fold.foldMap (fun us c -> enumeratedConstraint2ValidationCodeBlock l en.baseInfo typeDefinition c us) us
| true -> createEfficientEnumValidation r l en.baseInfo us
| Choice ch ->
let valToStrFunc (p:CallerScope) (v:Asn1AcnAst.ChValue) = VCBTrue
let cons = refCons |> List.choose(fun c -> match c with Asn1AcnAst.ChoiceConstraint z -> Some z | _ -> None )
Expand Down
6 changes: 4 additions & 2 deletions CommonTypes/AbstractMacros.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Generated by the C stg macros with the following command
abstract member Define_new_enumerated_item_macro : td:FE_EnumeratedTypeDefinition -> sAsn1Name:string -> sCName:string -> string;
abstract member Define_new_enumerated : td:FE_EnumeratedTypeDefinition -> arrsEnumNames:seq<string> -> arrsEnumNamesAndValues:seq<string> -> nIndexMax:BigInteger -> arrsResolvingMacros:seq<string> -> string;
abstract member Define_subType_enumerated : td:FE_EnumeratedTypeDefinition -> prTd:FE_EnumeratedTypeDefinition -> soParentTypePackage:string option -> string;
abstract member Define_new_enumerated_private : td:FE_EnumeratedTypeDefinition -> arrsValidEnumNames:seq<string> -> string;
abstract member Define_new_enumerated_private : td:FE_EnumeratedTypeDefinition -> arrsValidEnumNames:seq<string> -> arrsEnumNames:seq<string> -> string;
abstract member Define_subType_enumerated_private : td:FE_EnumeratedTypeDefinition -> prTd:FE_EnumeratedTypeDefinition -> arrsValidEnumNames:seq<string> -> arrsEnumNames:seq<string> -> string;
abstract member Define_new_ia5string : td:FE_StringTypeDefinition -> nMin:BigInteger -> nMax:BigInteger -> nCMax:BigInteger -> arrnAlphaChars:seq<BigInteger> -> string;
abstract member Define_subType_ia5string : td:FE_StringTypeDefinition -> prTd:FE_StringTypeDefinition -> soParentTypePackage:string option -> string;
abstract member Define_new_octet_string : td:FE_SizeableTypeDefinition -> nMin:BigInteger -> nMax:BigInteger -> bFixedSize:bool -> string;
Expand Down Expand Up @@ -176,7 +177,7 @@ Generated by the C stg macros with the following command
abstract member EmitTypeAssignment_composite_def_err_code : sErrCode:string -> nErrValue:BigInteger -> arrsErrorCodeComments:seq<string> -> string;
abstract member EmitTypeAssignment_composite_def : sVarName:string -> sStar:string -> sFuncName:string -> sTypeDefName:string -> arrsErrcodes:seq<string> -> string;
abstract member EmitTypeAssignment_composite : sVarName:string -> sPtrPrefix:string -> sPtrSuffix:string -> sFuncName:string -> sTypeDefName:string -> sContent:string -> arrsAlphaFuncs:seq<string> -> arrsLocalVars:seq<string> -> bUnreferenced:bool -> string;
abstract member GetEnumIndexByName : sEnumValuesArray:string -> sEnumValuesArrayCount:string -> sExp:string -> string;
abstract member GetEnumIndexByName : sEnumValuesArray:string -> sEnumValuesArrayCount:string -> sExp:string -> bBinarySearch:bool -> string;
abstract member ExpEqual : sExp1:string -> sExp2:string -> string;
abstract member ExpStringEqual : sExp1:string -> sExp2:string -> string;
abstract member ExpGt : sExp1:string -> sExp2:string -> string;
Expand Down Expand Up @@ -326,6 +327,7 @@ Generated by the C stg macros with the following command
abstract member Time : p:string -> sTimeSubType:string -> sErrCode:string -> codec:Codec -> string;
abstract member Enumerated_item : p:string -> sName:string -> nIndex:BigInteger -> nLastItemIndex:BigInteger -> codec:Codec -> string;
abstract member Enumerated : p:string -> td:FE_EnumeratedTypeDefinition -> arrsItem:seq<string> -> nMin:BigInteger -> nMax:BigInteger -> nBits:BigInteger -> sErrCode:string -> nLastItemIndex:BigInteger -> sFirstItemName:string -> codec:Codec -> string;
abstract member Enumerated_no_switch : p:string -> td:FE_EnumeratedTypeDefinition -> sErrCode:string -> sEnumIndex:string -> nLastItemIndex:BigInteger -> sFirstItemName:string -> codec:Codec -> string;
abstract member choice_child : p:string -> sAcc:string -> sChildID:string -> nChildIndex:BigInteger -> nIndexSizeInBits:BigInteger -> nLastItemIndex:BigInteger -> sChildContent:string -> sChildName:string -> sChildTypeDef:string -> sChoiceTypeName:string -> sChildInitExpr:string -> bIsSequence:bool -> bIsEnum:bool -> codec:Codec -> string;
abstract member choice : p:string -> sAcc:string -> arrsChildren:seq<string> -> nLastItemIndex:BigInteger -> sChoiceIndexName:string -> sErrCode:string -> td:FE_ChoiceTypeDefinition -> nIndexSizeInBits:BigInteger -> codec:Codec -> string;
abstract member sequence_presence_bit : p:string -> sAcc:string -> sChName:string -> soExistVar:string option -> sErrCode:string -> codec:Codec -> string;
Expand Down
2 changes: 2 additions & 0 deletions CommonTypes/CommonTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ type FE_EnumeratedTypeDefinition = {
index_range : string
values_array : string //The name of the array that holds the possible values of the enumeration.
values_array_count : string //The name of the variable that holds the number of elements in the enumeration.
encoded_values_array : string //The name of the array that holds the encoded values of the enumeration.
encoded_values_array_count : string //The name of the variable that holds the number of encoded elements in the enumeration.
kind : FE_NonPrimitiveTypeDefinitionKind<FE_EnumeratedTypeDefinition>
}
with
Expand Down
5 changes: 3 additions & 2 deletions FrontEndAst/AcnCreateFromAntlr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ let private mergeEnumerated (asn1: Asn1Ast.AstRoot) (items: Asn1Ast.NamedItem li
allocateItems xs newAllocatedItems (vl + 1I)
let newItems = allocateItems unallocated allocated 0I |> List.sortBy(fun ni -> namedItems |> Seq.findIndex(fun x -> x.Name.Value = ni.Name.Value) )
newItems
//newItems |> List.sortBy(fun ni -> ni.Name.Value)

let mapItem (i:int) (itm:Asn1Ast.NamedItem) =
let definitionValue = Asn1Ast.GetValueAsInt itm._value.Value asn1
Expand Down Expand Up @@ -635,7 +636,7 @@ let private mergeEnumerated (asn1: Asn1Ast.AstRoot) (items: Asn1Ast.NamedItem li
| false -> allocatedValuesToAllEnumItems items, false
| true -> allocatedValuesToAllEnumItems items, true
let uperSizeInBits = GetNumberOfBitsForNonNegativeInteger(BigInteger((Seq.length items) - 1))
let items = items0|> List.mapi mapItem
let items = items0|> List.mapi mapItem |> List.sortBy(fun x -> x.definitionValue)

let acnProperties =
match acnErrLoc with
Expand All @@ -651,7 +652,7 @@ let private mergeEnumerated (asn1: Asn1Ast.AstRoot) (items: Asn1Ast.NamedItem li
let alignment = tryGetProp props (fun x -> match x with ALIGNTONEXT e -> Some e | _ -> None)
let acnEncodingClass, acnMinSizeInBits, acnMaxSizeInBits= AcnEncodingClasses.GetEnumeratedEncodingClass asn1.args.integerSizeInBytes items alignment loc acnProperties uperSizeInBits uperSizeInBits encodeValues

let validItems = items |> List.filter (Asn1Fold.isValidValueGeneric cons (fun a b -> a = b.Name.Value))
let validItems = items |> List.filter (Asn1Fold.isValidValueGeneric cons (fun a b -> a = b.Name.Value)) |> List.sortBy(fun x -> x.definitionValue)

match validItems with
| [] -> raise(SemanticError(loc, (sprintf "The constraints defined for this type do not allow any value" )))
Expand Down
8 changes: 6 additions & 2 deletions FrontEndAst/FE_TypeDefinition.fs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ let rec registerEnumeratedTypeDefinition (us:Asn1AcnMergeState) (l:ProgrammingLa
let index_range, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_index_range")
let values_array, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_values_array")
let values_array_count, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_values_array_count")
let itm = {FE_EnumeratedTypeDefinition.programUnit = programUnit; typeName = typeName; asn1Name = asn1Name; asn1Module = Some id.ModName; kind=NonPrimitiveNewTypeDefinition; index_range=index_range; values_array=values_array;values_array_count=values_array_count}
let encoded_values_array , newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_encoded_values_array")
let encoded_values_array_count, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_encoded_values_array_count")
let itm = {FE_EnumeratedTypeDefinition.programUnit = programUnit; typeName = typeName; asn1Name = asn1Name; asn1Module = Some id.ModName; kind=NonPrimitiveNewTypeDefinition; index_range=index_range; values_array=values_array;values_array_count=values_array_count; encoded_values_array=encoded_values_array;encoded_values_array_count=encoded_values_array_count}
itm, {us with allocatedTypeNames = newAllocatedTypeNames; allocatedFE_TypeDefinition = us.allocatedFE_TypeDefinition.Add((l,id), (FE_EnumeratedTypeDefinition itm))}
| FEI_NewSubTypeDefinition subId ->
let subType, ns1 = registerEnumeratedTypeDefinition us (l,ib) subId FEI_NewTypeDefinition
Expand All @@ -355,7 +357,9 @@ let rec registerEnumeratedTypeDefinition (us:Asn1AcnMergeState) (l:ProgrammingLa
let index_range, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_index_range")
let values_array, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_values_array")
let values_array_count, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_values_array_count")
let itm = {FE_EnumeratedTypeDefinition.programUnit = programUnit; typeName = typeName; asn1Name = asn1Name; asn1Module = Some id.ModName; kind=(NonPrimitiveNewSubTypeDefinition subType); index_range=index_range; values_array=values_array;values_array_count=values_array_count}
let encoded_values_array , newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_encoded_values_array")
let encoded_values_array_count, newAllocatedTypeNames = reserveTypeDefinitionName us.args.TypePrefix newAllocatedTypeNames (l,ib) programUnit (proposedTypeDefName + "_encoded_values_array_count")
let itm = {FE_EnumeratedTypeDefinition.programUnit = programUnit; typeName = typeName; asn1Name = asn1Name; asn1Module = Some id.ModName; kind=(NonPrimitiveNewSubTypeDefinition subType); index_range=index_range; values_array=values_array;values_array_count=values_array_count; encoded_values_array=encoded_values_array;encoded_values_array_count=encoded_values_array_count}
let ns2 = {ns1 with allocatedTypeNames = newAllocatedTypeNames; allocatedFE_TypeDefinition = ns1.allocatedFE_TypeDefinition.Add((l,id), (FE_EnumeratedTypeDefinition itm))}
itm, ns2
| FEI_Reference2OtherType refId ->
Expand Down
2 changes: 1 addition & 1 deletion StgAda/isvalid_a.stg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end if;
>>


GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp) ::= <<
GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp, bBinarySearch) ::= <<
>>


Expand Down
5 changes: 4 additions & 1 deletion StgAda/spec_a.stg
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ Define_subType_enumerated(td/*:FE_EnumeratedTypeDefinition*/, prTd/*:FE_Enumerat
<define_subtype(thisType=td.index_range, parentType=prTd.index_range, soParentTypePackage=soParentTypePackage)>
>>

Define_new_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames) ::= <<
Define_new_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames, arrsEnumNames) ::= <<
>>

Define_subType_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, prTd/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames, arrsEnumNames) ::= <<
>>


Expand Down
8 changes: 8 additions & 0 deletions StgAda/uper_a.stg
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ begin
end;
>>

Enumerated_no_switch_encode(p, td/*:FE_EnumeratedTypeDefinition*/, sErrCode, sEnumIndex, nLastItemIndex, sFirstItemName) ::= <<
>>

Enumerated_no_switch_decode(p, td/*:FE_EnumeratedTypeDefinition*/, sErrCode, sEnumIndex, nLastItemIndex, sFirstItemName) ::= <<
>>



/* ENUMERATED START */

/* SEQUENCE START */
Expand Down
Loading

0 comments on commit 2f26a6f

Please sign in to comment.