home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!rphroy!hobbes!tkacik
- From: tkacik@hobbes.hobbes.cs.gmr.com (Tom Tkacik)
- Newsgroups: comp.sys.3b1
- Subject: Re: Lack of libPW.a module
- Message-ID: <89582@rphroy.ph.gmr.com>
- Date: 3 Sep 92 11:54:45 GMT
- Sender: news@rphroy.ph.gmr.com
- Reply-To: tkacik@hobbes.hobbes.cs.gmr.com
- Organization: GM Research Labs
- Lines: 77
- Nntp-Posting-Host: hobbes.cs.gmr.com
-
- In article 4632@digibd.com, steve@digibd.com (Steve Wahl writes:
- > I don't think anyone understands me yet. First, some context.
- >
- > In article <1992Aug29.052011.24352@colnet.cmhnet.org> res@colnet.cmhnet.org (Rob Stampfli) writes:
- > >In article <1992Aug27.213851.20578@digibd.com> steve@digibd.com (Steve Wahl) writes:
- > >[ alloca.s code deleted ]
- > >>
- > >>Rob's post mentions that you can't call this alloca() when there's
- > >>something on the stack. But beware: I believe this aproach also fails
- > >>if you use register variables in your functions: think about how the
- > >>compiler saves and restores the registers.
-
- All passed parameters and local variables will be ahead of the alloca'd
- space, and will be accessed via the frame pointer (%a6) rather than the
- stack pointer (%sp aka %a7). Only temporary variables are accessed via
- the stack pointer.
-
- Register variables are ok, as they will be saved before the alloca. The stack
- will be clear. The only problem is using alloca in an expression.
- Never do something like char *c; c=i*(j+alloca(10))
- Any temporaries that are put on the stack for this expression are accessed via
- the stack pointer, and will be corrupted.
-
- Also, note that this alloca is non-reentrant. Do not do c=alloca(alloca(10)).
- This one is guarenteed not to work.
-
- > Yes, but the problem is in what is restored by the called function...
-
- No, that is not a problem. The unlink instruction will return everything to
- normal when the routine ends.
-
-
- An example is given (most of which is now deleted):
- > f2()
- > {
- > register int b;
- > char *p;
- >
- > b = 7;
- > p = alloca(10);
- > return(b);
- > }
-
- And the approximate assembly code:
-
- > f2:
- > link.l %a6,-4 # create stack frame
- > mov.l %d2,-(%sp) # save regsiter variable
- > mov.l &7,%d2 # set b to 5
- > pea.l 10 # parameter to alloca
- > bsr alloca # call alloca
- > add.w &4,%sp # pop argument to alloca
- > mov.l $d0,-4(%a6) # store alloca return in p
- > mov.l %d2,%d0 # return value
- > mov.l (%sp)+,%d2 # restore register variable **************
- > unlk %a6 # remove stack frame
- > rts
-
- All of the saved parameters, passed parameters, and local variables are accessed
- via the frame pointer, not the stack pointer. Your line marked "**************"
- should use %a6. (Actually, what will happen is the link instruction is given
- a mask telling it which registers to save. Neither your save %d2, nor restore %d2
- will be generated by the compiler. The Unlink instruction will restore the
- proper registers from the stack, again using %a6 not %sp.)
-
- Everything will work out just fine. The Unlink will also restore the stack
- pointer to its proper value before calling alloca.
-
- > Anyone disagree with my analysis?
-
- Yes.
-
- Tom Tkacik
- GM Research Labs
- tkacik@hobbes.cs.gmr.com
- tkacik@kyzyl.mi.org
-
-