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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2011 by the Free Pascal development team.
Processor dependent implementation for the system unit for
JVM
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
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.
**********************************************************************}
{****************************************************************************
JVM specific stuff
****************************************************************************}
{$define FPC_SYSTEM_HAS_SYSINITFPU}
Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
softfloat_exception_mask:=[float_flag_underflow, float_flag_inexact, float_flag_denormal];
end;
{$define FPC_SYSTEM_HAS_SYSRESETFPU}
Procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
begin
softfloat_exception_flags:=[];
end;
procedure fpc_cpuinit;
begin
SysResetFPU;
if not(IsLibrary) then
SysInitFPU;
end;
{$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;
begin
result:=nil;
end;
{$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;
begin
result:=nil;
end;
{$define FPC_SYSTEM_HAS_SPTR}
function Sptr:Pointer;
begin
result:=nil;
end;
{****************************************************************************
Primitives
****************************************************************************}
{ lie so that the non-compilable generic versions will be skipped }
{$define FPC_SYSTEM_HAS_MOVE}
{$define FPC_SYSTEM_HAS_FILLCHAR}
{$push}
{$q-,r-}
procedure fillchar(var arr: array of jbyte; len: sizeint; val: byte);
begin
JUArrays.fill(arr,0,len,jbyte(val));
end;
{ boolean maps to a different signature }
procedure fillchar(var arr: array of jbyte; len: sizeint; val: jboolean);
begin
JUArrays.fill(arr,0,len,jbyte(val));
end;
{ don't define since the signature would be the same as the one above (well,
we could cheat by changing the case since the JVM is case-sensitive, but
this way we also save on code size) -> map it to the byte version via
"external" }
procedure fillchar(var arr: array of boolean; len: sizeint; val: byte);
begin
JUArrays.fill(TJBooleanArray(@arr),0,len,jboolean(val));
end;
procedure fillchar(var arr: array of boolean; len: sizeint; val: boolean);
begin
JUArrays.fill(TJBooleanArray(@arr),0,len,val);
end;
procedure fillchar(var arr: array of jshort; len: sizeint; val: byte);
var
w: jshort;
begin
w:=(val shl 8) or val;
JUArrays.fill(arr,0,len div 2,w);
if (len and 1) <> 0 then
arr[len div 2 + 1]:=(arr[len div 2 + 1] and $ff) or (val shl 8);
end;
procedure fillchar(var arr: array of jshort; len: sizeint; val: boolean);
begin
fillchar(arr,len,jbyte(val));
end;
{ widechar maps to a different signature }
procedure fillchar(var arr: array of widechar; len: sizeint; val: byte);
var
w: widechar;
begin
w:=widechar((val shl 8) or val);
JUArrays.fill(arr,0,len div 2,w);
{ jvm is big endian -> set top byte of last word }
if (len and 1) <> 0 then
arr[len shr 1+1]:=widechar((ord(arr[len shr 1+1]) and $ff) or (val shl 8));
end;
procedure fillchar(var arr: array of widechar; len: sizeint; val: boolean);
begin
fillchar(arr,len,byte(val));
end;
procedure fillchar(var arr: array of jint; len: sizeint; val: byte);
var
d, dmask: jint;
begin
d:=(val shl 8) or val;
d:=(d shl 16) or d;
JUArrays.fill(arr,0,len div 4,d);
len:=len and 3;
if len<>0 then
begin
dmask:=not((1 shl (32-8*len))-1);
d:=d and dmask;
arr[len shr 2+1]:=(arr[len shr 2+1] and not(dmask)) or d;
end;
end;
procedure fillchar(var arr: array of jint; len: sizeint; val: boolean);
begin
fillchar(arr,len,jbyte(val));
end;
procedure fillchar(var arr: array of jlong; len: sizeint; val: byte);
var
i, imask: jlong;
begin
i:=(val shl 8) or val;
i:=cardinal(i shl 16) or i;
i:=(i shl 32) or i;
JUArrays.fill(arr,0,len shr 3,i);
len:=len and 7;
if len<>0 then
begin
imask:=not((jlong(1) shl (64-8*len))-1);
i:=i and imask;
arr[len shr 3+1]:=(arr[len shr 3+1] and not(imask)) or i;
end;
end;
procedure fillchar(var arr: array of jlong; len: sizeint; val: boolean);
begin
fillchar(arr,len,jbyte(val));
end;
{$pop}
{$define FPC_SYSTEM_HAS_FILLWORD}
{$define FPC_SYSTEM_HAS_FILLDWORD}
{$define FPC_SYSTEM_HAS_FILLQWORD}
{$define FPC_SYSTEM_HAS_INDEXBYTE}
function IndexByte(const buf: array of jbyte;len:SizeInt;b:jbyte):SizeInt;
var
i: SizeInt;
begin
if len<0 then
len:=high(buf)+1;
for i:=0 to len-1 do
if buf[i]=b then
exit(i);
IndexByte:=-1;
end;
function IndexByte(const buf: array of boolean;len:SizeInt;b:jbyte):SizeInt;
var
i: SizeInt;
begin
if len<0 then
len:=high(buf)+1;
for i:=0 to len-1 do
if jbyte(buf[i])=b then
exit(i);
IndexByte:=-1;
end;
function IndexChar(const buf: array of boolean;len:SizeInt;b:ansichar):SizeInt;
begin
IndexChar:=IndexByte(buf,len,jbyte(b));
end;
function IndexChar(const buf: array of jbyte;len:SizeInt;b:ansichar):SizeInt;
begin
IndexChar:=IndexByte(buf,len,jbyte(b));
end;
{$define FPC_SYSTEM_HAS_INDEXWORD}
function IndexWord(const buf: array of jshort;len:SizeInt;b:jshort):SizeInt;
var
i: SizeInt;
begin
if len<0 then
len:=high(buf)+1;
for i:=0 to len-1 do
if buf[i]=b then
exit(i);
IndexWord:=-1;
end;
function IndexWord(const buf: array of jchar;len:SizeInt;b:jchar):SizeInt;
var
i: SizeInt;
begin
if len<0 then
len:=high(buf)+1;
for i:=0 to len-1 do
if buf[i]=b then
exit(i);
IndexWord:=-1;
end;
function IndexWord(const buf: array of jchar;len:SizeInt;b:jshort):SizeInt;
var
i: SizeInt;
c: jchar;
begin
c:=jchar(b);
if len<0 then
len:=high(buf)+1;
for i:=0 to len-1 do
if buf[i]=c then
exit(i);
IndexWord:=-1;
end;
{$define FPC_SYSTEM_HAS_INDEXDWORD}
{$define FPC_SYSTEM_HAS_INDEXQWORD}
{$define FPC_SYSTEM_HAS_COMPAREBYTE}
{$define FPC_SYSTEM_HAS_COMPAREWORD}
{$define FPC_SYSTEM_HAS_COMPAREDWORD}
{$define FPC_SYSTEM_HAS_MOVECHAR0}
{$define FPC_SYSTEM_HAS_INDEXCHAR0}
{$define FPC_SYSTEM_HAS_COMPARECHAR0}
{****************************************************************************
String
****************************************************************************}
{$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
begin
if assigned(p) then
Result:=IndexByte(TAnsiCharArray(p),high(Result),0)
else
Result:=0;
end;
{$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar); compilerproc;
var
i, len: longint;
arr: TAnsiCharArray;
begin
arr:=TAnsiCharArray(p);
i:=0;
while arr[i]<>#0 do
inc(i);
if i<>0 then
res:=pshortstring(ShortStringClass.create(arr,min(i,high(res))))^
else
res:=''
end;
{$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring); compilerproc;
var
len: longint;
begin
len:=length(sstr);
if len>high(res) then
len:=high(res);
ShortstringClass(@res).curlen:=len;
if len>0 then
JLSystem.ArrayCopy(JLObject(ShortstringClass(@sstr).fdata),0,JLObject(ShortstringClass(@res).fdata),0,len);
end;
{$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring); compilerproc;
var
s1l, s2l : integer;
begin
s1l:=length(s1);
s2l:=length(s2);
if s1l+s2l>high(s1) then
s2l:=high(s1)-s1l;
if s2l>0 then
JLSystem.ArrayCopy(JLObject(ShortstringClass(@s2).fdata),0,JLObject(ShortstringClass(@s1).fdata),s1l,s2l);
s1[0]:=chr(s1l+s2l);
end;
{$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
function fpc_shortstr_compare(const left,right:shortstring) : longint; compilerproc;
Var
MaxI,Temp, i : SizeInt;
begin
if ShortstringClass(@left)=ShortstringClass(@right) then
begin
result:=0;
exit;
end;
Maxi:=Length(left);
temp:=Length(right);
If MaxI>Temp then
MaxI:=Temp;
if MaxI>0 then
begin
for i:=0 to MaxI-1 do
begin
result:=ord(ShortstringClass(@left).fdata[i])-ord(ShortstringClass(@right).fdata[i]);
if result<>0 then
exit;
end;
result:=Length(left)-Length(right);
end
else
result:=Length(left)-Length(right);
end;
{$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
function fpc_shortstr_compare_intern(const left,right:shortstring) : longint; external name 'fpc_shortstr_compare';
function fpc_shortstr_compare_equal(const left,right:shortstring) : longint; compilerproc;
begin
{ perform normal comparsion, because JUArrays.equals() only returns true if
the arrays have equal length, while we only want to compare curlen bytes }
result:=fpc_shortstr_compare_intern(left,right);
end;
{$define FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of AnsiChar; zerobased: boolean = true); compilerproc;
var
l: longint;
index: longint;
len: byte;
foundnull: boolean;
begin
l:=high(arr)+1;
if l>=high(res)+1 then
l:=high(res)
else if l<0 then
l:=0;
if zerobased then
begin
foundnull:=false;
index:=0;
for index:=low(arr) to l-1 do
if arr[index]=#0 then
begin
foundnull:=true;
break;
end;
if not foundnull then
len:=l
else
len:=index;
end
else
len:=l;
if len>0 then
JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(ShortstringClass(@res).fdata),0,len);
ShortstringClass(@res).curlen:=len;
end;
{$define FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
procedure fpc_shortstr_to_chararray(out res: array of AnsiChar; const src: ShortString); compilerproc;
var
len: longint;
begin
len:=length(src);
if len>length(res) then
len:=length(res);
{ make sure we don't access char 1 if length is 0 (JM) }
if len>0 then
JLSystem.ArrayCopy(JLObject(ShortstringClass(@src).fdata),0,JLObject(@res),0,len);
if len<=high(res) then
JUArrays.fill(TJByteArray(@res),len,high(res),0);
end;
{****************************************************************************
Str()
****************************************************************************}
{$define FPC_SYSTEM_HAS_INT_STR_LONGINT}
procedure int_str(l:longint;out s:shortstring);
begin
s:=unicodestring(JLInteger.valueOf(l).toString);
end;
{$define FPC_SYSTEM_HAS_INT_STR_LONGWORD}
procedure int_str_unsigned(l:longword;out s:shortstring);
begin
s:=unicodestring(JLLong.valueOf(l).toString);
end;
{$define FPC_SYSTEM_HAS_INT_STR_INT64}
procedure int_str(l:int64;out s:shortstring);
begin
s:=unicodestring(JLLong.valueOf(l).toString);
end;
{$define FPC_SYSTEM_HAS_INT_STR_QWORD}
procedure int_str_unsigned(l:qword;out s:shortstring);
var
tmp: int64;
tmpstr: JLString;
bi: JMBigInteger;
begin
tmp:=int64(l);
tmpstr:=JLLong.valueOf(tmp and $7fffffffffffffff).toString;
if tmp<0 then
begin
{ no unsigned 64 bit types in Java -> use big integer to add
high(int64) to the string representation }
bi:=JMBigInteger.Create(tmpstr);
bi:=bi.add(JMBigInteger.Create('9223372036854775808'));
tmpstr:=bi.toString;
end;
s:=unicodestring(tmpstr);
end;
{ lies... }
{$define FPC_SYSTEM_HAS_ODD_LONGWORD}
{$define FPC_SYSTEM_HAS_ODD_QWORD}
{$define FPC_SYSTEM_HAS_SQR_QWORD}
|