Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sample.eng.ohio-state.edu!purdue!news.cs.indiana.edu!spool.mu.edu!mips!apple!stadler From: stadler@Apple.COM (Andy Stadler) Newsgroups: comp.sys.apple2 Subject: Re: Any Pascal programmers for Apple II GS? Message-ID: <52655@apple.Apple.COM> Date: 9 May 91 18:31:20 GMT Organization: Apple Computer Inc., Cupertino, CA Lines: 77 In article <13419@goofy.Apple.COM> jeffh@HyperMail.apple.com (Jeff Holcomb) writes: >Does the MPW IIgs Pascal compiler do any optimizations on the code? If so, >what kinds? I don't know about Orca/Pascal, but Orca/C is definitely not an >optimizing compiler. The same for MPW IIgs C, I think. The MPW IIGS Pascal compiler contains a huge library of "code templates" which allow it to select the best series of opcodes for a given set of operations. Each template includes information on the size (# of bytes) and speed (# of cycles) and the compiler iterates over every set of templates which could work, and then chooses the set which have the best (speed or space, depending on a command-line option). These templates range from the mundane to the complex.... But you'd be amazed at some of the templates we threw in. Some examples: IF var = 2 THEN ... is coded as lda var, dec, dec, bne. Branches are ALWAYS short unless the distance requires long. CASE statements are generated as tables when the selectors are dense, and computed IF's when they are sparse. In addition, "fast math" is used (it wouldn't be legal pascal for computations but it takes advantage of 816 ALU quirks). Many branches are generated directly using status register flags. For example, most compilers will compile IF var > 0 using a subtract of zero! But PascalIIGS instead uses lda var, bmi, beq. It knows that load's set the flags (many compilers don't). Even better, it knows that for many long integer compares, you DON'T need to look at the low word. For example, IF longVar < 0 can be coded as lda longvar+2 bmi Every other compiler I've ever seen loads all 32 bits, and performs a 32 bit comparison of the value. Long integer math is also quite efficient. Adds and subtracts of longs with constants are extremely optimized, depending on the constant. For example, if you add 1 to a long integer in A and X, it's just inc a bne *+3 inx And if you added 100 to the long, it would be clc adc #100 bcc *+3 inx Once again it's the only compiler I've looked at (on the GS) which performs these types of optimizations. All of the examples I've listed so far are "code generation" optimizations. In addition, the "front end" (the language parser) is also extremely efficient. It actually does things like rearranging code to put things in the most efficient order, and actually throwing out unused code. A simple "throwaway" example might be the code REPEAT UNTIL FALSE; an endless loop. Most compilers would generate: loop lda #0000 cmp #0000 beq loop But pascal IIGS recognizes this and just generates loop bra loop Finally, there are some simple rules programmers can follow which will help the compiler generate the best code possible. If there's anyone out there actually using this compiler, let me know and I can explain some of them... Which brings up the point, a lot of people misunderstood an earlier posting of mine. I said something like "Alas, not too many programmers use MPWIIGS". I did NOT mean this as a plug for MPW or buying Macintoshes. All I meant to say was that there's a nice compiler out there, and it's too bad it's on a development system which isn't widely used. That's all. A hint: Try not to be so defensive, folks. Focus some of that great energy on writing the next great GS applications! Andy Stadler Apple Computer, Inc. P.S. One final word on HLL vs. ML: Remember to choose the right tool for the job. AND, remember that a single program doesn't have to be written in a single language. Assembly isn't the perfect language for every programming problem. Neither are C or Pascal. Use each when appropriate and let your linker sort out the results.