Page 1 of 1

Unused variables,constants, functions subs warnings.

Posted: 08 March 2006, 12:39 PM
by zbasicandy
As programs get larger in size, is there a way that the programmer can be warned of unused variables, constants, functions and subs?

Posted: 08 March 2006, 15:57 PM
by stevech
partial answer...
I read in the docs that there's a compiler command line option to omit subs and fuctions that aren't called.

I wondered if this would be a way to have a "library" at the source code level, of favorite goodies. Put them all into some standard source files. Include all those with any project and be assured that only the ones really used would be compiled.

Posted: 08 March 2006, 17:50 PM
by dkinzer
The compiler will normally (with the default optimization options) omit unused subs/functions/variables. It may even optimize away variables that are used but aren't really necessary. Consider this trivial example:

Code: Select all

Sub Main()
	Dim pin as Byte
	Dim level as Byte
	pin = 12
	level = 2
	Call PutPin(pin, level)
End Sub
With the default optimization levels, the generated code is:

Code: Select all

            Sub Main()
            	Dim pin as Byte
           var:pin (1) [not used]
            	Dim level as Byte
           var:level (1) [not used]
            	pin = 12
            	level = 2
            	Call PutPin(pin, level)
0011 1a0c        PSHI_B         0x0c (12)
0013 1a02        PSHI_B         0x02 (2)
0015 d4          PUTPIN
            End Sub
0016 06          RET
You can see that no local variable space was allocated at the beginning of Main() and constant values are pushed in place of the variable values. Those variables are unnecessary in this case since the compiler can determine what the value of those variables will be at the point of use at run-time and they aren't otherwise used.

Re: Unused variables,constants, functions subs warnings.

Posted: 08 March 2006, 18:45 PM
by mikep
zbasicandy wrote:As programs get larger in size, is there a way that the programmer can be warned of unused variables, constants, functions and subs?
The answer to the question is to look at the listing file and also generate the called-by and called-from lists. The compiler options to add to the PJT file are:

Code: Select all

--list=<filename>
--called-by-list
--calls-list
You can also turn on additional compiler warnings to help track unused stuff such as:

Code: Select all

--warn=unused-param

Posted: 08 March 2006, 19:05 PM
by stevech
Impressive!

Is there a way to see what the default compiler options are?

Posted: 08 March 2006, 19:38 PM
by mikep
stevech wrote:Impressive!
This is a result of the lessons learnt when Don and I did some similar work for the predecessor to ZBasic called BasicX - see the bxDism utility on my website. So this is second generation technology at least (produced by a different company).
stevech wrote:Is there a way to see what the default compiler options are?
This sounds like a good idea to add to the documentation.

Posted: 18 March 2006, 8:00 AM
by dkinzer
Is there a way to see what the default compiler options are?
The help text output for optimization and warning options indicates which options are in effect by default.

Code: Select all

zbasic --help-optimize
zbasic --help-warning
Other than those, the only default options are:
- the executable output file has the same name as the project file (or the first file compiled) with the extension replaced by .zxb
- the map file has the same name as the project file (or the first file compiled) with the extension replaced by .map