generic.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Processor independent implementation for the system unit
  6. (adapted for intel i386.inc file)
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {****************************************************************************
  14. Primitives
  15. ****************************************************************************}
  16. type
  17. pstring = ^shortstring;
  18. {$ifndef FPC_SYSTEM_HAS_MOVE}
  19. procedure Move(const source;var dest;count:longint);
  20. type
  21. bytearray = array [0..maxlongint-1] of byte;
  22. var
  23. i:longint;
  24. begin
  25. if count <= 0 then exit;
  26. Dec(count);
  27. for i:=0 to count do
  28. bytearray(dest)[i]:=bytearray(source)[i];
  29. end;
  30. {$endif not FPC_SYSTEM_HAS_MOVE}
  31. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  32. Procedure FillChar(var x;count:longint;value:byte);
  33. type
  34. longintarray = array [0..maxlongint div 4] of longint;
  35. bytearray = array [0..maxlongint-1] of byte;
  36. var
  37. i,v : longint;
  38. begin
  39. if count <= 0 then exit;
  40. v := 0;
  41. v:=(value shl 8) or (value and $FF);
  42. v:=(v shl 16) or (v and $ffff);
  43. for i:=0 to (count div 4) -1 do
  44. longintarray(x)[i]:=v;
  45. for i:=(count div 4)*4 to count-1 do
  46. bytearray(x)[i]:=value;
  47. end;
  48. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  49. {$ifndef FPC_SYSTEM_HAS_FILLBYTE}
  50. procedure FillByte (var x;count : longint;value : byte );
  51. begin
  52. FillChar (X,Count,CHR(VALUE));
  53. end;
  54. {$endif not FPC_SYSTEM_HAS_FILLBYTE}
  55. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  56. procedure fillword(var x;count : longint;value : word);
  57. type
  58. longintarray = array [0..maxlongint div 4] of longint;
  59. wordarray = array [0..maxlongint div 2] of word;
  60. var
  61. i,v : longint;
  62. begin
  63. if Count <= 0 then exit;
  64. v:=value*$10000+value;
  65. for i:=0 to (count div 2) -1 do
  66. longintarray(x)[i]:=v;
  67. for i:=(count div 2)*2 to count-1 do
  68. wordarray(x)[i]:=value;
  69. end;
  70. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  71. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  72. procedure FillDWord(var x;count : longint;value : DWord);
  73. type
  74. longintarray = array [0..maxlongint div 4] of longint;
  75. begin
  76. if count <= 0 then exit;
  77. while Count<>0 do
  78. begin
  79. { range checking must be disabled here }
  80. longintarray(x)[count-1]:=longint(value);
  81. Dec(count);
  82. end;
  83. end;
  84. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  85. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR}
  86. function IndexChar(Const buf;len:longint;b:char):longint;
  87. begin
  88. IndexChar:=IndexByte(Buf,Len,byte(B));
  89. end;
  90. {$endif not FPC_SYSTEM_HAS_INDEXCHAR}
  91. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  92. function IndexByte(Const buf;len:longint;b:byte):longint;
  93. type
  94. bytearray = array [0..maxlongint-1] of byte;
  95. var
  96. I : longint;
  97. begin
  98. I:=0;
  99. while (I<Len) and (bytearray(buf)[I]<>b) do
  100. inc(I);
  101. if (i=Len) then
  102. i:=-1; {Can't use 0, since it is a possible value}
  103. IndexByte:=I;
  104. end;
  105. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  106. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  107. function Indexword(Const buf;len:longint;b:word):longint;
  108. type
  109. wordarray = array [0..maxlongint div 2] of word;
  110. var
  111. I : longint;
  112. begin
  113. I:=0;
  114. while (I<Len) and (wordarray(buf)[I]<>b) do
  115. inc(I);
  116. if (i=Len) then
  117. i:=-1; {Can't use 0, since it is a possible value for index}
  118. Indexword:=I;
  119. end;
  120. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  121. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  122. function IndexDWord(Const buf;len:longint;b:DWord):longint;
  123. type
  124. longintarray = array [0..maxlongint div 4] of longint;
  125. var
  126. I : longint;
  127. begin
  128. I:=0;
  129. while (I<Len) and (longintarray(buf)[I]<>b) do inc(I);
  130. if (i=Len) then
  131. i:=-1; {Can't use 0, since it is a possible value for index}
  132. IndexDWord:=I;
  133. end;
  134. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  135. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR}
  136. function CompareChar(Const buf1,buf2;len:longint):longint;
  137. begin
  138. CompareChar:=CompareByte(buf1,buf2,len);
  139. end;
  140. {$endif not FPC_SYSTEM_HAS_COMPARECHAR}
  141. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  142. function CompareByte(Const buf1,buf2;len:longint):longint;
  143. type
  144. bytearray = array [0..maxlongint-1] of byte;
  145. var
  146. I : longint;
  147. begin
  148. I:=0;
  149. if (Len<>0) and (@Buf1<>@Buf2) then
  150. begin
  151. while (bytearray(Buf1)[I]=bytearray(Buf2)[I]) and (I<Len) do
  152. inc(I);
  153. if I=Len then {No difference}
  154. I:=0
  155. else
  156. begin
  157. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  158. if I>0 then
  159. I:=1
  160. else
  161. if I<0 then
  162. I:=-1;
  163. end;
  164. end;
  165. CompareByte:=I;
  166. end;
  167. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  168. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  169. function CompareWord(Const buf1,buf2;len:longint):longint;
  170. type
  171. wordarray = array [0..maxlongint div 2] of word;
  172. var
  173. I : longint;
  174. begin
  175. I:=0;
  176. if (Len<>0) and (@Buf1<>@Buf2) then
  177. begin
  178. while (wordarray(Buf1)[I]=wordarray(Buf2)[I]) and (I<Len) do
  179. inc(I);
  180. if I=Len then {No difference}
  181. I:=0
  182. else
  183. begin
  184. I:=wordarray(Buf1)[I]-wordarray(Buf2)[I];
  185. if I>0 then
  186. I:=1
  187. else
  188. if I<0 then
  189. I:=-1;
  190. end;
  191. end;
  192. CompareWord:=I;
  193. end;
  194. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  195. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  196. function CompareDWord(Const buf1,buf2;len:longint):longint;
  197. type
  198. longintarray = array [0..maxlongint div 4] of longint;
  199. var
  200. I : longint;
  201. begin
  202. I:=0;
  203. if (Len<>0) and (@Buf1<>@Buf2) then
  204. begin
  205. while (longintarray(Buf1)[I]=longintarray(Buf2)[I]) and (I<Len) do
  206. inc(I);
  207. if I=Len then {No difference}
  208. I:=0
  209. else
  210. begin
  211. I:=longintarray(Buf1)[I]-longintarray(Buf2)[I];
  212. if I>0 then
  213. I:=1
  214. else
  215. if I<0 then
  216. I:=-1;
  217. end;
  218. end;
  219. CompareDWord:=I;
  220. end;
  221. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  222. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  223. procedure MoveChar0(Const buf1;var buf2;len:longint);
  224. var
  225. I : longint;
  226. begin
  227. if Len = 0 then exit;
  228. I:=IndexByte(Buf1,Len,0);
  229. if I<>-1 then
  230. Move(Buf1,Buf2,I)
  231. else
  232. Move(Buf1,Buf2,len);
  233. end;
  234. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  235. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  236. function IndexChar0(Const buf;len:longint;b:Char):longint;
  237. var
  238. I : longint;
  239. begin
  240. if Len<>0 then
  241. begin
  242. I:=IndexByte(Buf,Len,0);
  243. IndexChar0:=IndexByte(Buf,I,0);
  244. end
  245. else
  246. IndexChar0:=0;
  247. end;
  248. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  249. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  250. function CompareChar0(Const buf1,buf2;len:longint):longint;
  251. type
  252. bytearray = array [0..maxlongint-1] of byte;
  253. Var i : longint;
  254. begin
  255. I:=0;
  256. if (Len<>0) and (@Buf1<>@Buf2) then
  257. begin
  258. while (I<Len) And
  259. ((Pbyte(@Buf1)[i]<>0) and (PByte(@buf2)[i]<>0)) and
  260. (pbyte(@Buf1)[I]=pbyte(@Buf2)[I]) do
  261. inc(I);
  262. if (I=Len) or
  263. (PByte(@Buf1)[i]=0) or
  264. (PByte(@buf2)[I]=0) then {No difference or 0 reached }
  265. I:=0
  266. else
  267. begin
  268. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  269. if I>0 then
  270. I:=1
  271. else
  272. if I<0 then
  273. I:=-1;
  274. end;
  275. end;
  276. CompareChar0:=I;
  277. end;
  278. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  279. {****************************************************************************
  280. Object Helpers
  281. ****************************************************************************}
  282. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  283. { Note: _vmt will be reset to -1 when memory is allocated,
  284. this is needed for fpc_help_fail }
  285. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];{$ifdef hascompilerproc}compilerproc;{$endif}
  286. type
  287. ppointer = ^pointer;
  288. pvmt = ^tvmt;
  289. tvmt=packed record
  290. size,msize:longint;
  291. parent:pointer;
  292. end;
  293. var
  294. vmtcopy : pointer;
  295. begin
  296. { Inherited call? }
  297. if _vmt=nil then
  298. begin
  299. fpc_help_constructor:=_self;
  300. exit;
  301. end;
  302. vmtcopy:=_vmt;
  303. if (_self=nil) and
  304. (pvmt(_vmt)^.size>0) then
  305. begin
  306. getmem(_self,pvmt(_vmt)^.size);
  307. { reset vmt needed for fail }
  308. _vmt:=pointer(-1);
  309. end;
  310. if _self<>nil then
  311. begin
  312. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  313. ppointer(_self+_vmt_pos)^:=vmtcopy;
  314. end;
  315. fpc_help_constructor:=_self;
  316. end;
  317. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  318. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  319. { Note: _self will not be reset, the compiler has to generate the reset }
  320. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  321. type
  322. ppointer = ^pointer;
  323. pvmt = ^tvmt;
  324. tvmt = packed record
  325. size,msize : longint;
  326. parent : pointer;
  327. end;
  328. begin
  329. { already released? }
  330. if (_self=nil) or
  331. (_vmt=nil) or
  332. (ppointer(_self+vmt_pos)^=nil) then
  333. exit;
  334. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  335. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  336. RunError(210);
  337. { reset vmt to nil for protection }
  338. ppointer(_self+vmt_pos)^:=nil;
  339. freemem(_self);
  340. end;
  341. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  342. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  343. { Note: _self will not be reset, the compiler has to generate the reset }
  344. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  345. type
  346. ppointer = ^pointer;
  347. pvmt = ^tvmt;
  348. tvmt = packed record
  349. size,msize : longint;
  350. parent : pointer;
  351. end;
  352. begin
  353. if (_self=nil) or (_vmt=nil) then
  354. exit;
  355. { vmt=-1 when memory was allocated }
  356. if longint(_vmt)=-1 then
  357. begin
  358. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  359. HandleError(210)
  360. else
  361. begin
  362. ppointer(_self+vmt_pos)^:=nil;
  363. freemem(_self);
  364. { reset _vmt to 0 so it will not be freed a
  365. second time }
  366. _vmt:=0;
  367. end;
  368. end
  369. else
  370. ppointer(_self+vmt_pos)^:=nil;
  371. end;
  372. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  373. {$ifndef NOCLASSHELPERS}
  374. {$ifndef FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  375. function fpc_new_class(_self,_vmt:pointer):pointer;[public,alias:'FPC_NEW_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  376. begin
  377. { Inherited call? }
  378. if _vmt=nil then
  379. begin
  380. fpc_new_class:=_self;
  381. exit;
  382. end;
  383. fpc_new_class := tclass(_vmt).NewInstance
  384. end;
  385. {$endif FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  386. {$ifndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  387. procedure fpc_dispose_class(_self: pointer; flag : longint);[public,alias:'FPC_DISPOSE_CLASS'];compilerproc;
  388. begin
  389. { inherited -> flag = 0 -> no destroy }
  390. { normal -> flag = 1 -> destroy }
  391. if (_self <> nil) and (flag = 1) then
  392. tobject(_self).FreeInstance;
  393. end;
  394. {$endif ndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  395. {$endif NOCLASSHELPERS}
  396. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  397. procedure fpc_check_object(_vmt : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  398. type
  399. pvmt = ^tvmt;
  400. tvmt = packed record
  401. size,msize : longint;
  402. parent : pointer;
  403. end;
  404. begin
  405. if (_vmt=nil) or
  406. (pvmt(_vmt)^.size=0) or
  407. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  408. RunError(210);
  409. end;
  410. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  411. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  412. { checks for a correct vmt pointer }
  413. { deeper check to see if the current object is }
  414. { really related to the true }
  415. procedure fpc_check_object_ext(vmt, expvmt : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  416. type
  417. pvmt = ^tvmt;
  418. tvmt = packed record
  419. size,msize : longint;
  420. parent : pointer;
  421. end;
  422. begin
  423. if (vmt=nil) or
  424. (pvmt(vmt)^.size=0) or
  425. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  426. RunError(210);
  427. while assigned(vmt) do
  428. if vmt=expvmt then
  429. exit
  430. else
  431. vmt:=pvmt(vmt)^.parent;
  432. RunError(219);
  433. end;
  434. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  435. {****************************************************************************
  436. String
  437. ****************************************************************************}
  438. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  439. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  440. var
  441. slen : byte;
  442. begin
  443. { these are shortstrings, not pointers! (JM)
  444. if dstr=nil then
  445. exit;
  446. if sstr=nil then
  447. begin
  448. if dstr<>nil then
  449. pstring(dstr)^[0]:=#0;
  450. exit;
  451. end;
  452. }
  453. slen:=length(sstr);
  454. if slen<len then
  455. len:=slen;
  456. { don't forget the length character }
  457. if len <> 0 then
  458. move(sstr[0],result[0],len+1);
  459. end;
  460. {$ifdef interncopy}
  461. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  462. {$else}
  463. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  464. {$endif}
  465. var
  466. slen : byte;
  467. type
  468. pstring = ^string;
  469. begin
  470. { these are shortstrings, not pointers! (JM)
  471. if dstr=nil then
  472. exit;
  473. if sstr=nil then
  474. begin
  475. if dstr<>nil then
  476. pstring(dstr)^[0]:=#0;
  477. exit;
  478. end;
  479. }
  480. slen:=length(pstring(sstr)^);
  481. if slen<len then
  482. len:=slen;
  483. { don't forget the length character }
  484. if len <> 0 then
  485. move(sstr^,dstr^,len+1);
  486. { already done by the move above (JM)
  487. pstring(dstr)^[0]:=chr(len);
  488. }
  489. end;
  490. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  491. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  492. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  493. var
  494. s1l, s2l : byte;
  495. begin
  496. s1l:=length(s1);
  497. s2l:=length(s2);
  498. if s1l+s2l>255 then
  499. s2l:=255-s1l;
  500. move(s1[1],fpc_shortstr_concat[1],s1l);
  501. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  502. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  503. end;
  504. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  505. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  506. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);{$ifdef hascompilerproc} compilerproc; {$endif}
  507. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  508. var
  509. s1l, s2l : byte;
  510. begin
  511. s1l:=length(s1);
  512. s2l:=length(s2);
  513. if s1l+s2l>high(s1) then
  514. s2l:=high(s1)-s1l;
  515. move(s2[1],s1[s1l+1],s2l);
  516. s1[0]:=chr(s1l+s2l);
  517. end;
  518. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  519. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  520. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  521. var
  522. s1,s2,max,i : byte;
  523. d : longint;
  524. begin
  525. s1:=length(left);
  526. s2:=length(right);
  527. if s1<s2 then
  528. max:=s1
  529. else
  530. max:=s2;
  531. for i:=1 to max do
  532. begin
  533. d:=byte(left[i])-byte(right[i]);
  534. if d>0 then
  535. exit(1)
  536. else if d<0 then
  537. exit(-1);
  538. end;
  539. if s1>s2 then
  540. exit(1)
  541. else if s1<s2 then
  542. exit(-1)
  543. else
  544. exit(0);
  545. end;
  546. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  547. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  548. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  549. var
  550. l : longint;
  551. s: shortstring;
  552. begin
  553. if p=nil then
  554. l:=0
  555. else
  556. l:=strlen(p);
  557. if l>255 then
  558. l:=255;
  559. if l>0 then
  560. move(p^,s[1],l);
  561. s[0]:=chr(l);
  562. fpc_pchar_to_shortstr := s;
  563. end;
  564. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  565. { also add a strpas alias for internal use in the system unit (JM) }
  566. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  567. { also add a strlen alias for internal use in the system unit (JM) }
  568. function strlen(p:pchar):longint; [external name 'FPC_PCHAR_LENGTH'];
  569. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  570. {$ifdef hascompilerproc}
  571. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  572. var
  573. l: longint;
  574. {$else hascompilerproc}
  575. function fpc_chararray_to_shortstr(arr:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  576. var
  577. {$endif hascompilerproc}
  578. index: longint;
  579. len: byte;
  580. begin
  581. {$ifdef hascompilerproc}
  582. l := high(arr)+1;
  583. {$endif hascompilerproc}
  584. if l>=256 then
  585. l:=255
  586. else if l<0 then
  587. l:=0;
  588. index:=IndexByte(arr[0],l,0);
  589. if (index < 0) then
  590. len := l
  591. else
  592. len := index;
  593. move(arr[0],fpc_chararray_to_shortstr[1],len);
  594. fpc_chararray_to_shortstr[0]:=chr(len);
  595. end;
  596. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  597. {$ifdef hascompilerproc}
  598. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  599. { inside the compiler, the resulttype is modified to that of the actual }
  600. { chararray we're converting to (JM) }
  601. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  602. var
  603. len: longint;
  604. begin
  605. len := length(src);
  606. if len > arraysize then
  607. len := arraysize;
  608. { make sure we don't access char 1 if length is 0 (JM) }
  609. if len > 0 then
  610. move(src[1],fpc_shortstr_to_chararray[0],len);
  611. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  612. end;
  613. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  614. {$else hascompilerproc}
  615. {$ifopt r+}
  616. {$define rangeon}
  617. {$r-}
  618. {$endif}
  619. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  620. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  621. type
  622. plongint = ^longint;
  623. var
  624. len: longint;
  625. begin
  626. case strtyp of
  627. { shortstring }
  628. 0:
  629. begin
  630. len := byte(src[0]);
  631. inc(src);
  632. end;
  633. {$ifdef SUPPORT_ANSISTRING}
  634. { ansistring}
  635. 1: len := length(ansistring(pointer(src)));
  636. {$endif SUPPORT_ANSISTRING}
  637. { longstring }
  638. 2:;
  639. { widestring }
  640. 3: ;
  641. end;
  642. if len > arraysize then
  643. len := arraysize;
  644. { make sure we don't dereference src if it can be nil (JM) }
  645. if len > 0 then
  646. move(src^,dest^,len);
  647. fillchar(dest[len],arraysize-len,0);
  648. end;
  649. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  650. {$ifdef rangeon}
  651. {$r+}
  652. {undef rangeon}
  653. {$endif rangeon}
  654. {$endif hascompilerproc}
  655. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  656. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  657. var i : longint;
  658. begin
  659. i:=0;
  660. while p[i]<>#0 do inc(i);
  661. exit(i);
  662. end;
  663. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  664. {$ifdef HASWIDESTRING}
  665. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  666. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  667. var i : longint;
  668. begin
  669. i:=0;
  670. while p[i]<>#0 do inc(i);
  671. exit(i);
  672. end;
  673. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  674. {$endif HASWIDESTRING}
  675. {****************************************************************************
  676. Caller/StackFrame Helpers
  677. ****************************************************************************}
  678. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  679. {_$error Get_frame must be defined for each processor }
  680. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  681. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  682. {_$error Get_caller_addr must be defined for each processor }
  683. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  684. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  685. {_$error Get_caller_frame must be defined for each processor }
  686. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  687. {****************************************************************************
  688. Math
  689. ****************************************************************************}
  690. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  691. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  692. begin
  693. if l<0 then
  694. abs:=-l
  695. else
  696. abs:=l;
  697. end;
  698. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  699. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  700. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  701. begin
  702. odd:=boolean(l and 1);
  703. end;
  704. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  705. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  706. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  707. begin
  708. odd:=boolean(l and 1);
  709. end;
  710. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  711. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  712. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  713. begin
  714. odd:=boolean(longint(l) and 1);
  715. end;
  716. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  717. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  718. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  719. begin
  720. odd:=boolean(longint(l) and 1);
  721. end;
  722. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  723. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  724. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  725. begin
  726. sqr:=l*l;
  727. end;
  728. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  729. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  730. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  731. begin
  732. if l < 0 then
  733. abs := -l
  734. else
  735. abs := l;
  736. end;
  737. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  738. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  739. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  740. begin
  741. sqr := l*l;
  742. end;
  743. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  744. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  745. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  746. begin
  747. sqr := l*l;
  748. end;
  749. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  750. {$ifndef FPC_SYSTEM_HAS_DECLOCKED}
  751. function declocked(var l:longint):boolean;
  752. begin
  753. Dec(l);
  754. declocked:=(l=0);
  755. end;
  756. {$endif FPC_SYSTEM_HAS_DECLOCKED}
  757. {$ifndef FPC_SYSTEM_HAS_INCLOCKED}
  758. procedure inclocked(var l:longint);
  759. begin
  760. Inc(l);
  761. end;
  762. {$endif FPC_SYSTEM_HAS_INCLOCKED}
  763. {$ifndef FPC_SYSTEM_HAS_SPTR}
  764. {_$error Sptr must be defined for each processor }
  765. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  766. {****************************************************************************
  767. Str()
  768. ****************************************************************************}
  769. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  770. procedure int_str(l : longint;var s : string);
  771. var
  772. value: longint;
  773. negative: boolean;
  774. begin
  775. negative := false;
  776. s:='';
  777. { Workaround: }
  778. if l=$80000000 then
  779. begin
  780. s:='-2147483648';
  781. exit;
  782. end;
  783. { handle case where l = 0 }
  784. if l = 0 then
  785. begin
  786. s:='0';
  787. exit;
  788. end;
  789. If l < 0 then
  790. begin
  791. negative := true;
  792. value:=abs(l);
  793. end
  794. else
  795. value:=l;
  796. { handle non-zero case }
  797. while value>0 do
  798. begin
  799. s:=char((value mod 10)+ord('0'))+s;
  800. value := value div 10;
  801. end;
  802. if negative then
  803. s := '-' + s;
  804. end;
  805. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  806. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  807. procedure int_str(l : longword;var s : string);
  808. begin
  809. s:='';
  810. if l = 0 then
  811. begin
  812. s := '0';
  813. exit;
  814. end;
  815. while l>0 do
  816. begin
  817. s:=char(ord('0')+(l mod 10))+s;
  818. l:=l div 10;
  819. end;
  820. end;
  821. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  822. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  823. procedure SysResetFpu;
  824. begin
  825. { nothing todo }
  826. end;
  827. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  828. {
  829. $Log$
  830. Revision 1.60 2003-06-01 14:50:17 jonas
  831. * fpc_shortstr_append_shortstr has to use high(s1) instead of 255 as
  832. maxlen
  833. + ppc version of fpc_shortstr_append_shortstr
  834. Revision 1.59 2003/05/26 21:18:13 peter
  835. * FPC_SHORTSTR_APPEND_SHORTSTR public added
  836. Revision 1.58 2003/05/26 19:36:46 peter
  837. * fpc_shortstr_concat is now the same for all targets
  838. * fpc_shortstr_append_shortstr added for optimized code generation
  839. Revision 1.57 2003/05/16 22:40:11 florian
  840. * fixed generic shortstr_compare
  841. Revision 1.56 2003/05/13 20:52:50 peter
  842. * extra check for self and empty objects
  843. Revision 1.55 2003/05/13 19:18:08 peter
  844. * fpc_help_fail compilerproc
  845. * fpc_new_class, fpc_dispose_class not needed by latest compiler
  846. Revision 1.54 2003/04/23 13:10:09 peter
  847. * remvoe objectsize loading from help_destructor
  848. * implement fpc_check_object
  849. * saveregistrers for check_object
  850. Revision 1.53 2003/04/02 14:05:45 peter
  851. * undo previous commit
  852. Revision 1.51 2003/03/26 00:17:34 peter
  853. * generic constructor/destructor fixes
  854. Revision 1.50 2003/02/18 17:56:06 jonas
  855. - removed buggy i386-specific FPC_CHARARRAY_TO_SHORTSTR
  856. * fixed generic FPC_CHARARRAY_TO_SHORTSTR (web bug 2382)
  857. * fixed some potential range errors in indexchar/word/dword
  858. Revision 1.49 2003/01/20 22:21:36 mazen
  859. * many stuff related to RTL fixed
  860. Revision 1.48 2003/01/09 20:14:20 florian
  861. * fixed helper declarations
  862. Revision 1.47 2003/01/07 22:04:12 mazen
  863. - space removed
  864. Revision 1.46 2003/01/06 23:04:21 mazen
  865. * functions headers modified in generic.inc to make it possible compiling sparc
  866. RTL based on generic code
  867. Revision 1.45 2003/01/05 21:32:35 mazen
  868. * fixing several bugs compiling the RTL
  869. Revision 1.44 2002/12/23 21:27:13 peter
  870. * fix wrong var names for shortstr_compare
  871. Revision 1.43 2002/10/20 11:51:54 carl
  872. * avoid crashes with negative len counts on fills/moves
  873. * movechar0 was wrong and did not do the behavior as
  874. described in docs
  875. Revision 1.42 2002/10/14 19:39:17 peter
  876. * threads unit added for thread support
  877. Revision 1.41 2002/10/12 20:32:41 carl
  878. * RunError 220 -> RunError 219 to be more consistent with as operator
  879. Revision 1.40 2002/10/10 16:08:50 florian
  880. + several widestring/pwidechar related helpers added
  881. Revision 1.39 2002/10/05 14:20:16 peter
  882. * fpc_pchar_length compilerproc and strlen alias
  883. Revision 1.38 2002/10/02 18:21:51 peter
  884. * Copy() changed to internal function calling compilerprocs
  885. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  886. new copy functions
  887. Revision 1.37 2002/09/27 21:10:40 carl
  888. * fix 2GB limit problem
  889. Revision 1.36 2002/09/13 19:13:06 carl
  890. * FPC_HELP_FAIL : reset _self to nil
  891. Revision 1.35 2002/09/10 21:29:44 jonas
  892. * added some missing compilerproc directives
  893. Revision 1.34 2002/09/07 21:08:42 carl
  894. * cardinal -> longword
  895. - remove generic boundcheck (does not exist in v1.1)
  896. Revision 1.33 2002/09/07 15:07:45 peter
  897. * old logs removed and tabs fixed
  898. Revision 1.32 2002/08/19 19:34:02 peter
  899. * SYSTEMINLINE define that will add inline directives for small
  900. functions and wrappers. This will be defined automaticly when
  901. the compiler defines the HASINLINE directive
  902. Revision 1.31 2002/07/29 21:28:16 florian
  903. * several fixes to get further with linux/ppc system unit compilation
  904. Revision 1.30 2002/07/29 09:23:11 jonas
  905. * fixed some datastructures > 2GB
  906. Revision 1.29 2002/07/28 21:39:28 florian
  907. * made abs a compiler proc if it is generic
  908. Revision 1.28 2002/07/28 20:43:47 florian
  909. * several fixes for linux/powerpc
  910. * several fixes to MT
  911. Revision 1.27 2002/06/16 08:19:03 carl
  912. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  913. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  914. Revision 1.25 2002/05/16 19:58:05 carl
  915. * generic constructor implemented
  916. Revision 1.24 2002/03/30 13:08:54 carl
  917. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  918. Revision 1.23 2002/01/25 17:38:55 peter
  919. * add internconst for all overloaded types of Odd/Abs/Sqr
  920. Revision 1.22 2002/01/24 12:33:53 jonas
  921. * adapted ranges of native types to int64 (e.g. high cardinal is no
  922. longer longint($ffffffff), but just $fffffff in psystem)
  923. * small additional fix in 64bit rangecheck code generation for 32 bit
  924. processors
  925. * adaption of ranges required the matching talgorithm used for selecting
  926. which overloaded procedure to call to be adapted. It should now always
  927. select the closest match for ordinal parameters.
  928. + inttostr(qword) in sysstr.inc/sysstrh.inc
  929. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  930. fixes were required to be able to add them)
  931. * is_in_limit() moved from ncal to types unit, should always be used
  932. instead of direct comparisons of low/high values of orddefs because
  933. qword is a special case
  934. }