-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdisa_sparc.adb
281 lines (241 loc) · 9.11 KB
/
disa_sparc.adb
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
------------------------------------------------------------------------------
-- --
-- GNATcoverage --
-- --
-- Copyright (C) 2006-2024, AdaCore --
-- --
-- GNATcoverage is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This software is distributed in the hope that it will be useful --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY 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 distributed with this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
with Interfaces; use Interfaces;
with Disa_Common; use Disa_Common;
with Sparc_Descs; use Sparc_Descs;
package body Disa_Sparc is
function Get_Field
(Field : Sparc_Fields; V : Unsigned_32) return Unsigned_32;
function Get_Field_Sext
(Field : Sparc_Fields; V : Unsigned_32) return Unsigned_32;
-- Extract a field from an instruction.
---------------
-- Get_Field --
---------------
function Get_Field
(Field : Sparc_Fields; V : Unsigned_32) return Unsigned_32
is
F : constant Field_Type := Fields_Mask (Field);
Len : constant Natural := F.First - F.Last + 1;
begin
return Shift_Right (Shift_Left (V, 31 - F.First), 32 - Len);
end Get_Field;
--------------------
-- Get_Field_Sext --
--------------------
function Get_Field_Sext
(Field : Sparc_Fields; V : Unsigned_32) return Unsigned_32
is
F : constant Field_Type := Fields_Mask (Field);
Len : constant Natural := F.First - F.Last + 1;
begin
return Shift_Right_Arithmetic (Shift_Left (V, 31 - F.First), 32 - Len);
end Get_Field_Sext;
----------------
-- Initialize --
----------------
overriding procedure Initialize
(Object : in out SPARC_Disassembler) is
begin
Object.Handle := Dis_Opcodes.Create_Sparc_Disassembler;
end Initialize;
--------------
-- Finalize --
--------------
overriding procedure Finalize
(Object : in out SPARC_Disassembler) is
begin
Dis_Opcodes.Delete_Disassembler (Object.Handle);
end Finalize;
---------------------
-- Get_Insn_Length --
---------------------
function Get_Insn_Length
(Self : SPARC_Disassembler;
Insn_Bin : Binary_Content) return Positive
is
pragma Unreferenced (Self);
pragma Unreferenced (Insn_Bin);
begin
return 4;
end Get_Insn_Length;
----------------------
-- Disassemble_Insn --
----------------------
procedure Disassemble_Insn
(Self : SPARC_Disassembler;
Insn_Bin : Binary_Content;
Pc : Traces.Pc_Type;
Buffer : in out Highlighting.Buffer_Type;
Insn_Len : out Natural;
Sym : Symbolizer'Class) is
begin
Opcodes_Disassemble_Insn
(Self.Handle, Insn_Bin, Pc, Buffer, Insn_Len, Sym, 4);
end Disassemble_Insn;
-------------------------
-- Get_Insn_Properties --
-------------------------
procedure Get_Insn_Properties
(Self : SPARC_Disassembler;
Insn_Bin : Binary_Content;
Pc : Pc_Type;
Branch : out Branch_Kind;
Flag_Indir : out Boolean;
Flag_Cond : out Boolean;
Branch_Dest : out Dest;
FT_Dest : out Dest)
is
pragma Unreferenced (Self);
W : Unsigned_32;
R : Unsigned_32;
begin
-- Make sure OUT parameters have a valid value
Branch_Dest := (No_PC, No_PC);
FT_Dest := (No_PC, No_PC);
Branch := Br_None;
if Length (Insn_Bin) < 4 then
raise Program_Error;
end if;
W := To_Big_Endian_U32
(Slice (Insn_Bin, Insn_Bin.First, Insn_Bin.First + 3));
Flag_Cond := False;
Flag_Indir := False;
case Get_Field (F_Op, W) is
when 2#00# =>
case Get_Field (F_Op2, W) is
when 2#010# | 2#110# =>
-- Bicc / FBfcc
-- Sparc v7 spec:
-- [...] the branch is taken, causing a delayed, PC-relative
-- control transfer to the address
-- PC + (sign extnd (disp22) * 4)
Branch_Dest.Target :=
Pc + Pc_Type (Get_Field_Sext (F_Disp22, W)) * 4;
-- Sparc v7 spec:
-- If the branch is not taken, the annul bit field (a) is
-- checked. [...]
-- If the branch is taken, the annul field is ignored, and
-- the delay instruction is executed.
Branch_Dest.Delay_Slot := Pc + 4;
FT_Dest.Target := Pc + 8;
if Get_Field (F_A, W) = 0 then
-- If the annul field is zero, the delay instruction is
-- executed.
FT_Dest.Delay_Slot := Pc + 4;
else
-- If a is set, the instruction immediately following
-- the branch instruction is not executed (ie, it is
-- annulled).
FT_Dest.Delay_Slot := No_PC;
end if;
if (Get_Field (F_Cond, W) and 2#0111#) /= 0 then
Flag_Cond := True;
elsif Get_Field (F_Cond, W) = 2#1000# then
-- BA
-- Sparc v7 spec:
-- Branch Always (BA), because it always branch
-- regardless of the condition code, would normally
-- ignore the annul field. Instead it follows the same
-- annul field rules:
-- Isn't this redundant with the above setting???
if Get_Field (F_A, W) = 1 then
-- if a = 1, the delay instruction is annulled;
Branch_Dest.Delay_Slot := No_PC;
else
-- if a = 0, the delay instruction is executed.
Branch_Dest.Delay_Slot := Pc + 4;
end if;
end if;
Branch := Br_Jmp;
return;
when others =>
null;
end case;
when 2#01# =>
-- Call
-- Sparc v7 spec:
-- The CALL instruction causes a delayed, unconditionnal,
-- PC-relative control transfer to the address PC + (disp30 * 4).
-- [...], therefore the delay slot instruction following the CALL
-- instruction is always executed.
Branch_Dest.Target :=
Pc + Pc_Type (Get_Field_Sext (F_Disp30, W)) * 4;
Branch_Dest.Delay_Slot := Pc + 4;
Branch := Br_Call;
return;
when 2#10# =>
if Get_Field (F_Op3, W) = 16#38# then
Branch_Dest.Delay_Slot := Pc + 4;
-- jmpl.
Flag_Indir := True;
R := Get_Field (F_Rd, W);
if R = 15 then
-- Register indirect call.
Branch := Br_Call;
return;
elsif R = 0
and then Get_Field (F_I, W) = 1
and then Get_Field (F_Simm13, W) = 8
then
-- Ret or retl.
R := Get_Field (F_Rs1, W);
if R = 15 or else R = 31 then
Branch := Br_Ret;
return;
end if;
end if;
-- Unknown.
Branch := Br_Jmp;
return;
end if;
when others =>
null;
end case;
Branch := Br_None;
end Get_Insn_Properties;
----------------
-- Is_Padding --
----------------
function Is_Padding
(Self : SPARC_Disassembler;
Insn_Bin : Binary_Content;
Pc : Pc_Type) return Boolean
is
pragma Unreferenced (Self, Insn_Bin, Pc);
begin
return False;
end Is_Padding;
------------
-- Is_Nop --
------------
function Is_Nop
(Self : SPARC_Disassembler;
Insn_Bin : Binary_Content;
Pc : Pc_Type) return Boolean
is
W : Unsigned_32;
begin
if Length (Insn_Bin) < 4 then
raise Program_Error;
end if;
W := To_Big_Endian_U32
(Slice (Insn_Bin, Insn_Bin.First, Insn_Bin.First + 3));
return Get_Field (F_Op2, W) = 2#100#;
end Is_Nop;
end Disa_Sparc;