May 19, 2024, 11:38:36 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Unused Procedures In Executable

Started by Logman, October 07, 2009, 05:10:50 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Logman

October 07, 2009, 05:10:50 AM Last Edit: November 12, 2009, 08:42:02 AM by Logman
Greetings:

I put together the following code to see how Emergence Basic compiles unused procedures.


OPENCONSOLE      ' open the console

DEF Obj AS INT
Obj = 0

PRINT
PRINT "Hello, World!"
PRINT "Obj has a value of: ", Obj

SUB Proc1()          ' this procedure is never called in this program
_asm
mov eax, 1
_endasm
ENDSUB

SUB Proc2()          ' this procedure is called in this program
_asm
mov eax, [$Obj]
add eax, 1
mov [$Obj], eax
_endasm
ENDSUB

Proc2()          ' call only the second procedure
PRINT "Obj has changed to: ", Obj,

DO: UNTIL INKEY$ <> ""
CLOSECONSOLE
END


I included two procedures in my test program, but only call the second one.

When I dissassemble the executable, I find that both procedures have been included in the code. EBasic did not eliminate the unused procedure during compilation.

Is this correct? Why wouldn't the compiler exclude unused procedures/variables/etc.?

Just curious :)

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

sapero

October 07, 2009, 05:39:11 AM #1 Last Edit: October 07, 2009, 05:56:33 AM by sapero
This is correct, the linker does not split .o files to "functions". But if you manually split all functions into separate .eba files, compile as a static library and then use this library in another project, only the required functions (modules) will be included in final program.

I remember I have created a small piece of code with RemoveUnusedFunctions function. The name may be different, it was long time ago. You should be able to find it here. After you compilled your program and ran the exe, this function compilled all the files again, removed itself from the code and also removed all unused functions.

I can't find it on my hdd.

Logman

October 07, 2009, 08:19:19 AM #2 Last Edit: October 07, 2009, 08:32:41 AM by Logman
Thanks for the heads-up Sapero. I was just poking around in the executable and noticed that unused code gets output into the executable file. It was just an observation because a couple of previous versions of BASIC and a version of "C" I use to use quite awhile ago automatically removed unused code during compilation/linking.

I've never had a problem with this as far as I know because I manually check my code, but you gave me an idea for writing a small routine that would check the appropriate files for unused code and remove it. Might be a useful tool to keep on hand as the number of lines of code in my programs grow. I'll bet it would be a real challenge to write and get it to work properly though, so splitting my functions into separate .eba files and compiling as static libraries might just do the trick for now. I hadn't thought of that; great idea.

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

sapero

Such set of routines is easy to write. If you look at the .map file, you will be able to see what libs are neccesary for the linker, and what .asm files should be scanned and recompilled. A subroutine can be removed if:
1. is not global, and has no references in .asm file
2. is global, has no references from current and other .asm files, is not exported (not listed in .exp or .eba file) and it is not the entry point symbol.
3. optional and advanced/complicated: if the second condition will allow you to remove the subroutine, scan all object files from all referenced .lib files, because a subroutine may be referenced from static library.

Beginning of a subroutine in .asm file is easy to find: ";subroutine sub_name begin\n", and it ends at ".ef\n"

Logman

October 07, 2009, 09:35:17 AM #4 Last Edit: October 07, 2009, 09:36:53 AM by Logman
Sapero:

Nice explanation. I think I'll try to put a little program together based on your information. It sure would be handy on some of my larger programs and I think I can do it now that you laid out the process. Your details will save me an enormous amount of research and trial & error.

Thanks for getting back to me.

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

ZeroDog

EBasic also includes conditional compiling.  You can use the $IFDEF, $IFNDEF,$ELSE and $ENDIF commands to tell the compiler what portions of code to include and what not to include at compile time.

Logman

ZeroDog:

Of course; you are right. These compiler commands are also available, I forgot about them.

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!