Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!uokmax!d.cs.okstate.edu!minich From: minich@d.cs.okstate.edu (Robert Minich) Newsgroups: comp.sys.mac.programmer Subject: Re: Pascal deficiency? Message-ID: <1990Dec18.042748.6765@d.cs.okstate.edu> Date: 18 Dec 90 04:27:48 GMT References: <13650001@hpspdra.HP.COM> Organization: Oklahoma State University Lines: 117 by stevewi@hpspdra.HP.COM (Steve Witten): | You can use the 'succ' and 'pred' operators/functions in Pascal to | accomplish the same function as '++' and '--'. So: | | integer i,j,k; | | ... | | j := succ(i); { same as 'j=i++' } | k := pred(i); { same as 'k=i--' } | | ... | | However, you have to remember that '++/--' are positional as well. | You cannot recreate this behavior. You also cannot recreate the | behavior of 'x++' without an assignment in Pascal. Do you not use C very often or are you VERY tired? The above Pascal statements are equivalent to j := i + 1; k := i - 1; whereas the C translates to Pascal as j = i++; | j := j + i; | i := i + 1; k = i--; | k := i - 1; | i := i - 1; The pred() and succ() functions are really used to get increment ordinal types whereas C doesn't care too much about types and will increment, say, a char by 1 without blinking. Someone has already posted MPW C's output so I thought I'd see what THINK C 4.0.2 does with the following: alpha() { int x; x = 1; x = x + 1; } beta () { int x; x = 1; x++; } main () { alpha(); beta(); } ALPHA +0000 LINK A6,#$FFFE +0004 MOVEQ #$01,D0 | tmp = 1; +0006 MOVE.W D0,-$0002(A6) | x = tmp; <== ick +000A MOVE.W -$0002(A6),D0 | tmp = x; <== +000E ADDQ.W #$1,D0 | tmp = tmp + 1; +0010 MOVE.W D0,-$0002(A6) | x = tmp; +0014 UNLK A6 +0016 RTS BETA +0000 LINK A6,#$FFFE +0004 MOVEQ #$01,D0 | tmp = 1; +0006 MOVE.W D0,-$0002(A6) | x = tmp; +000A ADDQ.W #$1,-$0002(A6) | x = x + 1; +000E UNLK A6 +0010 RTS I also tried {x += 1;} and it produced identical asm to BETA, which is OK. What I don't understand is what the heck is going on around what I marked "ick." Surely such a minute optimisation as not reading back what you just wrote is not too difficult to code. (Of course, I don't write compilers, either. :-) However, I think the ideal would be to replace ALPHA's +0004 MOVEQ #$01,D0 | tmp = 1; +0006 MOVE.W D0,-$0002(A6) | x = tmp; <== ick +000A MOVE.W -$0002(A6),D0 | tmp = x; <== +000E ADDQ.W #$1,D0 | tmp = tmp + 1; +0010 MOVE.W D0,-$0002(A6) | x = tmp; with MOVEQ.W #$1,-$0002(A6) | x = 1; ADDQ.W #$1,-$0002(A6) | x = x + 1; and be done with it. BETA is almost that ideal but Th C insists on using a register temp for x=1. I guess I can live with it if only because I don't like modifying asm by hand on a regular basis. :-) Now a really fancy optimizer would figure out that x always ends up as 2 or even that x is never used outside this scope and so forget about the calling ALPHA altogether. For the heck of it, I looked at what THINK Pascal 2.02 generated for the program below. It looks like the old Th Pascal betters the current Th C... program Test; procedure alpha; var i: integer; begin i := 1; i := i + 1; end; begin alpha; end. ALPHA +0000 2840EA LINK A6,#$FFEE +0004 2840EE MOVE.L D7,-(A7) push(D7) +0006 2840F0 MOVEQ #$01,D7 i := 1; +0008 2840F2 ADDQ.W #$1,D7 i := i + 1; +000A 2840F4 MOVE.L (A7)+,D7 pop(D7) +000C 2840F6 UNLK A6 +000E 2840F8 RTS Here, I don't understand what the deal is with saving D7 before use but I also don't remember Pascal calling conventions, so this may be proper. If anyone would like to add Th Pascal 3.0 output, that would be great, too. (I decided Pascal was getting on my nerves and the 3.0 upgrade was way too expensive for "just to have it around.") -- |_ /| | Robert Minich | |\'o.O' | Oklahoma State University| "I'm a newcomer here, but does the |=(___)= | minich@d.cs.okstate.edu | net ever lay any argument to rest?" | U | - Ackphtth | -- dan herrick