generic.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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] 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] 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] 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] 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] 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] 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] 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] 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] 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_COPY}
  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. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  472. var
  473. slen : byte;
  474. type
  475. pstring = ^string;
  476. begin
  477. { these are shortstrings, not pointers! (JM)
  478. if dstr=nil then
  479. exit;
  480. if sstr=nil then
  481. begin
  482. if dstr<>nil then
  483. pstring(dstr)^[0]:=#0;
  484. exit;
  485. end;
  486. }
  487. slen:=length(pstring(sstr)^);
  488. if slen<len then
  489. len:=slen;
  490. { don't forget the length character }
  491. if len <> 0 then
  492. move(sstr^,dstr^,len+1);
  493. { already done by the move above (JM)
  494. pstring(dstr)^[0]:=chr(len);
  495. }
  496. end;
  497. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  498. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  499. { note: this routine is *DIFFERENT* from the routine in i386.inc and as such you }
  500. { cannot use it with the i386 compiler, unless you remove the }
  501. { ti386addnode.first_string method (JM) }
  502. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  503. var
  504. s1l, s2l : byte;
  505. begin
  506. { these are shortstrings, they can't be nil! (JM)
  507. if (s1=nil) or (s2=nil) then
  508. exit;
  509. }
  510. s1l:=length(s1);
  511. s2l:=length(s2);
  512. if s1l+s2l>255 then
  513. s2l:=255-s1l;
  514. fpc_shortstr_concat := s1;
  515. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  516. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  517. end;
  518. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  519. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  520. function fpc_shortstr_compare(const rightstr,leftstr:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  521. var
  522. s1,s2,max,i : byte;
  523. d : longint;
  524. begin
  525. s1:=length(rightstr);
  526. s2:=length(leftstr);
  527. if s1<s2 then
  528. max:=s1
  529. else
  530. max:=s2;
  531. for i:=1 to max do
  532. begin
  533. d:=byte(leftstr[i])-byte(rightstr[i]);
  534. if d>0 then
  535. exit(1)
  536. else if d<0 then
  537. exit(-1);
  538. end;
  539. if s1>s2 then
  540. exit(1)
  541. else if s1<s2 then
  542. exit(-1)
  543. else
  544. exit(0);
  545. end;
  546. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  547. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  548. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  549. var
  550. l : longint;
  551. s: shortstring;
  552. begin
  553. if p=nil then
  554. l:=0
  555. else
  556. l:=strlen(p);
  557. if l>255 then
  558. l:=255;
  559. if l>0 then
  560. move(p^,s[1],l);
  561. s[0]:=chr(l);
  562. fpc_pchar_to_shortstr := s;
  563. end;
  564. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  565. { also add a strpas alias for internal use in the system unit (JM) }
  566. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  567. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  568. {$ifdef hascompilerproc}
  569. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  570. var
  571. l: longint;
  572. {$else hascompilerproc}
  573. function fpc_chararray_to_shortstr(p:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  574. {$endif hascompilerproc}
  575. begin
  576. {$ifdef hascompilerproc}
  577. l := high(arr)+1;
  578. {$endif hascompilerproc}
  579. if l>=256 then
  580. l:=255
  581. else if l<0 then
  582. l:=0;
  583. move(arr[0],fpc_chararray_to_shortstr[1],l);
  584. fpc_chararray_to_shortstr[0]:=chr(l);
  585. end;
  586. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  587. {$ifdef hascompilerproc}
  588. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  589. { inside the compiler, the resulttype is modified to that of the actual }
  590. { chararray we're converting to (JM) }
  591. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  592. var
  593. len: longint;
  594. begin
  595. len := length(src);
  596. if len > arraysize then
  597. len := arraysize;
  598. { make sure we don't access char 1 if length is 0 (JM) }
  599. if len > 0 then
  600. move(src[1],fpc_shortstr_to_chararray[0],len);
  601. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  602. end;
  603. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  604. {$else hascompilerproc}
  605. {$ifopt r+}
  606. {$define rangeon}
  607. {$r-}
  608. {$endif}
  609. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  610. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  611. type
  612. plongint = ^longint;
  613. var
  614. len: longint;
  615. begin
  616. case strtyp of
  617. { shortstring }
  618. 0:
  619. begin
  620. len := byte(src[0]);
  621. inc(src);
  622. end;
  623. {$ifdef SUPPORT_ANSISTRING}
  624. { ansistring}
  625. 1: len := length(ansistring(pointer(src)));
  626. {$endif SUPPORT_ANSISTRING}
  627. { longstring }
  628. 2:;
  629. { widestring }
  630. 3: ;
  631. end;
  632. if len > arraysize then
  633. len := arraysize;
  634. { make sure we don't dereference src if it can be nil (JM) }
  635. if len > 0 then
  636. move(src^,dest^,len);
  637. fillchar(dest[len],arraysize-len,0);
  638. end;
  639. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  640. {$ifdef rangeon}
  641. {$r+}
  642. {undef rangeon}
  643. {$endif rangeon}
  644. {$endif hascompilerproc}
  645. {$ifndef FPC_SYSTEM_HAS_STRLEN}
  646. function strlen(p:pchar):longint;
  647. var i : longint;
  648. begin
  649. i:=0;
  650. while p[i]<>#0 do inc(i);
  651. exit(i);
  652. end;
  653. {$endif ndef FPC_SYSTEM_HAS_STRLEN}
  654. {****************************************************************************
  655. Caller/StackFrame Helpers
  656. ****************************************************************************}
  657. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  658. {$error Get_frame must be defined for each processor }
  659. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  660. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  661. {$error Get_caller_addr must be defined for each processor }
  662. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  663. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  664. {$error Get_caller_frame must be defined for each processor }
  665. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  666. {****************************************************************************
  667. Math
  668. ****************************************************************************}
  669. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  670. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  671. begin
  672. if l<0 then
  673. abs:=-l
  674. else
  675. abs:=l;
  676. end;
  677. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  678. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  679. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  680. begin
  681. odd:=boolean(l and 1);
  682. end;
  683. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  684. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  685. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  686. begin
  687. odd:=boolean(l and 1);
  688. end;
  689. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  690. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  691. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  692. begin
  693. odd:=boolean(longint(l) and 1);
  694. end;
  695. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  696. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  697. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  698. begin
  699. odd:=boolean(longint(l) and 1);
  700. end;
  701. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  702. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  703. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  704. begin
  705. sqr:=l*l;
  706. end;
  707. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  708. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  709. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  710. begin
  711. if l < 0 then
  712. abs := -l
  713. else
  714. abs := l;
  715. end;
  716. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  717. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  718. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  719. begin
  720. sqr := l*l;
  721. end;
  722. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  723. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  724. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  725. begin
  726. sqr := l*l;
  727. end;
  728. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  729. {$ifndef FPC_SYSTEM_HAS_SPTR}
  730. {$error Sptr must be defined for each processor }
  731. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  732. {****************************************************************************
  733. Str()
  734. ****************************************************************************}
  735. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  736. procedure int_str(l : longint;var s : string);
  737. var
  738. value: longint;
  739. negative: boolean;
  740. begin
  741. negative := false;
  742. s:='';
  743. { Workaround: }
  744. if l=$80000000 then
  745. begin
  746. s:='-2147483648';
  747. exit;
  748. end;
  749. { handle case where l = 0 }
  750. if l = 0 then
  751. begin
  752. s:='0';
  753. exit;
  754. end;
  755. If l < 0 then
  756. begin
  757. negative := true;
  758. value:=abs(l);
  759. end
  760. else
  761. value:=l;
  762. { handle non-zero case }
  763. while value>0 do
  764. begin
  765. s:=char((value mod 10)+ord('0'))+s;
  766. value := value div 10;
  767. end;
  768. if negative then
  769. s := '-' + s;
  770. end;
  771. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  772. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  773. procedure int_str(l : longword;var s : string);
  774. begin
  775. s:='';
  776. if l = 0 then
  777. begin
  778. s := '0';
  779. exit;
  780. end;
  781. while l>0 do
  782. begin
  783. s:=char(ord('0')+(l mod 10))+s;
  784. l:=l div 10;
  785. end;
  786. end;
  787. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  788. {
  789. $Log$
  790. Revision 1.36 2002-09-13 19:13:06 carl
  791. * FPC_HELP_FAIL : reset _self to nil
  792. Revision 1.35 2002/09/10 21:29:44 jonas
  793. * added some missing compilerproc directives
  794. Revision 1.34 2002/09/07 21:08:42 carl
  795. * cardinal -> longword
  796. - remove generic boundcheck (does not exist in v1.1)
  797. Revision 1.33 2002/09/07 15:07:45 peter
  798. * old logs removed and tabs fixed
  799. Revision 1.32 2002/08/19 19:34:02 peter
  800. * SYSTEMINLINE define that will add inline directives for small
  801. functions and wrappers. This will be defined automaticly when
  802. the compiler defines the HASINLINE directive
  803. Revision 1.31 2002/07/29 21:28:16 florian
  804. * several fixes to get further with linux/ppc system unit compilation
  805. Revision 1.30 2002/07/29 09:23:11 jonas
  806. * fixed some datastructures > 2GB
  807. Revision 1.29 2002/07/28 21:39:28 florian
  808. * made abs a compiler proc if it is generic
  809. Revision 1.28 2002/07/28 20:43:47 florian
  810. * several fixes for linux/powerpc
  811. * several fixes to MT
  812. Revision 1.27 2002/06/16 08:19:03 carl
  813. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  814. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  815. Revision 1.25 2002/05/16 19:58:05 carl
  816. * generic constructor implemented
  817. Revision 1.24 2002/03/30 13:08:54 carl
  818. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  819. Revision 1.23 2002/01/25 17:38:55 peter
  820. * add internconst for all overloaded types of Odd/Abs/Sqr
  821. Revision 1.22 2002/01/24 12:33:53 jonas
  822. * adapted ranges of native types to int64 (e.g. high cardinal is no
  823. longer longint($ffffffff), but just $fffffff in psystem)
  824. * small additional fix in 64bit rangecheck code generation for 32 bit
  825. processors
  826. * adaption of ranges required the matching talgorithm used for selecting
  827. which overloaded procedure to call to be adapted. It should now always
  828. select the closest match for ordinal parameters.
  829. + inttostr(qword) in sysstr.inc/sysstrh.inc
  830. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  831. fixes were required to be able to add them)
  832. * is_in_limit() moved from ncal to types unit, should always be used
  833. instead of direct comparisons of low/high values of orddefs because
  834. qword is a special case
  835. }