home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / 3b1 / 3268 < prev    next >
Encoding:
Text File  |  1992-09-02  |  3.4 KB  |  105 lines

  1. Newsgroups: comp.sys.3b1
  2. Path: sparky!uunet!digibd!steve
  3. From: steve@digibd.com (Steve Wahl)
  4. Subject: Re: Lack of libPW.a module
  5. Organization: DigiBoard Incorporated, Eden Prairie, MN
  6. Date: Wed, 02 Sep 1992 20:12:16 GMT
  7. Message-ID: <1992Sep02.201216.4632@digibd.com>
  8. Keywords: libPW PW alloca xmalloc
  9. References: <1992Aug25.235036.28469@colnet.cmhnet.org> <1992Aug27.213851.20578@digibd.com> <1992Aug29.052011.24352@colnet.cmhnet.org>
  10. Lines: 93
  11.  
  12. I don't think anyone understands me yet.  First, some context.
  13.  
  14. In article <1992Aug29.052011.24352@colnet.cmhnet.org> res@colnet.cmhnet.org (Rob Stampfli) writes:
  15. >In article <1992Aug27.213851.20578@digibd.com> steve@digibd.com (Steve Wahl) writes:
  16. >[ alloca.s code deleted ]
  17. >>
  18. >>Rob's post mentions that you can't call this alloca() when there's
  19. >>something on the stack.  But beware: I believe this aproach also fails
  20. >>if you use register variables in your functions: think about how the
  21. >>compiler saves and restores the registers.
  22. >>
  23. >
  24. >Register variables are not saved prior to calling a function, but rather are
  25. >saved and restored by the called function, and then only if the register is
  26. >needed therein.
  27.  
  28. Yes, but the problem is in what is restored by the called function...
  29.  
  30. >  This is true of both the stock cc and gcc compilers for the
  31. >3B1.  The alloca function I posted uses only those register variables that
  32. >are defined to be scratch variables by the compilers, so there is no need
  33. >for it to save %d0 or %a0.  (In fact, this function doesn't really set up a
  34. >standard 'C' stack frame for itself -- since it is so simple and calls no
  35. >one, it uses an efficient, but externally equivalent alternative.)
  36.  
  37. Ok, so consider this code:
  38.  
  39. f1()
  40. {
  41.     register int a;
  42.  
  43.     a = 5;
  44.     a += f2();
  45.     return(a);
  46. }
  47.  
  48. f2()
  49. {
  50.     register int b;
  51.     char *p;
  52.  
  53.     b = 7;
  54.     p = alloca(10);
  55.     return(b);
  56. }
  57.  
  58. I don't have direct access to my 3b1 right at this moment, but I believe the
  59. compiler will pick register d2 for both variables a and b.  So the
  60. code for f1 would be something like:
  61.  
  62.      [ I don't have immediate access to my 3b1 , so this could be off ]
  63.  
  64. f1:
  65.     link.l    %a6,0        # create stack frame
  66.     mov.l    %d2,-(%sp)    # save register variable
  67.     mov.l    &5,%d2        # set a to 5
  68.     bsr    f2        # call f2
  69.     add.l    %d0,%d2        # add result
  70.     mov.l    %d2,%d0        # set return value
  71.     mov.l    (%sp)+,%d2    # restore register variable
  72.     unlk    %a6        # remove stack frame
  73.     rts
  74.  
  75. f2:
  76.     link.l    %a6,-4        # create stack frame
  77.     mov.l    %d2,-(%sp)    # save regsiter variable
  78.     mov.l    &7,%d2        # set b to 5
  79.     pea.l    10        # parameter to alloca
  80.     bsr    alloca        # call alloca
  81.     add.w    &4,%sp        # pop argument to alloca
  82.     mov.l    $d0,-4(%a6)    # store alloca return in p
  83.     mov.l    %d2,%d0        # return value
  84.     mov.l    (%sp)+,%d2    # restore register variable **************    
  85.     unlk    %a6        # remove stack frame
  86.     rts
  87.  
  88. The restore register variable step marked with "*******" is the
  89. problem, because it restores garbage from within the alloca'd area
  90. instead of the value pushed on the stack (alloca made the stack
  91. pointer move, unknown to the compiler).  This could be a very hard
  92. problem to figure out, since it doesn't affect variables in f2, only
  93. in f1!  (BTW, I believe gcc may restore the register contents with
  94. known offsets from the frame pointer %a6, rather than the stack
  95. pointer - so GCC may not have the problem, but I'm quite sure the
  96. stock cc has it.)
  97.  
  98. Anyone disagree with my analysis?
  99.  
  100. --> Steve
  101. -- 
  102. Steve Wahl               steve@digibd.com
  103. Digi International, Inc.
  104. Eden Prairie, MN         (612) 943-5387
  105.