Skip to content

Commit

Permalink
Remove unused functions from RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
usr3-1415 committed Nov 18, 2023
1 parent 6b319f7 commit 015a7ca
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 126 deletions.
6 changes: 3 additions & 3 deletions BackendAst/GenerateFiles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,19 @@ let private printUnit (r:DAst.AstRoot) (lm:LanguageMacros) (encodings: CommonTy

tstCasesHdrContent |> Option.iter(fun tstCasesHdrContent -> File.WriteAllText(testcase_SrcFileName, tstCasesHdrContent.Replace("\r","")))


(defintionsContntent, srcBody)



let generateAll (di:DirInfo) (r:DAst.AstRoot) (lm:LanguageMacros) (encodings: CommonTypes.Asn1Encoding list) =
r.programUnits |> Seq.iter (printUnit r lm encodings di.srcDir)
let generatedContent = r.programUnits |> List.map(printUnit r lm encodings di.srcDir) |> List.map snd |> Seq.StrJoin "\n"
match r.args.generateAutomaticTestCases with
| false -> ()
| true ->
lm.lg.CreateMakeFile r di
let arrsSrcTstFiles, arrsHdrTstFiles = DastTestCaseCreation.printAllTestCasesAndTestCaseRunner r lm di.srcDir
lm.lg.CreateAuxFiles r di (arrsSrcTstFiles, arrsHdrTstFiles)

generatedContent


let EmmitDefaultACNGrammar (r:AstRoot) outDir =
Expand Down
1 change: 1 addition & 0 deletions CommonTypes/CommonTypes.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<Compile Include="FsUtils.fs" />
<Compile Include="RemoveUnusedRtlFunction.fs" />
<Compile Include="CommonTypes.fs" />
<Compile Include="AbstractMacros.fs" />
<Compile Include="RangeSets.fs" />
Expand Down
16 changes: 16 additions & 0 deletions CommonTypes/RemoveUnusedRtlFunction.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module RemoveUnusedRtlFunction


let findFunctionNames (headerContents: string) : string list =
let pattern = @"\b[a-zA-Z_][a-zA-Z0-9_]*\s+[a-zA-Z_][a-zA-Z0-9_]*\s*\([^\)]*\)\s*;"
let regex = System.Text.RegularExpressions.Regex(pattern)
let matches = regex.Matches(headerContents)

[ for matchResult in matches do
let declaration = matchResult.Value
// Split the declaration at whitespace and take the second to last element,
// which should be the function name (assuming no return type modifiers like pointers)
let parts = declaration.Split([|' '; '\t'; '\n'; '\r'|], System.StringSplitOptions.RemoveEmptyEntries)
if parts.Length > 1 then yield parts.[parts.Length - 2] ]


12 changes: 12 additions & 0 deletions FrontEndAst/Language.fs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ type ILangGeneric () =
abstract member SpecExtention : string
abstract member BodyExtention : string

abstract member RtlFuncNames : string list
abstract member detectFunctionCalls : string -> string -> string list
abstract member removeFunctionFromHeader : string -> string -> string
abstract member removeFunctionFromBody : string -> string -> string


abstract member getRtlFiles : Asn1Encoding list -> string list -> string list

abstract member getAsn1ChildBackendName0 : Asn1AcnAst.Asn1Child -> string
Expand Down Expand Up @@ -147,6 +153,12 @@ type ILangGeneric () =
this.getParamTypeSuffix t "" c
default this.requiresHandlingOfEmptySequences = false
default this.requiresHandlingOfZeroArrays = false
default this.RtlFuncNames = []
default this.detectFunctionCalls (sourceCode: string) (functionName: string) = []
default this.removeFunctionFromHeader (sourceCode: string) (functionName: string) : string =
sourceCode
default this.removeFunctionFromBody (sourceCode: string) (functionName: string) : string =
sourceCode


type LanguageMacros = {
Expand Down
328 changes: 326 additions & 2 deletions StgC/LangGeneric_c.fs

Large diffs are not rendered by default.

81 changes: 0 additions & 81 deletions asn1crt/asn1crt_encoding_acn.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,36 +846,6 @@ void Acn_Enc_SInt_ASCII_VarSize_NullTerminated(BitStream* pBitStrm, asn1SccSint
Acn_Enc_UInt_ASCII_VarSize_NullTerminated(pBitStrm, absValue, null_characters, null_characters_size);
}

/*
flag Acn_Dec_String_Ascii_Null_Teminated_mult(BitStream* pBitStrm, asn1SccSint max, const byte null_character[], size_t null_character_size, char* strVal)
{
byte tmp[10];
size_t sz = null_character_size < 10 ? null_character_size : 10;
memset(tmp, 0x0, 10);
memset(strVal, 0x0, (size_t)max + 1);
//read null_character_size characters into the tmp buffer
for (int j = 0; j < (int)null_character_size; j++) {
if (!BitStream_ReadByte(pBitStrm, &(tmp[j])))
return FALSE;
}
asn1SccSint i = 0;
while (i <= max && (memcmp(null_character, tmp, sz) != 0)) {
strVal[i] = tmp[0];
i++;
for (int j = 0; j < (int)null_character_size - 1; j++)
tmp[j] = tmp[j + 1];
if (!BitStream_ReadByte(pBitStrm, &(tmp[null_character_size - 1])))
return FALSE;
}
strVal[i] = 0x0;
return memcmp(null_character, tmp, sz) == 0;
}
*/

flag Acn_Dec_UInt_ASCII_VarSize_NullTerminated(BitStream* pBitStrm, asn1SccUint* pIntVal, const byte null_characters[], size_t null_characters_size)
{
Expand Down Expand Up @@ -1271,57 +1241,6 @@ flag Acn_Dec_String_Ascii_FixSize(BitStream* pBitStrm, asn1SccSint max, char* st
return Acn_Dec_String_Ascii_private(pBitStrm, max, max, strVal);
}

/*
int put_byte_in_last_dec_bytes(byte last_dec_bytes[], size_t* pCur_size, size_t null_characters_size, byte decodedCharacter, byte *pDiscardedCharacter) {
int i;
if (*pCur_size < null_characters_size) {
last_dec_bytes[*pCur_size] = decodedCharacter;
(*pCur_size)++;
*pDiscardedCharacter = NULL;
return 0;
} else {
*pDiscardedCharacter = last_dec_bytes[0];
for (i = 1; i < null_characters_size; i++) {
last_dec_bytes[i - 1] = last_dec_bytes[i];
}
last_dec_bytes[null_characters_size - 1] = decodedCharacter;
return 1;
}
}
flag Acn_Dec_String_Ascii_Null_Teminated(BitStream* pBitStrm, asn1SccSint max, const byte null_characters[], size_t null_characters_size, char* strVal)
{
asn1SccSint i = 0;
byte decodedCharacter;
byte characterToAppendInString;
size_t cur_size_of_last_dec_bytes = 0;
byte last_dec_bytes[128];
int ret;
assert(null_characters_size<128);
memset(last_dec_bytes, 0x0, sizeof(last_dec_bytes));
memset(strVal, 0x0, (size_t)max+1);
while (i<=max) {
if (!BitStream_ReadByte(pBitStrm, &decodedCharacter))
return FALSE;
ret = put_byte_in_last_dec_bytes(last_dec_bytes, &cur_size_of_last_dec_bytes, null_characters_size, decodedCharacter, &characterToAppendInString);
//if (decodedCharacter == (byte)null_character) {
if ((ret == 1) && (memcmp(last_dec_bytes,null_characters,null_characters_size) == 0)) {
strVal[i] = 0x0;
return TRUE;
} else if (ret == 1) {
strVal[i] = characterToAppendInString;
i++;
}
}
return FALSE;
}
*/


flag Acn_Dec_String_Ascii_Null_Teminated(BitStream* pBitStrm, asn1SccSint max, char null_character, char* strVal)
Expand Down
6 changes: 3 additions & 3 deletions asn1crt/asn1crt_encoding_uper.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static flag ObjectIdentifier_subidentifiers_uper_decode(BitStream* pBitStrm, asn
return TRUE;
}

static flag ObjectIdentifier_uper_decode_lentg(BitStream* pBitStrm, asn1SccSint* totalSize) {
static flag ObjectIdentifier_uper_decode_length(BitStream* pBitStrm, asn1SccSint* totalSize) {
asn1SccSint len2;
if (!BitStream_DecodeConstraintWholeNumber(pBitStrm, totalSize, 0, 0xFF))
return FALSE;
Expand All @@ -121,7 +121,7 @@ flag ObjectIdentifier_uper_decode(BitStream* pBitStrm, Asn1ObjectIdentifier *pVa
ObjectIdentifier_Init(pVal);


if (!ObjectIdentifier_uper_decode_lentg(pBitStrm, &totalSize))
if (!ObjectIdentifier_uper_decode_length(pBitStrm, &totalSize))
return FALSE;

if (!ObjectIdentifier_subidentifiers_uper_decode(pBitStrm, &totalSize, &si))
Expand All @@ -146,7 +146,7 @@ flag RelativeOID_uper_decode(BitStream* pBitStrm, Asn1ObjectIdentifier *pVal) {
asn1SccSint totalSize;
ObjectIdentifier_Init(pVal);

if (!ObjectIdentifier_uper_decode_lentg(pBitStrm, &totalSize))
if (!ObjectIdentifier_uper_decode_length(pBitStrm, &totalSize))
return FALSE;

while (totalSize > 0 && pVal->nCount < OBJECT_IDENTIFIER_MAX_LENGTH)
Expand Down
88 changes: 53 additions & 35 deletions asn1scc/GenerateRTL.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open CommonTypes
open AbstractMacros
open OutDirectories
open System.Resources
open Language

let writeTextFile fileName (content:String) =
System.IO.File.WriteAllText(fileName, content.Replace("\r",""))
Expand All @@ -33,7 +34,26 @@ let writeResource (di:DirInfo) (rsName:string) (fn) : unit=
writeTextFile (Path.Combine(asn1rtlDirName, rsName)) newContent


let exportRTL (di:DirInfo) (l:ProgrammingLanguage) (args:CommandLineSettings)=

let findUnusedRtlFunctions (lm:LanguageMacros) (rtlContent:string) (generatedContent:string) =
let rec findAllUsedRtlFunctions (lm:LanguageMacros) (sourceCode:string)(currentlyUsedFunctionNames:string list) =
let indirectlyUsedFunctions =
currentlyUsedFunctionNames |> List.collect (lm.lg.detectFunctionCalls sourceCode) |> List.distinct
let newCurrentlyUsedFunctionNames = currentlyUsedFunctionNames @ indirectlyUsedFunctions |> List.distinct
match newCurrentlyUsedFunctionNames.Length = currentlyUsedFunctionNames.Length with
| true -> newCurrentlyUsedFunctionNames
| false -> findAllUsedRtlFunctions lm sourceCode newCurrentlyUsedFunctionNames

//first detect all function names
let directlyUsedFunctions =
lm.lg.RtlFuncNames |> List.filter (fun fn -> generatedContent.Contains(fn))

let allUsedFunctions = findAllUsedRtlFunctions lm rtlContent ("BitStream_AttachBuffer"::"BitStream_Init"::directlyUsedFunctions) |> Set.ofList
lm.lg.RtlFuncNames |> List.filter (fun fn -> not (allUsedFunctions.Contains(fn)))



let exportRTL (di:DirInfo) (l:ProgrammingLanguage) (args:CommandLineSettings) (lm:LanguageMacros) (generatedContent:string) =
let rootDir = di.rootDir
//let asn1rtlDirName = di.asn1rtlDir
let boardsDirName = di.boardsDir
Expand All @@ -46,49 +66,53 @@ let exportRTL (di:DirInfo) (l:ProgrammingLanguage) (args:CommandLineSettings)=
let hasBer = args.encodings |> Seq.exists(fun e -> e = BER)
match l with
| ProgrammingLanguage.C ->
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt.c")) (rm.GetString("asn1crt_c",null))
writeResource di "asn1crt.c" None
let rtlContent =
["asn1crt.c";"asn1crt_encoding.c";"asn1crt_encoding_uper.c";"asn1crt_encoding_acn.c"] |> List.map getResourceAsString |> Seq.StrJoin "\n"
let unusedRtlFunctions = findUnusedRtlFunctions lm rtlContent generatedContent


let removeUnusedRtlFunctionsFromHeader (sourceCode:string) =
unusedRtlFunctions |> List.fold (fun acc fn -> lm.lg.removeFunctionFromHeader acc fn) sourceCode
let removeUnusedRtlFunctionsFromBody (sourceCode:string) =
unusedRtlFunctions |> List.fold (fun acc fn -> lm.lg.removeFunctionFromBody acc fn) sourceCode

writeResource di "asn1crt.c" (Some removeUnusedRtlFunctionsFromBody)

//let asn1crt_h = rm.GetString("asn1crt_h",null)
let intSize = sprintf "#define WORD_SIZE %d" (int args.integerSizeInBytes)
let fpSize = sprintf "#define FP_WORD_SIZE %d" (int args.floatingPointSizeInBytes)
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt.h")) (asn1crt_h.Replace("#define WORD_SIZE 8", intSize).Replace("#define FP_WORD_SIZE 8", fpSize) )
writeResource di "asn1crt.h" (Some (fun (s:string) -> s.Replace("#define WORD_SIZE 8", intSize).Replace("#define FP_WORD_SIZE 8", fpSize)) )

let fix_asn1crt_h (s:string) =
let ret = s.Replace("#define WORD_SIZE 8", intSize).Replace("#define FP_WORD_SIZE 8", fpSize)
removeUnusedRtlFunctionsFromHeader ret

writeResource di "asn1crt.h" (Some fix_asn1crt_h)

match args.encodings with
| [] -> ()
| _ ->

writeResource di "asn1crt_encoding.c" None
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding.c")) asn1crt_encoding_c
writeResource di "asn1crt_encoding.c" (Some removeUnusedRtlFunctionsFromBody)


//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding.h")) (rm.GetString("asn1crt_encoding_h",null))
writeResource di "asn1crt_encoding.h" None
writeResource di "asn1crt_encoding.h" (Some removeUnusedRtlFunctionsFromHeader)

if hasUper || hasAcn then
writeResource di "asn1crt_encoding_uper.c" None
writeResource di "asn1crt_encoding_uper.h" None
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_uper.c")) (rm.GetString("asn1crt_encoding_uper_c",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_uper.h")) (rm.GetString("asn1crt_encoding_uper_h",null))
writeResource di "asn1crt_encoding_uper.c" (Some removeUnusedRtlFunctionsFromBody)
writeResource di "asn1crt_encoding_uper.h" (Some removeUnusedRtlFunctionsFromHeader)

if hasAcn then
writeResource di "asn1crt_encoding_acn.c" None
writeResource di "asn1crt_encoding_acn.h" None
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_acn.c")) (rm.GetString("asn1crt_encoding_acn_c",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_acn.h")) (rm.GetString("asn1crt_encoding_acn_h",null))
writeResource di "asn1crt_encoding_acn.c" (Some removeUnusedRtlFunctionsFromBody)
writeResource di "asn1crt_encoding_acn.h" (Some removeUnusedRtlFunctionsFromHeader)

if hasXer then
writeResource di "asn1crt_encoding_xer.c" None
writeResource di "asn1crt_encoding_xer.h" None
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_xer.c")) (rm.GetString("asn1crt_encoding_xer_c",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_xer.h")) (rm.GetString("asn1crt_encoding_xer_h",null))

if hasBer then
writeResource di "asn1crt_encoding_ber.c" None
writeResource di "asn1crt_encoding_ber.h" None
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_ber.c")) (rm.GetString("asn1crt_encoding_ber_c",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_ber.h")) (rm.GetString("asn1crt_encoding_ber_h",null))

