TweetFollow Us on Twitter

PowerPC Series
Volume Number:10
Issue Number:1
Column Tag:PowerPC Series

PowerPC Code Generation

What’s the difference between PowerPC and 68K machines?

By Peter A. Jacobson, Absoft Corp.

About the author

Peter is a principle of Absoft. Along with his partner Wood Lotz, he has been developing scientific and engineering software since 1979 for a wide variety of micro- and mini-computers.

This article will discuss some of this issues concerning code generation by high level language compilers for the IBM PowerPC RISC microprocessor. It will compare and contrast typical code generation strategies employed on CISC based architectures, such as the Motorola M68000 family of microprocessors, against the approach that might be taken with the PowerPC. The topics addressed will include addressing modes, register sets, instruction sets, instruction pipelines, and superscalar considerations. It should be understood that certain features of the PowerPC will be simplified and various aspects of code generation will be trivialized in order to facilitate this discussion.

It is difficult to arrive at a precise definition of what constitutes a RISC microprocessor. It rarely means that an individual machine actually has fewer instructions than its CISC counterpart. The PowerPC has over 230 instructions while an MC68020 has barely 100. The technology progresses so quickly that definitions are amended before they even come into common usage. In addition, features that were once ascribed only to RISC technology have found their way into CISC architectures. However, one of the most significant differences affecting code generation for RISC microprocessors is that instructions are restricted to one machine word in length and there are consequently only a limited number of instruction formats available which can access memory. Typically, load and store are the only memory operations provided and usually with extremely limited effective addressing modes. The PowerPC provides just one fundamental addressing mode: register indirect with index. The index, which may be either an immediate operand or a general purpose register, is added to a general purpose register to form the effective address. The immediate operand is encoded in the instruction and consists of a 16-bit value, sign extended to 32 bits. The register can be suppressed by specifying general purpose register R0 so that an address is formed from just the index. In this way, absolute addresses can be formed from the immediate operand, but they are limited to just the lowest and the highest 32768 bytes of memory.

Obviously, this restraint seriously affects code generation strategies. With a Motorola MC680x0, an efficient code generator could add 1 to a variable by incrementing a memory location directly with an ADDQ instruction. While for the PowerPC, it is necessary to first load the variable from memory, perform the addition, and then store the result. It might appear that this model leads to very inefficient code production, but there are many other factors that must be considered in generating code. First, most program variables are usually accessed more than once in a given procedure. Therefore, for either type of microprocessor, it is almost always more efficient to have the variable already available in a register, rather than repeatedly accessing main memory for it. Since RISC microprocessors provide such a limited number of instructions which can access memory, code generators must be capable of performing a very sophisticated analysis of program flow and allocate registers accordingly. RISC microprocessors typically provide a large set of registers that can be used to maintain copies of program variables. The Motorola MC68040, widely used in the current generation of Macintoshes, is limited to 8 data registers, 8 address registers, 8 floating point registers, and a condition code register. The PowerPC provides 32 general purpose registers, 32 floating point registers, a condition register divided into eight 4-bit fields, and six user-level special purpose registers. The general purpose registers can be used for both addresses and data. Just as with the MC68040, some of the registers are reserved for special purposes (such as stack pointer, data space pointer, etc.), but the PowerPC still provides a large number of registers for program use.

Compilers also create their own variables, many of which can have short, but very active life spans. Such compiler created variables are used for loop induction, array indexing, maintaining the intermediate results of expression evaluation, and so on. The register set represents the fastest memory available to the microprocessor and efficient register allocation is critical to program performance. Various register allocation schemes are used by code generators to insure that the most appropriate variables are allocated to registers, either temporarily within a region of code, or permanently, for the length of the procedure. Further, compilers do not necessarily immediately write the result of an assignment statement to memory. This is known as a delayed store and is employed to allow efficient scheduling of the instruction stream (discussed below). Indeed, variables which are local to a procedure may never be written to memory. However, regardless of how efficiently the compiler allocates variables to registers, it must provide a mechanism by which a programmer can indicate that a variable (and its associated memory location) is volatile. Processes in many real time systems often communicate with each other through memory locations and use memory mapped I/O to control or react to external devices. If, by setting a variable to a specific value, the programmer intends to control a valve or launch a missile, it would be inappropriate (to say the least) for the code generator not to update the associated memory location immediately.

