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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
{
Copyright (c) 1998-2002 by Florian Klaempfl
Contains the base types for the RiscV64
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.
****************************************************************************
}
{ This Unit contains the base types for the RiscV64
}
unit cpubase;
{$I fpcdefs.inc}
interface
uses
strings, globtype,
cutils, cclasses, aasmbase, cpuinfo, cgbase;
{*****************************************************************************
Assembler Opcodes
*****************************************************************************}
type
TAsmOp=(A_None,
{ Pseudo instructions }
A_NOP,
{ normal opcodes }
A_LUI,A_AUIPC,A_JAL,A_JALR,
A_Bxx,A_LB,A_LH,A_LW,A_LBU,A_LHU,
A_SB,A_SH,A_SW,
A_ADDI,A_SLTI,A_SLTIU,
A_XORI,A_ORI,A_ANDI,
A_SLLI,A_SRLI,A_SRAI,
A_ADD,A_SUB,A_SLL,A_SLT,A_SLTU,
A_XOR,A_SRL,A_SRA,A_OR,A_AND,
A_FENCE,A_FENCE_I,
A_ECALL,A_EBREAK,
A_CSRRW,A_CSRRS,A_CSRRC,A_CSRRWI,A_CSRRSI,A_CSRRCI,
{ 64-bit }
A_ADDIW,A_SLLIW,A_SRLIW,A_SRAIW,
A_ADDW,A_SLLW,A_SRLW,A_SUBW,A_SRAW,
A_LD,A_SD,A_LWU,
{ M-extension }
A_MUL,A_MULH,A_MULHSU,A_MULHU,
A_DIV,A_DIVU,A_REM,A_REMU,
{ 64-bit }
A_MULW,
A_DIVW,A_DIVUW,A_REMW,A_REMUW,
{ A-extension }
A_LR_W,A_SC_W,A_AMOSWAP_W,A_AMOADD_W,A_AMOXOR_W,A_AMOAND_W,
A_AMOOR_W,A_AMOMIN_W,A_AMOMAX_W,A_AMOMINU_W,A_AMOMAXU_W,
{ 64-bit }
A_LR_D,A_SC_D,A_AMOSWAP_D,A_AMOADD_D,A_AMOXOR_D,A_AMOAND_D,
A_AMOOR_D,A_AMOMIN_D,A_AMOMAX_D,A_AMOMINU_D,A_AMOMAXU_D,
{ F-extension }
A_FLW,A_FSW,
A_FMADD_S,A_FMSUB_S,A_FNMSUB_S,A_FNMADD_S,
A_FADD_S,A_FSUB_S,A_FMUL_S,A_FDIV_S,
A_FSQRT_S,A_FSGNJ_S,A_FSGNJN_S,A_FSGNJX_S,
A_FMIN_S,A_FMAX_S,
A_FMV_X_S,A_FEQ_S,A_FLT_S,A_FLE_S,A_FCLASS_S,
A_FCVT_W_S,A_FCVT_WU_S,A_FCVT_S_W,A_FCVT_S_WU,
A_FMV_S_X,
A_FRCSR,A_FRRM,A_FRFLAGS,A_FSCSR,A_FSRM,
A_FSFLAGS,A_FSRMI,A_FSFLAGSI,
{ 64-bit }
A_FCVT_L_S,A_FCVT_LU_S,
A_FCVT_S_L,A_FCVT_S_LU,
{ D-extension }
A_FLD,A_FSD,
A_FMADD_D,A_FMSUB_D,A_FNMSUB_D,A_FNMADD_D,
A_FADD_D,A_FSUB_D,A_FMUL_D,A_FDIV_D,
A_FSQRT_D,A_FSGNJ_D,A_FSGNJN_D,A_FSGNJX_D,
A_FMIN_D,A_FMAX_D,
A_FEQ_D,A_FLT_D,A_FLE_D,A_FCLASS_D,
A_FCVT_D_S,A_FCVT_S_D,
A_FCVT_W_D,A_FCVT_WU_D,A_FCVT_D_W,A_FCVT_D_WU,
{ 64-bit }
A_FCVT_L_D,A_FCVT_LU_D,A_FMV_X_D,
A_FCVT_D_L,A_FCVT_D_LU,A_FMV_D_X,
{ Machine mode }
A_MRET,A_HRET,A_SRET,A_URET,
A_WFI,
{ Supervisor }
A_SFENCE_VM
);
TAsmOps = set of TAsmOp;
{# This should define the array of instructions as string }
op2strtable = array[tasmop] of string[8];
const
{# First value of opcode enumeration }
firstop = low(tasmop);
{# Last value of opcode enumeration }
lastop = high(tasmop);
{*****************************************************************************
Registers
*****************************************************************************}
type
{ Number of registers used for indexing in tables }
tregisterindex=0..{$i rrv32nor.inc}-1;
const
maxvarregs = 32-6; { 32 int registers - r0 - stackpointer - r2 - 3 scratch registers }
maxfpuvarregs = 28; { 32 fpuregisters - some scratch registers (minimally 2) }
{ Available Superregisters }
{$i rrv32sup.inc}
{ No Subregisters }
R_SUBWHOLE=R_SUBNONE;
{ Available Registers }
{$i rrv32con.inc}
{ Integer Super registers first and last }
first_int_imreg = $20;
{ Float Super register first and last }
first_fpu_imreg = $20;
{ MM Super register first and last }
first_mm_imreg = $20;
{ TODO: Calculate bsstart}
regnumber_count_bsstart = 64;
regnumber_table : array[tregisterindex] of tregister = (
{$i rrv32num.inc}
);
regstabs_table : array[tregisterindex] of shortint = (
{$i rrv32sta.inc}
);
regdwarf_table : array[tregisterindex] of shortint = (
{$i rrv32dwa.inc}
);
{*****************************************************************************
Operands
*****************************************************************************}
type
TMemoryOrderingFlag = (moRl, moAq);
TMemoryOrdering = set of TMemoryOrderingFlag;
TFenceFlag = (ffI, ffO, ffR, ffW);
TFenceFlags = set of TFenceFlag;
TRoundingMode = (RM_Default,
RM_RNE,
RM_RTZ,
RM_RDN,
RM_RUP,
RM_RMM);
const
roundingmode2str : array[TRoundingMode] of string[3] = ('',
'rne','rtz','rdn','rup','rmm');
{*****************************************************************************
Conditions
*****************************************************************************}
type
TAsmCond = (C_None { unconditional jumps },
C_LT,C_LTU,C_GE,C_GEU,C_NE,C_EQ);
TAsmConds = set of TAsmCond;
const
cond2str: Array[TAsmCond] of string[4] = ({cf_none}'',
{ conditions when not using ctr decrement etc}
'lt','ltu','ge','geu','ne','eq');
uppercond2str: Array[TAsmCond] of string[4] = ({cf_none}'',
{ conditions when not using ctr decrement etc}
'LT','LTU','GE','GEU','NE','EQ');
{*****************************************************************************
Flags
*****************************************************************************}
type
TResFlagsEnum = (F_EQ,F_NE,F_LT,F_LTU,F_GE,F_GEU);
{*****************************************************************************
Reference
*****************************************************************************}
{*****************************************************************************
Operand Sizes
*****************************************************************************}
{*****************************************************************************
Constants
*****************************************************************************}
const
max_operands = 5;
{*****************************************************************************
Default generic sizes
*****************************************************************************}
{# Defines the default address size for a processor, }
OS_ADDR = OS_64;
{# the natural int size for a processor,
has to match osuinttype/ossinttype as initialized in psystem }
OS_INT = OS_64;
OS_SINT = OS_S64;
{# the maximum float size for a processor, }
OS_FLOAT = OS_F64;
{# the size of a vector register for a processor }
OS_VECTOR = OS_M128;
{*****************************************************************************
GDB Information
*****************************************************************************}
stab_regindex: array[tregisterindex] of shortint = (
{$I rrv32sta.inc}
);
{*****************************************************************************
Generic Register names
*****************************************************************************}
{# Stack pointer register }
NR_STACK_POINTER_REG = NR_X2;
RS_STACK_POINTER_REG = RS_X2;
{# Frame pointer register }
NR_FRAME_POINTER_REG = NR_X8;
RS_FRAME_POINTER_REG = RS_X8;
NR_PIC_OFFSET_REG = NR_X3;
{ Return address of a function }
NR_RETURN_ADDRESS_REG = NR_X1;
RS_RETURN_ADDRESS_REG = RS_X1;
{ Results are returned in this register (32-bit values) }
NR_FUNCTION_RETURN_REG = NR_X10;
RS_FUNCTION_RETURN_REG = RS_X10;
{ Low part of 64bit return value }
NR_FUNCTION_RETURN64_LOW_REG = NR_X10;
RS_FUNCTION_RETURN64_LOW_REG = RS_X10;
{ High part of 64bit return value }
NR_FUNCTION_RETURN64_HIGH_REG = NR_X11;
RS_FUNCTION_RETURN64_HIGH_REG = RS_X11;
{ The value returned from a function is available in this register }
NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
{ The lowh part of 64bit value returned from a function }
NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
{ The high part of 64bit value returned from a function }
NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
NR_FPU_RESULT_REG = NR_F10;
NR_MM_RESULT_REG = NR_NO;
NR_DEFAULTFLAGS = NR_NO;
RS_DEFAULTFLAGS = RS_NO;
{*****************************************************************************
GCC /ABI linking information
*****************************************************************************}
{# Registers which must be saved when calling a routine declared as
cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
saved should be the ones as defined in the target ABI and / or GCC.
This value can be deduced from CALLED_USED_REGISTERS array in the
GCC source.
}
saved_standard_registers: array[0..12] of tsuperregister = (
RS_X2,
RS_X8,RS_X9,
RS_X18,RS_X19,
RS_X20,RS_X21,RS_X22,RS_X23,RS_X24,RS_X25,RS_X26,RS_X27
);
{ this is only for the generic code which is not used for this architecture }
saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
{# Required parameter alignment when calling a routine declared as
stdcall and cdecl. The alignment value should be the one defined
by GCC or the target ABI.
The value of this constant is equal to the constant
PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
}
std_param_align = 8; { for 32-bit version only }
{*****************************************************************************
CPU Dependent Constants
*****************************************************************************}
maxfpuregs = 8;
{*****************************************************************************
Helpers
*****************************************************************************}
function is_imm12(value: tcgint): boolean;
function is_lui_imm(value: tcgint): boolean;
function is_calljmp(o:tasmop):boolean;
function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
{ Returns the tcgsize corresponding with the size of reg.}
function reg_cgsize(const reg: tregister) : tcgsize;
function findreg_by_number(r:Tregister):tregisterindex;
function std_regnum_search(const s:string):Tregister;
function std_regname(r:Tregister):string;
function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
function dwarf_reg(r:tregister):shortint;
function dwarf_reg_no_error(r:tregister):shortint;
function eh_return_data_regno(nr: longint): longint;
function conditions_equal(const c1,c2: TAsmCond): boolean;
{ Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
function condition_in(const Subset, c: TAsmCond): Boolean;
implementation
uses
rgbase,verbose;
const
std_regname_table : TRegNameTable = (
{$i rrv32std.inc}
);
regnumber_index : array[tregisterindex] of tregisterindex = (
{$i rrv32rni.inc}
);
std_regname_index : array[tregisterindex] of tregisterindex = (
{$i rrv32sri.inc}
);
{*****************************************************************************
Helpers
*****************************************************************************}
function is_imm12(value: tcgint): boolean;
begin
result:=(value >= -2048) and (value <= 2047);
end;
function is_lui_imm(value: tcgint): boolean;
begin
result:=SarInt64((value and $FFFFF000) shl 32, 32) = value;
end;
function is_calljmp(o:tasmop):boolean;
begin
is_calljmp:=false;
case o of
A_JAL,A_JALR,A_Bxx:
is_calljmp:=true;
else
;
end;
end;
function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
const
inv_condflags:array[TAsmCond] of TAsmCond=(C_None,
C_GE,C_GEU,C_LT,C_LTU,C_EQ,C_NE);
begin
result := inv_condflags[c];
end;
function reg_cgsize(const reg: tregister): tcgsize;
begin
case getregtype(reg) of
R_INTREGISTER :
result:=OS_64;
R_MMREGISTER:
result:=OS_M128;
R_FPUREGISTER:
result:=OS_F64;
else
internalerror(200303181);
end;
end;
function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
begin
cgsize2subreg:=R_SUBWHOLE;
end;
function findreg_by_number(r:Tregister):tregisterindex;
begin
result:=rgBase.findreg_by_number_table(r,regnumber_index);
end;
function std_regnum_search(const s:string):Tregister;
begin
result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
end;
function std_regname(r:Tregister):string;
var
p : tregisterindex;
begin
p:=findreg_by_number_table(r,regnumber_index);
if p<>0 then
result:=std_regname_table[p]
else
result:=generic_regname(r);
end;
function dwarf_reg(r:tregister):shortint;
begin
result:=regdwarf_table[findreg_by_number(r)];
if result=-1 then
internalerror(200603251);
end;
function dwarf_reg_no_error(r:tregister):shortint;
begin
result:=regdwarf_table[findreg_by_number(r)];
end;
function eh_return_data_regno(nr: longint): longint;
begin
if (nr>=0) and (nr<4) then
result:=nr+10
else
result:=-1;
end;
function conditions_equal(const c1, c2: TAsmCond): boolean;
begin
result:=c1=c2;
end;
{ Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
function condition_in(const Subset, c: TAsmCond): Boolean;
begin
Result := (c = C_None) or conditions_equal(Subset, c);
if not Result then
case Subset of
C_EQ:
Result := (c in [C_GE, C_GEU]);
else
Result := False;
end;
end;
end.
|