generic.inc 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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(219);
  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. { also add a strlen alias for internal use in the system unit (JM) }
  572. function strlen(p:pchar):longint; [external name 'FPC_PCHAR_LENGTH'];
  573. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  574. {$ifdef hascompilerproc}
  575. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  576. var
  577. l: longint;
  578. {$else hascompilerproc}
  579. function fpc_chararray_to_shortstr(p:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  580. {$endif hascompilerproc}
  581. begin
  582. {$ifdef hascompilerproc}
  583. l := high(arr)+1;
  584. {$endif hascompilerproc}
  585. if l>=256 then
  586. l:=255
  587. else if l<0 then
  588. l:=0;
  589. move(arr[0],fpc_chararray_to_shortstr[1],l);
  590. fpc_chararray_to_shortstr[0]:=chr(l);
  591. end;
  592. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  593. {$ifdef hascompilerproc}
  594. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  595. { inside the compiler, the resulttype is modified to that of the actual }
  596. { chararray we're converting to (JM) }
  597. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  598. var
  599. len: longint;
  600. begin
  601. len := length(src);
  602. if len > arraysize then
  603. len := arraysize;
  604. { make sure we don't access char 1 if length is 0 (JM) }
  605. if len > 0 then
  606. move(src[1],fpc_shortstr_to_chararray[0],len);
  607. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  608. end;
  609. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  610. {$else hascompilerproc}
  611. {$ifopt r+}
  612. {$define rangeon}
  613. {$r-}
  614. {$endif}
  615. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  616. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  617. type
  618. plongint = ^longint;
  619. var
  620. len: longint;
  621. begin
  622. case strtyp of
  623. { shortstring }
  624. 0:
  625. begin
  626. len := byte(src[0]);
  627. inc(src);
  628. end;
  629. {$ifdef SUPPORT_ANSISTRING}
  630. { ansistring}
  631. 1: len := length(ansistring(pointer(src)));
  632. {$endif SUPPORT_ANSISTRING}
  633. { longstring }
  634. 2:;
  635. { widestring }
  636. 3: ;
  637. end;
  638. if len > arraysize then
  639. len := arraysize;
  640. { make sure we don't dereference src if it can be nil (JM) }
  641. if len > 0 then
  642. move(src^,dest^,len);
  643. fillchar(dest[len],arraysize-len,0);
  644. end;
  645. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  646. {$ifdef rangeon}
  647. {$r+}
  648. {undef rangeon}
  649. {$endif rangeon}
  650. {$endif hascompilerproc}
  651. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  652. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  653. var i : longint;
  654. begin
  655. i:=0;
  656. while p[i]<>#0 do inc(i);
  657. exit(i);
  658. end;
  659. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  660. {$ifdef HASWIDESTRING}
  661. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  662. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  663. var i : longint;
  664. begin
  665. i:=0;
  666. while p[i]<>#0 do inc(i);
  667. exit(i);
  668. end;
  669. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  670. {$endif HASWIDESTRING}
  671. {****************************************************************************
  672. Caller/StackFrame Helpers
  673. ****************************************************************************}
  674. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  675. {$error Get_frame must be defined for each processor }
  676. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  677. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  678. {$error Get_caller_addr must be defined for each processor }
  679. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  680. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  681. {$error Get_caller_frame must be defined for each processor }
  682. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  683. {****************************************************************************
  684. Math
  685. ****************************************************************************}
  686. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  687. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  688. begin
  689. if l<0 then
  690. abs:=-l
  691. else
  692. abs:=l;
  693. end;
  694. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  695. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  696. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  697. begin
  698. odd:=boolean(l and 1);
  699. end;
  700. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  701. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  702. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  703. begin
  704. odd:=boolean(l and 1);
  705. end;
  706. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  707. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  708. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  709. begin
  710. odd:=boolean(longint(l) and 1);
  711. end;
  712. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  713. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  714. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  715. begin
  716. odd:=boolean(longint(l) and 1);
  717. end;
  718. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  719. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  720. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  721. begin
  722. sqr:=l*l;
  723. end;
  724. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  725. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  726. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  727. begin
  728. if l < 0 then
  729. abs := -l
  730. else
  731. abs := l;
  732. end;
  733. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  734. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  735. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  736. begin
  737. sqr := l*l;
  738. end;
  739. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  740. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  741. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  742. begin
  743. sqr := l*l;
  744. end;
  745. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  746. {$ifndef FPC_SYSTEM_HAS_SPTR}
  747. {$error Sptr must be defined for each processor }
  748. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  749. {****************************************************************************
  750. Str()
  751. ****************************************************************************}
  752. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  753. procedure int_str(l : longint;var s : string);
  754. var
  755. value: longint;
  756. negative: boolean;
  757. begin
  758. negative := false;
  759. s:='';
  760. { Workaround: }
  761. if l=$80000000 then
  762. begin
  763. s:='-2147483648';
  764. exit;
  765. end;
  766. { handle case where l = 0 }
  767. if l = 0 then
  768. begin
  769. s:='0';
  770. exit;
  771. end;
  772. If l < 0 then
  773. begin
  774. negative := true;
  775. value:=abs(l);
  776. end
  777. else
  778. value:=l;
  779. { handle non-zero case }
  780. while value>0 do
  781. begin
  782. s:=char((value mod 10)+ord('0'))+s;
  783. value := value div 10;
  784. end;
  785. if negative then
  786. s := '-' + s;
  787. end;
  788. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  789. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  790. procedure int_str(l : longword;var s : string);
  791. begin
  792. s:='';
  793. if l = 0 then
  794. begin
  795. s := '0';
  796. exit;
  797. end;
  798. while l>0 do
  799. begin
  800. s:=char(ord('0')+(l mod 10))+s;
  801. l:=l div 10;
  802. end;
  803. end;
  804. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  805. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  806. procedure SysResetFpu;
  807. begin
  808. { nothing todo }
  809. end;
  810. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  811. {
  812. $Log$
  813. Revision 1.42 2002-10-14 19:39:17 peter
  814. * threads unit added for thread support
  815. Revision 1.41 2002/10/12 20:32:41 carl
  816. * RunError 220 -> RunError 219 to be more consistent with as operator
  817. Revision 1.40 2002/10/10 16:08:50 florian
  818. + several widestring/pwidechar related helpers added
  819. Revision 1.39 2002/10/05 14:20:16 peter
  820. * fpc_pchar_length compilerproc and strlen alias
  821. Revision 1.38 2002/10/02 18:21:51 peter
  822. * Copy() changed to internal function calling compilerprocs
  823. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  824. new copy functions
  825. Revision 1.37 2002/09/27 21:10:40 carl
  826. * fix 2GB limit problem
  827. Revision 1.36 2002/09/13 19:13:06 carl
  828. * FPC_HELP_FAIL : reset _self to nil
  829. Revision 1.35 2002/09/10 21:29:44 jonas
  830. * added some missing compilerproc directives
  831. Revision 1.34 2002/09/07 21:08:42 carl
  832. * cardinal -> longword
  833. - remove generic boundcheck (does not exist in v1.1)
  834. Revision 1.33 2002/09/07 15:07:45 peter
  835. * old logs removed and tabs fixed
  836. Revision 1.32 2002/08/19 19:34:02 peter
  837. * SYSTEMINLINE define that will add inline directives for small
  838. functions and wrappers. This will be defined automaticly when
  839. the compiler defines the HASINLINE directive
  840. Revision 1.31 2002/07/29 21:28:16 florian
  841. * several fixes to get further with linux/ppc system unit compilation
  842. Revision 1.30 2002/07/29 09:23:11 jonas
  843. * fixed some datastructures > 2GB
  844. Revision 1.29 2002/07/28 21:39:28 florian
  845. * made abs a compiler proc if it is generic
  846. Revision 1.28 2002/07/28 20:43:47 florian
  847. * several fixes for linux/powerpc
  848. * several fixes to MT
  849. Revision 1.27 2002/06/16 08:19:03 carl
  850. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  851. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  852. Revision 1.25 2002/05/16 19:58:05 carl
  853. * generic constructor implemented
  854. Revision 1.24 2002/03/30 13:08:54 carl
  855. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  856. Revision 1.23 2002/01/25 17:38:55 peter
  857. * add internconst for all overloaded types of Odd/Abs/Sqr
  858. Revision 1.22 2002/01/24 12:33:53 jonas
  859. * adapted ranges of native types to int64 (e.g. high cardinal is no
  860. longer longint($ffffffff), but just $fffffff in psystem)
  861. * small additional fix in 64bit rangecheck code generation for 32 bit
  862. processors
  863. * adaption of ranges required the matching talgorithm used for selecting
  864. which overloaded procedure to call to be adapted. It should now always
  865. select the closest match for ordinal parameters.
  866. + inttostr(qword) in sysstr.inc/sysstrh.inc
  867. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  868. fixes were required to be able to add them)
  869. * is_in_limit() moved from ncal to types unit, should always be used
  870. instead of direct comparisons of low/high values of orddefs because
  871. qword is a special case
  872. }