<div dir="ltr"><div>The TypeInfo function checks the code during run-time thus the generated code is 'bloated'. Better use the compiler intrinsic GetTypeKind.</div><div><br></div><div>{$mode objfpc}<br><br>uses<br>   typinfo;<br><br>generic procedure Add<T>;<br>begin<br>    if GetTypeKind(T) = tkInteger then WriteLn('an integer');<br>    if GetTypeKind(T) = tkString then WriteLn('a string');<br>end;<br><br>begin<br>   specialize Add<Integer>;<br>   specialize Add<String>;<br>end.</div><div><br></div><div>for 'specialize Add<Integer>;' the following code is generated:</div><div><br></div><div># [46] if GetTypeKind(T) = tkInteger then WriteLn('an integer');<br> call    fpc_get_output<br>        movq    %rax,%rbx<br>     leaq    _$VT$_Ld1(%rip),%r8<br>   movq    %rbx,%rdx<br>     movl    $0,%ecx<br>       call    fpc_write_text_shortstr<br>       call    fpc_iocheck<br>   movq    %rbx,%rcx<br>     call    fpc_writeln_end<br>       call    fpc_iocheck<br># [48] end;</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Di., 25. Aug. 2020 um 23:54 Uhr schrieb Benito van der Zander via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  
    
  
  <div>
    <div id="gmail-m_-5043164764094412657smartTemplate4-template">Hi,</div>
    <div><br>
    </div>
    <div>that is generating rather odd code (r40721)<br>
      <p>project1.lpr:9                            begin<br>
        0000000000401090 55                       push   %rbp<br>
        0000000000401091 4889e5                   mov    %rsp,%rbp<br>
        0000000000401094 488d6424f0               lea   
        -0x10(%rsp),%rsp<br>
        0000000000401099 48895df8                 mov    %rbx,-0x8(%rbp)<br>
        project1.lpr:10                           if TypeInfo(T) =
        TypeInfo(Integer) then WriteLn('an integer');<br>
        000000000040109D 488d1584af0700           lea   
        0x7af84(%rip),%rdx        # 0x47c028
        <RTTI_$SYSTEM_$$_LONGINT><br>
        00000000004010A4 488d057daf0700           lea   
        0x7af7d(%rip),%rax        # 0x47c028
        <RTTI_$SYSTEM_$$_LONGINT><br>
        00000000004010AB 4839c2                   cmp    %rax,%rdx<br>
        00000000004010AE 752b                     jne    0x4010db
        <ADD$1$CRC713F463B+75><br>
        00000000004010B0 e8cbcf0100               callq  0x41e080
        <fpc_get_output><br>
        00000000004010B5 4889c3                   mov    %rax,%rbx<br>
        00000000004010B8 488d15412f0600           lea   
        0x62f41(%rip),%rdx        # 0x464000 <_$PROJECT1$_Ld1><br>
        00000000004010BF 4889de                   mov    %rbx,%rsi<br>
        00000000004010C2 31ff                     xor    %edi,%edi<br>
        00000000004010C4 e867d30100               callq  0x41e430
        <fpc_write_text_shortstr><br>
        00000000004010C9 e8d2700100               callq  0x4181a0
        <fpc_iocheck><br>
        00000000004010CE 4889df                   mov    %rbx,%rdi<br>
        00000000004010D1 e85ad20100               callq  0x41e330
        <fpc_writeln_end><br>
        00000000004010D6 e8c5700100               callq  0x4181a0
        <fpc_iocheck><br>
        project1.lpr:11                           if TypeInfo(T) =
        TypeInfo(String) then WriteLn('a string');<br>
        00000000004010DB 488d0546af0700           lea   
        0x7af46(%rip),%rax        # 0x47c028
        <RTTI_$SYSTEM_$$_LONGINT><br>
        00000000004010E2 488d1537b20700           lea   
        0x7b237(%rip),%rdx        # 0x47c320
        <RTTI_$SYSTEM_$$_SHORTSTRING><br>
        00000000004010E9 4839d0                   cmp    %rdx,%rax<br>
        00000000004010EC 752b                     jne    0x401119
        <ADD$1$CRC713F463B+137><br>
        00000000004010EE e88dcf0100               callq  0x41e080
        <fpc_get_output><br>
        00000000004010F3 4889c3                   mov    %rax,%rbx<br>
        00000000004010F6 488d15132f0600           lea   
        0x62f13(%rip),%rdx        # 0x464010 <_$PROJECT1$_Ld2><br>
        00000000004010FD 4889de                   mov    %rbx,%rsi<br>
        0000000000401100 31ff                     xor    %edi,%edi<br>
        0000000000401102 e829d30100               callq  0x41e430
        <fpc_write_text_shortstr><br>
        0000000000401107 e894700100               callq  0x4181a0
        <fpc_iocheck><br>
        000000000040110C 4889df                   mov    %rbx,%rdi<br>
        000000000040110F e81cd20100               callq  0x41e330
        <fpc_writeln_end><br>
        0000000000401114 e887700100               callq  0x4181a0
        <fpc_iocheck><br>
        project1.lpr:12                           end;<br>
        <br>
      </p>
      The compiler should know everything about TypeInfo(T) and optimize
      it away where possible<br>
      <br>
      Cheers,<br>
      Benito </div>
    <div>On 25.08.20 04:32, leledumbo via
      fpc-pascal wrote:<br>
    </div>
    <blockquote type="cite">
      <blockquote type="cite">
        <pre>I remember something like this in RTTI though but can't find it in your
</pre>
      </blockquote>
      <pre>docs yet for generics.

{$mode objfpc}

uses
  typinfo;

generic procedure Add<T>;
begin
   if TypeInfo(T) = TypeInfo(Integer) then WriteLn('an integer');
   if TypeInfo(T) = TypeInfo(String) then WriteLn('a string');
end;

begin
  specialize Add<Integer>;
  specialize Add<String>;
end.



--
Sent from: <a href="http://free-pascal-general.1045716.n5.nabble.com/" target="_blank">http://free-pascal-general.1045716.n5.nabble.com/</a>
_______________________________________________
fpc-pascal maillist  -  <a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.org</a>
<a href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal" target="_blank">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a>
</pre>
    </blockquote>
  </div>

_______________________________________________<br>
fpc-pascal maillist  -  <a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.org</a><br>
<a href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal" rel="noreferrer" target="_blank">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a><br>
</blockquote></div>