Difference between revisions of "Porting from Mac Pascal"

From Lazarus wiki
Jump to navigationJump to search
 
(outdated)
Line 79: Line 79:
 
becomes:
 
becomes:
 
   theRoutine:= NewAEEventHandlerUPP(HandleODOC);  // FPC Apple Event Handler
 
   theRoutine:= NewAEEventHandlerUPP(HandleODOC);  // FPC Apple Event Handler
 
== Using C in your FPC-XCode project ==
 
 
* FPC must find the object files. Add -Fo<x> to the FPC parmeter line, where <x> is a path to a directory in which to search for object files. In XCode 1.1 gcc puts the object files in the directory: build/$(PROJECT_NAME).build/$(TARGET_NAME).build/Objects-normal/ppc
 
 
* Add {$LINK <objectfile> } e. g. {$LINK test.o } somewhere in the pascal code, to tell FPC, to tell the linker to actually link in the object file.
 

Revision as of 18:33, 14 November 2007

Tips and tricks for porting from Mac Pascals to FPC

Although FPC today supports many mac pascal constructs, there are a lot of issues one has to be aware of. Tips to make the code work with GPC are also welcome.

Uses clause

Depending on the compiler you're moving from, you may have a simple or complex uses clause to include all of the toolbox calls. Either way, it can all be replaced with:

 uses
   FPCMacOSAll;

Bit Order

(this paragraph is mainly relevant for users of FPC 2.0.4; as of FPC 2.1.2, the BTst, BSet and BClr routines are available in MacPas mode in FPC)

The Macintosh Toolbox functions BitTst, BitSet and BitClr addresses bits form the highest bit in the byte pointed to by the parameter bytePtr. This is in opposition to what one traditionally count as first bit, namely the least significant bit. The reason of this reversed addressing was that these functions could address unlimited bitvectors on the 68K.

This will cause significant problems if you have used longints to store bit values in CodeWarrior, MPW or Think Pascal variants. Because the order is backwards, you will have to code against the problem going forth. The simplest solution is to wrap BitTst, BitSet and BitClr (not having BTst, BSet and BClr in FPC helps here) and flip the order in that function, ala:

 function BTst(theLong: longint; theBit: integer): boolean;
 begin
   BTst:= BitTst(theLong, (31 - theBit));
 end;  { BTst }

Subtracting the bit from 31 inverts the order automatically for you (0 becomes 31, 31 becomes 0, etc.)

Ordinal sizes

In Traditional Mac Pascal, the types Byte and Char occupies 2 bytes, except in packed records and in arrays. If you need data to be sharable (e g through files), you need to be aware of this. One remedy is to make such records packed. If possible use SignedByte (which actually occupy only one byte) or Integer (or better SInt16 et. al.).

Set sizes (Style in particular)

Sets (eg: mySet = (opt1, opt2, opt3);) may be different sizes if you stored them to disk using CodeWarrior. This is evident with the MacOS type Style:

sizeof(Style) = 1 in CodeWarrior sizeof(Style) = 4 in FPC

If you want to convert on the fly, in your structures, replace Style with Byte, then do a conversion in code.

 function ByteToStyle(byteStyle: byte): Style;
 
 var
   returnValue: Style;
   count: StyleItem;
 
 begin
   returnValue:= [];
   for count:= bold to extend do
     if ((byteStyle shr Ord(count)) and 1) <> 0
       then returnValue:= returnValue + [count];
   ByteToStyle:= returnValue;
 end;  { ByteToStyle }

FPC 2.2.2 and later will support packing sets to smaller sizes (this support is already available in FPC 2.3.1, if you use the {$packset 1} directive).

Missing CW Functions

CodeWarrior provided built in functions for stripping the Hi and Lo words from a longint. Although most uses of this have been deprecated over the years (like menu commands,) you may still need the functions for old code. Here are substitutions:

 function HiWrd(aLong: cardinal): word;
 
 begin
   HiWrd:= hi(aLong);
 end;  { HiWrd }
 function LoWrd(aLong: cardinal): word;
 
 begin
   LoWrd:= lo(aLong);
 end;  { LoWrd }

UPP differences

In Codewarrior Pascal, one declared a UPP generically with an "@functionName" parameter. With Carbon, Apple introduced specific UPP creator functions, and with the change to FPC, you simply pass the functionName without the "@" symbol.

Thus,

 theRoutine:= NewAEEventHandlerUPP(@HandleODOC); // CW Apple Event Handler

becomes:

 theRoutine:= NewAEEventHandlerUPP(HandleODOC);  // FPC Apple Event Handler