system.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2006 by Florian Klaempfl
  4. member of the Free Pascal development team.
  5. System unit for embedded systems
  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. Unit system;
  13. {*****************************************************************************}
  14. interface
  15. {*****************************************************************************}
  16. {$define FPC_IS_SYSTEM}
  17. {$I-,Q-,H-,R-,V-}
  18. {$implicitexceptions off}
  19. {$mode objfpc}
  20. {$undef FPC_HAS_FEATURE_RTTI}
  21. Type
  22. { The compiler has all integer types defined internally. Here
  23. we define only aliases }
  24. DWord = LongWord;
  25. Cardinal = LongWord;
  26. Integer = SmallInt;
  27. UInt64 = QWord;
  28. ValReal = Double;
  29. { map comp to int64, }
  30. Comp = Int64;
  31. HResult = type longint;
  32. { Java primitive types }
  33. jboolean = boolean;
  34. jbyte = shortint;
  35. jshort = smallint;
  36. jint = longint;
  37. jlong = int64;
  38. jchar = widechar;
  39. jfloat = single;
  40. jdouble = double;
  41. Arr1jboolean = array of jboolean;
  42. Arr1jbyte = array of jbyte;
  43. Arr1jshort = array of jshort;
  44. Arr1jint = array of jint;
  45. Arr1jlong = array of jlong;
  46. Arr1jchar = array of jchar;
  47. Arr1jfloat = array of jfloat;
  48. Arr1jdouble = array of jdouble;
  49. Arr2jboolean = array of Arr1jboolean;
  50. Arr2jbyte = array of Arr1jbyte;
  51. Arr2jshort = array of Arr1jshort;
  52. Arr2jint = array of Arr1jint;
  53. Arr2jlong = array of Arr1jlong;
  54. Arr2jchar = array of Arr1jchar;
  55. Arr2jfloat = array of Arr1jfloat;
  56. Arr2jdouble = array of Arr1jdouble;
  57. Arr3jboolean = array of Arr2jboolean;
  58. Arr3jbyte = array of Arr2jbyte;
  59. Arr3jshort = array of Arr2jshort;
  60. Arr3jint = array of Arr2jint;
  61. Arr3jlong = array of Arr2jlong;
  62. Arr3jchar = array of Arr2jchar;
  63. Arr3jfloat = array of Arr2jfloat;
  64. Arr3jdouble = array of Arr2jdouble;
  65. const
  66. { max. values for longint and int}
  67. maxLongint = $7fffffff;
  68. maxSmallint = 32767;
  69. maxint = maxsmallint;
  70. { Java base class type }
  71. {$i java_sysh.inc}
  72. {$i java_sys.inc}
  73. type
  74. TObject = class(JLObject)
  75. strict private
  76. DestructorCalled: Boolean;
  77. public
  78. procedure Free;
  79. destructor Destroy; virtual;
  80. procedure finalize; override;
  81. end;
  82. {$i innr.inc}
  83. {$i jmathh.inc}
  84. {$i jrech.inc}
  85. {$i jdynarrh.inc}
  86. {$i compproc.inc}
  87. {*****************************************************************************}
  88. implementation
  89. {*****************************************************************************}
  90. {i jdynarr.inc}
  91. {
  92. This file is part of the Free Pascal run time library.
  93. Copyright (c) 2011 by Jonas Maebe
  94. member of the Free Pascal development team.
  95. This file implements the helper routines for dyn. Arrays in FPC
  96. See the file COPYING.FPC, included in this distribution,
  97. for details about the copyright.
  98. This program is distributed in the hope that it will be useful,
  99. but WITHOUT ANY WARRANTY; without even the implied warranty of
  100. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  101. **********************************************************************
  102. }
  103. {$i rtti.inc}
  104. {$i jrec.inc}
  105. function min(a,b : longint) : longint;
  106. begin
  107. if a<=b then
  108. min:=a
  109. else
  110. min:=b;
  111. end;
  112. { copying helpers }
  113. { also for booleans }
  114. procedure fpc_copy_jbyte_array(src, dst: TJByteArray);
  115. var
  116. i: longint;
  117. begin
  118. for i:=0 to min(high(src),high(dst)) do
  119. dst[i]:=src[i];
  120. end;
  121. procedure fpc_copy_jshort_array(src, dst: TJShortArray);
  122. var
  123. i: longint;
  124. begin
  125. for i:=0 to min(high(src),high(dst)) do
  126. dst[i]:=src[i];
  127. end;
  128. procedure fpc_copy_jint_array(src, dst: TJIntArray);
  129. var
  130. i: longint;
  131. begin
  132. for i:=0 to min(high(src),high(dst)) do
  133. dst[i]:=src[i];
  134. end;
  135. procedure fpc_copy_jlong_array(src, dst: TJLongArray);
  136. var
  137. i: longint;
  138. begin
  139. for i:=0 to min(high(src),high(dst)) do
  140. dst[i]:=src[i];
  141. end;
  142. procedure fpc_copy_jchar_array(src, dst: TJCharArray);
  143. var
  144. i: longint;
  145. begin
  146. for i:=0 to min(high(src),high(dst)) do
  147. dst[i]:=src[i];
  148. end;
  149. procedure fpc_copy_jfloat_array(src, dst: TJFloatArray);
  150. var
  151. i: longint;
  152. begin
  153. for i:=0 to min(high(src),high(dst)) do
  154. dst[i]:=src[i];
  155. end;
  156. procedure fpc_copy_jdouble_array(src, dst: TJDoubleArray);
  157. var
  158. i: longint;
  159. begin
  160. for i:=0 to min(high(src),high(dst)) do
  161. dst[i]:=src[i];
  162. end;
  163. procedure fpc_copy_jobject_array(src, dst: TJObjectArray);
  164. var
  165. i: longint;
  166. begin
  167. for i:=0 to min(high(src),high(dst)) do
  168. dst[i]:=src[i];
  169. end;
  170. procedure fpc_copy_jrecord_array(src, dst: TJRecordArray);
  171. var
  172. i: longint;
  173. begin
  174. for i:=0 to min(high(src),high(dst)) do
  175. dst[i]:=FpcBaseRecordType(src[i].clone);
  176. end;
  177. { 1-dimensional setlength routines }
  178. function fpc_setlength_dynarr_jbyte(aorg, anew: TJByteArray; deepcopy: boolean): TJByteArray;
  179. begin
  180. if deepcopy or
  181. (length(aorg)<>length(anew)) then
  182. begin
  183. fpc_copy_jbyte_array(aorg,anew);
  184. result:=anew
  185. end
  186. else
  187. result:=aorg;
  188. end;
  189. function fpc_setlength_dynarr_jshort(aorg, anew: TJShortArray; deepcopy: boolean): TJShortArray;
  190. begin
  191. if deepcopy or
  192. (length(aorg)<>length(anew)) then
  193. begin
  194. fpc_copy_jshort_array(aorg,anew);
  195. result:=anew
  196. end
  197. else
  198. result:=aorg;
  199. end;
  200. function fpc_setlength_dynarr_jint(aorg, anew: TJIntArray; deepcopy: boolean): TJIntArray;
  201. begin
  202. if deepcopy or
  203. (length(aorg)<>length(anew)) then
  204. begin
  205. fpc_copy_jint_array(aorg,anew);
  206. result:=anew
  207. end
  208. else
  209. result:=aorg;
  210. end;
  211. function fpc_setlength_dynarr_jlong(aorg, anew: TJLongArray; deepcopy: boolean): TJLongArray;
  212. begin
  213. if deepcopy or
  214. (length(aorg)<>length(anew)) then
  215. begin
  216. fpc_copy_jlong_array(aorg,anew);
  217. result:=anew
  218. end
  219. else
  220. result:=aorg;
  221. end;
  222. function fpc_setlength_dynarr_jchar(aorg, anew: TJCharArray; deepcopy: boolean): TJCharArray;
  223. begin
  224. if deepcopy or
  225. (length(aorg)<>length(anew)) then
  226. begin
  227. fpc_copy_jchar_array(aorg,anew);
  228. result:=anew
  229. end
  230. else
  231. result:=aorg;
  232. end;
  233. function fpc_setlength_dynarr_jfloat(aorg, anew: TJFloatArray; deepcopy: boolean): TJFloatArray;
  234. begin
  235. if deepcopy or
  236. (length(aorg)<>length(anew)) then
  237. begin
  238. fpc_copy_jfloat_array(aorg,anew);
  239. result:=anew
  240. end
  241. else
  242. result:=aorg;
  243. end;
  244. function fpc_setlength_dynarr_jdouble(aorg, anew: TJDoubleArray; deepcopy: boolean): TJDoubleArray;
  245. begin
  246. if deepcopy or
  247. (length(aorg)<>length(anew)) then
  248. begin
  249. fpc_copy_jdouble_array(aorg,anew);
  250. result:=anew
  251. end
  252. else
  253. result:=aorg;
  254. end;
  255. function fpc_setlength_dynarr_jobject(aorg, anew: TJObjectArray; deepcopy: boolean; docopy : boolean = true): TJObjectArray;
  256. begin
  257. if deepcopy or
  258. (length(aorg)<>length(anew)) then
  259. begin
  260. if docopy then
  261. fpc_copy_jobject_array(aorg,anew);
  262. result:=anew
  263. end
  264. else
  265. result:=aorg;
  266. end;
  267. function fpc_setlength_dynarr_jrecord(aorg, anew: TJRecordArray; deepcopy: boolean): TJRecordArray;
  268. begin
  269. if deepcopy or
  270. (length(aorg)<>length(anew)) then
  271. begin
  272. fpc_copy_jrecord_array(aorg,anew);
  273. result:=anew
  274. end
  275. else
  276. result:=aorg;
  277. end;
  278. { multi-dimensional setlength routine }
  279. function fpc_setlength_dynarr_multidim(aorg, anew: TJObjectArray; deepcopy: boolean; ndim: longint; eletype: jchar): TJObjectArray;
  280. var
  281. partdone,
  282. i: longint;
  283. begin
  284. { resize the current dimension; no need to copy the subarrays of the old
  285. array, as the subarrays will be (re-)initialised immediately below }
  286. result:=fpc_setlength_dynarr_jobject(aorg,anew,deepcopy,false);
  287. { if aorg was empty, there's nothing else to do since result will now
  288. contain anew, of which all other dimensions are already initialised
  289. correctly since there are no aorg elements to copy }
  290. if not assigned(aorg) and
  291. not deepcopy then
  292. exit;
  293. partdone:=min(high(result),high(aorg));
  294. { ndim must be >=2 when this routine is called, since it has to return
  295. an array of java.lang.Object! (arrays are also objects, but primitive
  296. types are not) }
  297. if ndim=2 then
  298. begin
  299. { final dimension -> copy the primitive arrays }
  300. case eletype of
  301. FPCJDynArrTypeJByte:
  302. begin
  303. for i:=low(result) to partdone do
  304. result[i]:=JLObject(fpc_setlength_dynarr_jbyte(TJByteArray(aorg[i]),TJByteArray(anew[i]),deepcopy));
  305. for i:=succ(partdone) to high(result) do
  306. result[i]:=JLObject(fpc_setlength_dynarr_jbyte(nil,TJByteArray(anew[i]),deepcopy));
  307. end;
  308. FPCJDynArrTypeJShort:
  309. begin
  310. for i:=low(result) to partdone do
  311. result[i]:=JLObject(fpc_setlength_dynarr_jshort(TJShortArray(aorg[i]),TJShortArray(anew[i]),deepcopy));
  312. for i:=succ(partdone) to high(result) do
  313. result[i]:=JLObject(fpc_setlength_dynarr_jshort(nil,TJShortArray(anew[i]),deepcopy));
  314. end;
  315. FPCJDynArrTypeJInt:
  316. begin
  317. for i:=low(result) to partdone do
  318. result[i]:=JLObject(fpc_setlength_dynarr_jint(TJIntArray(aorg[i]),TJIntArray(anew[i]),deepcopy));
  319. for i:=succ(partdone) to high(result) do
  320. result[i]:=JLObject(fpc_setlength_dynarr_jint(nil,TJIntArray(anew[i]),deepcopy));
  321. end;
  322. FPCJDynArrTypeJLong:
  323. begin
  324. for i:=low(result) to partdone do
  325. result[i]:=JLObject(fpc_setlength_dynarr_jlong(TJLongArray(aorg[i]),TJLongArray(anew[i]),deepcopy));
  326. for i:=succ(partdone) to high(result) do
  327. result[i]:=JLObject(fpc_setlength_dynarr_jlong(nil,TJLongArray(anew[i]),deepcopy));
  328. end;
  329. FPCJDynArrTypeJChar:
  330. begin
  331. for i:=low(result) to partdone do
  332. result[i]:=JLObject(fpc_setlength_dynarr_jchar(TJCharArray(aorg[i]),TJCharArray(anew[i]),deepcopy));
  333. for i:=succ(partdone) to high(result) do
  334. result[i]:=JLObject(fpc_setlength_dynarr_jchar(nil,TJCharArray(anew[i]),deepcopy));
  335. end;
  336. FPCJDynArrTypeJFloat:
  337. begin
  338. for i:=low(result) to partdone do
  339. result[i]:=JLObject(fpc_setlength_dynarr_jfloat(TJFloatArray(aorg[i]),TJFloatArray(anew[i]),deepcopy));
  340. for i:=succ(partdone) to high(result) do
  341. result[i]:=JLObject(fpc_setlength_dynarr_jfloat(nil,TJFloatArray(anew[i]),deepcopy));
  342. end;
  343. FPCJDynArrTypeJDouble:
  344. begin
  345. for i:=low(result) to partdone do
  346. result[i]:=JLObject(fpc_setlength_dynarr_jdouble(TJDoubleArray(aorg[i]),TJDoubleArray(anew[i]),deepcopy));
  347. for i:=succ(partdone) to high(result) do
  348. result[i]:=JLObject(fpc_setlength_dynarr_jdouble(nil,TJDoubleArray(anew[i]),deepcopy));
  349. end;
  350. FPCJDynArrTypeJObject:
  351. begin
  352. for i:=low(result) to partdone do
  353. result[i]:=JLObject(fpc_setlength_dynarr_jobject(TJObjectArray(aorg[i]),TJObjectArray(anew[i]),deepcopy,true));
  354. for i:=succ(partdone) to high(result) do
  355. result[i]:=JLObject(fpc_setlength_dynarr_jobject(nil,TJObjectArray(anew[i]),deepcopy,true));
  356. end;
  357. FPCJDynArrTypeRecord:
  358. begin
  359. for i:=low(result) to partdone do
  360. result[i]:=JLObject(fpc_setlength_dynarr_jrecord(TJRecordArray(aorg[i]),TJRecordArray(anew[i]),deepcopy));
  361. for i:=succ(partdone) to high(result) do
  362. result[i]:=JLObject(fpc_setlength_dynarr_jrecord(nil,TJRecordArray(anew[i]),deepcopy));
  363. end;
  364. end;
  365. end
  366. else
  367. begin
  368. { recursively handle the next dimension }
  369. for i:=low(result) to partdone do
  370. result[i]:=JLObject(fpc_setlength_dynarr_multidim(TJObjectArray(aorg[i]),TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
  371. for i:=succ(partdone) to high(result) do
  372. result[i]:=JLObject(fpc_setlength_dynarr_multidim(nil,TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
  373. end;
  374. end;
  375. {i jdynarr.inc end}
  376. {*****************************************************************************
  377. Misc. System Dependent Functions
  378. *****************************************************************************}
  379. procedure TObject.Free;
  380. begin
  381. if not DestructorCalled then
  382. begin
  383. DestructorCalled:=true;
  384. Destroy;
  385. end;
  386. end;
  387. destructor TObject.Destroy;
  388. begin
  389. end;
  390. procedure TObject.Finalize;
  391. begin
  392. Free;
  393. end;
  394. {*****************************************************************************
  395. SystemUnit Initialization
  396. *****************************************************************************}
  397. end.