[comp.sys.mac.programmer] Executing a code segement from LS Pascal? LONG

han@Apple.COM (Byron Han, Architect) (10/05/88)

Two ways for doing this.
1.  Use INLINE code
2.  Use an external Assembler routine

Solution 1
----------
Declare a procedure as such:
    PROCEDURE CallTheProc(theHandle : Handle); INLINE $205F, $2050, $4E90;

In your application/program, use this:
    theHandle := GetResource(yourResType, yourResID);
    IF theHandle <> NIl THEN
        BEGIN
        HLock(theHandle);
        CallTheProc(theHandle);
        HUnlock(theHandle);
        END;

The code resource entry point is a standard pascal entry point with no
parameters in this case.

If the code resource has parameters or is a function like those below:
    PROCEDURE MyProcEntry(var1, var2, var3 ...);
    FUNCTION  MyFuncEntry(var1, var2, var3 ...) : foobar;

you would need to declare procedures (INLINE) 
    PROCEDURE CallMyProcEntry(var1, var2, var3 ... , theHandle); 
                             INLINE $205F,$2050, $4E90;
    FUNCTION  CallMyFuncEntry(var1, var2, var3 ... , theHandle) : foobar; 
                             INLINE $205F,$2050,$4E90;

Calling conventions:
    CallMyProcEntry(var1, var2, var3, ... theHandle);
    moof := CallMyFuncEntry(var1, var2, var3, ... theHandle);

All the inline stuff does is
     MOVE.L 4(A7),A0
     MOVE.L (A0),A0
     JSR    (A0)

Note that this method of using INLINE presumes that the code resource 
preserves registers!.

Solution 2
----------
Perhaps more elegant, more correct, but more complex involves writing 
assembler stubs and linking them into your application.
;
;       PROCEDURE DoIt(theHandle:Handle); EXTERNAL
;       extern pascal void DoIt(theHandle)
;       Handle theHandle;
;
DoIt    PROC    EXPORT
        MOVEM.L D0-D7/A0-A6,-(SP)     ; preserve registers
        MOVE.L  64(SP),A0             ; get the handle
        MOVE.L  (A0),A1               ; leave handle in A0

;       INIT resources think that they are getting a handle to their
;       potentially unlocked code resource in A0.
;       An INIT without the locked bit set should have as its first 
;       instruction _HLock to prevent memory dislocation.
;       From Pascal, Use 
;              theHandle:=RecoverHandle(@mainentrypoint);
;              HLock(theHandle);

        JSR     (A1)                  ; call it
        MOVEM.L (SP)+,D0-D7/A0-A6     ; restore registers
        RTS                           ; back to normal space

Note that you will need to do some sticky stack fiddling if you want to
pass/receive parameters/function results.

Hope this helps.

This is not an official Apple policy statement.  Use code examples 
*at your own risk*.  I have no connection with Symantec Corporation
except as a customer.  I have no connection with Consulair.
Either use the Macintosh Development System (MDS) now called the Consulair
Development System.  Or MPW - Macintosh Programmers Workshop - the current
Apple state of the art development platform.
------------------------------------------------------------------------------
Byron Han, Communications Architect      TANSTAAFL - You Get What You Pay For.
Apple Computer, Inc.                     -------------------------------------
20525 Mariani Ave, MS27Y                 Internet: han@apple.COM
Cupertino, CA 95014                      UUCP:{sun,voder,nsc,decwrl}!apple!han
--------------------------------------   GENIE: BYRONHAN
ATTnet: 408-973-6450   Applelink: HAN1   CompuServe: 72167,1664
------------------------------------------------------------------------------