generic.inc 25 KB

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