jvm.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2011 by the Free Pascal development team.
  4. Processor dependent implementation for the system unit for
  5. JVM
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. JVM specific stuff
  14. ****************************************************************************}
  15. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  16. Procedure SysInitFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  17. begin
  18. softfloat_exception_mask:=[float_flag_underflow, float_flag_inexact, float_flag_denormal];
  19. end;
  20. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  21. Procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  22. begin
  23. softfloat_exception_flags:=[];
  24. end;
  25. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  26. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;
  27. begin
  28. result:=nil;
  29. end;
  30. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  31. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;
  32. begin
  33. result:=nil;
  34. end;
  35. {$define FPC_SYSTEM_HAS_SPTR}
  36. function Sptr:Pointer;
  37. begin
  38. result:=nil;
  39. end;
  40. {****************************************************************************
  41. Primitives
  42. ****************************************************************************}
  43. { lie so that the non-compilable generic versions will be skipped }
  44. {$define FPC_SYSTEM_HAS_MOVE}
  45. {$define FPC_SYSTEM_HAS_FILLCHAR}
  46. {$push}
  47. {$q-,r-}
  48. procedure fillchar(var arr: array of jbyte; len: sizeint; val: byte);
  49. begin
  50. JUArrays.fill(arr,0,len,jbyte(val));
  51. end;
  52. { boolean maps to a different signature }
  53. procedure fillchar(var arr: array of jbyte; len: sizeint; val: jboolean);
  54. begin
  55. JUArrays.fill(arr,0,len,jbyte(val));
  56. end;
  57. { don't define since the signature would be the same as the one above (well,
  58. we could cheat by changing the case since the JVM is case-sensitive, but
  59. this way we also save on code size) -> map it to the byte version via
  60. "external" }
  61. procedure fillchar(var arr: array of boolean; len: sizeint; val: byte);
  62. begin
  63. JUArrays.fill(TJBooleanArray(@arr),0,len,jboolean(val));
  64. end;
  65. procedure fillchar(var arr: array of boolean; len: sizeint; val: boolean);
  66. begin
  67. JUArrays.fill(TJBooleanArray(@arr),0,len,val);
  68. end;
  69. procedure fillchar(var arr: array of jshort; len: sizeint; val: byte);
  70. var
  71. w: jshort;
  72. begin
  73. w:=(val shl 8) or val;
  74. JUArrays.fill(arr,0,len div 2,w);
  75. if (len and 1) <> 0 then
  76. arr[len div 2 + 1]:=(arr[len div 2 + 1] and $ff) or (val shl 8);
  77. end;
  78. procedure fillchar(var arr: array of jshort; len: sizeint; val: boolean);
  79. begin
  80. fillchar(arr,len,jbyte(val));
  81. end;
  82. { widechar maps to a different signature }
  83. procedure fillchar(var arr: array of widechar; len: sizeint; val: byte);
  84. var
  85. w: widechar;
  86. begin
  87. w:=widechar((val shl 8) or val);
  88. JUArrays.fill(arr,0,len div 2,w);
  89. { jvm is big endian -> set top byte of last word }
  90. if (len and 1) <> 0 then
  91. arr[len shr 1+1]:=widechar((ord(arr[len shr 1+1]) and $ff) or (val shl 8));
  92. end;
  93. procedure fillchar(var arr: array of widechar; len: sizeint; val: boolean);
  94. begin
  95. fillchar(arr,len,byte(val));
  96. end;
  97. procedure fillchar(var arr: array of jint; len: sizeint; val: byte);
  98. var
  99. d, dmask: jint;
  100. begin
  101. d:=(val shl 8) or val;
  102. d:=(d shl 16) or d;
  103. JUArrays.fill(arr,0,len div 4,d);
  104. len:=len and 3;
  105. if len<>0 then
  106. begin
  107. dmask:=not((1 shl (32-8*len))-1);
  108. d:=d and dmask;
  109. arr[len shr 2+1]:=(arr[len shr 2+1] and not(dmask)) or d;
  110. end;
  111. end;
  112. procedure fillchar(var arr: array of jint; len: sizeint; val: boolean);
  113. begin
  114. fillchar(arr,len,jbyte(val));
  115. end;
  116. procedure fillchar(var arr: array of jlong; len: sizeint; val: byte);
  117. var
  118. i, imask: jlong;
  119. begin
  120. i:=(val shl 8) or val;
  121. i:=cardinal(i shl 16) or i;
  122. i:=(i shl 32) or i;
  123. JUArrays.fill(arr,0,len shr 3,i);
  124. len:=len and 7;
  125. if len<>0 then
  126. begin
  127. imask:=not((jlong(1) shl (64-8*len))-1);
  128. i:=i and imask;
  129. arr[len shr 3+1]:=(arr[len shr 3+1] and not(imask)) or i;
  130. end;
  131. end;
  132. procedure fillchar(var arr: array of jlong; len: sizeint; val: boolean);
  133. begin
  134. fillchar(arr,len,jbyte(val));
  135. end;
  136. {$define FPC_SYSTEM_HAS_FILLWORD}
  137. procedure fillword(var arr: array of jshort; len: sizeint; val: word);
  138. begin
  139. JUArrays.fill(arr,0,len,jshort(val));
  140. end;
  141. procedure fillword(var arr: array of jshort; len: sizeint; val: boolean);
  142. begin
  143. fillword(arr,len,jshort(jbyte(val)));
  144. end;
  145. { widechar maps to a different signature }
  146. procedure fillword(var arr: array of widechar; len: sizeint; val: word);
  147. var
  148. w : widechar;
  149. begin
  150. w:=widechar(val);
  151. JUArrays.fill(arr,0,len,w);
  152. end;
  153. procedure fillword(var arr: array of widechar; len: sizeint; val: boolean);
  154. begin
  155. fillword(arr,len,jshort(jbyte(val)));
  156. end;
  157. procedure fillword(var arr: array of jint; len: sizeint; val: word);
  158. var
  159. d, dmask: jint;
  160. begin
  161. d:=cardinal(val shl 16) or val;
  162. JUArrays.fill(arr,0,len div 2,d);
  163. len:=len and 1;
  164. if len<>0 then
  165. begin
  166. dmask:=not((1 shl (32-8*len))-1);
  167. d:=d and dmask;
  168. arr[len shr 2+1]:=(arr[len shr 2+1] and not(dmask)) or d;
  169. end;
  170. end;
  171. procedure fillword(var arr: array of jint; len: sizeint; val: boolean);
  172. begin
  173. fillword(arr,len,jshort(jbyte(val)));
  174. end;
  175. procedure fillword(var arr: array of jlong; len: sizeint; val: word);
  176. var
  177. i, imask: jlong;
  178. begin
  179. i:=cardinal(val shl 16) or val;
  180. i:=(i shl 32) or i;
  181. JUArrays.fill(arr,0,len shr 2,i);
  182. len:=len and 3;
  183. if len<>0 then
  184. begin
  185. imask:=not((1 shl (32-8*len))-1);
  186. i:=i and imask;
  187. arr[len shr 2+1]:=(arr[len shr 2+1] and not(imask)) or i;
  188. end;
  189. end;
  190. procedure fillword(var arr: array of jlong; len: sizeint; val: boolean);
  191. begin
  192. fillword(arr,len,jshort(jbyte(val)));
  193. end;
  194. {$define FPC_SYSTEM_HAS_FILLDWORD}
  195. {$define FPC_SYSTEM_HAS_FILLQWORD}
  196. {$pop}
  197. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  198. function IndexByte(const buf: array of jbyte;len:SizeInt;b:jbyte):SizeInt;
  199. var
  200. i: SizeInt;
  201. begin
  202. if len<0 then
  203. len:=high(buf)+1;
  204. for i:=0 to len-1 do
  205. if buf[i]=b then
  206. exit(i);
  207. IndexByte:=-1;
  208. end;
  209. function IndexByte(const buf: array of boolean;len:SizeInt;b:jbyte):SizeInt;
  210. var
  211. i: SizeInt;
  212. begin
  213. if len<0 then
  214. len:=high(buf)+1;
  215. for i:=0 to len-1 do
  216. if jbyte(buf[i])=b then
  217. exit(i);
  218. IndexByte:=-1;
  219. end;
  220. function IndexChar(const buf: array of boolean;len:SizeInt;b:ansichar):SizeInt;
  221. begin
  222. IndexChar:=IndexByte(buf,len,jbyte(b));
  223. end;
  224. function IndexChar(const buf: array of jbyte;len:SizeInt;b:ansichar):SizeInt;
  225. begin
  226. IndexChar:=IndexByte(buf,len,jbyte(b));
  227. end;
  228. {$define FPC_SYSTEM_HAS_INDEXWORD}
  229. function IndexWord(const buf: array of jshort;len:SizeInt;b:jshort):SizeInt;
  230. var
  231. i: SizeInt;
  232. begin
  233. if len<0 then
  234. len:=high(buf)+1;
  235. for i:=0 to len-1 do
  236. if buf[i]=b then
  237. exit(i);
  238. IndexWord:=-1;
  239. end;
  240. function IndexWord(const buf: array of jchar;len:SizeInt;b:jchar):SizeInt;
  241. var
  242. i: SizeInt;
  243. begin
  244. if len<0 then
  245. len:=high(buf)+1;
  246. for i:=0 to len-1 do
  247. if buf[i]=b then
  248. exit(i);
  249. IndexWord:=-1;
  250. end;
  251. function IndexWord(const buf: array of jchar;len:SizeInt;b:jshort):SizeInt;
  252. var
  253. i: SizeInt;
  254. c: jchar;
  255. begin
  256. c:=jchar(b);
  257. if len<0 then
  258. len:=high(buf)+1;
  259. for i:=0 to len-1 do
  260. if buf[i]=c then
  261. exit(i);
  262. IndexWord:=-1;
  263. end;
  264. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  265. {$define FPC_SYSTEM_HAS_INDEXQWORD}
  266. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  267. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  268. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  269. {$define FPC_SYSTEM_HAS_MOVECHAR0}
  270. {$define FPC_SYSTEM_HAS_INDEXCHAR0}
  271. {$define FPC_SYSTEM_HAS_COMPARECHAR0}
  272. {****************************************************************************
  273. String
  274. ****************************************************************************}
  275. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  276. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  277. begin
  278. if assigned(p) then
  279. Result:=IndexByte(TAnsiCharArray(p),high(Result),0)
  280. else
  281. Result:=0;
  282. end;
  283. {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  284. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar); compilerproc;
  285. var
  286. i, len: longint;
  287. arr: TAnsiCharArray;
  288. begin
  289. arr:=TAnsiCharArray(p);
  290. i:=0;
  291. while arr[i]<>#0 do
  292. inc(i);
  293. if i<>0 then
  294. res:=pshortstring(ShortStringClass.create(arr,min(i,high(res))))^
  295. else
  296. res:=''
  297. end;
  298. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  299. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring); compilerproc;
  300. var
  301. len: longint;
  302. begin
  303. len:=length(sstr);
  304. if len>high(res) then
  305. len:=high(res);
  306. ShortstringClass(@res).curlen:=len;
  307. if len>0 then
  308. JLSystem.ArrayCopy(JLObject(ShortstringClass(@sstr).fdata),0,JLObject(ShortstringClass(@res).fdata),0,len);
  309. end;
  310. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  311. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring); compilerproc;
  312. var
  313. s1l, s2l : integer;
  314. begin
  315. s1l:=length(s1);
  316. s2l:=length(s2);
  317. if s1l+s2l>high(s1) then
  318. s2l:=high(s1)-s1l;
  319. if s2l>0 then
  320. JLSystem.ArrayCopy(JLObject(ShortstringClass(@s2).fdata),0,JLObject(ShortstringClass(@s1).fdata),s1l,s2l);
  321. s1[0]:=chr(s1l+s2l);
  322. end;
  323. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  324. function fpc_shortstr_compare(const left,right:shortstring) : longint; compilerproc;
  325. Var
  326. MaxI,Temp, i : SizeInt;
  327. begin
  328. if ShortstringClass(@left)=ShortstringClass(@right) then
  329. begin
  330. result:=0;
  331. exit;
  332. end;
  333. Maxi:=Length(left);
  334. temp:=Length(right);
  335. If MaxI>Temp then
  336. MaxI:=Temp;
  337. if MaxI>0 then
  338. begin
  339. for i:=0 to MaxI-1 do
  340. begin
  341. result:=ord(ShortstringClass(@left).fdata[i])-ord(ShortstringClass(@right).fdata[i]);
  342. if result<>0 then
  343. exit;
  344. end;
  345. result:=Length(left)-Length(right);
  346. end
  347. else
  348. result:=Length(left)-Length(right);
  349. end;
  350. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  351. function fpc_shortstr_compare_intern(const left,right:shortstring) : longint; external name 'fpc_shortstr_compare';
  352. function fpc_shortstr_compare_equal(const left,right:shortstring) : longint; compilerproc;
  353. begin
  354. { perform normal comparsion, because JUArrays.equals() only returns true if
  355. the arrays have equal length, while we only want to compare curlen bytes }
  356. result:=fpc_shortstr_compare_intern(left,right);
  357. end;
  358. {$define FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  359. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of AnsiChar; zerobased: boolean = true); compilerproc;
  360. var
  361. l: longint;
  362. index: longint;
  363. len: byte;
  364. foundnull: boolean;
  365. begin
  366. l:=high(arr)+1;
  367. if l>=high(res)+1 then
  368. l:=high(res)
  369. else if l<0 then
  370. l:=0;
  371. if zerobased then
  372. begin
  373. foundnull:=false;
  374. index:=0;
  375. for index:=low(arr) to l-1 do
  376. if arr[index]=#0 then
  377. begin
  378. foundnull:=true;
  379. break;
  380. end;
  381. if not foundnull then
  382. len:=l
  383. else
  384. len:=index;
  385. end
  386. else
  387. len:=l;
  388. if len>0 then
  389. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(ShortstringClass(@res).fdata),0,len);
  390. ShortstringClass(@res).curlen:=len;
  391. end;
  392. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  393. procedure fpc_shortstr_to_chararray(out res: array of AnsiChar; const src: ShortString); compilerproc;
  394. var
  395. len: longint;
  396. begin
  397. len:=length(src);
  398. if len>length(res) then
  399. len:=length(res);
  400. { make sure we don't access char 1 if length is 0 (JM) }
  401. if len>0 then
  402. JLSystem.ArrayCopy(JLObject(ShortstringClass(@src).fdata),0,JLObject(@res),0,len);
  403. if len<=high(res) then
  404. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  405. end;
  406. {****************************************************************************
  407. Str()
  408. ****************************************************************************}
  409. {$define FPC_SYSTEM_HAS_INT_STR_LONGINT}
  410. procedure int_str(l:longint;out s:shortstring);
  411. begin
  412. s:=unicodestring(JLInteger.valueOf(l).toString);
  413. end;
  414. {$define FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  415. procedure int_str_unsigned(l:longword;out s:shortstring);
  416. begin
  417. s:=unicodestring(JLLong.valueOf(l).toString);
  418. end;
  419. {$define FPC_SYSTEM_HAS_INT_STR_INT64}
  420. procedure int_str(l:int64;out s:shortstring);
  421. begin
  422. s:=unicodestring(JLLong.valueOf(l).toString);
  423. end;
  424. {$define FPC_SYSTEM_HAS_INT_STR_QWORD}
  425. procedure int_str_unsigned(l:qword;out s:shortstring);
  426. var
  427. tmp: int64;
  428. tmpstr: JLString;
  429. bi: JMBigInteger;
  430. begin
  431. tmp:=int64(l);
  432. tmpstr:=JLLong.valueOf(tmp and $7fffffffffffffff).toString;
  433. if tmp<0 then
  434. begin
  435. { no unsigned 64 bit types in Java -> use big integer to add
  436. high(int64) to the string representation }
  437. bi:=JMBigInteger.Create(tmpstr);
  438. bi:=bi.add(JMBigInteger.Create('9223372036854775808'));
  439. tmpstr:=bi.toString;
  440. end;
  441. s:=unicodestring(tmpstr);
  442. end;
  443. { lies... }
  444. {$define FPC_SYSTEM_HAS_ODD_LONGWORD}
  445. {$define FPC_SYSTEM_HAS_ODD_QWORD}
  446. {$define FPC_SYSTEM_HAS_SQR_QWORD}