Unused variables,constants, functions subs warnings.

Questions and discussion about the ZBasic IDE.
Post Reply
zbasicandy
Posts: 193
Joined: 25 January 2006, 19:56 PM

Unused variables,constants, functions subs warnings.

Post 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?
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post 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.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: Unused variables,constants, functions subs warnings.

Post 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
Mike Perks
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

Impressive!

Is there a way to see what the default compiler options are?
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post 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.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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
- Don Kinzer
Post Reply