// TODO: Scala
| ProgrammingLanguage.Scala ->
Expand Down Expand Up @@ -128,55 +152,41 @@ let exportRTL (di:DirInfo) (l:ProgrammingLanguage) (args:CommandLineSettings)=
// //writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_ber.c")) (rm.GetString("asn1crt_encoding_ber_c",null))
// //writeTextFile (Path.Combine(asn1rtlDirName, "asn1crt_encoding_ber.h")) (rm.GetString("asn1crt_encoding_ber_h",null))
| ProgrammingLanguage.Ada ->
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl.adb")) (rm.GetString("adaasn1rtl_adb",null))
writeResource di "adaasn1rtl.adb" None
//let adaasn1rtl_ads = rm.GetString("adaasn1rtl_ads",null)
match args.floatingPointSizeInBytes = 4I with
| true ->
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl.ads")) (adaasn1rtl_ads.Replace("subtype Asn1Real is Standard.Long_Float;","subtype Asn1Real is Standard.Float;"))
writeResource di "adaasn1rtl.ads" (Some (fun (s:string) -> s.Replace("subtype Asn1Real is Standard.Long_Float;","subtype Asn1Real is Standard.Float;")))
| false ->
writeResource di "adaasn1rtl.ads" None
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl.ads")) (adaasn1rtl_ads)

