forked from red-prig/fpPS4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrBitcast.pas
151 lines (123 loc) · 2.89 KB
/
srBitcast.pas
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
unit srBitcast;
{$mode ObjFPC}{$H+}
interface
uses
ginodes,
srNode,
srType,
srConst,
srReg;
type
PsrBitcast=^TsrBitcast;
TsrBitcast=object
pLeft,pRight:PsrBitcast;
//----
key:packed record
dtype:TsrDataType;
src:PsrRegNode;
end;
dst:PsrRegNode;
function c(n1,n2:PsrBitcast):Integer; static;
end;
PsrBitcastList=^TsrBitcastList;
TsrBitcastList=object
type
TNodeFetch=specialize TNodeFetch<PsrBitcast,TsrBitcast>;
var
FNTree:TNodeFetch;
rSlot:TsrRegSlot;
procedure Init(Emit:TCustomEmit); inline;
function Find(dtype:TsrDataType;src:PsrRegNode):PsrBitcast;
function FetchRead(dtype:TsrDataType;src:PsrRegNode):PsrRegNode;
function FetchDstr(dtype:TsrDataType;src:PsrRegNode):PsrRegNode;
function FetchCast(dtype:TsrDataType;src:PsrRegNode):PsrRegNode;
end;
implementation
function TsrBitcast.c(n1,n2:PsrBitcast):Integer;
begin
//first dtype
Result:=Integer(n1^.key.dtype>n2^.key.dtype)-Integer(n1^.key.dtype<n2^.key.dtype);
if (Result<>0) then Exit;
//second src
Result:=Integer(n1^.key.src>n2^.key.src)-Integer(n1^.key.src<n2^.key.src);
end;
procedure TsrBitcastList.Init(Emit:TCustomEmit); inline;
begin
rSlot.Init(Emit,'BCAST');
end;
function TsrBitcastList.Find(dtype:TsrDataType;src:PsrRegNode):PsrBitcast;
var
node:TsrBitcast;
begin
node:=Default(TsrBitcast);
node.key.dtype:=dtype;
node.key.src:=src;
Result:=FNTree.Find(@node);
end;
function TsrBitcastList.FetchRead(dtype:TsrDataType;src:PsrRegNode):PsrRegNode;
var
dst:PsrRegNode;
begin
Result:=src;
if (src=nil) then Exit;
if (dtype=dtUnknow) or (dtype=src^.dtype) then Exit;
dst:=rSlot.New(src^.pLine,dtype);
dst^.pWriter:=src;
Result:=dst;
end;
function TsrBitcastList.FetchDstr(dtype:TsrDataType;src:PsrRegNode):PsrRegNode;
var
dst:PsrRegNode;
begin
Result:=src;
if (src=nil) then Exit;
if (dtype=dtUnknow) or (dtype=src^.dtype) then Exit;
dst:=rSlot.New(src^.pLine,dtype);
dst^.pWriter:=src^.pWriter;
src^.pWriter:=dst;
Result:=dst;
end;
function TsrBitcastList.FetchCast(dtype:TsrDataType;src:PsrRegNode):PsrRegNode;
var
pConstList:PsrConstList;
node:PsrBitcast;
dst:PsrRegNode;
pConst:PsrConst;
begin
Result:=src;
if (src=nil) then Exit;
if (dtype=dtUnknow) or (dtype=src^.dtype) then Exit;
dst:=nil;
node:=Find(dtype,src);
if (node<>nil) then
begin
Result:=node^.dst;
Exit;
end;
if src^.is_const then
begin
pConst:=src^.AsConst;
pConstList:=rSlot.Emit.GetConstList;
pConst:=pConstList^.Bitcast(dtype,pConst);
dst:=rSlot.New(src^.pLine,dtype);
dst^.pWriter:=pConst;
end else
begin
if TryBitcastType(src^.dtype,dtype) then
begin
dst:=rSlot.New(src^.pLine,dtype);
rSlot.Emit.OpCast(src^.pLine,dst,src)
end else
begin
Assert(false,'bitcast');
end;
end;
node:=rSlot.Emit.Alloc(SizeOf(TsrBitcast));
node^:=Default(TsrBitcast);
node^.key.dtype:=dtype;
node^.key.src:=src;
node^.dst:=dst;
FNTree.Insert(node);
Result:=dst;
end;
end.