Page 1 of 1

Experimental Compiler with Task Stack Size Estimation

Posted: 28 July 2008, 10:00 AM
by dkinzer
I have uploaded an experimental version of the compiler that implements task stack size estimation for native mode. The .zip file at the link below also includes updated ZX Library files that are required for the task stack size estimation to work.

http://www.zbasic.net/download/ZBasic/2 ... _2-5-6.zip

We would like to have the new feature tested on as much "real world" code as possible. In particular, we are interested in data comparing the values produced by the estimation algorithm to the values determined by System.TaskHeadRoom(). There has been one reported case where the estimated value is less than that reported by the headroom analysis but we haven't yet been able to analyze the ZBasic source code to determine what circumstances lead to that result. In most cases, the estimated value should be higher than the measured value because the estimate is based on "worst case" combinations that may not occur during the test execution period.

The native task stack size estimation uses a technique similar to that used for VM mode. The compiler determines the minimum stack size for a task by summing these values:
  • the maximum stack depth of the task together with all the routines it calls
  • the maximum of:
    • the stack use of each ISR considered separately
    • the task switch context size (37 or 38 bytes, the latter for devices with > 64K of Flash)
  • the size of the Task Control Block (13 bytes)
  • the stack margin (default: 10 bytes)
The minimum task stack size for a task routine containing an empty Do loop on a ZX-24n with the stack margin set to 10 is 60 bytes.

The highest stack use of the ISRs provided by the ZBasic Library is around 22 bytes so unless you define your own ISR, the stack depth used for the task switch context will always be larger than the largest ISR stack use.

The updated compiler now outputs information to the .map file for native mode compilations. Although the amount of data in the .map file is less than for VM mode, the most important data found there is the list of known tasks and the minimum stack size for each. There may be cases where the task stack size cannot be determined. In such a case, the task stack size is listed as ???. We are especially interested in the cases where this occurs.

You can suppress the task size analysis (for either VM or native mode) using the new compiler option --no-analyze-stack-use.

For special circumstances, a new pragma was introduced that allows you to tell the compiler how much stack space a routine or ISR uses. When used within a routine or an ISR, the syntax is:

Code: Select all

#pragma StackUse&#40;<stack-depth>&#41;
An alternate syntax that can be use within a routine or outside of all routines is:

Code: Select all

#pragma Stackuse&#40;<routine-name>, <stack-depth>&#41;
The latter form cannot be used for an ISR because ISRs don't have normal routine names. These pragma directives will be most useful in cases where you introduce indeterminate code (most likely via #asm).

Lastly, the Option StackMargin directive has been modified to allow a negative value to be specified. This might be useful if you are convinced that the compiler over estimates the minimum stack size and you'd like to tune it downward.