To the programmer accustomed to the Motorola M68000 family of microprocessors and unfamiliar with RISC architectures, the instruction set of the PowerPC may seem initially puzzling. Nevertheless, the PowerPC architecture has much in common with other RISC microprocessors such as the SPARC, MC88110, R4400, and obviously POWER. The first significant difference is that most of the instructions take three operands, two sources and a destination, and several instructions take more. Also, there is no stack pointer, no instructions for calling subroutines, no obvious way to move the contents of a general purpose register to another general purpose register, and many other apparent deficiencies. (However, programmers familiar will older mainframes and mini-computers will find nothing new here.) Consider the following instruction:

 fnmsubs6,12,13,18

This is the “Floating Point Negative Multiply-Subtract (Single-Precision)” instruction. Since there is no ambiguity in the instruction set, registers are indicated by number only - register numbers cannot be confused with immediate values. This instruction says to multiply the operand in floating point register 12 by the operand in floating point register 13 and then subtract the operand in floating point register 18 from this intermediate value. The result is rounded, then negated, and finally placed in floating point register 6. The latency of this instruction is just 4 clocks - the total time it takes to execute the instruction and for the result to be available in the destination floating point register.

Since every instruction can have a destination operand different from its source(s), compilers are not forced to either copy or reload values (variables or expressions) that will be used multiple times in a block of code. This is important not only in avoiding unnecessary memory accesses, but as will be seen later, provides opportunities for exploiting the instruction pipeline and the superscalar nature of the PowerPC.

The problem of there being no stack pointer in the PowerPC architecture has been addressed by the various standards bodies concerned with the PowerPC. Through the formalization and adoption of ABIs (Application Binary Interfaces) the needs of high-level languages for a uniform stack pointer and stack frame have been addressed. General purpose register 1 is normally designated as the stack pointer and various locations in the frame have been reserved for house keeping purposes. A frame is often created by saving the current stack pointer and then subtracting the required frame amount from the stack pointer to create the new frame. In practice it is easier to accomplish this than it appears since one form of the store instruction will write the effective address of the destination into the register used to calculate the effective address:

 stwu   rS,d(rA)

This is the “Store Word with Update” instruction which says to store the contents of the source register rS at an effective address equal to the contents of general purpose register rA plus the immediate index value d and then place that effective address in rA. To create a frame, rS and rA would be 1 and d would be negative. The instruction would cause r1 to be stored at the location resulting from the calculation of the effective address r1-d and then update r1 to r1-d.

One of the most important locations in the frame is naturally where the return address for a subroutine call is stored. As stated earlier, the PowerPC does not have a subroutine call instruction - instead the branch instruction is used. A form of this instruction places the address of the instruction that follows the branch into a special purpose register called the link register. Any procedure which is not a leaf (i.e. a procedure which calls other procedures) must save the link register before calling another procedure. A subroutine return is accomplished by simply branching to the contents of the link register.

The so-called fused multiply-add instructions are another feature of the PowerPC instruction set that is important enough to be mentioned here. These instructions can perform a multiplication and an addition in the same amount of time as just a single multiplication or a single addition alone. In other words, twice as fast as the combined operations. Fortunately, this type of operation occurs often enough in mathematical software that the alert code generator will find ample opportunities to exploit them. For example, expressions of the form:

 a1 = a0 + b x c

appear in matrix operations and in polynomial expansions.

The PowerPC implements a true superscalar architecture. A superscalar machine is one which can issue multiple instructions to different execution units during each clock cycle. The PowerPC incorporates three different execution units that can operate independently and in parallel. They are the integer unit which affects the general purpose registers, the floating point unit which affects the floating point registers, and the branch unit which affects certain of the special purpose registers. Therefore, an integer shift, a floating point addition, and a branch instruction could all be issued during the same clock cycle. It is important to understand that not all of the PowerPC instructions can execute in a single clock cycle and it would be extremely difficult to schedule all three execution units for simultaneous execution on every cycle, but with careful code generation and attention paid to data dependencies, an exceptionally efficient throughput can be achieved.

It is not necessary for an instruction to completely finish in an individual execution unit before another instruction can be issued. The execution of an instruction consists of multiple stages that can be viewed (very roughly for the PowerPC is far more complicated) as fetch, decode, execute, and writeback. Each instruction is fetched from an instruction queue, decoded, executed, and the result is then written to the appropriate register file. These stages are called the pipeline and it is possible and certainly desirable for multiple instructions to be in the pipeline at once - each at a different stage. The basic limitation which would cause an instruction to stall is data dependency, which means that the execution of the instruction is dependant on the result of the preceding instruction. An instruction can also be stalled if it is waiting for an instruction with a latency greater than once clock to finish executing. That is, an instruction takes more cycles than there are stages in the pipeline for that execution unit. Instruction latency is determined by how complicated an instruction is (division takes longer than addition) and by memory access considerations. An instruction may stall while waiting for an operand to be delivered from memory. The issues of cache arbitration, both for instructions and data, are beyond the scope of this article.

