jvm.inc 12 KB

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