From 2f26a6ffba373f040f3397ad23f2389c09e296e0 Mon Sep 17 00:00:00 2001 From: George Mamais Date: Sun, 14 Apr 2024 20:39:16 +0300 Subject: [PATCH] UPER encode an decode enums withou switches --- BackendAst/DAstTypeDefinition.fs | 26 ++++++++++++++++++-------- BackendAst/DAstUPer.fs | 31 +++++++++++++++++++++---------- BackendAst/DastValidate2.fs | 14 ++++++++++---- CommonTypes/AbstractMacros.fs | 6 ++++-- CommonTypes/CommonTypes.fs | 2 ++ FrontEndAst/AcnCreateFromAntlr.fs | 5 +++-- FrontEndAst/FE_TypeDefinition.fs | 8 ++++++-- StgAda/isvalid_a.stg | 2 +- StgAda/spec_a.stg | 5 ++++- StgAda/uper_a.stg | 8 ++++++++ StgC/header_c.stg | 22 +++++++++++++++++++--- StgC/isvalid_c.stg | 4 ++-- StgC/uper_c.stg | 20 ++++++++++++++++++++ StgScala/header_scala.stg | 5 ++++- StgScala/isvalid_scala.stg | 2 +- StgScala/uper_scala.stg | 7 +++++++ asn1crt/asn1crt.c | 6 +++--- asn1crt/asn1crt.h | 4 ++-- 18 files changed, 135 insertions(+), 42 deletions(-) diff --git a/BackendAst/DAstTypeDefinition.fs b/BackendAst/DAstTypeDefinition.fs index 48ba0e9bb..154ec9a15 100644 --- a/BackendAst/DAstTypeDefinition.fs +++ b/BackendAst/DAstTypeDefinition.fs @@ -237,6 +237,7 @@ 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) @@ -244,22 +245,31 @@ let createEnumerated (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst.A 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 diff --git a/BackendAst/DAstUPer.fs b/BackendAst/DAstUPer.fs index 3848a3b04..72b206da5 100644 --- a/BackendAst/DAstUPer.fs +++ b/BackendAst/DAstUPer.fs @@ -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 diff --git a/BackendAst/DastValidate2.fs b/BackendAst/DastValidate2.fs index cd4809220..4088d1eea 100644 --- a/BackendAst/DastValidate2.fs +++ b/BackendAst/DastValidate2.fs @@ -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 @@ -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 ) diff --git a/CommonTypes/AbstractMacros.fs b/CommonTypes/AbstractMacros.fs index 94e7b33cb..31bae7358 100644 --- a/CommonTypes/AbstractMacros.fs +++ b/CommonTypes/AbstractMacros.fs @@ -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 -> arrsEnumNamesAndValues:seq -> nIndexMax:BigInteger -> arrsResolvingMacros:seq -> 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; + abstract member Define_new_enumerated_private : td:FE_EnumeratedTypeDefinition -> arrsValidEnumNames:seq -> arrsEnumNames:seq -> string; + abstract member Define_subType_enumerated_private : td:FE_EnumeratedTypeDefinition -> prTd:FE_EnumeratedTypeDefinition -> arrsValidEnumNames:seq -> arrsEnumNames:seq -> string; abstract member Define_new_ia5string : td:FE_StringTypeDefinition -> nMin:BigInteger -> nMax:BigInteger -> nCMax:BigInteger -> arrnAlphaChars:seq -> 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; @@ -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; abstract member EmitTypeAssignment_composite_def : sVarName:string -> sStar:string -> sFuncName:string -> sTypeDefName:string -> arrsErrcodes:seq -> string; abstract member EmitTypeAssignment_composite : sVarName:string -> sPtrPrefix:string -> sPtrSuffix:string -> sFuncName:string -> sTypeDefName:string -> sContent:string -> arrsAlphaFuncs:seq -> arrsLocalVars:seq -> 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; @@ -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 -> 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 -> 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; diff --git a/CommonTypes/CommonTypes.fs b/CommonTypes/CommonTypes.fs index f04a94a05..68197adcb 100644 --- a/CommonTypes/CommonTypes.fs +++ b/CommonTypes/CommonTypes.fs @@ -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 } with diff --git a/FrontEndAst/AcnCreateFromAntlr.fs b/FrontEndAst/AcnCreateFromAntlr.fs index bbef2f7c5..2b71e22aa 100644 --- a/FrontEndAst/AcnCreateFromAntlr.fs +++ b/FrontEndAst/AcnCreateFromAntlr.fs @@ -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 @@ -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 @@ -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" ))) diff --git a/FrontEndAst/FE_TypeDefinition.fs b/FrontEndAst/FE_TypeDefinition.fs index 0aa662075..4668aca2d 100644 --- a/FrontEndAst/FE_TypeDefinition.fs +++ b/FrontEndAst/FE_TypeDefinition.fs @@ -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 @@ -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 -> diff --git a/StgAda/isvalid_a.stg b/StgAda/isvalid_a.stg index 4211be2bc..d29e55262 100644 --- a/StgAda/isvalid_a.stg +++ b/StgAda/isvalid_a.stg @@ -42,7 +42,7 @@ end if; >> -GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp) ::= << +GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp, bBinarySearch) ::= << >> diff --git a/StgAda/spec_a.stg b/StgAda/spec_a.stg index a6cef44a2..339afd338 100644 --- a/StgAda/spec_a.stg +++ b/StgAda/spec_a.stg @@ -187,7 +187,10 @@ Define_subType_enumerated(td/*:FE_EnumeratedTypeDefinition*/, prTd/*:FE_Enumerat >> -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) ::= << >> diff --git a/StgAda/uper_a.stg b/StgAda/uper_a.stg index 8895a4e28..423ef6fda 100644 --- a/StgAda/uper_a.stg +++ b/StgAda/uper_a.stg @@ -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 */ diff --git a/StgC/header_c.stg b/StgC/header_c.stg index 4a47da195..f9d58cd40 100644 --- a/StgC/header_c.stg +++ b/StgC/header_c.stg @@ -140,12 +140,28 @@ Define_subType_enumerated(td/*:FE_EnumeratedTypeDefinition*/, prTd/*:FE_Enumerat typedef ; >> -Define_new_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames) ::= << -static asn1SccSint [] = { +Define_new_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames, arrsEnumNames) ::= << +static const asn1SccSint [] = { }; separator=","> }; -static int = ; +#define + +#define + +#define +>> + +Define_subType_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, prTd/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames, arrsEnumNames) ::= << +static const asn1SccSint [] = { + }; separator=","> +}; + +#define + +#define + +#define >> /*********************************** STRING ************************************************************/ diff --git a/StgC/isvalid_c.stg b/StgC/isvalid_c.stg index ea4dabe3e..3aa438b9a 100644 --- a/StgC/isvalid_c.stg +++ b/StgC/isvalid_c.stg @@ -103,8 +103,8 @@ flag (const , int* pErrCode) >> -GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp) ::= << -binarySearch(, , ) >= 0 +GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp, bBinarySearch) ::= << +binarySearchlinearSearch(, , ) >= 0 >> ExpEqual(sExp1, sExp2) ::= "( == )" diff --git a/StgC/uper_c.stg b/StgC/uper_c.stg index aa882a684..d5975d8f1 100644 --- a/StgC/uper_c.stg +++ b/StgC/uper_c.stg @@ -321,7 +321,27 @@ Enumerated_decode(p, td/*:FE_EnumeratedTypeDefinition*/, arrsItem, nMin, nMax, n } >> +Enumerated_no_switch_encode(p, td/*:FE_EnumeratedTypeDefinition*/, sErrCode, sEnumIndex, nLastItemIndex, sFirstItemName) ::= << += binarySearch(, ,

); +ret = \>= 0 && \<= ; +if (ret) { + BitStream_EncodeConstraintWholeNumber(pBitStrm, , 0, ); +} +*pErrCode = ret ? 0 : ERR_UPER_ENCODE_MY_ENUM1; +>> + +Enumerated_no_switch_decode(p, td/*:FE_EnumeratedTypeDefinition*/, sErrCode, sEnumIndex, nLastItemIndex, sFirstItemName) ::= << +ret = BitStream_DecodeConstraintWholeNumber(pBitStrm, &, 0, ); +ret = ret && \>= 0 && \<= ; +*pErrCode = ret ? 0 : ; +if (ret) { +

= []; +} else { + /* we could not decode a valid index, so we set the first value */ /*COVERAGE_IGNORE*/ +

= ; /*COVERAGE_IGNORE*/ +} +>> /* CHOICE START*/ diff --git a/StgScala/header_scala.stg b/StgScala/header_scala.stg index 7078ac080..ee80c0719 100644 --- a/StgScala/header_scala.stg +++ b/StgScala/header_scala.stg @@ -124,7 +124,10 @@ Define_subType_enumerated(td/*:FE_EnumeratedTypeDefinition*/, prTd/*:FE_Enumerat typedef >> -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) ::= << >> /*********************************** STRING ************************************************************/ diff --git a/StgScala/isvalid_scala.stg b/StgScala/isvalid_scala.stg index dc95e8568..8309326df 100644 --- a/StgScala/isvalid_scala.stg +++ b/StgScala/isvalid_scala.stg @@ -78,7 +78,7 @@ def (@annotation.unused : > -GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp) ::= << +GetEnumIndexByName(sEnumValuesArray, sEnumValuesArrayCount, sExp, bBinarySearch) ::= << >> diff --git a/StgScala/uper_scala.stg b/StgScala/uper_scala.stg index ad13b5500..cde0e98e4 100644 --- a/StgScala/uper_scala.stg +++ b/StgScala/uper_scala.stg @@ -264,6 +264,13 @@ val

= codec.decodeConstrainedWholeNumber(0, ) match // uper: return LeftMut() >> +Enumerated_no_switch_encode(p, td/*:FE_EnumeratedTypeDefinition*/, sErrCode, sEnumIndex, nLastItemIndex, sFirstItemName) ::= << +>> + +Enumerated_no_switch_decode(p, td/*:FE_EnumeratedTypeDefinition*/, sErrCode, sEnumIndex, nLastItemIndex, sFirstItemName) ::= << +>> + + /* CHOICE START*/ choice_child_encode(p, sAcc, sChildID, nChildIndex, nIndexSizeInBits, nLastItemIndex, sChildContent, sChildName, sChildTypeDef, sChoiceTypeName, sChildInitExpr, bIsSequence, bIsEnum) ::= << diff --git a/asn1crt/asn1crt.c b/asn1crt/asn1crt.c index 765fa3316..ffab9ebaa 100644 --- a/asn1crt/asn1crt.c +++ b/asn1crt/asn1crt.c @@ -55,7 +55,7 @@ int GetCharIndex(char ch, byte Set[], int setLen) return 0; } -flag Asn1Real_Equal(asn1Real Left, asn1Real Right) { +flag Asn1Real_Equal(const asn1Real Left, asn1Real Right) { if (Left == Right) { return true; } else if (Left == 0.0) { @@ -69,7 +69,7 @@ flag Asn1Real_Equal(asn1Real Left, asn1Real Right) { } } -int binarySearch(asn1SccSint arr[], int size, asn1SccSint value) { +int binarySearch(const asn1SccSint arr[], int size, asn1SccSint value) { int low = 0; int high = size - 1; while (low <= high) { @@ -84,7 +84,7 @@ int binarySearch(asn1SccSint arr[], int size, asn1SccSint value) { return -1; // Value not found } -int linearSearch(asn1SccSint arr[], int size, asn1SccSint value) { +int linearSearch(const asn1SccSint arr[], int size, asn1SccSint value) { for (int i = 0; i < size; i++) { if (arr[i] == value) return i; // Value found diff --git a/asn1crt/asn1crt.h b/asn1crt/asn1crt.h index fdf6b6c86..3b48de989 100644 --- a/asn1crt/asn1crt.h +++ b/asn1crt/asn1crt.h @@ -210,7 +210,7 @@ flag Asn1Real_Equal(asn1Real Left, asn1Real Right); * @param value The value to search for. * @return The index of the value if found, -1 otherwise. */ -int binarySearch(asn1SccSint arr[], int size, asn1SccSint value); +int binarySearch(const asn1SccSint arr[], int size, asn1SccSint value); /** * Performs a linear search on an array to find a specific value. @@ -220,7 +220,7 @@ int binarySearch(asn1SccSint arr[], int size, asn1SccSint value); * @param value The value to be searched for. * @return The index of the value if found, or -1 if not found. */ -int linearSearch(asn1SccSint arr[], int size, asn1SccSint value); +int linearSearch(const asn1SccSint arr[], int size, asn1SccSint value); void ObjectIdentifier_Init(Asn1ObjectIdentifier* pVal); flag ObjectIdentifier_equal(const Asn1ObjectIdentifier* pVal1, const Asn1ObjectIdentifier* pVal2);