Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-02

[OT] inline procedures in TP 7.0

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anton Shepelev

Hello, all

I don't know a better place to ask this, and beg your pardon for an off-topic post.

The Language Guide for Turbo Pascal 7.0 has this to say about `inline' statements and proceudres:

When a normal procedure or function is called, the com- piler generates code that pushes the procedure's or function's parameters onto the stack and then generates a CALL instruction to call the procedure or function. When you call an inline procedure or function, the com- piler generates code from the inline directive instead of the CALL. Therefore, an inline procedure or function is expanded every time you refer to it, just like a macro in assembly language.
[...]
With inline directives, you can write procedures and functions that expand into a given sequence of machine code instructions whenever they are called. These are comparable to macros in assembly language.
[...]
Registers BP, SP, SS, and DS must be preserved by inline statements: all other registers can be modified.

But which registers shall be preserved by inline proceudres and functions? In this example from the manual, the SP register is not preserved within the statement:

    function LongMul(X, Y: Integer): Longint;
    inline (
    $5A/        {POP  AX}
    $58/        {POP  DX}
    $F7/$EA);   {IMUL DX}

Does it mean it shall reset SP to a position before the parameters? What about the other registers -- does Turbo Pascal take care of them, perhaps enveloping each expansion of an inline procedure into a POPs and PUSHes?

Please, do not forward replies to my e-mail.

Reply by email

Florian Klämpfl
to Anton Shepelev
Anton Shepelev wrote
But which registers shall be preserved by inline proceudres and functions?

IIRC (it is 25 years ago :)): sp and bp

In this example from the manual, the SP register is not preserved within the statement:

It is reset to it's value before the parameters were pushed.

Show quoted text 9 lines
function LongMul(X, Y: Integer): Longint; inline ( $5A/ {POP AX} $58/ {POP DX} $F7/$EA); {IMUL DX} Does it mean it shall reset SP to a position before the parameters?

Yes. TP calling conventions means the callee (in this case the inline code) clears the stack (remove parameters).

What about the other registers -- does Turbo Pascal take care of them, perhaps enveloping each expansion of an inline procedure into a POPs and PUSHes?

No.

Reply by email

Anton Shepelev
to Florian Klämpfl

Florian Klampfl to Anton Shepelev:

Show quoted text 5 lines
> What about the other registers -- does Turbo Pascal take > care of them, perhaps enveloping each expansion of an > inline procedure into a POPs and PUSHes? No.

Thank you for the reply, Florian. Does that mean that I must take utmost care to preserve every single register when using inline procedures and functions?

Please, do not forward replies to the list to my e-mail.

Reply by email

Florian Klämpfl
to Anton Shepelev
Anton Shepelev wrote 12 lines
Florian Klampfl to Anton Shepelev: >> What about the other registers -- does Turbo Pascal take >> care of them, perhaps enveloping each expansion of an >> inline procedure into a POPs and PUSHes? > > No. Thank you for the reply, Florian. Does that mean that I must take utmost care to preserve every single register when using inline procedures and functions?

No. The other way round, except sp and bp, you do not have to take care of any register.

Reply by email