match args.encodings with
| [] -> ()
| _ ->
let adaasn1rtl_encoding_adb_fn (s:string) =
match args.streamingModeSupport with
| false -> s // rm.GetString("adaasn1rtl_encoding_adb",null)//.Replace("-- with user_code;","").Replace("-- user_code.push_data(bs, bs.pushDataPrm);","--").Replace("-- user_code.fetch_data(bs, bs.fetchDataPrm);","")
| false -> s
| true -> s.Replace("-- with user_code;","with user_code;").Replace("-- bs.Current_Bit_Pos := 0;","bs.Current_Bit_Pos := 0;").Replace("-- user_code.push_data(bs, bs.pushDataPrm);","user_code.push_data (bs, bs.pushDataPrm);").Replace("-- user_code.fetch_data(bs, bs.fetchDataPrm);","user_code.fetch_data (bs, bs.fetchDataPrm);")

//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding.adb")) adaasn1rtl_encoding_adb
writeResource di "adaasn1rtl-encoding.adb" (Some adaasn1rtl_encoding_adb_fn)

//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding.ads")) (rm.GetString("adaasn1rtl_encoding_ads",null))
writeResource di "adaasn1rtl-encoding.ads" None

if hasUper || hasAcn then
writeResource di "adaasn1rtl-encoding-uper.adb" None
writeResource di "adaasn1rtl-encoding-uper.ads" None
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-uper.adb")) (rm.GetString("adaasn1rtl_encoding_uper_adb",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-uper.ads")) (rm.GetString("adaasn1rtl_encoding_uper_ads",null))

