Recent

Author Topic: call aliased routines in asm-block  (Read 3159 times)

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
call aliased routines in asm-block
« on: January 24, 2019, 05:34:03 am »
I can't call any aliased routine from my asm-blocks. Even the example program aliases does not compile. (FPC version 3.0.4+dfsg-11)

I keep getting error messages such as
Code: [Select]
aliases.pas(9,8) Error: Unknown identifier "DOIT"
Yes, I have seen the deprecated-remark, but replacing “alias:” by “public name” does not make any difference.

I've tried both {$asmMode}, without luck. Any suggestions?
« Last Edit: January 24, 2019, 09:42:28 am by marcov »
Yours Sincerely
Kai Burghardt

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: call aliased routines in asm-block
« Reply #1 on: January 24, 2019, 06:19:31 am »
try adding:
Code: Pascal  [Select]
  1. procedure DOIT; external name 'DOIT';

Thaddy

  • Hero Member
  • *****
  • Posts: 9425
Re: call aliased routines in asm-block
« Reply #2 on: January 24, 2019, 11:21:48 am »
try adding:
Code: Pascal  [Select]
  1. procedure DOIT; external name 'DOIT';
Yes, but external routines are case sensitive and usually lower case.....
also related to equus asinus.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7850
Re: call aliased routines in asm-block
« Reply #3 on: January 24, 2019, 11:31:41 am »
External links a symbol to a routine. public is the declaration of such symbol.

See e.g. rtl/linux/i386/syscall*;   syscall.inc are the declarations of functions together with their implementation and they use:

Code: Pascal  [Select]
  1. function FpSysCall(sysnr,param1,param2,param3:TSysParam):TSysResult; assembler; register; [public,alias:'FPC_SYSCALL3'];

This is because these functions are used in system, but not exported from system.

In other units they are used (e.g. via unit syscall), and syscallh.inc declares how they are imported: 

Code: Pascal  [Select]
  1. function Do_SysCall(sysnr,param1,param2,param3:TSysParam):TSysResult; register; external name 'FPC_SYSCALL3';

(Note that do_syscall/fpsyscall are all heavily overloaded, both the original declarations in system, as their usage elsewhere. However the assembler labels must be distinct, so the number of parameters (3 in this case) is suffixed to the asm symbol)

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: call aliased routines in asm-block
« Reply #4 on: January 25, 2019, 02:50:11 pm »
try adding:
Nope, it is not external which is missing. Anyway, I wanna use a routine that's been defined in another (Pascal) unit. I really don't wanna replicate signatures from there.

Does it (the example from the reference) work anywhere?
Yours Sincerely
Kai Burghardt

PascalDragon

  • Hero Member
  • *****
  • Posts: 895
  • Compiler Developer
Re: call aliased routines in asm-block
« Reply #5 on: January 25, 2019, 04:00:20 pm »
The hint provided by engkin is the correct one. The assembler reader does not check aliases and thus it needs to be at least a procedure declaration with no parameters and the name you want to use to call it from the assembly:
Code: Pascal  [Select]
  1. Program Aliases;  
  2.  
  3. Procedure Printit;alias : ’DOIT’;  
  4. begin  
  5.   WriteLn (In Printit (alias : "DOIT"));  
  6. end;
  7.  
  8. procedure Foo; external name 'DOIT';
  9.  
  10. begin  
  11.   asm  
  12.   call Foo  
  13.   end;  
  14. end.
  15.  
Though in general I wouldn't suggest to use this scheme. Just put the procedure into the interface section and be done with it.

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: call aliased routines in asm-block
« Reply #6 on: January 25, 2019, 06:32:45 pm »
The hint provided by engkin is the correct one. […]
Whoops. Yeah, OK it works here, too. I must have copied it wrong. I don't know, I must have attempted to insert
Code: Pascal  [Select]
  1. procedure printit; external name 'doit';
which of course fails. I don't wanna register another mangled name for the same procedure, but just alter the assembler label and use that alteration.

[…] Just put the procedure into the interface section and be done with it.
Huh, well. How? When I try:
Code: Pascal  [Select]
  1. program aliases(input, output, stderr);
  2. uses
  3.         myUnit;
  4. begin
  5.         asm
  6.                 call print_it // Unknown identifier "PRINT_IT"
  7.         end;
  8. end.
Code: Pascal  [Select]
  1. unit myUnit;
  2. interface
  3. procedure printIt;
  4.  
  5. implementation
  6. procedure printIt; public name 'print_it';
  7. begin
  8.         writeLn('in printIt');
  9. end;
  10. end.
Only when I insert
Code: Pascal  [Select]
  1. procedure print_it; external name 'print_it';
into program aliases, the program compiles.

This really doesn't sound right. I shouldn't have to clone the interface of another Pascal unit, which is already included via the uses-clause.
Yours Sincerely
Kai Burghardt

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: call aliased routines in asm-block
« Reply #7 on: January 25, 2019, 07:08:12 pm »
[…] Just put the procedure into the interface section and be done with it.
Huh, well. How?
He meant call printit directly in your asm block instead of creating a new name. You seem to have a reason preventing you from doing that! What are you trying to solve?

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: call aliased routines in asm-block
« Reply #8 on: January 25, 2019, 07:33:09 pm »
[…] He meant call printit directly in your asm block instead of creating a new name. You seem to have a reason preventing you from doing that! What are you trying to solve?
Within a pure assembler-function I want to call a routine whose implementation resides in another Pascal unit. I want to use a readable identifier while doing so, so I want to define an alias there. Basically what my previous post already shows, except the program is yet another unit.
Yours Sincerely
Kai Burghardt

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: call aliased routines in asm-block
« Reply #9 on: January 26, 2019, 05:55:29 pm »
OK, fixed in documentation. I missed some key aspect. The function I wanna call is overloaded
Code: Pascal  [Select]
  1. program aliases(input, output, stderr);
  2.  
  3. function identity(const r: extended): extended;
  4. begin
  5.         identity := r;
  6. end;
  7.  
  8. function identity(const x: int64): int64; register;
  9. begin
  10.         identity := x;
  11. end;
  12.  
  13. var
  14.         x: int64;
  15. begin
  16.         x := 42;
  17.         asm
  18.                 push x
  19.                 call identity
  20.         end;
  21. end.
This will pick the wrong the function. (If you define the function in reverse order, it will pick the one defined first.)

I mean, the compiler's so nice and prints a warning:
Quote
aliases.pas(19,8) Warning: Calling an overload function in assembler
but I wanna avoid the situation altogether. That's why I started thinking about using the alias directive.
Yours Sincerely
Kai Burghardt