2
Copyright (c) 1998-2002 by Florian Klaempfl
4
Generate x86-64 assembler for math nodes
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
****************************************************************************
29
node,nmat,ncgmat,nx86mat;
32
tx8664moddivnode = class(tmoddivnode)
33
procedure pass_generate_code;override;
36
tx8664shlshrnode = class(tshlshrnode)
37
procedure pass_generate_code;override;
40
tx8664unaryminusnode = class(tx86unaryminusnode)
43
tx8664notnode = class(tx86notnode)
50
cutils,verbose,globals,
51
symconst,symdef,aasmbase,aasmtai,aasmdata,defutil,
55
cgbase,cgutils,cga,cgobj,cgx86,
58
{*****************************************************************************
60
*****************************************************************************}
62
procedure tx8664moddivnode.pass_generate_code;
64
hreg1,hreg2:Tregister;
75
{ put numerator in register }
76
location_reset(location,LOC_REGISTER,OS_INT);
77
location_force_reg(current_asmdata.CurrAsmList,left.location,OS_INT,false);
78
hreg1:=left.location.register;
80
if (nodetype=divn) and (right.nodetype=ordconstn) and
81
ispowerof2(int64(tordconstnode(right).value),power) then
83
{ for signed numbers, the numerator must be adjusted before the
84
shift instruction, but not wih unsigned numbers! Otherwise,
85
"Cardinal($ffffffff) div 16" overflows! (JM) }
86
if is_signed(left.resultdef) Then
88
{ use a sequence without jumps, saw this in
90
{ no jumps, but more operations }
91
hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
92
emit_reg_reg(A_MOV,S_Q,hreg1,hreg2);
93
{If the left value is signed, hreg2=$ffffffff, otherwise 0.}
94
emit_const_reg(A_SAR,S_Q,63,hreg2);
95
{If signed, hreg2=right value-1, otherwise 0.}
96
emit_const_reg(A_AND,S_Q,tordconstnode(right).value-1,hreg2);
97
{ add to the left value }
98
emit_reg_reg(A_ADD,S_Q,hreg2,hreg1);
100
emit_const_reg(A_SAR,S_Q,power,hreg1);
103
emit_const_reg(A_SHR,S_Q,power,hreg1);
104
location.register:=hreg1;
108
{Bring denominator to a register.}
109
cg.getcpuregister(current_asmdata.CurrAsmList,NR_RAX);
110
emit_reg_reg(A_MOV,S_Q,hreg1,NR_RAX);
111
cg.getcpuregister(current_asmdata.CurrAsmList,NR_RDX);
112
{Sign extension depends on the left type.}
113
if torddef(left.resultdef).ordtype=u64bit then
114
emit_reg_reg(A_XOR,S_Q,NR_RDX,NR_RDX)
116
emit_none(A_CQO,S_NO);
118
{Division depends on the right type.}
119
if Torddef(right.resultdef).ordtype=u64bit then
124
if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
125
emit_ref(op,S_Q,right.location.reference)
126
else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
127
emit_reg(op,S_Q,right.location.register)
130
hreg1:=cg.getintregister(current_asmdata.CurrAsmList,right.location.size);
131
cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_64,right.location,hreg1);
132
emit_reg(op,S_Q,hreg1);
135
{ Copy the result into a new register. Release RAX & RDX.}
136
cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RDX);
137
cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RAX);
138
location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
139
if nodetype=divn then
140
cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_RAX,location.register)
142
cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_RDX,location.register);
147
{*****************************************************************************
149
*****************************************************************************}
152
procedure tx8664shlshrnode.pass_generate_code;
161
{ determine operator }
162
if nodetype=shln then
167
{ special treatment of 32bit values for backwards compatibility }
168
{ mul optimizations require to keep the sign (FK) }
169
if left.resultdef.size<=4 then
171
if is_signed(left.resultdef) then
179
if is_signed(left.resultdef) then
186
{ load left operators in a register }
187
location_copy(location,left.location);
188
location_force_reg(current_asmdata.CurrAsmList,location,opsize,false);
190
{ shifting by a constant directly coded: }
191
if (right.nodetype=ordconstn) then
192
emit_const_reg(op,tcgsize2opsize[opsize],tordconstnode(right).value and mask,location.register)
195
{ load right operators in a RCX }
196
cg.getcpuregister(current_asmdata.CurrAsmList,NR_RCX);
197
cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,NR_RCX);
199
{ right operand is in ECX }
200
cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RCX);
201
emit_reg_reg(op,tcgsize2opsize[opsize],NR_CL,location.register);
207
cunaryminusnode:=tx8664unaryminusnode;
208
cmoddivnode:=tx8664moddivnode;
209
cshlshrnode:=tx8664shlshrnode;
210
cnotnode:=tx8664notnode;