if hasAcn then
writeResource di "adaasn1rtl-encoding-acn.adb" None
writeResource di "adaasn1rtl-encoding-acn.ads" None
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-acn.adb")) (rm.GetString("adaasn1rtl_encoding_acn_adb",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-acn.ads")) (rm.GetString("adaasn1rtl_encoding_acn_ads",null))

if hasXer then
writeResource di "adaasn1rtl-encoding-xer.adb" None
writeResource di "adaasn1rtl-encoding-xer.ads" None
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-xer.adb")) (rm.GetString("adaasn1rtl_encoding_xer_adb",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-xer.ads")) (rm.GetString("adaasn1rtl_encoding_xer_ads",null))

match args.generateAutomaticTestCases with
| true ->
writeResource di "adaasn1rtl-encoding-test_cases_aux.adb" None
writeResource di "adaasn1rtl-encoding-test_cases_aux.ads" None
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-test_cases_aux.adb")) (rm.GetString("adaasn1rtl_encoding_test_cases_aux_adb",null))
//writeTextFile (Path.Combine(asn1rtlDirName, "adaasn1rtl-encoding-test_cases_aux.ads")) (rm.GetString("adaasn1rtl_encoding_test_cases_aux_ads",null))
| false -> ()

let writeBoard boardName =
Expand All @@ -203,3 +213,11 @@ let exportRTL (di:DirInfo) (l:ProgrammingLanguage) (args:CommandLineSettings)=
writeTextFile filename content)


