jvm.inc 14 KB

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