generic.inc 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. { Generic code does not set the register used for self !
  284. So this needs to be done by the compiler after calling
  285. FPC_HELP_CONSTRUCTOR : generic allways means aa little less efficient (PM) }
  286. { I don't think we really need to save any registers here }
  287. { since this is called at the start of the constructor (CEC) }
  288. procedure fpc_help_constructor;[public,alias:'FPC_HELP_CONSTRUCTOR'];{$ifdef hascompilerproc}compilerproc;{$endif}
  289. type
  290. ppointer = ^pointer;
  291. pvmt = ^tvmt;
  292. tvmt=packed record
  293. size,msize:longint;
  294. parent:pointer;
  295. end;
  296. var
  297. objectsize:longint;
  298. vmtcopy:pointer;
  299. _self:pointer;
  300. vmt:pointer;
  301. vmt_pos:cardinal;
  302. begin
  303. if vmt=nil
  304. then
  305. exit;
  306. vmtcopy:=vmt;
  307. objectsize:=pvmt(vmtcopy)^.size;
  308. if _self=nil
  309. then
  310. begin
  311. getmem(_self,objectsize);
  312. longint(vmt):=-1; { needed for fail }
  313. end;
  314. if _self<>nil
  315. then
  316. begin
  317. fillchar(_self^,objectsize,#0);
  318. ppointer(_self+vmt_pos)^:=vmtcopy;
  319. end;
  320. end;
  321. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  322. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  323. procedure fpc_help_destructor(var _self : pointer; vmt : pointer; vmt_pos : cardinal);[public,alias:'fpc_help_destructor']; {$ifdef hascompilerproc} compilerproc; {$endif}
  324. type
  325. ppointer = ^pointer;
  326. pvmt = ^tvmt;
  327. tvmt = packed record
  328. size,msize : longint;
  329. parent : pointer;
  330. end;
  331. var
  332. objectsize : longint;
  333. begin
  334. if (_self=nil) then
  335. exit;
  336. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  337. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  338. RunError(210);
  339. if (vmt = nil) then
  340. exit;
  341. objectsize:=pvmt(vmt)^.size;
  342. { reset vmt to nil for protection }
  343. ppointer(_self+vmt_pos)^:=nil;
  344. freemem(_self,objectsize);
  345. _self:=nil;
  346. end;
  347. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  348. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  349. procedure fpc_help_fail(var _self:pointer;var vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  350. type
  351. ppointer = ^pointer;
  352. pvmt = ^tvmt;
  353. tvmt = packed record
  354. size,msize : longint;
  355. parent : pointer;
  356. end;
  357. var
  358. objectsize : longint;
  359. begin
  360. if vmt=nil then
  361. exit;
  362. if longint(vmt)=-1 then
  363. begin
  364. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  365. HandleError(210)
  366. else
  367. begin
  368. ppointer(_self+vmt_pos)^:=nil;
  369. freemem(_self);
  370. _self:=nil;
  371. vmt:=nil;
  372. end;
  373. end
  374. else
  375. ppointer(_self+vmt_pos)^:=nil;
  376. _self := nil;
  377. end;
  378. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  379. {$ifndef FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  380. { the constructor receives as first parameter a pointer }
  381. { to the vmt or nil, if called when class already instanciated }
  382. { RETURNS SELF }
  383. { IMPORTANT: SELF REGISTER should be pre-loaded before call to }
  384. { constructor for this to work! }
  385. function fpc_new_class(_vmt:pointer;_self:pointer):pointer;[public,alias:'FPC_NEW_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  386. begin
  387. if _vmt <> nil then
  388. begin
  389. fpc_new_class := tclass(_vmt).NewInstance;
  390. end
  391. else
  392. begin
  393. { calling when class already instanciated }
  394. { then simply returna a boolean value <> 0 }
  395. fpc_new_class := _self;
  396. end;
  397. end;
  398. {$endif FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  399. {$ifndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  400. procedure fpc_dispose_class(_self: tobject; flag : longint);[public,alias:'FPC_DISPOSE_CLASS'];compilerproc;
  401. begin
  402. if (_self <> nil) and (flag = 1) then
  403. _self.FreeInstance;
  404. end;
  405. {$endif ndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  406. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  407. procedure fpc_check_object(obj : pointer);[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  408. type
  409. pvmt = ^tvmt;
  410. tvmt = packed record
  411. size,msize : longint;
  412. parent : pointer;
  413. end;
  414. begin
  415. (* if (vmt=nil) or
  416. (pvmt(vmt)^.size=0) or
  417. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  418. RunError(210);*)
  419. end;
  420. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  421. { checks for a correct vmt pointer }
  422. { deeper check to see if the current object is }
  423. { really related to the true }
  424. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  425. procedure fpc_check_object_ext(vmt, expvmt : pointer);[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  426. type
  427. pvmt = ^tvmt;
  428. tvmt = packed record
  429. size,msize : longint;
  430. parent : pointer;
  431. end;
  432. begin
  433. if (vmt=nil) or
  434. (pvmt(vmt)^.size=0) or
  435. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  436. RunError(210);
  437. while assigned(vmt) do
  438. if vmt=expvmt then
  439. exit
  440. else
  441. vmt:=pvmt(vmt)^.parent;
  442. RunError(219);
  443. end;
  444. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  445. {****************************************************************************
  446. String
  447. ****************************************************************************}
  448. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  449. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  450. var
  451. slen : byte;
  452. begin
  453. { these are shortstrings, not pointers! (JM)
  454. if dstr=nil then
  455. exit;
  456. if sstr=nil then
  457. begin
  458. if dstr<>nil then
  459. pstring(dstr)^[0]:=#0;
  460. exit;
  461. end;
  462. }
  463. slen:=length(sstr);
  464. if slen<len then
  465. len:=slen;
  466. { don't forget the length character }
  467. if len <> 0 then
  468. move(sstr[0],result[0],len+1);
  469. end;
  470. {$ifdef interncopy}
  471. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  472. {$else}
  473. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  474. {$endif}
  475. var
  476. slen : byte;
  477. type
  478. pstring = ^string;
  479. begin
  480. { these are shortstrings, not pointers! (JM)
  481. if dstr=nil then
  482. exit;
  483. if sstr=nil then
  484. begin
  485. if dstr<>nil then
  486. pstring(dstr)^[0]:=#0;
  487. exit;
  488. end;
  489. }
  490. slen:=length(pstring(sstr)^);
  491. if slen<len then
  492. len:=slen;
  493. { don't forget the length character }
  494. if len <> 0 then
  495. move(sstr^,dstr^,len+1);
  496. { already done by the move above (JM)
  497. pstring(dstr)^[0]:=chr(len);
  498. }
  499. end;
  500. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  501. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  502. { note: this routine is *DIFFERENT* from the routine in i386.inc and as such you }
  503. { cannot use it with the i386 compiler, unless you remove the }
  504. { ti386addnode.first_string method (JM) }
  505. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  506. var
  507. s1l, s2l : byte;
  508. begin
  509. { these are shortstrings, they can't be nil! (JM)
  510. if (s1=nil) or (s2=nil) then
  511. exit;
  512. }
  513. s1l:=length(s1);
  514. s2l:=length(s2);
  515. if s1l+s2l>255 then
  516. s2l:=255-s1l;
  517. fpc_shortstr_concat := s1;
  518. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  519. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  520. end;
  521. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  522. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  523. function fpc_shortstr_compare(const dstr,sstr:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  524. var
  525. s1,s2,max,i : byte;
  526. d : longint;
  527. begin
  528. s1:=length(dstr);
  529. s2:=length(sstr);
  530. if s1<s2 then
  531. max:=s1
  532. else
  533. max:=s2;
  534. for i:=1 to max do
  535. begin
  536. d:=byte(sstr[i])-byte(dstr[i]);
  537. if d>0 then
  538. exit(1)
  539. else if d<0 then
  540. exit(-1);
  541. end;
  542. if s1>s2 then
  543. exit(1)
  544. else if s1<s2 then
  545. exit(-1)
  546. else
  547. exit(0);
  548. end;
  549. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  550. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  551. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  552. var
  553. l : longint;
  554. s: shortstring;
  555. begin
  556. if p=nil then
  557. l:=0
  558. else
  559. l:=strlen(p);
  560. if l>255 then
  561. l:=255;
  562. if l>0 then
  563. move(p^,s[1],l);
  564. s[0]:=chr(l);
  565. fpc_pchar_to_shortstr := s;
  566. end;
  567. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  568. { also add a strpas alias for internal use in the system unit (JM) }
  569. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  570. { also add a strlen alias for internal use in the system unit (JM) }
  571. function strlen(p:pchar):longint; [external name 'FPC_PCHAR_LENGTH'];
  572. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  573. {$ifdef hascompilerproc}
  574. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  575. var
  576. l: longint;
  577. {$else hascompilerproc}
  578. function fpc_chararray_to_shortstr(arr:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  579. var
  580. {$endif hascompilerproc}
  581. index: longint;
  582. len: byte;
  583. begin
  584. {$ifdef hascompilerproc}
  585. l := high(arr)+1;
  586. {$endif hascompilerproc}
  587. if l>=256 then
  588. l:=255
  589. else if l<0 then
  590. l:=0;
  591. index:=IndexByte(arr[0],l,0);
  592. if (index < 0) then
  593. len := l
  594. else
  595. len := index;
  596. move(arr[0],fpc_chararray_to_shortstr[1],len);
  597. fpc_chararray_to_shortstr[0]:=chr(len);
  598. end;
  599. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  600. {$ifdef hascompilerproc}
  601. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  602. { inside the compiler, the resulttype is modified to that of the actual }
  603. { chararray we're converting to (JM) }
  604. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  605. var
  606. len: longint;
  607. begin
  608. len := length(src);
  609. if len > arraysize then
  610. len := arraysize;
  611. { make sure we don't access char 1 if length is 0 (JM) }
  612. if len > 0 then
  613. move(src[1],fpc_shortstr_to_chararray[0],len);
  614. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  615. end;
  616. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  617. {$else hascompilerproc}
  618. {$ifopt r+}
  619. {$define rangeon}
  620. {$r-}
  621. {$endif}
  622. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  623. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  624. type
  625. plongint = ^longint;
  626. var
  627. len: longint;
  628. begin
  629. case strtyp of
  630. { shortstring }
  631. 0:
  632. begin
  633. len := byte(src[0]);
  634. inc(src);
  635. end;
  636. {$ifdef SUPPORT_ANSISTRING}
  637. { ansistring}
  638. 1: len := length(ansistring(pointer(src)));
  639. {$endif SUPPORT_ANSISTRING}
  640. { longstring }
  641. 2:;
  642. { widestring }
  643. 3: ;
  644. end;
  645. if len > arraysize then
  646. len := arraysize;
  647. { make sure we don't dereference src if it can be nil (JM) }
  648. if len > 0 then
  649. move(src^,dest^,len);
  650. fillchar(dest[len],arraysize-len,0);
  651. end;
  652. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  653. {$ifdef rangeon}
  654. {$r+}
  655. {undef rangeon}
  656. {$endif rangeon}
  657. {$endif hascompilerproc}
  658. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  659. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  660. var i : longint;
  661. begin
  662. i:=0;
  663. while p[i]<>#0 do inc(i);
  664. exit(i);
  665. end;
  666. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  667. {$ifdef HASWIDESTRING}
  668. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  669. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  670. var i : longint;
  671. begin
  672. i:=0;
  673. while p[i]<>#0 do inc(i);
  674. exit(i);
  675. end;
  676. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  677. {$endif HASWIDESTRING}
  678. {****************************************************************************
  679. Caller/StackFrame Helpers
  680. ****************************************************************************}
  681. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  682. {_$error Get_frame must be defined for each processor }
  683. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  684. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  685. {_$error Get_caller_addr must be defined for each processor }
  686. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  687. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  688. {_$error Get_caller_frame must be defined for each processor }
  689. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  690. {****************************************************************************
  691. Math
  692. ****************************************************************************}
  693. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  694. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  695. begin
  696. if l<0 then
  697. abs:=-l
  698. else
  699. abs:=l;
  700. end;
  701. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  702. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  703. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  704. begin
  705. odd:=boolean(l and 1);
  706. end;
  707. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  708. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  709. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  710. begin
  711. odd:=boolean(l and 1);
  712. end;
  713. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  714. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  715. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  716. begin
  717. odd:=boolean(longint(l) and 1);
  718. end;
  719. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  720. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  721. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  722. begin
  723. odd:=boolean(longint(l) and 1);
  724. end;
  725. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  726. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  727. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  728. begin
  729. sqr:=l*l;
  730. end;
  731. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  732. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  733. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  734. begin
  735. if l < 0 then
  736. abs := -l
  737. else
  738. abs := l;
  739. end;
  740. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  741. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  742. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  743. begin
  744. sqr := l*l;
  745. end;
  746. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  747. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  748. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  749. begin
  750. sqr := l*l;
  751. end;
  752. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  753. {$ifndef FPC_SYSTEM_HAS_DECLOCKED}
  754. function declocked(var l:longint):boolean;
  755. begin
  756. Dec(l);
  757. declocked:=(l=0);
  758. end;
  759. {$endif FPC_SYSTEM_HAS_DECLOCKED}
  760. {$ifndef FPC_SYSTEM_HAS_INCLOCKED}
  761. procedure inclocked(var l:longint);
  762. begin
  763. Inc(l);
  764. end;
  765. {$endif FPC_SYSTEM_HAS_INCLOCKED}
  766. {$ifndef FPC_SYSTEM_HAS_SPTR}
  767. {_$error Sptr must be defined for each processor }
  768. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  769. {****************************************************************************
  770. Str()
  771. ****************************************************************************}
  772. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  773. procedure int_str(l : longint;var s : string);
  774. var
  775. value: longint;
  776. negative: boolean;
  777. begin
  778. negative := false;
  779. s:='';
  780. { Workaround: }
  781. if l=$80000000 then
  782. begin
  783. s:='-2147483648';
  784. exit;
  785. end;
  786. { handle case where l = 0 }
  787. if l = 0 then
  788. begin
  789. s:='0';
  790. exit;
  791. end;
  792. If l < 0 then
  793. begin
  794. negative := true;
  795. value:=abs(l);
  796. end
  797. else
  798. value:=l;
  799. { handle non-zero case }
  800. while value>0 do
  801. begin
  802. s:=char((value mod 10)+ord('0'))+s;
  803. value := value div 10;
  804. end;
  805. if negative then
  806. s := '-' + s;
  807. end;
  808. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  809. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  810. procedure int_str(l : longword;var s : string);
  811. begin
  812. s:='';
  813. if l = 0 then
  814. begin
  815. s := '0';
  816. exit;
  817. end;
  818. while l>0 do
  819. begin
  820. s:=char(ord('0')+(l mod 10))+s;
  821. l:=l div 10;
  822. end;
  823. end;
  824. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  825. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  826. procedure SysResetFpu;
  827. begin
  828. { nothing todo }
  829. end;
  830. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  831. {
  832. $Log$
  833. Revision 1.50 2003-02-18 17:56:06 jonas
  834. - removed buggy i386-specific FPC_CHARARRAY_TO_SHORTSTR
  835. * fixed generic FPC_CHARARRAY_TO_SHORTSTR (web bug 2382)
  836. * fixed some potential range errors in indexchar/word/dword
  837. Revision 1.49 2003/01/20 22:21:36 mazen
  838. * many stuff related to RTL fixed
  839. Revision 1.48 2003/01/09 20:14:20 florian
  840. * fixed helper declarations
  841. Revision 1.47 2003/01/07 22:04:12 mazen
  842. - space removed
  843. Revision 1.46 2003/01/06 23:04:21 mazen
  844. * functions headers modified in generic.inc to make it possible compiling sparc
  845. RTL based on generic code
  846. Revision 1.45 2003/01/05 21:32:35 mazen
  847. * fixing several bugs compiling the RTL
  848. Revision 1.44 2002/12/23 21:27:13 peter
  849. * fix wrong var names for shortstr_compare
  850. Revision 1.43 2002/10/20 11:51:54 carl
  851. * avoid crashes with negative len counts on fills/moves
  852. * movechar0 was wrong and did not do the behavior as
  853. described in docs
  854. Revision 1.42 2002/10/14 19:39:17 peter
  855. * threads unit added for thread support
  856. Revision 1.41 2002/10/12 20:32:41 carl
  857. * RunError 220 -> RunError 219 to be more consistent with as operator
  858. Revision 1.40 2002/10/10 16:08:50 florian
  859. + several widestring/pwidechar related helpers added
  860. Revision 1.39 2002/10/05 14:20:16 peter
  861. * fpc_pchar_length compilerproc and strlen alias
  862. Revision 1.38 2002/10/02 18:21:51 peter
  863. * Copy() changed to internal function calling compilerprocs
  864. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  865. new copy functions
  866. Revision 1.37 2002/09/27 21:10:40 carl
  867. * fix 2GB limit problem
  868. Revision 1.36 2002/09/13 19:13:06 carl
  869. * FPC_HELP_FAIL : reset _self to nil
  870. Revision 1.35 2002/09/10 21:29:44 jonas
  871. * added some missing compilerproc directives
  872. Revision 1.34 2002/09/07 21:08:42 carl
  873. * cardinal -> longword
  874. - remove generic boundcheck (does not exist in v1.1)
  875. Revision 1.33 2002/09/07 15:07:45 peter
  876. * old logs removed and tabs fixed
  877. Revision 1.32 2002/08/19 19:34:02 peter
  878. * SYSTEMINLINE define that will add inline directives for small
  879. functions and wrappers. This will be defined automaticly when
  880. the compiler defines the HASINLINE directive
  881. Revision 1.31 2002/07/29 21:28:16 florian
  882. * several fixes to get further with linux/ppc system unit compilation
  883. Revision 1.30 2002/07/29 09:23:11 jonas
  884. * fixed some datastructures > 2GB
  885. Revision 1.29 2002/07/28 21:39:28 florian
  886. * made abs a compiler proc if it is generic
  887. Revision 1.28 2002/07/28 20:43:47 florian
  888. * several fixes for linux/powerpc
  889. * several fixes to MT
  890. Revision 1.27 2002/06/16 08:19:03 carl
  891. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  892. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  893. Revision 1.25 2002/05/16 19:58:05 carl
  894. * generic constructor implemented
  895. Revision 1.24 2002/03/30 13:08:54 carl
  896. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  897. Revision 1.23 2002/01/25 17:38:55 peter
  898. * add internconst for all overloaded types of Odd/Abs/Sqr
  899. Revision 1.22 2002/01/24 12:33:53 jonas
  900. * adapted ranges of native types to int64 (e.g. high cardinal is no
  901. longer longint($ffffffff), but just $fffffff in psystem)
  902. * small additional fix in 64bit rangecheck code generation for 32 bit
  903. processors
  904. * adaption of ranges required the matching talgorithm used for selecting
  905. which overloaded procedure to call to be adapted. It should now always
  906. select the closest match for ordinal parameters.
  907. + inttostr(qword) in sysstr.inc/sysstrh.inc
  908. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  909. fixes were required to be able to add them)
  910. * is_in_limit() moved from ncal to types unit, should always be used
  911. instead of direct comparisons of low/high values of orddefs because
  912. qword is a special case
  913. }