let test2() =
let headerFiles = [ "asn1crt.h"; "asn1crt_encoding.h"; "asn1crt_encoding_uper.h"; "asn1crt_encoding_acn.h"]
for hd in headerFiles do
printfn "Processing %s" hd
let headerContents = getResourceAsString hd
let functionNames = RemoveUnusedRtlFunction.findFunctionNames headerContents
for fn in functionNames do
printfn "\t%s" fn
7 changes: 5 additions & 2 deletions asn1scc/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ let getLanguageMacro (l:ProgrammingLanguage) =

let main0 argv =

//RemoveUnusedRtlFunction.test1 ()
//GenerateRTL.test3 (RemoveUnusedRtlFunction.C_RTL_FUNCTIONS |> List.map fst)

let parser = ArgumentParser.Create<CliArguments>(programName = "asn1scc.exe")
try
let parserResults = parser.Parse argv
Expand Down Expand Up @@ -404,8 +407,8 @@ let main0 argv =
//let asn1rtlDirName = Path.Combine(outDir, OutDirectories.asn1rtlDirName r.lang)
//let boardsDirName = Path.Combine(outDir, OutDirectories.boardsDirName r.lang)
let lm = getLanguageMacro r.lang
GenerateFiles.generateAll dirInfo r lm args.encodings
GenerateRTL.exportRTL dirInfo r.lang args
let generatedContent = GenerateFiles.generateAll dirInfo r lm args.encodings
GenerateRTL.exportRTL dirInfo r.lang args lm generatedContent
match args.AstXmlAbsFileName with
| "" -> ()
| _ -> DAstExportToXml.exportFile r acnDeps ("backend_" + args.AstXmlAbsFileName)
Expand Down

0 comments on commit 015a7ca

Please sign in to comment.