I apologize for the long nature of this message (with the assembly code in
it, well it gets pretty large).
This is just a snippet of working code, please disregard the fact that it
returns no useful result. It is intended as an example only.
I was compiling a much larger version of this function ("useful results
version") and kept having problems with stack overflows on Palm OS 3.0, and
2.0, but the function would work fine on Palm OS 3.5.
After taking a look at the disassembled version of the code, and trying to
make heads of tails of what is going on, the conclusion that I have come up
with is that CodeWarrior is setting aside space on the stack for temporary
or intermediate results, but doesn't appear to resuse that space once an
expression is done with the intermediate result. If you pile a bunch of
expressions into the function, then codewarrior allocates a larger amount of
stack space, and eventually you can overflow the stack.
Specifically, take a look at address: 0000, where it sets aside space.
Each expression below is of the form: some_answer = (some_var -
floor(some_var)) * PI2;
(PI2 is the constant 2 * 3.141592653591, and floor() is a call to MathLib's
floor function).
The first expression seems to start at address 0006. At address 0018, space
appears to be set aside for a result or intermediate result.
Expression two, starts at address 006A, and at address 0072, space appears
to be set aside for a result. Notice it is different by 8 bytes.
This continues on with each expression appearing to use further offsets into
the stack.
I find this kind of odd, shouldn't temporary space be resused and done in
such a way as to minimize the stack space required?
Of course my analysis could be all wrong... But I would like to understand
what is going on.
See the assembly below for further details. Thanks!
Ed
Hunk: Kind=HUNK_GLOBAL_CODE Name="MoonPosTest"(31) Size=596
void MoonPosTest(double t, double *a5, double *d5, double *r5) {
MOONPOSDATA *mp;
00000000: 4E56 FFC0 link a6,#-64
00000004: 2F0A move.l a2,-(a7)
mp = MemPtrNew(sizeof(MOONPOSDATA));
00000006: 4878 0070 pea 0x00000070
0000000A: 4E4F trap #15
0000000C: A013 sysTrapMemPtrNew
0000000E: 2448 movea.l a0,a2
mp->l = (mp->l - floor(mp->l)) * PI2;
00000010: 2F2A 0004 move.l 4(a2),-(a7)
00000014: 2F2A 0000 move.l 0(a2),-(a7)
00000018: 486E FFF0 pea -16(a6)
0000001C: 487A 000E pea *+16 ; 0x0000002c
00000020: 487A 0004 pea *+6 ; 0x00000026
00000024: 0697 0000 0000 addi.l floor,(a7)
0000002A: 4E75 rts
0000002C: 4FEF 000C lea 12(a7),a7
00000030: 2F2E FFF4 move.l -12(a6),-(a7)
00000034: 2F2E FFF0 move.l -16(a6),-(a7)
00000038: 2F2A 0004 move.l 4(a2),-(a7)
0000003C: 2F2A 0000 move.l 0(a2),-(a7)
00000040: 486E FFC0 pea -64(a6)
00000044: 7435 moveq #53,d2
00000046: 4E4F trap #15
00000048: A306 sysTrapFlpEmDispatch
0000004A: 4FEF 0014 lea 20(a7),a7
0000004E: 2F28 0004 move.l 4(a0),-(a7)
00000052: 2F28 0000 move.l 0(a0),-(a7)
00000056: 2F3C 5444 2EEA move.l #1413754602,-(a7)
0000005C: 2F3C 4019 21FB move.l #1075388923,-(a7)
00000062: 2F0A move.l a2,-(a7)
00000064: 7434 moveq #52,d2
00000066: 4E4F trap #15
00000068: A306 sysTrapFlpEmDispatch
mp->m = (mp->m - floor(mp->m)) * PI2;
0000006A: 2F2A 000C move.l 12(a2),-(a7)
0000006E: 2F2A 0008 move.l 8(a2),-(a7)
00000072: 486E FFE8 pea -24(a6)
00000076: 487A 000E pea *+16 ; 0x00000086
0000007A: 487A 0004 pea *+6 ; 0x00000080
0000007E: 0697 0000 0000 addi.l floor,(a7)
00000084: 4E75 rts
00000086: 4FEF 000C lea 12(a7),a7
0000008A: 2F2E FFEC move.l -20(a6),-(a7)
0000008E: 2F2E FFE8 move.l -24(a6),-(a7)
00000092: 2F2A 000C move.l 12(a2),-(a7)
00000096: 2F2A 0008 move.l 8(a2),-(a7)
0000009A: 486E FFF8 pea -8(a6)
0000009E: 7435 moveq #53,d2
000000A0: 4E4F trap #15
000000A2: A306 sysTrapFlpEmDispatch
000000A4: 4FEF 0014 lea 20(a7),a7
000000A8: 2F28 0004 move.l 4(a0),-(a7)
000000AC: 2F28 0000 move.l 0(a0),-(a7)
000000B0: 2F3C 5444 2EEA move.l #1413754602,-(a7)
000000B6: 2F3C 4019 21FB move.l #1075388923,-(a7)
000000BC: 486A 0008 pea 8(a2)
000000C0: 7434 moveq #52,d2
000000C2: 4E4F trap #15
000000C4: A306 sysTrapFlpEmDispatch
mp->f = (mp->f - floor(mp->f)) * PI2;
000000C6: 2F2A 0014 move.l 20(a2),-(a7)
000000CA: 2F2A 0010 move.l 16(a2),-(a7)
000000CE: 486E FFE0 pea -32(a6)
000000D2: 487A 000E pea *+16 ; 0x000000e2
000000D6: 487A 0004 pea *+6 ; 0x000000dc
000000DA: 0697 0000 0000 addi.l floor,(a7)
000000E0: 4E75 rts
000000E2: 4FEF 000C lea 12(a7),a7
000000E6: 2F2E FFE4 move.l -28(a6),-(a7)
000000EA: 2F2E FFE0 move.l -32(a6),-(a7)
000000EE: 2F2A 0014 move.l 20(a2),-(a7)
000000F2: 2F2A 0010 move.l 16(a2),-(a7)
000000F6: 486E FFF8 pea -8(a6)
000000FA: 7435 moveq #53,d2
000000FC: 4E4F trap #15
000000FE: A306 sysTrapFlpEmDispatch
00000100: 4FEF 0014 lea 20(a7),a7
00000104: 2F28 0004 move.l 4(a0),-(a7)
00000108: 2F28 0000 move.l 0(a0),-(a7)
0000010C: 2F3C 5444 2EEA move.l #1413754602,-(a7)
00000112: 2F3C 4019 21FB move.l #1075388923,-(a7)
00000118: 486A 0010 pea 16(a2)
0000011C: 7434 moveq #52,d2
0000011E: 4E4F trap #15
00000120: A306 sysTrapFlpEmDispatch
mp->d = (mp->d - floor(mp->d)) * PI2;
00000122: 2F2A 001C move.l 28(a2),-(a7)
00000126: 2F2A 0018 move.l 24(a2),-(a7)
0000012A: 486E FFD8 pea -40(a6)
0000012E: 487A 000E pea *+16 ; 0x0000013e
00000132: 487A 0004 pea *+6 ; 0x00000138
00000136: 0697 0000 0000 addi.l floor,(a7)
0000013C: 4E75 rts
0000013E: 4FEF 000C lea 12(a7),a7
00000142: 2F2E FFDC move.l -36(a6),-(a7)
00000146: 2F2E FFD8 move.l -40(a6),-(a7)
0000014A: 2F2A 001C move.l 28(a2),-(a7)
0000014E: 2F2A 0018 move.l 24(a2),-(a7)
00000152: 486E FFF8 pea -8(a6)
00000156: 7435 moveq #53,d2
00000158: 4E4F trap #15
0000015A: A306 sysTrapFlpEmDispatch
0000015C: 4FEF 0014 lea 20(a7),a7
00000160: 2F28 0004 move.l 4(a0),-(a7)
00000164: 2F28 0000 move.l 0(a0),-(a7)
00000168: 2F3C 5444 2EEA move.l #1413754602,-(a7)
0000016E: 2F3C 4019 21FB move.l #1075388923,-(a7)
00000174: 486A 0018 pea 24(a2)
00000178: 7434 moveq #52,d2
0000017A: 4E4F trap #15
0000017C: A306 sysTrapFlpEmDispatch
mp->n = (mp->n - floor(mp->n)) * PI2;
0000017E: 2F2A 0024 move.l 36(a2),-(a7)
00000182: 2F2A 0020 move.l 32(a2),-(a7)
00000186: 486E FFD0 pea -48(a6)
0000018A: 487A 000E pea *+16 ; 0x0000019a
0000018E: 487A 0004 pea *+6 ; 0x00000194
00000192: 0697 0000 0000 addi.l floor,(a7)
00000198: 4E75 rts
0000019A: 4FEF 000C lea 12(a7),a7
0000019E: 2F2E FFD4 move.l -44(a6),-(a7)
000001A2: 2F2E FFD0 move.l -48(a6),-(a7)
000001A6: 2F2A 0024 move.l 36(a2),-(a7)
000001AA: 2F2A 0020 move.l 32(a2),-(a7)
000001AE: 486E FFF8 pea -8(a6)
000001B2: 7435 moveq #53,d2
000001B4: 4E4F trap #15
000001B6: A306 sysTrapFlpEmDispatch
000001B8: 4FEF 0014 lea 20(a7),a7
000001BC: 2F28 0004 move.l 4(a0),-(a7)
000001C0: 2F28 0000 move.l 0(a0),-(a7)
000001C4: 2F3C 5444 2EEA move.l #1413754602,-(a7)
000001CA: 2F3C 4019 21FB move.l #1075388923,-(a7)
000001D0: 486A 0020 pea 32(a2)
000001D4: 7434 moveq #52,d2
000001D6: 4E4F trap #15
000001D8: A306 sysTrapFlpEmDispatch
mp->g = (mp->g - floor(mp->g)) * PI2;
000001DA: 2F2A 002C move.l 44(a2),-(a7)
000001DE: 2F2A 0028 move.l 40(a2),-(a7)
000001E2: 486E FFC8 pea -56(a6)
000001E6: 487A 000E pea *+16 ; 0x000001f6
000001EA: 487A 0004 pea *+6 ; 0x000001f0
000001EE: 0697 0000 0000 addi.l floor,(a7)
000001F4: 4E75 rts
000001F6: 4FEF 000C lea 12(a7),a7
000001FA: 2F2E FFCC move.l -52(a6),-(a7)
000001FE: 2F2E FFC8 move.l -56(a6),-(a7)
00000202: 2F2A 002C move.l 44(a2),-(a7)
00000206: 2F2A 0028 move.l 40(a2),-(a7)
0000020A: 486E FFF8 pea -8(a6)
0000020E: 7435 moveq #53,d2
00000210: 4E4F trap #15
00000212: A306 sysTrapFlpEmDispatch
00000214: 4FEF 0014 lea 20(a7),a7
00000218: 2F28 0004 move.l 4(a0),-(a7)
0000021C: 2F28 0000 move.l 0(a0),-(a7)
00000220: 2F3C 5444 2EEA move.l #1413754602,-(a7)
00000226: 2F3C 4019 21FB move.l #1075388923,-(a7)
0000022C: 486A 0028 pea 40(a2)
00000230: 7434 moveq #52,d2
00000232: 4E4F trap #15
00000234: A306 sysTrapFlpEmDispatch
MemPtrFree(mp);
00000236: 2F0A move.l a2,-(a7)
00000238: 4E4F trap #15
0000023A: A012 sysTrapMemChunkFree
0000023C: 4FEF 0080 lea 128(a7),a7
00000240: 245F movea.l (a7)+,a2
00000242: 4E5E unlk a6
00000244: 4E75 rts
00000246: 8B4D 6F6F 6E50 dc.b 0x8b,'MoonPosTest'
6F73 5465 7374
00000252: 0000
}
XRef: Kind=HUNK_XREF_PCREL32BIT Name="floor"(23) #Pairs=6
Offset=$000001F0 Value=$00000000
Offset=$00000194 Value=$00000000
Offset=$00000138 Value=$00000000
Offset=$000000DC Value=$00000000
Offset=$00000080 Value=$00000000
Offset=$00000026 Value=$00000000
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/