summaryrefslogtreecommitdiff
path: root/fpcsrc/compiler/x86_64/nx64add.pas
blob: 2888b7f326c16100729b5ab30204316157f61ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
    Copyright (c) 2000-2002 by Florian Klaempfl

    Code generation for add nodes on the x86-64

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit nx64add;

{$i fpcdefs.inc}

interface

    uses
       node,nadd,cpubase,nx86add;

    type
       tx8664addnode = class(tx86addnode)
          procedure second_addordinal; override;
          procedure second_mul;
       end;

  implementation

    uses
      globtype,globals,
      aasmbase,aasmtai,aasmdata,
      defutil,
      cgbase,cgutils,cga,cgobj,
      tgobj;

{*****************************************************************************
                                Addordinal
*****************************************************************************}

    procedure tx8664addnode.second_addordinal;
    begin
      { filter unsigned MUL opcode, which requires special handling }
      if (nodetype=muln) and
        (not(is_signed(left.resultdef)) or
         not(is_signed(right.resultdef))) then
      begin
        second_mul;
        exit;
      end;

      inherited second_addordinal;
    end;

{*****************************************************************************
                                MUL
*****************************************************************************}

    procedure tx8664addnode.second_mul;

    var reg:Tregister;
        ref:Treference;
        use_ref:boolean;
        hl4 : tasmlabel;

    begin
      pass_left_right;

      { The location.register will be filled in later (JM) }
      location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
      { Mul supports registers and references, so if not register/reference,
        load the location into a register}
      use_ref:=false;
      if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
        reg:=left.location.register
      else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
        begin
          ref:=left.location.reference;
          use_ref:=true;
        end
      else
        begin
          {LOC_CONSTANT for example.}
          reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
          cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,left.location,reg);
        end;
      { Allocate RAX. }
      cg.getcpuregister(current_asmdata.CurrAsmList,NR_RAX);
      { Load the right value. }
      cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,NR_RAX);
      { Also allocate RDX, since it is also modified by a mul (JM). }
      cg.getcpuregister(current_asmdata.CurrAsmList,NR_RDX);
      if use_ref then
        emit_ref(A_MUL,S_Q,ref)
      else
        emit_reg(A_MUL,S_Q,reg);
      if cs_check_overflow in current_settings.localswitches  then
       begin
         current_asmdata.getjumplabel(hl4);
         cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
         cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
         cg.a_label(current_asmdata.CurrAsmList,hl4);
       end;
      { Free RDX,RAX }
      cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RDX);
      cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RAX);
      { Allocate a new register and store the result in RAX in it. }
      location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
      emit_reg_reg(A_MOV,S_Q,NR_RAX,location.register);
      location_freetemp(current_asmdata.CurrAsmList,left.location);
      location_freetemp(current_asmdata.CurrAsmList,right.location);
    end;


begin
   caddnode:=tx8664addnode;
end.