A code generator which is aware of these two features, multiple execution units and their pipelines, attempts to schedule the instruction stream to make the most efficient use of the resources. Scheduling consists largely of the code generator rearranging or moving instructions to eliminate data dependencies and to keep the individual pipelines busy. This can cause expressions to executed out of order, array element address calculations to take place far from the memory references, and any number of other reorderings of the instruction stream to eliminate data dependencies. Obviously, register allocation seriously affects this scheduling process and is usually put off as long as possible to prevent any artificial or code-generator created dependencies.

“It projects a military coup!”

 
AAPL
$517.96
Apple Inc.
-3.72
MSFT
$39.75
Microsoft Corpora
+0.57
GOOG
$536.44
Google Inc.
+3.92

MacTech Search:
Community Search:

Software Updates via MacUpdate

Starcraft II: Wings of Liberty 1.1.1.180...
Download the patch by launching the Starcraft II game and downloading it through the Battle.net connection within the app. Starcraft II: Wings of Liberty is a strategy game played in real-time. You... Read more
Sibelius 7.5.0 - Music notation solution...
Sibelius is the world's best-selling music notation software for Mac. It is as intuitive to use as a pen, yet so powerful that it does most things in less than the blink of an eye. The demo includes... Read more
Typinator 5.9 - Speedy and reliable text...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more
MYStuff Pro 2.0.16 - Create inventories...
MYStuff Pro is the most flexible way to create detail-rich inventories for your home or small business. Add items to MYStuff by dragging and dropping existing information, uploading new images, or... Read more
TurboTax 2013.r17.002 - Manage your 2013...
TurboTax guides you through your tax return step by step, does all the calculations, and checks your return for errors and overlooked deductions. It lets you file your return electronically to get... Read more
TrailRunner 3.8.769 - Route planning for...
Note: While the software is classified as freeware, it is actually donationware. Please consider making a donation to help support development. TrailRunner is the perfect companion for runners,... Read more
Flavours 1.1.10 - Create and apply theme...
Flavours is a Mac application that allow users to create, apply and share beautifully designed themes. Classy Give your Mac a gorgeous new look by applying delicious themes! Easy Unleash your... Read more
Spotify 0.9.8.296. - Stream music, creat...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue... Read more
SlingPlayer Plugin 3.3.20.475 - Browser...
SlingPlayer is the screen interface software that works hand-in-hand with the hardware inside the Slingbox to make your TV viewing experience just like that at home. It features an array of... Read more
S.M.A.R.T. for USB and FireWire Drives 0...
S.M.A.R.T. for USB and FireWire Drives is a kernel driver for OS X external usb or firewire drives. It extends the standard driver behaviour by providing access to drive smart data. The interface to... Read more

Latest Forum Discussions

See All

148Apps Live on Twitch: Pivvot’s Looper...
On our latest Twitch stream, we’ll be playing a pair of minimalist arcade games, one that just got a big content update in Pivvot, and another that was inspired by it in 15 Coins. Whitaker Trebella, creator of Pivvot, will discuss the new modes... | Read more »
Word Cubes Review
Word Cubes Review By Jordan Minor on April 15th, 2014 Our Rating: :: SQUARESVILLEUniversal App - Designed for iPhone and iPad Word Cubes is fine, but it is barely any different from any other word game.   | Read more »
PAX East 2014 – Desert Fox: The Battle o...
PAX East 2014 – Desert Fox: The Battle of El Alamein is Coming to iOS Soon Posted by Rob Rich on April 15th, 2014 [ permalink ] Shenandoah Studio has become one of the go-to developers for war games on iOS, with | Read more »
Tank of Tanks Review
Tank of Tanks Review By Carter Dotson on April 15th, 2014 Our Rating: :: TANKS A LOT!iPad Only App - Designed for the iPad This multiplayer game played on a single iPad is simple, chaotic fun.   | Read more »
PAX East 2014 – Dungeon of the Endless J...
PAX East 2014 – Dungeon of the Endless Just Might Have a Shot at an iPad Release Posted by Rob Rich on April 15th, 2014 [ permalink ] I think it’s fair to say that | Read more »
SideSwype Review
SideSwype Review By Carter Dotson on April 15th, 2014 Our Rating: :: ON YOUR SIDEUniversal App - Designed for iPhone and iPad SideSwype is a puzzler that takes inspiration from Threes, but becomes its own incredibly fun game.   | Read more »
PAX East 2014 – Bigfoot Hunter Invites P...
PAX East 2014 – Bigfoot Hunter Invites Players on a Wild and Wooly Photo Safari Posted by Rob Rich on April 15th, 2014 [ permalink ] Yeti. Sasquatch. Wendigo. | Read more »
Dungeon Quest Review
Dungeon Quest Review By Cata Modorcea on April 15th, 2014 Our Rating: :: NO STORY, BUT GOOD FUNUniversal App - Designed for iPhone and iPad Dungeon Quest does a lot of things right, but ultimately forgets about one of the core... | Read more »
Tempo AI and Speek Join Forces to “Kill...
Tempo AI and Speek Join Forces to “Kill the Conference Call PIN” Posted by Rob Rich on April 15th, 2014 [ permalink ] Today Tempo AI, makers of Tempo Smart Calendar, and | Read more »
Fighting Fantasy: Starship Traveller Rev...
Fighting Fantasy: Starship Traveller Review By Jennifer Allen on April 15th, 2014 Our Rating: :: A SIGNIFICANT VOYAGEUniversal App - Designed for iPhone and iPad Continuing the release of Fighting Fantasy titles, Starship Traveller... | Read more »

