jvm.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. {$pop}
  143. {$define FPC_SYSTEM_HAS_FILLWORD}
  144. {$define FPC_SYSTEM_HAS_FILLDWORD}
  145. {$define FPC_SYSTEM_HAS_FILLQWORD}
  146. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  147. function IndexByte(const buf: array of jbyte;len:SizeInt;b:jbyte):SizeInt;
  148. var
  149. i: SizeInt;
  150. begin
  151. if len<0 then
  152. len:=high(buf)+1;
  153. for i:=0 to len-1 do
  154. if buf[i]=b then
  155. exit(i);
  156. IndexByte:=-1;
  157. end;
  158. function IndexByte(const buf: array of boolean;len:SizeInt;b:jbyte):SizeInt;
  159. var
  160. i: SizeInt;
  161. begin
  162. if len<0 then
  163. len:=high(buf)+1;
  164. for i:=0 to len-1 do
  165. if jbyte(buf[i])=b then
  166. exit(i);
  167. IndexByte:=-1;
  168. end;
  169. function IndexChar(const buf: array of boolean;len:SizeInt;b:ansichar):SizeInt;
  170. begin
  171. IndexChar:=IndexByte(buf,len,jbyte(b));
  172. end;
  173. function IndexChar(const buf: array of jbyte;len:SizeInt;b:ansichar):SizeInt;
  174. begin
  175. IndexChar:=IndexByte(buf,len,jbyte(b));
  176. end;
  177. {$define FPC_SYSTEM_HAS_INDEXWORD}
  178. function IndexWord(const buf: array of jshort;len:SizeInt;b:jshort):SizeInt;
  179. var
  180. i: SizeInt;
  181. begin
  182. if len<0 then
  183. len:=high(buf)+1;
  184. for i:=0 to len-1 do
  185. if buf[i]=b then
  186. exit(i);
  187. IndexWord:=-1;
  188. end;
  189. function IndexWord(const buf: array of jchar;len:SizeInt;b:jchar):SizeInt;
  190. var
  191. i: SizeInt;
  192. begin
  193. if len<0 then
  194. len:=high(buf)+1;
  195. for i:=0 to len-1 do
  196. if buf[i]=b then
  197. exit(i);
  198. IndexWord:=-1;
  199. end;
  200. function IndexWord(const buf: array of jchar;len:SizeInt;b:jshort):SizeInt;
  201. var
  202. i: SizeInt;
  203. c: jchar;
  204. begin
  205. c:=jchar(b);
  206. if len<0 then
  207. len:=high(buf)+1;
  208. for i:=0 to len-1 do
  209. if buf[i]=c then
  210. exit(i);
  211. IndexWord:=-1;
  212. end;
  213. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  214. {$define FPC_SYSTEM_HAS_INDEXQWORD}
  215. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  216. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  217. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  218. {$define FPC_SYSTEM_HAS_MOVECHAR0}
  219. {$define FPC_SYSTEM_HAS_INDEXCHAR0}
  220. {$define FPC_SYSTEM_HAS_COMPARECHAR0}
  221. {****************************************************************************
  222. String
  223. ****************************************************************************}
  224. {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  225. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  226. begin
  227. if assigned(p) then
  228. Result:=IndexByte(TAnsiCharArray(p),high(Result),0)
  229. else
  230. Result:=0;
  231. end;
  232. {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  233. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar); compilerproc;
  234. var
  235. i, len: longint;
  236. arr: TAnsiCharArray;
  237. begin
  238. arr:=TAnsiCharArray(p);
  239. i:=0;
  240. while arr[i]<>#0 do
  241. inc(i);
  242. if i<>0 then
  243. res:=pshortstring(ShortStringClass.create(arr,min(i,high(res))))^
  244. else
  245. res:=''
  246. end;
  247. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  248. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring); compilerproc;
  249. var
  250. len: longint;
  251. begin
  252. len:=length(sstr);
  253. if len>high(res) then
  254. len:=high(res);
  255. ShortstringClass(@res).curlen:=len;
  256. if len>0 then
  257. JLSystem.ArrayCopy(JLObject(ShortstringClass(@sstr).fdata),0,JLObject(ShortstringClass(@res).fdata),0,len);
  258. end;
  259. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  260. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring); compilerproc;
  261. var
  262. s1l, s2l : integer;
  263. begin
  264. s1l:=length(s1);
  265. s2l:=length(s2);
  266. if s1l+s2l>high(s1) then
  267. s2l:=high(s1)-s1l;
  268. if s2l>0 then
  269. JLSystem.ArrayCopy(JLObject(ShortstringClass(@s2).fdata),0,JLObject(ShortstringClass(@s1).fdata),s1l,s2l);
  270. s1[0]:=chr(s1l+s2l);
  271. end;
  272. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  273. function fpc_shortstr_compare(const left,right:shortstring) : longint; compilerproc;
  274. Var
  275. MaxI,Temp, i : SizeInt;
  276. begin
  277. if ShortstringClass(@left)=ShortstringClass(@right) then
  278. begin
  279. result:=0;
  280. exit;
  281. end;
  282. Maxi:=Length(left);
  283. temp:=Length(right);
  284. If MaxI>Temp then
  285. MaxI:=Temp;
  286. if MaxI>0 then
  287. begin
  288. for i:=0 to MaxI-1 do
  289. begin
  290. result:=ord(ShortstringClass(@left).fdata[i])-ord(ShortstringClass(@right).fdata[i]);
  291. if result<>0 then
  292. exit;
  293. end;
  294. result:=Length(left)-Length(right);
  295. end
  296. else
  297. result:=Length(left)-Length(right);
  298. end;
  299. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  300. function fpc_shortstr_compare_intern(const left,right:shortstring) : longint; external name 'fpc_shortstr_compare';
  301. function fpc_shortstr_compare_equal(const left,right:shortstring) : longint; compilerproc;
  302. begin
  303. { perform normal comparsion, because JUArrays.equals() only returns true if
  304. the arrays have equal length, while we only want to compare curlen bytes }
  305. result:=fpc_shortstr_compare_intern(left,right);
  306. end;
  307. {$define FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  308. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of AnsiChar; zerobased: boolean = true); compilerproc;
  309. var
  310. l: longint;
  311. index: longint;
  312. len: byte;
  313. foundnull: boolean;
  314. begin
  315. l:=high(arr)+1;
  316. if l>=high(res)+1 then
  317. l:=high(res)
  318. else if l<0 then
  319. l:=0;
  320. if zerobased then
  321. begin
  322. foundnull:=false;
  323. index:=0;
  324. for index:=low(arr) to l-1 do
  325. if arr[index]=#0 then
  326. begin
  327. foundnull:=true;
  328. break;
  329. end;
  330. if not foundnull then
  331. len:=l
  332. else
  333. len:=index;
  334. end
  335. else
  336. len:=l;
  337. if len>0 then
  338. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(ShortstringClass(@res).fdata),0,len);
  339. ShortstringClass(@res).curlen:=len;
  340. end;
  341. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  342. procedure fpc_shortstr_to_chararray(out res: array of AnsiChar; const src: ShortString); compilerproc;
  343. var
  344. len: longint;
  345. begin
  346. len:=length(src);
  347. if len>length(res) then
  348. len:=length(res);
  349. { make sure we don't access char 1 if length is 0 (JM) }
  350. if len>0 then
  351. JLSystem.ArrayCopy(JLObject(ShortstringClass(@src).fdata),0,JLObject(@res),0,len);
  352. if len<=high(res) then
  353. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  354. end;
  355. {****************************************************************************
  356. Str()
  357. ****************************************************************************}
  358. {$define FPC_SYSTEM_HAS_INT_STR_LONGINT}
  359. procedure int_str(l:longint;out s:shortstring);
  360. begin
  361. s:=unicodestring(JLInteger.valueOf(l).toString);
  362. end;
  363. {$define FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  364. procedure int_str_unsigned(l:longword;out s:shortstring);
  365. begin
  366. s:=unicodestring(JLLong.valueOf(l).toString);
  367. end;
  368. {$define FPC_SYSTEM_HAS_INT_STR_INT64}
  369. procedure int_str(l:int64;out s:shortstring);
  370. begin
  371. s:=unicodestring(JLLong.valueOf(l).toString);
  372. end;
  373. {$define FPC_SYSTEM_HAS_INT_STR_QWORD}
  374. procedure int_str_unsigned(l:qword;out s:shortstring);
  375. var
  376. tmp: int64;
  377. tmpstr: JLString;
  378. bi: JMBigInteger;
  379. begin
  380. tmp:=int64(l);
  381. tmpstr:=JLLong.valueOf(tmp and $7fffffffffffffff).toString;
  382. if tmp<0 then
  383. begin
  384. { no unsigned 64 bit types in Java -> use big integer to add
  385. high(int64) to the string representation }
  386. bi:=JMBigInteger.Create(tmpstr);
  387. bi:=bi.add(JMBigInteger.Create('9223372036854775808'));
  388. tmpstr:=bi.toString;
  389. end;
  390. s:=unicodestring(tmpstr);
  391. end;
  392. { lies... }
  393. {$define FPC_SYSTEM_HAS_ODD_LONGWORD}
  394. {$define FPC_SYSTEM_HAS_ODD_QWORD}
  395. {$define FPC_SYSTEM_HAS_SQR_QWORD}