Dead code removal at compile time

For other topics related to the FreeBASIC project or its community.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Dead code removal at compile time

Postby marpon » Mar 21, 2015 10:06

Is it some plan to put a compilation option to remove dead code at compile time?

at least , code function/sub not used , variables never used ...
working on the main .bas file and the includes files (excluding the .bi distribution files to simplify).

it could be an interresting option to reduce the size of the resulting exe and reducing the complexity and the maintening of big progs after various versions.

it could also be a pre-compilation option , regenerating the source code and commenting the dead code ...
dkl
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Dead code removal at compile time

Postby dkl » Mar 24, 2015 22:42

fbc already removes unused private procedures/variables, but that's pretty much it. There are no plans to improve this on the fbc side as far as I know, but of course with -gen gcc/llvm it will be done nevertheless (just not by fbc, but by gcc/llvm).
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Dead code removal at compile time

Postby marpon » Mar 25, 2015 16:58

@dkl

fbc already removes unused private procedures


what do you mean ? declared as : DECLARE PRIVATE FUNCTION / DECLARE PRIVATE SUB
dkl
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Dead code removal at compile time

Postby dkl » Mar 25, 2015 17:04

The Private goes on the body (definition), not the declaration:

Code: Select all

private sub f1()
end sub

sub f2()
end sub


Compile that with fbc -r, and look at the generated .asm: only f2 is emitted, not f1.
bcohio2001
Posts: 552
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Dead code removal at compile time

Postby bcohio2001 » Mar 25, 2015 17:28

@marpon
Are you referring to removing unused code from the source when compiling?
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Dead code removal at compile time

Postby marpon » Mar 25, 2015 17:32

@dkl

thanks, just made a test , and its very efficient,

it removes even better than what I am doing with extra tool , checking un-used funct/Sub/macro/define and commenting what not used


Why not have private as default for exe file
and in case of Dll the export key give info to convert to Public ,
and in case of lib or .o just convert all to public ....
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Dead code removal at compile time

Postby marpon » Mar 25, 2015 17:38

@ bcohio2001

sorry I was sending my reply when you post arrived

Yes, i was using a tool to regenerate the code with un-used procs commented...
checking on main and include files ( not checking the distrib "official" ones )
bcohio2001
Posts: 552
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Dead code removal at compile time

Postby bcohio2001 » Mar 25, 2015 17:56

Interested....
Does your tool automatically comment out unused or use setting?
Could it be modified to at least notify that something is unused and then later you can decide if to remove?
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: Dead code removal at compile time

Postby marpon » Mar 25, 2015 18:19

In fact it is a prototype option key included in my Csed_fb editor ( new evolution of the one posted on the projects here)

if the option is on , the complete code is revisited at compile time,
and a new .bas file produced directly on the editor incorporating in 1 single file , all the code ( main and include files)
with the commented non used parts and after, if no error found during generation, it compiles.

the original files remain unchanged and the generated file is saved and available directly on the editor
even if error during generation , it is possible to fix ...

The intention was to pack as much as possible the exe but also the source code in 1 file.

On my vision, the program is developed and tested with all the source parts as usual and when finalized it can be packed as clean as possible
bcohio2001
Posts: 552
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Dead code removal at compile time

Postby bcohio2001 » Mar 28, 2015 21:35

marpon wrote:The intention was to pack as much as possible the exe but also the source code in 1 file.

On my vision, the program is developed and tested with all the source parts as usual and when finalized it can be packed as clean as possible

To each his own.
I prefer to keep all my code in separate files.

My TarLib project is a library. So one 'source' file would be confusing for future editing.

Tar.bas

Code: Select all

#Include "Tar.bi"

'just to calc dateTime1970
'Sub CalcJan70()
'   Dim As FILETIME FT = Serial2FTime(DateSerial(1970, 1, 1))
'   dateTime1970 = FileTimeToULong(FT)
'   TarDebugLog("DefineJan70", "#Define dateTime1970 "+Str(dateTime1970))
'End Sub

'0.6.0 CreateFlag is ignored
Function TarOpen(FName As String, CreateFlag As Integer=0) As TarFile Ptr Export
.
.
.
.

Tar.bi

Code: Select all

#Include "windows.bi"
#Include "vbcompat.bi"
#Include "Defines.bi"
#Include "Time.bi"
#Include "PHeader.bi" '<-- does not consider reading or writing extended header data! Only used when creating an extended header.
#Include "xheader.bi" 'extended headers
#Include "GNU.bi" 'GNU headers and info
'#Include "Star.bi" 'Joerg Schilling's star implementation.
#Include "Links.bi"

'numerous types, enums, declares ....

'end of file

'just as long as everything is declared, no specific order needed
#Include "Misc.bas"
#Include "Entry.bas"
#Include "Backups.bas"
#Include "XHeader.bas"
#Include "PHeader.bas"
#Include "Sparse.bas"
#Include "GNU.bas"
#Include "Time.bas"
#Include "Dump.bas"
#Include "Links.bas"

compiled with 'fbc -lib Tar.bas'

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests