home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / 3b1 / 3274 < prev    next >
Encoding:
Internet Message Format  |  1992-09-03  |  3.1 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rphroy!hobbes!tkacik
  2. From: tkacik@hobbes.hobbes.cs.gmr.com (Tom Tkacik)
  3. Newsgroups: comp.sys.3b1
  4. Subject: Re: Lack of libPW.a module
  5. Message-ID: <89582@rphroy.ph.gmr.com>
  6. Date: 3 Sep 92 11:54:45 GMT
  7. Sender: news@rphroy.ph.gmr.com
  8. Reply-To: tkacik@hobbes.hobbes.cs.gmr.com
  9. Organization: GM Research Labs
  10. Lines: 77
  11. Nntp-Posting-Host: hobbes.cs.gmr.com
  12.  
  13. In article 4632@digibd.com, steve@digibd.com (Steve Wahl writes:
  14. > I don't think anyone understands me yet.  First, some context.
  15. > In article <1992Aug29.052011.24352@colnet.cmhnet.org> res@colnet.cmhnet.org (Rob Stampfli) writes:
  16. > >In article <1992Aug27.213851.20578@digibd.com> steve@digibd.com (Steve Wahl) writes:
  17. > >[ alloca.s code deleted ]
  18. > >>
  19. > >>Rob's post mentions that you can't call this alloca() when there's
  20. > >>something on the stack.  But beware: I believe this aproach also fails
  21. > >>if you use register variables in your functions: think about how the
  22. > >>compiler saves and restores the registers.
  23.  
  24. All passed parameters and local variables will be ahead of the alloca'd
  25. space, and will be accessed via the frame pointer (%a6) rather than the
  26. stack pointer (%sp aka %a7).  Only temporary variables are accessed via
  27. the stack pointer.
  28.  
  29. Register variables are ok, as they will be saved before the alloca.  The stack
  30. will be clear.  The only problem is using alloca in an expression.
  31. Never do something like   char *c; c=i*(j+alloca(10))
  32. Any temporaries that are put on the stack for this expression are accessed via
  33. the stack pointer, and will be corrupted.
  34.  
  35. Also, note that this alloca is non-reentrant.  Do not do c=alloca(alloca(10)).
  36. This one is guarenteed not to work.
  37.  
  38. > Yes, but the problem is in what is restored by the called function...
  39.  
  40. No, that is not a problem.  The unlink instruction will return everything to
  41. normal when the routine ends.
  42.  
  43.  
  44. An example is given (most of which is now deleted):
  45. > f2()
  46. > {
  47. >     register int b;
  48. >     char *p;
  49. >     b = 7;
  50. >     p = alloca(10);
  51. >     return(b);
  52. > }
  53.  
  54. And the approximate assembly code:
  55.  
  56. > f2:
  57. >     link.l    %a6,-4        # create stack frame
  58. >     mov.l    %d2,-(%sp)    # save regsiter variable
  59. >     mov.l    &7,%d2        # set b to 5
  60. >     pea.l    10        # parameter to alloca
  61. >     bsr    alloca        # call alloca
  62. >     add.w    &4,%sp        # pop argument to alloca
  63. >     mov.l    $d0,-4(%a6)    # store alloca return in p
  64. >     mov.l    %d2,%d0        # return value
  65. >     mov.l    (%sp)+,%d2    # restore register variable **************    
  66. >     unlk    %a6        # remove stack frame
  67. >     rts
  68.  
  69. All of the saved parameters, passed parameters, and local variables are accessed
  70. via the frame pointer, not the stack pointer.  Your line marked "**************"
  71. should use %a6.  (Actually, what will happen is the link instruction is given
  72. a mask telling it which registers to save.  Neither your save %d2, nor restore %d2
  73. will be generated by the compiler.  The Unlink instruction will restore the
  74. proper registers from the stack, again using %a6 not %sp.)
  75.  
  76. Everything will work out just fine.  The Unlink will also restore the stack
  77. pointer to its proper value before calling alloca.
  78.  
  79. > Anyone disagree with my analysis?
  80.  
  81. Yes.
  82.  
  83. Tom Tkacik
  84. GM Research Labs
  85. tkacik@hobbes.cs.gmr.com
  86. tkacik@kyzyl.mi.org
  87.  
  88.