Skip to content

Commit

Permalink
New mode (Enable Efficient Enumerations) implemented for C. Huge gain…
Browse files Browse the repository at this point in the history
…s in the generated code.
  • Loading branch information
usr3-1415 committed Apr 20, 2024
1 parent 06162de commit f9071b4
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion BackendAst/DAstACN.fs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ let createEnumCommon (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (codec:CommonTyp
let resultExpr, errCodes, typeEncodingKind =
intAcnFuncBdResult.resultExpr, intAcnFuncBdResult.errCodes, intAcnFuncBdResult.typeEncodingKind
let mainContent, localVariables =
match r.args.isEnumEfficientEnabled o.items.Length with
match r.args.isEnumEfficientEnabled o.items.Length with
| false ->
let arrItems =
o.items |>
Expand Down
2 changes: 1 addition & 1 deletion BackendAst/DAstTypeDefinition.fs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ let createEnumerated (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1AcnAst.A
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 arrsValidEnumNames = o.validItems |> List.sortBy(fun i -> i.definitionValue) |> List.map( fun i -> lm.lg.getNamedItemBackendName None i)

match td.kind with
| NonPrimitiveNewTypeDefinition ->
Expand Down
3 changes: 1 addition & 2 deletions FrontEndAst/AcnCreateFromAntlr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@ 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 @@ -636,7 +635,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 |> List.sortBy(fun x -> x.definitionValue)
let items = items0|> List.mapi mapItem

let acnProperties =
match acnErrLoc with
Expand Down
4 changes: 2 additions & 2 deletions StgC/header_c.stg
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ typedef <prTd.typeName> <td.typeName>;

Define_new_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames, arrsEnumNames) ::= <<
static const asn1SccSint <td.values_array>[] = {
<arrsValidEnumNames:{it|<it>}; separator=",">
<arrsValidEnumNames:{it|<it>}; wrap, anchor, separator=",">
};

#define <td.values_array_count> <arrsValidEnumNames.Length>
Expand All @@ -154,7 +154,7 @@ static const asn1SccSint <td.values_array>[] = {

Define_subType_enumerated_private(td/*:FE_EnumeratedTypeDefinition*/, prTd/*:FE_EnumeratedTypeDefinition*/, arrsValidEnumNames, arrsEnumNames) ::= <<
static const asn1SccSint <td.values_array>[] = {
<arrsValidEnumNames:{it|<it>}; separator=",">
<arrsValidEnumNames:{it|<it>}; wrap, anchor, separator=",">
};

#define <td.values_array_count> <arrsValidEnumNames.Length>
Expand Down
2 changes: 1 addition & 1 deletion StgC/uper_c.stg
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ ret = <sEnumIndex> \>= 0 && <sEnumIndex> \<= <nLastItemIndex>;
if (ret) {
BitStream_EncodeConstraintWholeNumber(pBitStrm, <sEnumIndex>, 0, <nLastItemIndex>);
}
*pErrCode = ret ? 0 : ERR_UPER_ENCODE_MY_ENUM1;
*pErrCode = ret ? 0 : <sErrCode>;

>>

Expand Down
12 changes: 7 additions & 5 deletions asn1scc/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ with
are renamed.
"""
| Enable_Efficient_Enumerations _ -> """Enable efficient enumerations. (Applicable only to C.)
In this mode, the generated validation, encoding and decoding
functions do not use switches but arrays.
The argument is number of enumerants in an enumerated type in order to enable this mode.
E.g. -eee 10, will enable this mode for enumerated types with 10 or more enumerants.
This mode optimizes the generated C code for ASN.1 Enumerated types with multiple enumerants (e.g., 50 or more).
Instead of generating switch statements, asn1scc generates sorted arrays containing the possible values.
Lookups (e.g., for validation or index retrieval in uPER encoding) are performed using an optimized binary search.
This results in more efficient and less verbose code.
The argument is the minimum number of enumerants in an enumerated type to enable this mode.
E.g., -eee 50 will enable this mode for enumerated types with 50 or more enumerants.
"""
| Field_Prefix _ -> """ Apply <prefix> string to any component or alternative fields present in the grammar.
If <prefix> is AUTO (i.e. -fp AUTO) then only the conflicting component or alternative names will be prefixed with the type name.
Expand Down Expand Up @@ -117,7 +119,7 @@ let printVersion () =
//let fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
//let version = fvi.FileVersion;

let version = "4.5.1.5"
let version = "4.5.2.0"
printfn "asn1scc version %s\n" version
()

Expand Down

0 comments on commit f9071b4

Please sign in to comment.