Page 1 of 1

Program size for generic targets

Posted: 01 May 2012, 13:08 PM
by RickB
Don:

Regarding your answer near the end of this thread.
http://www.zbasic.net/forum/about1134.html
dlh wrote:
Is there any way to estimate program storage requirements for VM vs. native code?
There are too many factors involved to permit even rule-of-thumb estimations. For example, consider the "null program" below that compiles to 18 bytes for the ZX-24a and 3808 bytes for the ZX-24n.
Code:
Sub Main()
End Sub

As a second data point consider the program below: ZX-24a 50 bytes, ZX-24n 3850 bytes.
Code:
Dim i as Integer, j as Integer
Sub Main()
For i = 1 to 10
j = i
Next
End Sub

Of course, the difference depends on the particular instructions used. Compare the data for the sample program below: ZX-24a 53 bytes, ZX-24n 4844 bytes.
Code:
Dim i as Integer
Sub Main()
For i = 1 to 10
Debug.Print i
Next
End Sub

The largest program that I have data for is the code for my machine tool controller comprising about 5300 lines of code and comments. This compiles to 18,070 for the ZX-24a and 30,790 for the ZX-24n. One last data point is for the RoZetta code that I have (dated June 2008). It compiles to 12,332 bytes for the ZX-40a and 40,660 bytes for the ZX-40n.

dlh wrote:
Is code written in C likely to be more efficient than ZBasic?"
For any specific routine, probably not. The C code generated from the ZBasic code is, in most cases, pretty much as you would have written it in C yourself so it then comes down to the efficiency of the code generator of the back-end compiler. The code size for a general application that doesn't exploit multi-tasking and doesn't use the RTC is probably going to be larger when written in ZBasic than if it were written in C because of the multi-tasking core infrastructure overhead that is included in every native mode ZBasic program.
How does the multitasking overhead affect smaller generic devices? Is it included without regard to flash size or has this been changed with the introduction of the generic compiler? If so, does that answer still apply to native mode devices?

Rick

Re: Program size for generic targets

Posted: 01 May 2012, 14:21 PM
by dkinzer
RickB wrote:Is [the multi-tasking code] included without regard to flash size or has this been changed with the introduction of the generic compiler?
No. We made many changes in the ZBasic library code in order to reduce the executable image size. This affects both ZX targets and generic targets.

One of the changes is that there is now a single-task core and a multi-task core with the former contributing less to the executable image size than the latter. If you don't invoke CallTask (and thus aren't using multi-tasking), the application is linked with the single-task core.

Another change is that for generic target devices, the Com1 serial channel is not open by default. This allows an application that does not explicitly open Com1 to be smaller than it would otherwise be. Related to this, applications no longer have the SW UART overhead unless those channels are specifically opened.

Further, generic devices do not have the RTC included in the application unless specifically requested. Of course, the RTC is required to be included if the application uses certain other features (e.g. multi-tasking, time-of-day functions, etc.).

There were quite a few other situations, too numerous to mention individually, where library modules were split up into smaller parts to reduce the executable image size in cases where certain functionality is not used in an application. Lastly, the back-end build scripts now use a special feature of the avr-gcc linker that allows it to remove functions that are not referenced and to perform other size-reducing optimizations.

The net effect of these efforts is reflected in the code sizes for a simple "empty" application shown below.

Code: Select all

'Option DeviceParameter ClockFrequency 14745600
'Option ConsoleSpeed 0
'Option RTC off
Sub Main()
End Sub
When compiled with v3.0.0 for a ZX-24n, this application is 3274 bytes but is 2212 but when compiled with v4.0.0 for the same target. Compiling the same application (with the first line uncommented) for an ATmega644P yields an application size of 504 bytes.

Note, too, that for ZX devices, the RTC and Com1 code can be excluded using some new directives (uncommenting the second and third lines); this yields an application that is 640 bytes for a ZX-24.