Price Scanner via MacPrices.net

Save $50 on Mac mini Server
B&H Photo has the 2012 Mac mini Server on sale for $949 including free shipping plus NY sales tax only. Their price is $50 off MSRP. Read more
PhatWare’s “Ultimate Writing App For iOS” Ren...
PhatWare Corp. has announced it has renamed its new WritePro word processing app for iPhone and iPad: WritePad Pro. The decision to change the app’s name to leverages the strong brand awareness and... Read more
Full Resolution Photo Editor Tint Mint 1.0 Re...
California based independent developer, Jeffrey Sun, creator of the iOS app Modern Editor, has released Tint Mint, a new photography app for editing enthusiasts. The app costs a dollar, and it packs... Read more
16GB iPad mini (Apple refurbished) available...
The Apple Store has refurbished 1st generation 16GB iPad minis available for $249 including free shipping. Both black and white models are available. Read more
Save $120 on the 27-inch 3.2GHz Haswell iMac
B&H Photo has the 27″ 3.2GHz iMac on sale for $1679.99 including free shipping plus NY sales tax only. Their price is about $120 off MSRP. Read more
Using a Mac Doesn’t Eliminate The Heartbleed...
Low End Mac’s Dan Knight notes that any time you visit a website with an https: prefix or see that secure lock icon on your browser, some type of security software is busy trying to protect your data... Read more
AirPrint Basics Tutorial Posted
A new Apple Knowledge Base article helps get you started using AirPrint, the Apple protocol that enables instant printing from iPad, iPhone, iPod touch, and Mac without the need to install drivers or... Read more
Speed Tips For Running LibreOffice On The Mac
LibreOffice is my favorite of several free, open-source application suites, and the one I have configured on my Mac as my default app for Word documents that one frequently has to deal with. It also... Read more
Snag a 15-inch Retina MacBook Pro for $115 of...
B&H Photo has 2013 15″ Retina MacBook Pros on sale for up to $115 off MSRP for a limited time. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.3GHz Retina MacBook Pro: $2489.99... Read more
MacBook Airs on sale for $50 to $100 off MSRP
Several resellers are offering $50-$100 discounts on 11″ and 13″ MacBook Airs today, including Amazon, Best Buy, B&H, and others. See the breakdown of deals on our MacBook Air Price Tracker,... Read more

Jobs Board

*Apple* Retail - Manager - SoHo - Apple (Uni...
Job SummaryKeeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you're a master of them all. In the store's fast-paced, dynamic Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** Every day, business customers come to the Apple Store to discover what powerful, easy-to-use Apple products can do for them. As a Business Leader, Read more
Position Opening at *Apple* - Apple (United...
…challenges of developing individuals, building teams, and affecting growth across Apple Stores. You demonstrate successful leadership ability - focusing on excellence Read more
Position Opening at *Apple* - Apple (United...
…Summary** As a Specialist, you help create the energy and excitement around Apple products, providing the right solutions and getting products into customers' hands. You Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** The Apple Store is a retail environment like no other - uniquely focused on delivering amazing customer experiences. As an Expert, you introduce people Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.