jvm.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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);
  153. for i:=0 to len 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);
  164. for i:=0 to len 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);
  184. for i:=0 to len 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);
  195. for i:=0 to len 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);
  208. for i:=0 to len 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_TO_SHORTSTR}
  225. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar); compilerproc;
  226. var
  227. i, len: longint;
  228. arr: TAnsiCharArray;
  229. begin
  230. arr:=TAnsiCharArray(p);
  231. i:=0;
  232. while arr[i]<>#0 do
  233. inc(i);
  234. if i<>0 then
  235. res:=pshortstring(ShortStringClass.create(arr,min(i,high(res))))^
  236. else
  237. res:=''
  238. end;
  239. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  240. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring); compilerproc;
  241. var
  242. len: longint;
  243. begin
  244. len:=length(sstr);
  245. if len>high(res) then
  246. len:=high(res);
  247. ShortstringClass(@res).curlen:=len;
  248. if len>0 then
  249. JLSystem.ArrayCopy(JLObject(ShortstringClass(@sstr).fdata),0,JLObject(ShortstringClass(@res).fdata),0,len);
  250. end;
  251. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  252. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring); compilerproc;
  253. var
  254. s1l, s2l : integer;
  255. begin
  256. s1l:=length(s1);
  257. s2l:=length(s2);
  258. if s1l+s2l>high(s1) then
  259. s2l:=high(s1)-s1l;
  260. if s2l>0 then
  261. JLSystem.ArrayCopy(JLObject(ShortstringClass(@s2).fdata),0,JLObject(ShortstringClass(@s1).fdata),s1l,s2l);
  262. s1[0]:=chr(s1l+s2l);
  263. end;
  264. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  265. function fpc_shortstr_compare(const left,right:shortstring) : longint; compilerproc;
  266. Var
  267. MaxI,Temp, i : SizeInt;
  268. begin
  269. if ShortstringClass(@left)=ShortstringClass(@right) then
  270. begin
  271. result:=0;
  272. exit;
  273. end;
  274. Maxi:=Length(left);
  275. temp:=Length(right);
  276. If MaxI>Temp then
  277. MaxI:=Temp;
  278. if MaxI>0 then
  279. begin
  280. for i:=0 to MaxI-1 do
  281. begin
  282. result:=ord(ShortstringClass(@left).fdata[i])-ord(ShortstringClass(@right).fdata[i]);
  283. if result<>0 then
  284. exit;
  285. end;
  286. result:=Length(left)-Length(right);
  287. end
  288. else
  289. result:=Length(left)-Length(right);
  290. end;
  291. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  292. function fpc_shortstr_compare_intern(const left,right:shortstring) : longint; external name 'fpc_shortstr_compare';
  293. function fpc_shortstr_compare_equal(const left,right:shortstring) : longint; compilerproc;
  294. begin
  295. { perform normal comparsion, because JUArrays.equals() only returns true if
  296. the arrays have equal length, while we only want to compare curlen bytes }
  297. result:=fpc_shortstr_compare_intern(left,right);
  298. end;
  299. {$define FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  300. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of AnsiChar; zerobased: boolean = true); compilerproc;
  301. var
  302. l: longint;
  303. index: longint;
  304. len: byte;
  305. foundnull: boolean;
  306. begin
  307. l:=high(arr)+1;
  308. if l>=high(res)+1 then
  309. l:=high(res)
  310. else if l<0 then
  311. l:=0;
  312. if zerobased then
  313. begin
  314. foundnull:=false;
  315. for index:=low(arr) to l-1 do
  316. if arr[index]=#0 then
  317. begin
  318. foundnull:=true;
  319. break;
  320. end;
  321. if not foundnull then
  322. len:=l
  323. else
  324. len:=index;
  325. end
  326. else
  327. len:=l;
  328. if len>0 then
  329. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(ShortstringClass(@res).fdata),0,len);
  330. ShortstringClass(@res).curlen:=len;
  331. end;
  332. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  333. procedure fpc_shortstr_to_chararray(out res: array of AnsiChar; const src: ShortString); compilerproc;
  334. var
  335. len: longint;
  336. begin
  337. len:=length(src);
  338. if len>length(res) then
  339. len:=length(res);
  340. { make sure we don't access char 1 if length is 0 (JM) }
  341. if len>0 then
  342. JLSystem.ArrayCopy(JLObject(ShortstringClass(@src).fdata),0,JLObject(@res),0,len);
  343. if len<=high(res) then
  344. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  345. end;
  346. {****************************************************************************
  347. Str()
  348. ****************************************************************************}
  349. {$define FPC_SYSTEM_HAS_INT_STR_LONGINT}
  350. procedure int_str(l:longint;out s:shortstring);
  351. begin
  352. s:=unicodestring(JLInteger.valueOf(l).toString);
  353. end;
  354. {$define FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  355. procedure int_str_unsigned(l:longword;out s:shortstring);
  356. begin
  357. s:=unicodestring(JLLong.valueOf(l).toString);
  358. end;
  359. {$define FPC_SYSTEM_HAS_INT_STR_INT64}
  360. procedure int_str(l:int64;out s:shortstring);
  361. begin
  362. s:=unicodestring(JLLong.valueOf(l).toString);
  363. end;
  364. {$define FPC_SYSTEM_HAS_INT_STR_QWORD}
  365. procedure int_str_unsigned(l:qword;out s:shortstring);
  366. var
  367. tmp: int64;
  368. tmpstr: JLString;
  369. bi: JMBigInteger;
  370. begin
  371. tmp:=int64(l);
  372. tmpstr:=JLLong.valueOf(tmp and $7fffffffffffffff).toString;
  373. if tmp<0 then
  374. begin
  375. { no unsigned 64 bit types in Java -> use big integer to add
  376. high(int64) to the string representation }
  377. bi:=JMBigInteger.Create(tmpstr);
  378. bi:=bi.add(JMBigInteger.Create('9223372036854775808'));
  379. tmpstr:=bi.toString;
  380. end;
  381. s:=unicodestring(tmpstr);
  382. end;
  383. { lies... }
  384. {$define FPC_SYSTEM_HAS_ODD_LONGWORD}
  385. {$define FPC_SYSTEM_HAS_ODD_QWORD}
  386. {$define FPC_SYSTEM_HAS_SQR_QWORD}