What Memory Model Should I Use?
One topic that appears from time to time is memory models. CodeWarrior for Palm OS V7 supports three models for applications: small, smart, and large. This setting is controlled by the 68K Processor preference panel.
In the small model, all calls to other functions are done through PC-relative JSR instructions. These can call anything within 32K of the current address -- its a short instruction, only taking up 4 bytes. The code generated for a jump looks like this:
00000010: 4EAD 0000 jsr FooPalmMain
Notice that the second two bytes are "0000" here. This is OK -- this value will be changed by the linker to be the appropriate location.
In the large model, all routine calls are emitted as a sequence of instructions which manually pushes a return address onto the program stack, then pushes the jump address onto the stack, and finally does a RTS to jump into the other routine. Its complex, and it takes four instructions -- it looks like this:
00000010: 487A 000E pea *+16 ; 0x00000020
00000014: 487A 0004 pea *+6 ; 0x0000001a
00000018: 0697 0000 0000 addi.l FooPalmMain,(a7)
0000001E: 4E75 rts
This uses 16 bytes, as opposed to the 4 bytes used by the small function call. The "pea" instructions stand for "push effective address"; the first one sets up the return addess, while the second one sets up the location of the routine you are calling. This code is used because it doesn't alter any registers (other than the stack pointer in A7), and because the addi instruction has a convienient 4-byte field that the linker can adjust with the offset of the routine.
The smart model is a combination of the two forms. Calls within the same source file are generated as small calls, but calls to routines outside the file are generated as large jumps.
I recommend that people only use the small unless they have an unusual need. The large and smart models were designed for the Macintosh, where it was possible to have segments that exceeded 64K. Since code segments are limited to 64K by Palm OS's resource model, there is no good reason to use the large or smart models, as they just add unneeded bulk to your code.
Its often possible to rearrange the order of your functions to get over link problems with small segments. You also can make your program multi-segment. If you do this, the linker will replace either style of call with a call through a global variable which will contain the location of the routine you're calling. Instead of a "JSR routine", you now have "JSR n(A5)". If the linker has to fixup a large jump, it will replace it the the JSR, followed by NOP instructions.
PalmSource 2001
"PalmSource" 2001 is coming soon. It's October 23-26 in Santa Jose, California, just miles away from Palm headquarters. Since it's September, you can't get the early-bird registration, but pre-registration does save you $100. If you've attended before, you also can get an alumni discount, saving $95.
Send feedback to combee@techwood.org
Copyright © 2004 Benjamin L. Combee
Palm OS is a registered trademark of PalmSource, Inc.
Metrowerks and CodeWarrior are registered trademarks of Metrowerks Inc.
The views expressed on this website/weblog are those of mine alone and do not necessarily reflect the views of PalmSource or Metrowerks.

qwertYAK / frobnovich
|