system.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. Type
  21. { The compiler has all integer types defined internally. Here
  22. we define only aliases }
  23. DWord = LongWord;
  24. Cardinal = LongWord;
  25. Integer = SmallInt;
  26. UInt64 = QWord;
  27. ValReal = Double;
  28. { map comp to int64, }
  29. Comp = Int64;
  30. HResult = type longint;
  31. { Java primitive types }
  32. jboolean = boolean;
  33. jbyte = shortint;
  34. jshort = smallint;
  35. jint = longint;
  36. jlong = int64;
  37. jchar = widechar;
  38. jfloat = single;
  39. jdouble = double;
  40. const
  41. { max. values for longint and int}
  42. maxLongint = $7fffffff;
  43. maxSmallint = 32767;
  44. maxint = maxsmallint;
  45. type
  46. { Java base class type }
  47. TObject = class external 'java.lang' name 'Object'
  48. protected
  49. function clone: TObject;
  50. public
  51. constructor create;
  52. function equals(obj: TObject): boolean;
  53. function hashcode: longint;
  54. end;
  55. { Java Float class type }
  56. TJFloat = class external 'java.lang' name 'Float'
  57. constructor create(f: jfloat);
  58. class function floatToRawIntBits(f: jfloat): jint; static;
  59. class function intBitsToFloat(j: jint): jfloat; static;
  60. end;
  61. { Java Dloat class type }
  62. TJDouble = class external 'java.lang' name 'Double'
  63. constructor create(d: jdouble);
  64. class function doubleToRawLongBits(d: jdouble): jlong; static;
  65. class function longBitsToDouble(l: jlong): jdouble; static;
  66. end;
  67. {$i innr.inc}
  68. {$i jmathh.inc}
  69. {$i jdynarrh.inc}
  70. {*****************************************************************************}
  71. implementation
  72. {*****************************************************************************}
  73. {i jdynarr.inc}
  74. {
  75. This file is part of the Free Pascal run time library.
  76. Copyright (c) 2011 by Jonas Maebe
  77. member of the Free Pascal development team.
  78. This file implements the helper routines for dyn. Arrays in FPC
  79. See the file COPYING.FPC, included in this distribution,
  80. for details about the copyright.
  81. This program is distributed in the hope that it will be useful,
  82. but WITHOUT ANY WARRANTY; without even the implied warranty of
  83. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  84. **********************************************************************
  85. }
  86. function min(a,b : longint) : longint;
  87. begin
  88. if a<=b then
  89. min:=a
  90. else
  91. min:=b;
  92. end;
  93. { copying helpers }
  94. { also for booleans }
  95. procedure copy_jbyte_array(src, dst: TJByteArray);
  96. var
  97. i: longint;
  98. begin
  99. for i:=0 to min(high(src),high(dst)) do
  100. dst[i]:=src[i];
  101. end;
  102. procedure copy_jshort_array(src, dst: TJShortArray);
  103. var
  104. i: longint;
  105. begin
  106. for i:=0 to min(high(src),high(dst)) do
  107. dst[i]:=src[i];
  108. end;
  109. procedure copy_jint_array(src, dst: TJIntArray);
  110. var
  111. i: longint;
  112. begin
  113. for i:=0 to min(high(src),high(dst)) do
  114. dst[i]:=src[i];
  115. end;
  116. procedure copy_jlong_array(src, dst: TJLongArray);
  117. var
  118. i: longint;
  119. begin
  120. for i:=0 to min(high(src),high(dst)) do
  121. dst[i]:=src[i];
  122. end;
  123. procedure copy_jchar_array(src, dst: TJCharArray);
  124. var
  125. i: longint;
  126. begin
  127. for i:=0 to min(high(src),high(dst)) do
  128. dst[i]:=src[i];
  129. end;
  130. procedure copy_jfloat_array(src, dst: TJFloatArray);
  131. var
  132. i: longint;
  133. begin
  134. for i:=0 to min(high(src),high(dst)) do
  135. dst[i]:=src[i];
  136. end;
  137. procedure copy_jdouble_array(src, dst: TJDoubleArray);
  138. var
  139. i: longint;
  140. begin
  141. for i:=0 to min(high(src),high(dst)) do
  142. dst[i]:=src[i];
  143. end;
  144. procedure copy_jobject_array(src, dst: TJObjectArray);
  145. var
  146. i: longint;
  147. begin
  148. for i:=0 to min(high(src),high(dst)) do
  149. dst[i]:=src[i];
  150. end;
  151. { 1-dimensional setlength routines }
  152. function fpc_setlength_dynarr_jbyte(aorg, anew: TJByteArray; deepcopy: boolean): TJByteArray;
  153. begin
  154. if deepcopy or
  155. (length(aorg)<>length(anew)) then
  156. begin
  157. copy_jbyte_array(aorg,anew);
  158. result:=anew
  159. end
  160. else
  161. result:=aorg;
  162. end;
  163. function fpc_setlength_dynarr_jshort(aorg, anew: TJShortArray; deepcopy: boolean): TJShortArray;
  164. begin
  165. if deepcopy or
  166. (length(aorg)<>length(anew)) then
  167. begin
  168. copy_jshort_array(aorg,anew);
  169. result:=anew
  170. end
  171. else
  172. result:=aorg;
  173. end;
  174. function fpc_setlength_dynarr_jint(aorg, anew: TJIntArray; deepcopy: boolean): TJIntArray;
  175. begin
  176. if deepcopy or
  177. (length(aorg)<>length(anew)) then
  178. begin
  179. copy_jint_array(aorg,anew);
  180. result:=anew
  181. end
  182. else
  183. result:=aorg;
  184. end;
  185. function fpc_setlength_dynarr_jlong(aorg, anew: TJLongArray; deepcopy: boolean): TJLongArray;
  186. begin
  187. if deepcopy or
  188. (length(aorg)<>length(anew)) then
  189. begin
  190. copy_jlong_array(aorg,anew);
  191. result:=anew
  192. end
  193. else
  194. result:=aorg;
  195. end;
  196. function fpc_setlength_dynarr_jchar(aorg, anew: TJCharArray; deepcopy: boolean): TJCharArray;
  197. begin
  198. if deepcopy or
  199. (length(aorg)<>length(anew)) then
  200. begin
  201. copy_jchar_array(aorg,anew);
  202. result:=anew
  203. end
  204. else
  205. result:=aorg;
  206. end;
  207. function fpc_setlength_dynarr_jfloat(aorg, anew: TJFloatArray; deepcopy: boolean): TJFloatArray;
  208. begin
  209. if deepcopy or
  210. (length(aorg)<>length(anew)) then
  211. begin
  212. copy_jfloat_array(aorg,anew);
  213. result:=anew
  214. end
  215. else
  216. result:=aorg;
  217. end;
  218. function fpc_setlength_dynarr_jdouble(aorg, anew: TJDoubleArray; deepcopy: boolean): TJDoubleArray;
  219. begin
  220. if deepcopy or
  221. (length(aorg)<>length(anew)) then
  222. begin
  223. copy_jdouble_array(aorg,anew);
  224. result:=anew
  225. end
  226. else
  227. result:=aorg;
  228. end;
  229. function fpc_setlength_dynarr_jobject(aorg, anew: TJObjectArray; deepcopy: boolean; docopy : boolean = true): TJObjectArray;
  230. begin
  231. if deepcopy or
  232. (length(aorg)<>length(anew)) then
  233. begin
  234. if docopy then
  235. copy_jobject_array(aorg,anew);
  236. result:=anew
  237. end
  238. else
  239. result:=aorg;
  240. end;
  241. { multi-dimensional setlength routine }
  242. function fpc_setlength_dynarr_multidim(aorg, anew: TJObjectArray; deepcopy: boolean; ndim: longint; eletype: jchar): TJObjectArray;
  243. var
  244. partdone,
  245. i: longint;
  246. begin
  247. { resize the current dimension; no need to copy the subarrays of the old
  248. array, as the subarrays will be (re-)initialised immediately below }
  249. result:=fpc_setlength_dynarr_jobject(aorg,anew,deepcopy,false);
  250. { if aorg was empty, there's nothing else to do since result will now
  251. contain anew, of which all other dimensions are already initialised
  252. correctly since there are no aorg elements to copy }
  253. if not assigned(aorg) and
  254. not deepcopy then
  255. exit;
  256. partdone:=min(high(result),high(aorg));
  257. { ndim must be >=2 when this routine is called, since it has to return
  258. an array of java.lang.Object! (arrays are also objects, but primitive
  259. types are not) }
  260. if ndim=2 then
  261. begin
  262. { final dimension -> copy the primitive arrays }
  263. case eletype of
  264. FPCJDynArrTypeJByte:
  265. begin
  266. for i:=low(result) to partdone do
  267. result[i]:=TObject(fpc_setlength_dynarr_jbyte(TJByteArray(aorg[i]),TJByteArray(anew[i]),deepcopy));
  268. for i:=succ(partdone) to high(result) do
  269. result[i]:=TObject(fpc_setlength_dynarr_jbyte(nil,TJByteArray(anew[i]),deepcopy));
  270. end;
  271. FPCJDynArrTypeJShort:
  272. begin
  273. for i:=low(result) to partdone do
  274. result[i]:=TObject(fpc_setlength_dynarr_jshort(TJShortArray(aorg[i]),TJShortArray(anew[i]),deepcopy));
  275. for i:=succ(partdone) to high(result) do
  276. result[i]:=TObject(fpc_setlength_dynarr_jshort(nil,TJShortArray(anew[i]),deepcopy));
  277. end;
  278. FPCJDynArrTypeJInt:
  279. begin
  280. for i:=low(result) to partdone do
  281. result[i]:=TObject(fpc_setlength_dynarr_jint(TJIntArray(aorg[i]),TJIntArray(anew[i]),deepcopy));
  282. for i:=succ(partdone) to high(result) do
  283. result[i]:=TObject(fpc_setlength_dynarr_jint(nil,TJIntArray(anew[i]),deepcopy));
  284. end;
  285. FPCJDynArrTypeJLong:
  286. begin
  287. for i:=low(result) to partdone do
  288. result[i]:=TObject(fpc_setlength_dynarr_jlong(TJLongArray(aorg[i]),TJLongArray(anew[i]),deepcopy));
  289. for i:=succ(partdone) to high(result) do
  290. result[i]:=TObject(fpc_setlength_dynarr_jlong(nil,TJLongArray(anew[i]),deepcopy));
  291. end;
  292. FPCJDynArrTypeJChar:
  293. begin
  294. for i:=low(result) to partdone do
  295. result[i]:=TObject(fpc_setlength_dynarr_jchar(TJCharArray(aorg[i]),TJCharArray(anew[i]),deepcopy));
  296. for i:=succ(partdone) to high(result) do
  297. result[i]:=TObject(fpc_setlength_dynarr_jchar(nil,TJCharArray(anew[i]),deepcopy));
  298. end;
  299. FPCJDynArrTypeJFloat:
  300. begin
  301. for i:=low(result) to partdone do
  302. result[i]:=TObject(fpc_setlength_dynarr_jfloat(TJFloatArray(aorg[i]),TJFloatArray(anew[i]),deepcopy));
  303. for i:=succ(partdone) to high(result) do
  304. result[i]:=TObject(fpc_setlength_dynarr_jfloat(nil,TJFloatArray(anew[i]),deepcopy));
  305. end;
  306. FPCJDynArrTypeJDouble:
  307. begin
  308. for i:=low(result) to partdone do
  309. result[i]:=TObject(fpc_setlength_dynarr_jdouble(TJDoubleArray(aorg[i]),TJDoubleArray(anew[i]),deepcopy));
  310. for i:=succ(partdone) to high(result) do
  311. result[i]:=TObject(fpc_setlength_dynarr_jdouble(nil,TJDoubleArray(anew[i]),deepcopy));
  312. end;
  313. FPCJDynArrTypeJObject:
  314. begin
  315. for i:=low(result) to partdone do
  316. result[i]:=TObject(fpc_setlength_dynarr_jobject(TJObjectArray(aorg[i]),TJObjectArray(anew[i]),deepcopy,true));
  317. for i:=succ(partdone) to high(result) do
  318. result[i]:=TObject(fpc_setlength_dynarr_jobject(nil,TJObjectArray(anew[i]),deepcopy,true));
  319. end;
  320. end;
  321. end
  322. else
  323. begin
  324. { recursively handle the next dimension }
  325. for i:=low(result) to partdone do
  326. result[i]:=TObject(fpc_setlength_dynarr_multidim(TJObjectArray(aorg[i]),TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
  327. for i:=succ(partdone) to high(result) do
  328. result[i]:=TObject(fpc_setlength_dynarr_multidim(nil,TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
  329. end;
  330. end;
  331. {i jdynarr.inc end}
  332. {*****************************************************************************
  333. Misc. System Dependent Functions
  334. *****************************************************************************}
  335. {*****************************************************************************
  336. SystemUnit Initialization
  337. *****************************************************************************}
  338. end.