View Issue Details

IDProjectCategoryView StatusLast Update
0038471FPCCompilerpublic2021-04-10 10:49
Reportershiraishi Assigned ToJonas Maebe  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformIntel MacOSOS 10.15 
Fixed in Version3.3.1 
Summary0038471: MAC(Intel) FPC 3.2.0: An assembler function causes an error on linking
DescriptionSet Project Option - parsing - Assembler Style Intel

The following goes on Quick Compile, but not on Compile.

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
type
  TForm1 = class(TForm)
  private
  public
  end;
var
  Form1: TForm1;
implementation
{$R *.lfm}
  function neg(a:shortint):shortint;assembler;
  asm
     mov al,a
     neg al
  end;
end.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget-
Attached Files

Activities

shiraishi

2021-02-11 01:22

reporter  

tmp2.zip (131,838 bytes)

J. Gareth Moreton

2021-02-18 23:25

developer   ~0129006

What error are you getting? Is it possible for you to provide an assembler dump with the -a option? That might offer some clues.

shiraishi

2021-02-28 23:28

reporter   ~0129262

Warning: (9011) Object unit1.o not found, Linking may fail !
(9015) Linking /Users/shiraishi/tmp2/project1
ld: file not found: unit1.o
An error occurred while linking
Error: (9013) Error while linking
Fatal: (10026) There were 1 errors compiling module, stopping
Fatal: (1018) Compilation aborted
Error: /usr/local/bin/ppcx64 returned an error exitcode

shiraishi

2021-03-01 10:15

reporter   ~0129264

Good

function neg(a:longint):longint;assembler;
  asm
     mov eax,a
     neg eax
  end;

Good

function neg(a:longint):longint;assembler;
  asm
     mov eax,a
     neg eax
  end;

Good

  function neg(a:shortint):shortint;assembler;
  asm
     mov ax,a
     neg ax
  end;

Good

  function neg(a:smallint):shortint;assembler;
  asm
     mov al,a
     neg ax
  end;


NG

 function neg(a:byte):byte;assembler;
  asm
     mov al,a
     //neg al
  end;

The following reports warning but compile goes.

Compile Project, Target: project1: Success, Warnings: 1
unit1.pas(41,18) Warning: Size suffix and destination or source size do not match

 function neg(a:shortint):shortint;assembler;
  asm
     mov eax,a
     neg al
  end;

ravi dion

2021-03-01 11:40

reporter   ~0129270

a is shortint so 8bit while eax register is 32bit. Use: mov al,a

Thaddy de Koning

2021-03-01 12:09

reporter   ~0129272

or use movsx. https://www.felixcloutier.com/x86/movsx:movsxd

shiraishi

2021-03-01 12:35

reporter   ~0129275

also

 function neg(a:shortint):shortint;assembler;
  asm
     mov al,a
     neg al
  end;

No good.

Jonas Maebe

2021-03-01 21:52

manager   ~0129294

It may be related to the fact that the Darwin/i386 ABI requires that 8/16 bit parameters are extended to 32 bits on the caller side. The assembler reader should use the callee side location information though, and that one should be untouched. So there's definitely a bug somewhere.

On the other hand, the Darwin/i386 ABI also requires that all returned 8/16 bit values are extended to 32 bits, so make sure to do that in your pure assembler functions.

shiraishi

2021-03-01 22:03

reporter   ~0129295

Also an assembler procedure can not be compiled.
procedure neg(a:shortint);assembler;
  asm
     mov al,a
     neg al
  end;

shiraishi

2021-03-01 22:11

reporter   ~0129296

This fault does not happen for regular functions.
For example, the following can be compiled.

function neg(a:shortint):shortint;
  begin
  asm
     mov al,a
     neg al
  end;
  end;

Jonas Maebe

2021-03-01 22:16

manager   ~0129297

That's because if it's not a pure assembler routine, the compiler moves the parameter to a local alias of the right size in the entry code.

Jonas Maebe

2021-03-01 22:42

manager   ~0129298

> Also an assembler procedure can not be compiled.
Yes, parameters to functions and procedures are handled the same way. My remark about functions was about how you have to also make sure that you sign/zero-extend your function results in pure assembler functions (the compiler is not be able to give an error if you forget that).

Jonas Maebe

2021-04-10 10:49

manager   ~0130216

This actually already works on current trunk. It's not fixed in 3.2.2 though.

Also, as mentioned earlier, the Darwin/x86-64 ABI requires that all returned 8/16 bit values are extended to 32 bits, so you will have to write this even on trunk:

  function neg(a:shortint):shortint;assembler;
  asm
     movsx eax,a
     neg eax
  end;

Issue History

Date Modified Username Field Change
2021-02-11 01:22 shiraishi New Issue
2021-02-11 01:22 shiraishi File Added: tmp2.zip
2021-02-18 22:59 Jonas Maebe Project Lazarus => FPC
2021-02-18 23:25 J. Gareth Moreton Note Added: 0129006
2021-02-28 23:28 shiraishi Note Added: 0129262
2021-03-01 10:15 shiraishi Note Added: 0129264
2021-03-01 11:40 ravi dion Note Added: 0129270
2021-03-01 12:09 Thaddy de Koning Note Added: 0129272
2021-03-01 12:35 shiraishi Note Added: 0129275
2021-03-01 21:52 Jonas Maebe Note Added: 0129294
2021-03-01 22:03 shiraishi Note Added: 0129295
2021-03-01 22:11 shiraishi Note Added: 0129296
2021-03-01 22:16 Jonas Maebe Note Added: 0129297
2021-03-01 22:42 Jonas Maebe Note Added: 0129298
2021-04-10 10:49 Jonas Maebe Assigned To => Jonas Maebe
2021-04-10 10:49 Jonas Maebe Status new => resolved
2021-04-10 10:49 Jonas Maebe Resolution open => fixed
2021-04-10 10:49 Jonas Maebe Fixed in Version => 3.3.1
2021-04-10 10:49 Jonas Maebe FPCTarget => -
2021-04-10 10:49 Jonas Maebe Note Added: 0130216