Sign in Join
Wiki copytemporary

PowerPC64 Port: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The **PowerPC64 port** supports code generation for 64-bit [[PowerPC]] systems. Historically it started as entry for the [Linux On Power](http://www.linuxonpower.com/) contest.
22
33This page contains a rough outline of what is working and especially what not, some notes about the port and several other related information. More, detailed information (about calling conventions and so on) later.
44
55## Status
66
77- 2.1.x compiler support
88- Supports POWER4 and derivative (G5) for Linux at the moment.
99- Not all instructions of the instruction set are recognized by the compiler yet, including missing support for all AltiVec (VMX) instructions.
1010- The compiler can be built via the standard procedure, e.g. a `make build`
1111- Failed test suite program count is around 20. See the [test suite results](http://www.freepascal.org/cgi-bin/testsuite.cgi) for yourselves for the latest numbers. Note that the results for the PowerPC64 compiler are not automatically created every day, but manually every now and then.
1212- The IDE is okay now, except for the missing debugging support.
1313- Many packages and their examples which are 64 bit big-endian safe already do work (OpenGL, threading, GTK, GTK2, ...)
1414- Default debugger information type is DWARF. Stabs are not supported and generation has been disabled. Note that DWARF support is not complete, and hence compiling some code with debugging support fails (e.g. Lazarus)
1515
1616## Installing and Building
1717
1818Although the current compiler is still beta, and still has a few bugs, it can already be used by interested people. To get a recent working compiler, it is easiest to start on a G5 using a snapshot, and then update it to the latest version using the development sources. Simply download one for the current development version (2.1.1 at the time of writing this) from the [FTP](ftp://ftp.freepascal.org/pub/fpc/snapshot/v21/). The snapshot for the compiler is located in the `powerpc64-linux` subdirectory.
1919
2020### Installing the snapshot
2121
2222Installing the snapshot is easy: Extract it somewhere (e.g. in `/usr`, the FreePascal default installation base directory), and if necessary set your `PATH` environment variable to point to the `bin` directory. Additionally, you need to add a symlink called `ppcppc64` pointing to the `ppcppc64` binary in the `lib` directory of the extracted snapshot directory. For example type
2323
2424` ln -s /usr/lib/fpc/2.1.1/ppcppc64 ppcppc64`
2525
2626if you are in the `/usr/bin` directory.
2727
2828To be able to use the latest features, it is recommened to update to the most current version using the source code, see below for more information on how to do this.
2929
3030### Updating to the latest version
3131
3232Another way, in particular if you do not want to install the compiler before updating to the latest version, get the `ppcppc64` binary from the snapshot file and use it to cycle the sources. Then do the following: Change into the directory where you have stored the current SVN sources from the current development branch, and enter the following command:
3333
3434` make build PP=<path_to_ppcppc64>/ppcppc64`
3535
3636Take care that the given path must be an absolute path, otherwise the compilation process immediately aborts. For the people which want a much faster binary, add `OPT=-O2` to the command line. After a while (and lots of compilation) it should leave you with an empty commandline, and you are ready for installing the compiler. Assuming that you compiled as non-root, do the following:
3737
3838` su   (You will be asked for your root password)`\
3939` make install`\
4040` exit (To give up root access again)`
4141
4242It is possible that compiling the compiler with the `-O2` or other additional options you specified in the `OPT` parameter does not successfully cycle. In that case, first compile and install a current compiler without any additional options, and retry with that one.
4343
4444### Compiling a program
4545
4646Compiling a Pascal program should be as easy as typing
4747
4848` fpc <pascal_sourcefile>`
4949
5050after installation completed successfully. If the `fpc` complains about not finding the ppcppc binary, you have to set a symbolic link to the actual binary in the installation directory as described above.
5151
5252For further information please also read the offical documentation ([PDF](ftp://ftp.freepascal.org/pub/fpc/docs-pdf)) ([HTML](http://community.freepascal.org:10000/docs-html/fpctoc.html)).
5353
5454## Register usage
5555
5656The table below shows how the compiler uses the registers for code generation and their meaning. The „volatile“ column indicates whether this register (or register set) is volatile or not, i.e. if a register is marked as volatile, the value is not preserved across function calls, otherwise they are automatically saved and restored by the function prolog and epilog. A value of „N/A“ means that the register is handled in a special way, and in general should not be modified by the programmer. This table basically contains a summary of the information in chapter 3.2.1 of the ABI specification.
5757
58-<TABLE BORDER=0 cellpadding=1>
59-
60-<TR>
61-
62-<TH>
63-
6458Register
6559
66-</TH>
67-
68-<TH>
69-
7060Volatile
7161
72-</TH>
73-
74-<TH>
75-
7662Description
7763
78-</TH>
79-
80-</TR>
81-
82-<TR>
83-
84-<TD>
85-
8664r0
8765
88-</TD>
89-
90-<TD>
91-
9266Y
9367
94-</TD>
95-
96-<TD>
97-
9868Volatile register used in function prologs and constant loading (i.e. scratch register)
9969
100-</TD>
101-
102-</TR>
103-
104-<TR>
105-
106-<TD>
107-
10870r1
10971
110-</TD>
111-
112-<TD>
113-
11472N/A
11573
116-</TD>
117-
118-<TD>
119-
12074Stack frame pointer. The stack must always be quadword (16 byte) aligned
12175
122-</TD>
123-
124-</TR>
125-
126-<TR>
127-
128-<TD>
129-
13076r2
13177
132-</TD>
133-
134-<TD>
135-
13678N/A
13779
138-</TD>
139-
140-<TD>
141-
14280TOC pointer. Used for the GOT/TOC when enabling generation of PIC code (with `-Cg`), otherwise unused by Pascal programs.
14381
144-</TD>
145-
146-</TR>
147-
148-<TR>
149-
150-<TD>
151-
15282r3
15383
154-</TD>
155-
156-<TD>
157-
15884Y
15985
160-</TD>
161-
162-<TD>
163-
16486Parameter register and return value register
16587
166-</TD>
167-
168-</TR>
169-
170-<TR>
171-
172-<TD>
173-
17488r4-r10
17589
176-</TD>
177-
178-<TD>
179-
18090Y
18191
182-</TD>
183-
184-<TD>
185-
18692Registers used for function parameters
18793
188-</TD>
189-
190-</TR>
191-
192-<TR>
193-
194-<TD>
195-
19694r11
19795
198-</TD>
199-
200-<TD>
201-
20296Y
20397
204-</TD>
205-
206-<TD>
207-
20898Register used in calls by pointer, otherwise used as scratch register
20999
210-</TD>
211-
212-</TR>
213-
214-<TR>
215-
216-<TD>
217-
218100r12
219101
220-</TD>
221-
222-<TD>
223-
224102Y
225103
226-</TD>
227-
228-<TD>
229-
230104Register used for glink code and scratch register
231105
232-</TD>
233-
234-</TR>
235-
236-<TR>
237-
238-<TD>
239-
240106r13
241107
242-</TD>
243-
244-<TD>
245-
246108N/A
247109
248-</TD>
249-
250-<TD>
251-
252110Reserved for use as system thread ID. Never touched by the FreePascal compiler
253111
254-</TD>
255-
256-</TR>
257-
258-<TR>
259-
260-<TD>
261-
262112r14-r31
263113
264-</TD>
265-
266-<TD>
267-
268114N
269115
270-</TD>
271-
272-<TD>
273-
274116Registers used for local variables
275117
276-</TD>
277-
278-</TR>
279-
280-<TR>
281-
282-<TD>
283-
284-\
285-
286-</TD>
287-
288-<TD>
289-
290-\
291-
292-</TD>
293-
294-<TD>
295-
296-\
297-
298-</TD>
299-
300-</TR>
301-
302-<TR>
303-
304-<TD>
305-
306118f0
307119
308-</TD>
309-
310-<TD>
311-
312120Y
313121
314-</TD>
315-
316-<TD>
317-
318122Scratch register
319123
320-</TD>
321-
322-</TR>
323-
324-<TR>
325-
326-<TD>
327-
328124f1
329125
330-</TD>
331-
332-<TD>
333-
334126Y
335127
336-</TD>
337-
338-<TD>
339-
340128Floating point parameter and return value register
341129
342-</TD>
343-
344-</TR>
345-
346-<TR>
347-
348-<TD>
349-
350130f2-f13
351131
352-</TD>
353-
354-<TD>
355-
356132Y
357133
358-</TD>
359-
360-<TD>
361-
362134Floating point parameter registers
363135
364-</TD>
365-
366-</TR>
367-
368-<TR>
369-
370-<TD>
371-
372136f14-f31
373137
374-</TD>
375-
376-<TD>
377-
378138N
379139
380-</TD>
381-
382-<TD>
383-
384140Registers used for local variables
385141
386-</TD>
387-
388-</TR>
389-
390-<TR>
391-
392-<TD>
393-
394-\
395-
396-</TD>
397-
398-<TD>
399-
400-\
401-
402-</TD>
403-
404-<TD>
405-
406-\
407-
408-</TD>
409-
410-</TR>
411-
412-<TR>
413-
414-<TD>
415-
416142LR
417143
418-</TD>
419-
420-<TD>
421-
422144Y
423145
424-</TD>
425-
426-<TD>
427-
428146Link register
429147
430-</TD>
431-
432-</TR>
433-
434-<TR>
435-
436-<TD>
437-
438148CTR
439149
440-</TD>
441-
442-<TD>
443-
444150Y
445151
446-</TD>
447-
448-<TD>
449-
450152Loop counter register. FreePascal uses it for special code patterns, but not in common code
451153
452-</TD>
453-
454-</TR>
455-
456-<TR>
457-
458-<TD>
459-
460154XER
461155
462-</TD>
463-
464-<TD>
465-
466156Y
467157
468-</TD>
469-
470-<TD>
471-
472158Fixed point exception register
473159
474-</TD>
475-
476-</TR>
477-
478-<TR>
479-
480-<TD>
481-
482160FPSCR
483161
484-</TD>
485-
486-<TD>
487-
488162Y
489163
490-</TD>
491-
492-<TD>
493-
494164Floating point status and control register
495165
496-</TD>
497-
498-</TR>
499-
500-<TR>
501-
502-<TD>
503-
504-\
505-
506-</TD>
507-
508-<TD>
509-
510-\
511-
512-</TD>
513-
514-<TD>
515-
516-\
517-
518-</TD>
519-
520-</TR>
521-
522-<TR>
523-
524-<TD>
525-
526166CR0-CR3
527167
528-</TD>
529-
530-<TD>
531-
532168Y
533169
534-</TD>
535-
536-<TD>
537-
538170Condition code register fields
539171
540-</TD>
541-
542-</TR>
543-
544-<TR>
545-
546-<TD>
547-
548172CR2-CR4
549173
550-</TD>
551-
552-<TD>
553-
554174N
555175
556-</TD>
557-
558-<TD>
559-
560176Nonvolatile condition code register fields
561177
562-</TD>
563-
564-</TR>
565-
566-<TR>
567-
568-<TD>
569-
570178CR5-CR7
571179
572-</TD>
573-
574-<TD>
575-
576180Y
577181
578-</TD>
579-
580-<TD>
581-
582182Volatile condition code register fields
583-
584-</TD>
585-
586-</TR>
587-
588-</TABLE>
589183
590184### Notes
591185
592186- The condition register is never saved to the stack in a function prolog, because FreePascal and its RTL only uses the volatile parts of this register.
593187- There is no support for VMX (AltiVec) extensions in FreePascal, so no register usage is given.
594188- r0, r2, r11 and r12 may be destroyed during a function call, i.e. you cannot pass a value to the callee using these registers.
595189- do not change r2 at any time, it is used to access the GOT/TOC and is managed automatically by the program loader and linker.
596190- The stack is always aligned to 16 bytes as per ABI convention.
597191
598192## 32 bit compatibility
599193
600194During porting several compatibility problems of 32 bit PowerPC programs with the 32 bit emulation layer of Linux were found. They are:
601195
602196- the default cache line size is 128 byte instead of 32 byte. This affects some RTL routines which assume 32 byte cache line length, i.e. the assembly fillchar() and move() methods. For this reason, most (except very trivial) FPC compiled programs immediately segfault on PowerPC64. Fixing this involves selecting cache line aware fillchar() and move() methods at program startup.
603197
604198There may (and I think there are) more issues. These are only those which I could reproducably pinpoint at this time.
605199
606200## Optimizing for PowerPC64
607201
608202There are a few rules of thumb to make PowerPC64 programs perform well. This section will present at least some of them. Of course, these are rather low-level, often a better algorithm is more advantageous than to do this sort of bean-counting.
609203
610204- Use at least the `-O2` (level 2 optimizations) compiler switch which enables register variables to minimize stores and loads to memory. As a side effect this also results in smaller executables. PowerPC64 is a RISC platform, and as such, does not have the complex instructions which can take memory operands. For example, using `-O2` more than halves the time required for cycling the compiler.
611205- Align your data if possible. Unaligned access (especially for 64 bit loads and stores on addresses not divisble by four) performs poorly. This basically means, do not use the `packed` modifier for records unless required. See also below for more information about misalignment costs.
612206- Use the appropriate data type, preferring 32 bit integers over 64 bit integers due to reduced memory bandwidth requirements. Using a data type less than 32 bits does not give any speed advantage (except maybe if you are bandwidth constrained) because there are only 32 and 64 bit variants of the "slow" integer arithmetic instructions (div, mul). (*)
613207- The compiler automatically replaces integer divisions and integer modulo operation with a constant by faster multiplication. Additionally it does proper replacement by shifts if possible, so you do not need to care too much about it. This can be activated using the `-O2` compiler switch.
614208- If you enable `-Os` (Optimize for size) the compiler does not inline function prolog and epilog, and uses a stub for calling methods by pointer. This decreases the size of these parts of the code at the expense of additional calls, maybe helping in situations where there are problems with instruction fetch bandwidth.
615209- Enable PIC code generation (with `-Cg`), resulting in smaller and faster code. The reason is that on the 64 bit PowerPC platform address loading takes five instructions, while when enabling this switch this instruction sequences are replaced by a single memory load.
616210
617211(*) Although these optimizations are not implemented yet for the PowerPC64 platform, they surely will be =). Maybe these optimizations will require setting some optimization switch though, or will be disabled for some settings.
618212
619213### Alignment rules
620214
621215Some rules and information on alignment penalties on the PPC970 processor, taken from the manuals. These are mainly of interest for assembly programmers though:
622216
623217- A memory reference is misaligned if its address is not divisible by the size of the access.
624218- The hardware handles all misaligned memory references, i.e. loads and stores. However, it does so with different penalties depending on the memory boundary crossed and the size of the memory access. The interesting boundary granularities are 32, 64 and 4096 bytes.
625219- In general the processor does a reasonable job of handling unaligned loads/stores which do not cross a lower than 32 byte boundary.
626220- Doubleword (8 byte) should be at least aligned on a word (4 byte) boundary. If you are sure to do a misaligned doubleword load/store, use two word sized operations.
627221- When using the loads and stores to copy data, and both source and destination are misaligned, align the source. Loads get penalties on misaligned 32/64 and 4k boundaries, while stores only on 4k boundaries. One can avoid both penalties in this case by aligning on one address, and constructing an aligned access to the other address by clever shifts.
628222- VMX memory loads/stores require 16 byte alignment. If you load/store from an unaligned address, the processor silently aligns the address, i.e. it works as if the lower 4 bits of the effective address are zero.
629223
630224## Debugging and Profiling
631225
632226This section contains some information on tools to debug and test applications for the PowerPC64 platform generated by the FreePascal compiler.
633227
634228In general, the FreePascal standard debugging facilities, i.e. enabling debug code generation (`-g` switch) in conjunction with line information (`-gl`) and heap tracing (`-gh`) works. See the FreePascal manual for more information about these things.
635229
636230Currently FreePascal the integrated debugger in the IDE does not work. You either have to resort to the [GNU GDB](http://www.gnu.org/software/gdb) commandline debugger and any of the backends (for example [DDD](http://www.gnu.org/software/ddd)). Make sure that debugging for the 64 bit PowerPC platform has been compiled in - some Linux distributions only provide a GDB capable of debugging 32 bit applications.
637231
638232Using the latest FreePascal compiler version, basic profiling can be done with [GNU gprof](http://www.gnu.org/software/binutils/). See the manual on how to enable generation of required code (Note: try the `-pg` compiler switch). Since gprof seems to be pretty basic and the technique used rather intrusive, requiring some additional code in the executable, make sure to also try the profilers mentioned in the subsection below.
639233
640234### Additional resources
641235
642236This subsection contains some information for additional debugging or profiling resources on the PowerPC64/Linux platform:
643237
644238- The current source tree of [Valgrind](http://www.valgrind.org) (3.2.0) supports the PowerPC64 processor on the Linux platform. Valgrind provides a set of tools for memory debugging, memory leak detection and profiling.\
645239 Since PowerPC64 support is only available in the source tree, you have to get the latest version from the SVN tree and compile it yourselves. The Valgrind homepage contains instructions for all these steps (see the [Valgrind Code Repository](http://valgrind.org/downloads/repository.html)).
646240- Another profiling tool available for profiling PowerPC64 applications is [OProfile](http://oprofile.sourceforge.net). Depending on your Linux distribution, it may or may not already be installed. Next to the introduction on the OProfile homepage, IBM provides some examples [here](http://www-128.ibm.com/developerworks/linux/library/l-oprof.html) and [here](http://www-128.ibm.com/developerworks/library/l-pow-oprofile/).
647241
648242## Troubleshooting
649243
650244This section contains some hints for solving mysterious behaviour of compiled code
651245
652246**Problem:** When linking to some C library, the code sometimes crashes.
653247
654248**Solution:** Some C libraries (for example OpenGL) sometimes give an "Invalid Floating Point Operation" (Underflow, Overflow) or similar when used in Pascal. This is due to bugs in the C library which are not detected there, because C by default disables these exceptions. Try adding the following assembly code to your main program: `asm mtfsfi 6, 1 end;`. This disables these exceptions globally (also in the Pascal program). To fix this, tell the maintainers of the library about this bug.
655249
656250**Problem:** The compiler gives strange errors, especially after updating to a new version
657251
658252**Solution:** Make sure that there are not any stale (old) compiled units lying around, and make sure that the compiler actually uses the recent ones (use the `-vt` switch to display tried files)
659253
660254**Problem:** Debugging information is displayed incorrectly or not at all.
661255
662256**Solution:** Enable debugging support using the `-g` switch to enable generation of debugging information.
663257
664258## More information
665259
666260This section contains links and notes to further information about the PowerPC64 architecture. This includes links to the ABI specification, general processor descriptions, instruction set documentation and general compiler notes.
667261
668262- Ian Lance Taylor, *64-bit PowerPC ELF Application Binary Interface Supplement 1.7*, Zenbu Labs IBM, 2004 ([HTML](http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html)) ([PDF](http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi-1.7.pdf)) - This was the original ABI specification the PowerPC64 compiler was developed with.
669263
670264- Ian Lance Taylor, *64-bit PowerPC ELF Application Binary Interface Supplement 1.9*, Zenbu Labs IBM, 2004 ([HTML](http://www.linuxbase.org/spec/ELF/ppc64/spec/)) (No PDF) - An update to the previous ABI specification. However, it only seems to add some clarifications compared to 1.7.
671265
672266- [PowerPC 970 and 970FX microprocessor documentation](http://www-306.ibm.com/chips/techlib/techlib.nsf/products/PowerPC_970_and_970FX_Microprocessors) - This website contains lots of information about the 970 and 970FX microprocessors. The (imo) most interesting references from this page are summarized below.
673267
674268- [IBM PowerPC 970FX RISC Microprocessor User's Manual](http://www-306.ibm.com/chips/techlib/techlib.nsf/techdocs/AE818B5D1DBB02EC87256DDE00007821) - This website contains a manual which contains the technical description of the 970FX (G5) processor with (mainly) interesting information about the storage and memory subsystem.
675269
676270- [Developing Embedded Software For The IBM PowerPC™ 970FX Processor](http://www-306.ibm.com/chips/techlib/techlib.nsf/techdocs/AB70A3470F9CC0E287256ECC006D6A54) - Contains an application note which describes the differences between PowerPC32 and PowerPC64, and the issues which need to be handled when porting from the PowerPC32 to the PowerPC64 architecture
677271
678272- [The Programming Environments Manual for 64-bit Microprocessors](http://www-306.ibm.com/chips/techlib/techlib.nsf/techdocs/F7E732FF811F783187256FDD004D3797) - This website contains a manual which summarizes the features and pecularities of the 64 bit processor from a programmer's point of view, including overview of the processor, instruction set and so on.
679273
680274- [PowerPC Architecture Book](http://www-128.ibm.com/developerworks/eserver/articles/archguide.html) - This website contains links to the PowerPC architecture manuals, i.e. a complete description of the instruction sets, containing both privileged and unprivileged instructions. The next links are direct references to the three books: Book I ([PDF](ftp://www6.software.ibm.com/software/developer/library/es-archpub1.zip)), Book I I ([PDF](ftp://www6.software.ibm.com/software/developer/library/es-archpub2.zip)), Book III ([PDF](ftp://www6.software.ibm.com/software/developer/library/es-archpub3.zip))
681275
682276- [PowerPC Compiler Writer's Guide](http://www-3.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF7785256996007558C6) - This website contains useful information for writing compilers for the PowerPC architecture, i.e. optimized code examples for common patterns. This manual has been written for the PowerPC32 architecture, but the techniques presented there can be easily ported over to the PowerPC64 platform.
683277
684278- [GNU C/C++ toolchain for Linux on POWER](http://www-128.ibm.com/developerworks/library/l-pow-gnutool/) page - Introductory text about the GNU C/C++ toolchain. Although this page seems to be totally unrelated to FreePascal, it also contains a short overview of the GOT and TOC sections.
685279
686280## Contact
687281
688282Message to the fpc-devel mailing list, or look for "tom_at_work" in the IRC channel (or of course discuss it here).
689283
690284---
691285*From the Free Pascal wiki, page "PowerPC64 Port", as it stood on 2016-11-27 after a change by Jwdietrich, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
692286Category: PowerPC

Back to history