system.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. {$namespace org.freepascal.rtl}
  14. {*****************************************************************************}
  15. interface
  16. {*****************************************************************************}
  17. {$define FPC_IS_SYSTEM}
  18. {$I-,Q-,H-,R-,V-,P+}
  19. {$implicitexceptions off}
  20. {$mode objfpc}
  21. {$undef FPC_HAS_FEATURE_ANSISTRINGS}
  22. {$undef FPC_HAS_FEATURE_TEXTIO}
  23. {$undef FPC_HAS_FEATURE_VARIANTS}
  24. {$undef FPC_HAS_FEATURE_CLASSES}
  25. {$undef FPC_HAS_FEATURE_EXCEPTIONS}
  26. {$undef FPC_HAS_FEATURE_OBJECTS}
  27. {$undef FPC_HAS_FEATURE_RTTI}
  28. {$undef FPC_HAS_FEATURE_FILEIO}
  29. {$undef FPC_INCLUDE_SOFTWARE_INT64_TO_DOUBLE}
  30. Type
  31. { The compiler has all integer types defined internally. Here
  32. we define only aliases }
  33. DWord = LongWord;
  34. Cardinal = LongWord;
  35. Integer = SmallInt;
  36. UInt64 = QWord;
  37. SizeInt = Longint;
  38. SizeUInt = Longint;
  39. PtrInt = Longint;
  40. PtrUInt = Longint;
  41. ValReal = Double;
  42. AnsiChar = Char;
  43. UnicodeChar = WideChar;
  44. { map comp to int64, }
  45. Comp = Int64;
  46. HResult = type longint;
  47. { Java primitive types }
  48. jboolean = boolean;
  49. jbyte = shortint;
  50. jshort = smallint;
  51. jint = longint;
  52. jlong = int64;
  53. jchar = widechar;
  54. jfloat = single;
  55. jdouble = double;
  56. Arr1jboolean = array of jboolean;
  57. Arr1jbyte = array of jbyte;
  58. Arr1jshort = array of jshort;
  59. Arr1jint = array of jint;
  60. Arr1jlong = array of jlong;
  61. Arr1jchar = array of jchar;
  62. Arr1jfloat = array of jfloat;
  63. Arr1jdouble = array of jdouble;
  64. Arr2jboolean = array of Arr1jboolean;
  65. Arr2jbyte = array of Arr1jbyte;
  66. Arr2jshort = array of Arr1jshort;
  67. Arr2jint = array of Arr1jint;
  68. Arr2jlong = array of Arr1jlong;
  69. Arr2jchar = array of Arr1jchar;
  70. Arr2jfloat = array of Arr1jfloat;
  71. Arr2jdouble = array of Arr1jdouble;
  72. Arr3jboolean = array of Arr2jboolean;
  73. Arr3jbyte = array of Arr2jbyte;
  74. Arr3jshort = array of Arr2jshort;
  75. Arr3jint = array of Arr2jint;
  76. Arr3jlong = array of Arr2jlong;
  77. Arr3jchar = array of Arr2jchar;
  78. Arr3jfloat = array of Arr2jfloat;
  79. Arr3jdouble = array of Arr2jdouble;
  80. const
  81. { max. values for longint and int}
  82. maxLongint = $7fffffff;
  83. maxSmallint = 32767;
  84. maxint = maxsmallint;
  85. { Java base class type }
  86. {$i java_sysh.inc}
  87. {$i java_sys.inc}
  88. type
  89. TObject = class(JLObject)
  90. strict private
  91. DestructorCalled: Boolean;
  92. public
  93. procedure Free;
  94. destructor Destroy; virtual;
  95. procedure finalize; override;
  96. end;
  97. {$i innr.inc}
  98. {$i jmathh.inc}
  99. {$i jrech.inc}
  100. {$i sstringh.inc}
  101. {$i jdynarrh.inc}
  102. {$i astringh.inc}
  103. {$ifndef nounsupported}
  104. type
  105. tmethod = record
  106. code: jlobject;
  107. end;
  108. const
  109. vtInteger = 0;
  110. vtBoolean = 1;
  111. vtChar = 2;
  112. {$ifndef FPUNONE}
  113. vtExtended = 3;
  114. {$endif}
  115. vtString = 4;
  116. vtPointer = 5;
  117. vtPChar = 6;
  118. vtObject = 7;
  119. vtClass = 8;
  120. vtWideChar = 9;
  121. vtPWideChar = 10;
  122. vtAnsiString = 11;
  123. vtCurrency = 12;
  124. vtVariant = 13;
  125. vtInterface = 14;
  126. vtWideString = 15;
  127. vtInt64 = 16;
  128. vtQWord = 17;
  129. vtUnicodeString = 18;
  130. type
  131. TVarRec = record
  132. case VType : sizeint of
  133. {$ifdef ENDIAN_BIG}
  134. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  135. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  136. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: Char);
  137. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  138. {$else ENDIAN_BIG}
  139. vtInteger : (VInteger: Longint);
  140. vtBoolean : (VBoolean: Boolean);
  141. vtChar : (VChar: Char);
  142. vtWideChar : (VWideChar: WideChar);
  143. {$endif ENDIAN_BIG}
  144. // vtString : (VString: PShortString);
  145. // vtPointer : (VPointer: Pointer);
  146. /// vtPChar : (VPChar: PChar);
  147. vtObject : (VObject: TObject);
  148. // vtClass : (VClass: TClass);
  149. // vtPWideChar : (VPWideChar: PWideChar);
  150. vtAnsiString : (VAnsiString: JLObject);
  151. vtCurrency : (VCurrency: Currency);
  152. // vtVariant : (VVariant: PVariant);
  153. vtInterface : (VInterface: JLObject);
  154. vtWideString : (VWideString: JLString);
  155. vtInt64 : (VInt64: Int64);
  156. vtUnicodeString : (VUnicodeString: JLString);
  157. vtQWord : (VQWord: QWord);
  158. end;
  159. {$endif}
  160. Function lo(i : Integer) : byte; [INTERNPROC: fpc_in_lo_Word];
  161. Function lo(w : Word) : byte; [INTERNPROC: fpc_in_lo_Word];
  162. Function lo(l : Longint) : Word; [INTERNPROC: fpc_in_lo_long];
  163. Function lo(l : DWord) : Word; [INTERNPROC: fpc_in_lo_long];
  164. Function lo(i : Int64) : DWord; [INTERNPROC: fpc_in_lo_qword];
  165. Function lo(q : QWord) : DWord; [INTERNPROC: fpc_in_lo_qword];
  166. Function hi(i : Integer) : byte; [INTERNPROC: fpc_in_hi_Word];
  167. Function hi(w : Word) : byte; [INTERNPROC: fpc_in_hi_Word];
  168. Function hi(l : Longint) : Word; [INTERNPROC: fpc_in_hi_long];
  169. Function hi(l : DWord) : Word; [INTERNPROC: fpc_in_hi_long];
  170. Function hi(i : Int64) : DWord; [INTERNPROC: fpc_in_hi_qword];
  171. Function hi(q : QWord) : DWord; [INTERNPROC: fpc_in_hi_qword];
  172. Function chr(b : byte) : AnsiChar; [INTERNPROC: fpc_in_chr_byte];
  173. function RorByte(Const AValue : Byte): Byte;[internproc:fpc_in_ror_x];
  174. function RorByte(Const AValue : Byte;Dist : Byte): Byte;[internproc:fpc_in_ror_x_x];
  175. function RolByte(Const AValue : Byte): Byte;[internproc:fpc_in_rol_x];
  176. function RolByte(Const AValue : Byte;Dist : Byte): Byte;[internproc:fpc_in_rol_x_x];
  177. function RorWord(Const AValue : Word): Word;[internproc:fpc_in_ror_x];
  178. function RorWord(Const AValue : Word;Dist : Byte): Word;[internproc:fpc_in_ror_x_x];
  179. function RolWord(Const AValue : Word): Word;[internproc:fpc_in_rol_x];
  180. function RolWord(Const AValue : Word;Dist : Byte): Word;[internproc:fpc_in_rol_x_x];
  181. function RorDWord(Const AValue : DWord): DWord;[internproc:fpc_in_ror_x];
  182. function RorDWord(Const AValue : DWord;Dist : Byte): DWord;[internproc:fpc_in_ror_x_x];
  183. function RolDWord(Const AValue : DWord): DWord;[internproc:fpc_in_rol_x];
  184. function RolDWord(Const AValue : DWord;Dist : Byte): DWord;[internproc:fpc_in_rol_x_x];
  185. function RorQWord(Const AValue : QWord): QWord;[internproc:fpc_in_ror_x];
  186. function RorQWord(Const AValue : QWord;Dist : Byte): QWord;[internproc:fpc_in_ror_x_x];
  187. function RolQWord(Const AValue : QWord): QWord;[internproc:fpc_in_rol_x];
  188. function RolQWord(Const AValue : QWord;Dist : Byte): QWord;[internproc:fpc_in_rol_x_x];
  189. function SarShortint(Const AValue : Shortint): Shortint;[internproc:fpc_in_sar_x];
  190. function SarShortint(Const AValue : Shortint;Shift : Byte): Shortint;[internproc:fpc_in_sar_x_y];
  191. function SarSmallint(Const AValue : Smallint): Smallint;[internproc:fpc_in_sar_x];
  192. function SarSmallint(Const AValue : Smallint;Shift : Byte): Smallint;[internproc:fpc_in_sar_x_y];
  193. function SarLongint(Const AValue : Longint): Longint;[internproc:fpc_in_sar_x];
  194. function SarLongint(Const AValue : Longint;Shift : Byte): Longint;[internproc:fpc_in_sar_x_y];
  195. function SarInt64(Const AValue : Int64): Int64;[internproc:fpc_in_sar_x];
  196. function SarInt64(Const AValue : Int64;Shift : Byte): Int64;[internproc:fpc_in_sar_x_y];
  197. {$i compproc.inc}
  198. {$i ustringh.inc}
  199. {*****************************************************************************}
  200. implementation
  201. {*****************************************************************************}
  202. {i jdynarr.inc}
  203. {
  204. This file is part of the Free Pascal run time library.
  205. Copyright (c) 2011 by Jonas Maebe
  206. member of the Free Pascal development team.
  207. This file implements the helper routines for dyn. Arrays in FPC
  208. See the file COPYING.FPC, included in this distribution,
  209. for details about the copyright.
  210. This program is distributed in the hope that it will be useful,
  211. but WITHOUT ANY WARRANTY; without even the implied warranty of
  212. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  213. **********************************************************************
  214. }
  215. function min(a,b : longint) : longint;
  216. begin
  217. if a<=b then
  218. min:=a
  219. else
  220. min:=b;
  221. end;
  222. {$i sstrings.inc}
  223. {$i astrings.inc}
  224. {$i ustrings.inc}
  225. {$i rtti.inc}
  226. {$i jrec.inc}
  227. {$i jint64.inc}
  228. { copying helpers }
  229. procedure fpc_copy_shallow_array(src, dst: JLObject; srcstart: jint = -1; srccopylen: jint = -1);
  230. var
  231. srclen, dstlen: jint;
  232. begin
  233. if assigned(src) then
  234. srclen:=JLRArray.getLength(src)
  235. else
  236. srclen:=0;
  237. if assigned(dst) then
  238. dstlen:=JLRArray.getLength(dst)
  239. else
  240. dstlen:=0;
  241. if srcstart=-1 then
  242. srcstart:=0
  243. else if srcstart>=srclen then
  244. exit;
  245. if srccopylen=-1 then
  246. srccopylen:=srclen
  247. else if srcstart+srccopylen>srclen then
  248. srccopylen:=srclen-srcstart;
  249. { causes exception in JLSystem.arraycopy }
  250. if (srccopylen=0) or
  251. (dstlen=0) then
  252. exit;
  253. JLSystem.arraycopy(src,srcstart,dst,0,min(srccopylen,dstlen));
  254. end;
  255. procedure fpc_copy_jrecord_array(src, dst: TJRecordArray; srcstart: jint = -1; srccopylen: jint = -1);
  256. var
  257. i: longint;
  258. srclen, dstlen: jint;
  259. begin
  260. srclen:=length(src);
  261. dstlen:=length(dst);
  262. if srcstart=-1 then
  263. srcstart:=0
  264. else if srcstart>=srclen then
  265. exit;
  266. if srccopylen=-1 then
  267. srccopylen:=srclen
  268. else if srcstart+srccopylen>srclen then
  269. srccopylen:=srclen-srcstart;
  270. { no arraycopy, have to clone each element }
  271. for i:=0 to min(srccopylen,dstlen)-1 do
  272. dst[i]:=FpcBaseRecordType(src[srcstart+i].clone);
  273. end;
  274. procedure fpc_copy_jshortstring_array(src, dst: TShortstringArray; srcstart: jint = -1; srccopylen: jint = -1);
  275. var
  276. i: longint;
  277. srclen, dstlen: jint;
  278. begin
  279. srclen:=length(src);
  280. dstlen:=length(dst);
  281. if srcstart=-1 then
  282. srcstart:=0
  283. else if srcstart>=srclen then
  284. exit;
  285. if srccopylen=-1 then
  286. srccopylen:=srclen
  287. else if srcstart+srccopylen>srclen then
  288. srccopylen:=srclen-srcstart;
  289. { no arraycopy, have to clone each element }
  290. for i:=0 to min(srccopylen,dstlen)-1 do
  291. dst[i]:=ShortstringClass(src[srcstart+i].clone);
  292. end;
  293. { 1-dimensional setlength routines }
  294. function fpc_setlength_dynarr_generic(aorg, anew: JLObject; deepcopy: boolean; docopy: boolean = true): JLObject;
  295. var
  296. orglen, newlen: jint;
  297. begin
  298. orglen:=0;
  299. newlen:=0;
  300. if not deepcopy then
  301. begin
  302. if assigned(aorg) then
  303. orglen:=JLRArray.getLength(aorg)
  304. else
  305. orglen:=0;
  306. if assigned(anew) then
  307. newlen:=JLRArray.getLength(anew)
  308. else
  309. newlen:=0;
  310. end;
  311. if deepcopy or
  312. (orglen<>newlen) then
  313. begin
  314. if docopy then
  315. fpc_copy_shallow_array(aorg,anew);
  316. result:=anew
  317. end
  318. else
  319. result:=aorg;
  320. end;
  321. function fpc_setlength_dynarr_jrecord(aorg, anew: TJRecordArray; deepcopy: boolean): TJRecordArray;
  322. begin
  323. if deepcopy or
  324. (length(aorg)<>length(anew)) then
  325. begin
  326. fpc_copy_jrecord_array(aorg,anew);
  327. result:=anew
  328. end
  329. else
  330. result:=aorg;
  331. end;
  332. function fpc_setlength_dynarr_jshortstring(aorg, anew: TShortstringArray; deepcopy: boolean): TShortstringArray;
  333. begin
  334. if deepcopy or
  335. (length(aorg)<>length(anew)) then
  336. begin
  337. fpc_copy_jshortstring_array(aorg,anew);
  338. result:=anew
  339. end
  340. else
  341. result:=aorg;
  342. end;
  343. { multi-dimensional setlength routine }
  344. function fpc_setlength_dynarr_multidim(aorg, anew: TJObjectArray; deepcopy: boolean; ndim: longint; eletype: jchar): TJObjectArray;
  345. var
  346. partdone,
  347. i: longint;
  348. begin
  349. { resize the current dimension; no need to copy the subarrays of the old
  350. array, as the subarrays will be (re-)initialised immediately below }
  351. { the srcstart/srccopylen always refers to the first dimension (since copy()
  352. performs a shallow copy of a dynamic array }
  353. result:=TJObjectArray(fpc_setlength_dynarr_generic(JLObject(aorg),JLObject(anew),deepcopy,false));
  354. { if aorg was empty, there's nothing else to do since result will now
  355. contain anew, of which all other dimensions are already initialised
  356. correctly since there are no aorg elements to copy }
  357. if not assigned(aorg) and
  358. not deepcopy then
  359. exit;
  360. partdone:=min(high(result),high(aorg));
  361. { ndim must be >=2 when this routine is called, since it has to return
  362. an array of java.lang.Object! (arrays are also objects, but primitive
  363. types are not) }
  364. if ndim=2 then
  365. begin
  366. { final dimension -> copy the primitive arrays }
  367. case eletype of
  368. FPCJDynArrTypeRecord:
  369. begin
  370. for i:=low(result) to partdone do
  371. result[i]:=JLObject(fpc_setlength_dynarr_jrecord(TJRecordArray(aorg[i]),TJRecordArray(anew[i]),deepcopy));
  372. for i:=succ(partdone) to high(result) do
  373. result[i]:=JLObject(fpc_setlength_dynarr_jrecord(nil,TJRecordArray(anew[i]),deepcopy));
  374. end;
  375. FPCJDynArrTypeShortstring:
  376. begin
  377. for i:=low(result) to partdone do
  378. result[i]:=JLObject(fpc_setlength_dynarr_jshortstring(TShortstringArray(aorg[i]),TShortstringArray(anew[i]),deepcopy));
  379. for i:=succ(partdone) to high(result) do
  380. result[i]:=JLObject(fpc_setlength_dynarr_jshortstring(nil,TShortstringArray(anew[i]),deepcopy));
  381. end;
  382. else
  383. begin
  384. for i:=low(result) to partdone do
  385. result[i]:=fpc_setlength_dynarr_generic(aorg[i],anew[i],deepcopy);
  386. for i:=succ(partdone) to high(result) do
  387. result[i]:=fpc_setlength_dynarr_generic(nil,anew[i],deepcopy);
  388. end;
  389. end;
  390. end
  391. else
  392. begin
  393. { recursively handle the next dimension }
  394. for i:=low(result) to partdone do
  395. result[i]:=JLObject(fpc_setlength_dynarr_multidim(TJObjectArray(aorg[i]),TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
  396. for i:=succ(partdone) to high(result) do
  397. result[i]:=JLObject(fpc_setlength_dynarr_multidim(nil,TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
  398. end;
  399. end;
  400. function fpc_dynarray_copy(src: JLObject; start, len: longint; ndim: longint; eletype: jchar): JLObject;
  401. var
  402. i: longint;
  403. srclen: longint;
  404. begin
  405. if not assigned(src) then
  406. begin
  407. result:=nil;
  408. exit;
  409. end;
  410. srclen:=JLRArray.getLength(src);
  411. if (start=-1) and
  412. (len=-1) then
  413. begin
  414. len:=srclen;
  415. start:=0;
  416. end
  417. else if (start+len>srclen) then
  418. len:=srclen-start+1;
  419. result:=JLRArray.newInstance(src.getClass.getComponentType,len);
  420. if ndim=1 then
  421. begin
  422. case eletype of
  423. FPCJDynArrTypeRecord:
  424. fpc_copy_jrecord_array(TJRecordArray(src),TJRecordArray(result),start,len);
  425. FPCJDynArrTypeShortstring:
  426. fpc_copy_jshortstring_array(TShortstringArray(src),TShortstringArray(result),start,len);
  427. else
  428. fpc_copy_shallow_array(src,result,start,len);
  429. end
  430. end
  431. else
  432. begin
  433. for i:=0 to len-1 do
  434. TJObjectArray(result)[i]:=fpc_dynarray_copy(TJObjectArray(src)[start+i],-1,-1,ndim-1,eletype);
  435. end;
  436. end;
  437. {i jdynarr.inc end}
  438. {*****************************************************************************
  439. Misc. System Dependent Functions
  440. *****************************************************************************}
  441. procedure TObject.Free;
  442. begin
  443. if not DestructorCalled then
  444. begin
  445. DestructorCalled:=true;
  446. Destroy;
  447. end;
  448. end;
  449. destructor TObject.Destroy;
  450. begin
  451. end;
  452. procedure TObject.Finalize;
  453. begin
  454. Free;
  455. end;
  456. {*****************************************************************************
  457. SystemUnit Initialization
  458. *****************************************************************************}
  459. end.