jvm.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. {$define FPC_SYSTEM_HAS_INDEXWORD}
  153. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  154. {$define FPC_SYSTEM_HAS_INDEXQWORD}
  155. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  156. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  157. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  158. {$define FPC_SYSTEM_HAS_MOVECHAR0}
  159. {$define FPC_SYSTEM_HAS_INDEXCHAR0}
  160. {$define FPC_SYSTEM_HAS_COMPARECHAR0}
  161. {****************************************************************************
  162. String
  163. ****************************************************************************}
  164. { more lies }
  165. {$define FPC_STRTOSHORTSTRINGPROC}
  166. {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  167. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar); compilerproc;
  168. var
  169. i, len: longint;
  170. arr: TAnsiCharArray;
  171. begin
  172. arr:=TAnsiCharArray(p);
  173. i:=0;
  174. while arr[i]<>#0 do
  175. inc(i);
  176. if i<>0 then
  177. res:=pshortstring(ShortStringClass.create(arr,min(i,high(res))))^
  178. else
  179. res:=''
  180. end;
  181. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  182. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring); compilerproc;
  183. var
  184. len: longint;
  185. begin
  186. len:=length(sstr);
  187. if len>high(res) then
  188. len:=high(res);
  189. ShortstringClass(@res).curlen:=len;
  190. JLSystem.ArrayCopy(JLObject(ShortstringClass(@sstr).fdata),0,JLObject(ShortstringClass(@res).fdata),0,len);
  191. end;
  192. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  193. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring); compilerproc;
  194. var
  195. s1l, s2l : integer;
  196. begin
  197. s1l:=length(s1);
  198. s2l:=length(s2);
  199. if s1l+s2l>high(s1) then
  200. s2l:=high(s1)-s1l;
  201. JLSystem.ArrayCopy(JLObject(ShortstringClass(@s2).fdata),0,JLObject(ShortstringClass(@s1).fdata),s1l,s2l);
  202. s1[0]:=chr(s1l+s2l);
  203. end;
  204. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  205. function fpc_shortstr_compare(const left,right:shortstring) : longint; compilerproc;
  206. Var
  207. MaxI,Temp, i : SizeInt;
  208. begin
  209. if ShortstringClass(@left)=ShortstringClass(@right) then
  210. begin
  211. result:=0;
  212. exit;
  213. end;
  214. Maxi:=Length(left);
  215. temp:=Length(right);
  216. If MaxI>Temp then
  217. MaxI:=Temp;
  218. if MaxI>0 then
  219. begin
  220. for i:=0 to MaxI-1 do
  221. begin
  222. result:=ord(ShortstringClass(@left).fdata[i])-ord(ShortstringClass(@right).fdata[i]);
  223. if result<>0 then
  224. exit;
  225. end;
  226. result:=Length(left)-Length(right);
  227. end
  228. else
  229. result:=Length(left)-Length(right);
  230. end;
  231. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  232. function fpc_shortstr_compare_intern(const left,right:shortstring) : longint; external name 'fpc_shortstr_compare';
  233. function fpc_shortstr_compare_equal(const left,right:shortstring) : longint; compilerproc;
  234. begin
  235. { perform normal comparsion, because JUArrays.equals() only returns true if
  236. the arrays have equal length, while we only want to compare curlen bytes }
  237. result:=fpc_shortstr_compare_intern(left,right);
  238. end;
  239. {$define FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  240. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of AnsiChar; zerobased: boolean = true); compilerproc;
  241. var
  242. l: longint;
  243. index: longint;
  244. len: byte;
  245. foundnull: boolean;
  246. begin
  247. l:=high(arr)+1;
  248. if l>=high(res)+1 then
  249. l:=high(res)
  250. else if l<0 then
  251. l:=0;
  252. if zerobased then
  253. begin
  254. foundnull:=false;
  255. for index:=low(arr) to l-1 do
  256. if arr[index]=#0 then
  257. begin
  258. foundnull:=true;
  259. break;
  260. end;
  261. if not foundnull then
  262. len:=l
  263. else
  264. len:=index;
  265. end
  266. else
  267. len:=l;
  268. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(ShortstringClass(@res).fdata),0,len);
  269. ShortstringClass(@res).curlen:=len;
  270. end;
  271. {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  272. procedure fpc_shortstr_to_chararray(out res: array of AnsiChar; const src: ShortString); compilerproc;
  273. var
  274. len: longint;
  275. begin
  276. len:=length(src);
  277. if len>length(res) then
  278. len:=length(res);
  279. { make sure we don't access char 1 if length is 0 (JM) }
  280. if len>0 then
  281. JLSystem.ArrayCopy(JLObject(ShortstringClass(@src).fdata),0,JLObject(@res),0,len);
  282. if len<=high(res) then
  283. JUArrays.fill(TJByteArray(@res),len,high(res),0);
  284. end;
  285. {****************************************************************************
  286. Str()
  287. ****************************************************************************}
  288. {$define FPC_SYSTEM_HAS_INT_STR_LONGINT}
  289. procedure int_str(l:longint;out s:shortstring);
  290. begin
  291. s:=unicodestring(JLInteger.valueOf(l).toString);
  292. end;
  293. {$define FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  294. procedure int_str_unsigned(l:longword;out s:shortstring);
  295. begin
  296. s:=unicodestring(JLLong.valueOf(l).toString);
  297. end;
  298. {$define FPC_SYSTEM_HAS_INT_STR_INT64}
  299. procedure int_str(l:int64;out s:shortstring);
  300. begin
  301. s:=unicodestring(JLLong.valueOf(l).toString);
  302. end;
  303. {$define FPC_SYSTEM_HAS_INT_STR_QWORD}
  304. procedure int_str_unsigned(l:qword;out s:shortstring);
  305. var
  306. tmp: int64;
  307. tmpstr: JLString;
  308. bi: JMBigInteger;
  309. begin
  310. tmp:=int64(l);
  311. tmpstr:=JLLong.valueOf(tmp and $7fffffffffffffff).toString;
  312. if tmp<0 then
  313. begin
  314. { no unsigned 64 bit types in Java -> use big integer to add
  315. high(int64) to the string representation }
  316. bi:=JMBigInteger.Create(tmpstr);
  317. bi:=bi.add(JMBigInteger.Create('9223372036854775808'));
  318. tmpstr:=bi.toString;
  319. end;
  320. s:=unicodestring(tmpstr);
  321. end;
  322. { lies... }
  323. {$define FPC_SYSTEM_HAS_ODD_LONGWORD}
  324. {$define FPC_SYSTEM_HAS_ODD_QWORD}
  325. {$define FPC_SYSTEM_HAS_SQR_QWORD}