generic.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. 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;saveregisters;[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);saveregisters;[public,alias:'FPC_DISPOSE_CLASS'];
  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);saveregisters;[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);saveregisters;[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(220);
  443. end;
  444. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  445. {****************************************************************************
  446. String
  447. ****************************************************************************}
  448. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  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. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY'];
  471. var
  472. slen : byte;
  473. type
  474. pstring = ^string;
  475. begin
  476. { these are shortstrings, not pointers! (JM)
  477. if dstr=nil then
  478. exit;
  479. if sstr=nil then
  480. begin
  481. if dstr<>nil then
  482. pstring(dstr)^[0]:=#0;
  483. exit;
  484. end;
  485. }
  486. slen:=length(pstring(sstr)^);
  487. if slen<len then
  488. len:=slen;
  489. { don't forget the length character }
  490. if len <> 0 then
  491. move(sstr^,dstr^,len+1);
  492. { already done by the move above (JM)
  493. pstring(dstr)^[0]:=chr(len);
  494. }
  495. end;
  496. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  497. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  498. { note: this routine is *DIFFERENT* from the routine in i386.inc and as such you }
  499. { cannot use it with the i386 compiler, unless you remove the }
  500. { ti386addnode.first_string method (JM) }
  501. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT'];
  502. var
  503. s1l, s2l : byte;
  504. begin
  505. { these are shortstrings, they can't be nil! (JM)
  506. if (s1=nil) or (s2=nil) then
  507. exit;
  508. }
  509. s1l:=length(s1);
  510. s2l:=length(s2);
  511. if s1l+s2l>255 then
  512. s2l:=255-s1l;
  513. fpc_shortstr_concat := s1;
  514. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  515. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  516. end;
  517. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  518. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  519. function fpc_shortstr_compare(const rightstr,leftstr:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  520. var
  521. s1,s2,max,i : byte;
  522. d : longint;
  523. begin
  524. s1:=length(rightstr);
  525. s2:=length(leftstr);
  526. if s1<s2 then
  527. max:=s1
  528. else
  529. max:=s2;
  530. for i:=1 to max do
  531. begin
  532. d:=byte(leftstr[i])-byte(rightstr[i]);
  533. if d>0 then
  534. exit(1)
  535. else if d<0 then
  536. exit(-1);
  537. end;
  538. if s1>s2 then
  539. exit(1)
  540. else if s1<s2 then
  541. exit(-1)
  542. else
  543. exit(0);
  544. end;
  545. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  546. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  547. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  548. var
  549. l : longint;
  550. s: shortstring;
  551. begin
  552. if p=nil then
  553. l:=0
  554. else
  555. l:=strlen(p);
  556. if l>255 then
  557. l:=255;
  558. if l>0 then
  559. move(p^,s[1],l);
  560. s[0]:=chr(l);
  561. fpc_pchar_to_shortstr := s;
  562. end;
  563. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  564. { also add a strpas alias for internal use in the system unit (JM) }
  565. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  566. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  567. {$ifdef hascompilerproc}
  568. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  569. var
  570. l: longint;
  571. {$else hascompilerproc}
  572. function fpc_chararray_to_shortstr(p:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  573. {$endif hascompilerproc}
  574. begin
  575. {$ifdef hascompilerproc}
  576. l := high(arr)+1;
  577. {$endif hascompilerproc}
  578. if l>=256 then
  579. l:=255
  580. else if l<0 then
  581. l:=0;
  582. move(arr[0],fpc_chararray_to_shortstr[1],l);
  583. fpc_chararray_to_shortstr[0]:=chr(l);
  584. end;
  585. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  586. {$ifdef hascompilerproc}
  587. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  588. { inside the compiler, the resulttype is modified to that of the actual }
  589. { chararray we're converting to (JM) }
  590. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  591. var
  592. len: longint;
  593. begin
  594. len := length(src);
  595. if len > arraysize then
  596. len := arraysize;
  597. { make sure we don't access char 1 if length is 0 (JM) }
  598. if len > 0 then
  599. move(src[1],fpc_shortstr_to_chararray[0],len);
  600. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  601. end;
  602. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  603. {$else hascompilerproc}
  604. {$ifopt r+}
  605. {$define rangeon}
  606. {$r-}
  607. {$endif}
  608. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  609. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  610. type
  611. plongint = ^longint;
  612. var
  613. len: longint;
  614. begin
  615. case strtyp of
  616. { shortstring }
  617. 0:
  618. begin
  619. len := byte(src[0]);
  620. inc(src);
  621. end;
  622. {$ifdef SUPPORT_ANSISTRING}
  623. { ansistring}
  624. 1: len := length(ansistring(pointer(src)));
  625. {$endif SUPPORT_ANSISTRING}
  626. { longstring }
  627. 2:;
  628. { widestring }
  629. 3: ;
  630. end;
  631. if len > arraysize then
  632. len := arraysize;
  633. { make sure we don't dereference src if it can be nil (JM) }
  634. if len > 0 then
  635. move(src^,dest^,len);
  636. fillchar(dest[len],arraysize-len,0);
  637. end;
  638. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  639. {$ifdef rangeon}
  640. {$r+}
  641. {undef rangeon}
  642. {$endif rangeon}
  643. {$endif hascompilerproc}
  644. {$ifndef FPC_SYSTEM_HAS_STRLEN}
  645. function strlen(p:pchar):longint;
  646. var i : longint;
  647. begin
  648. i:=0;
  649. while p[i]<>#0 do inc(i);
  650. exit(i);
  651. end;
  652. {$endif ndef FPC_SYSTEM_HAS_STRLEN}
  653. {****************************************************************************
  654. Caller/StackFrame Helpers
  655. ****************************************************************************}
  656. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  657. {$error Get_frame must be defined for each processor }
  658. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  659. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  660. {$error Get_caller_addr must be defined for each processor }
  661. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  662. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  663. {$error Get_caller_frame must be defined for each processor }
  664. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  665. {****************************************************************************
  666. Math
  667. ****************************************************************************}
  668. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  669. function abs(l:longint):longint;[internconst:in_const_abs];
  670. begin
  671. if l<0 then
  672. abs:=-l
  673. else
  674. abs:=l;
  675. end;
  676. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  677. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  678. function odd(l:longint):boolean;[internconst:in_const_odd];
  679. begin
  680. odd:=boolean(l and 1);
  681. end;
  682. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  683. {$ifndef FPC_SYSTEM_HAS_ODD_CARDINAL}
  684. function odd(l:cardinal):boolean;[internconst:in_const_odd];
  685. begin
  686. odd:=boolean(l and 1);
  687. end;
  688. {$endif ndef FPC_SYSTEM_HAS_ODD_CARDINAL}
  689. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  690. function odd(l:int64):boolean;[internconst:in_const_odd];
  691. begin
  692. odd:=boolean(longint(l) and 1);
  693. end;
  694. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  695. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  696. function odd(l:qword):boolean;[internconst:in_const_odd];
  697. begin
  698. odd:=boolean(longint(l) and 1);
  699. end;
  700. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  701. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  702. function sqr(l:longint):longint;[internconst:in_const_sqr];
  703. begin
  704. sqr:=l*l;
  705. end;
  706. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  707. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  708. function abs(l: Int64): Int64;[internconst:in_const_abs];
  709. begin
  710. if l < 0 then
  711. abs := -l
  712. else
  713. abs := l;
  714. end;
  715. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  716. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  717. function sqr(l: Int64): Int64;[internconst:in_const_sqr];
  718. begin
  719. sqr := l*l;
  720. end;
  721. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  722. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  723. function sqr(l: QWord): QWord;[internconst:in_const_sqr];
  724. begin
  725. sqr := l*l;
  726. end;
  727. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  728. {$ifndef FPC_SYSTEM_HAS_SPTR}
  729. {$error Sptr must be defined for each processor }
  730. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  731. {****************************************************************************
  732. Str()
  733. ****************************************************************************}
  734. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  735. procedure int_str(l : longint;var s : string);
  736. var
  737. value: longint;
  738. negative: boolean;
  739. begin
  740. negative := false;
  741. s:='';
  742. { Workaround: }
  743. if l=$80000000 then
  744. begin
  745. s:='-2147483648';
  746. exit;
  747. end;
  748. { handle case where l = 0 }
  749. if l = 0 then
  750. begin
  751. s:='0';
  752. exit;
  753. end;
  754. If l < 0 then
  755. begin
  756. negative := true;
  757. value:=abs(l);
  758. end
  759. else
  760. value:=l;
  761. { handle non-zero case }
  762. while value>0 do
  763. begin
  764. s:=char((value mod 10)+ord('0'))+s;
  765. value := value div 10;
  766. end;
  767. if negative then
  768. s := '-' + s;
  769. end;
  770. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  771. {$ifndef FPC_SYSTEM_HAS_INT_STR_CARDINAL}
  772. procedure int_str(l : cardinal;var s : string);
  773. begin
  774. s:='';
  775. if l = 0 then
  776. begin
  777. s := '0';
  778. exit;
  779. end;
  780. while l>0 do
  781. begin
  782. s:=char(ord('0')+(l mod 10))+s;
  783. l:=l div 10;
  784. end;
  785. end;
  786. {$endif ndef FPC_SYSTEM_HAS_INT_STR_CARDINAL}
  787. {****************************************************************************
  788. Bounds Check
  789. ****************************************************************************}
  790. {$ifndef NOBOUNDCHECK}
  791. {$ifndef FPC_SYSTEM_HAS_FPC_BOUNDCHECK}
  792. procedure int_boundcheck(l : longint; range : pointer);[public,alias: 'FPC_BOUNDCHECK'];
  793. type
  794. prange = ^trange;
  795. trange = packed record
  796. min,max : longint;
  797. end;
  798. begin
  799. if (l < prange(range)^.min) or
  800. (l > prange(range)^.max) then
  801. HandleError(201);
  802. end;
  803. {$endif ndef FPC_SYSTEM_HAS_FPC_BOUNDCHECK}
  804. {$endif NOBOUNDCHECK}
  805. {****************************************************************************
  806. IoCheck
  807. ****************************************************************************}
  808. {
  809. $Log$
  810. Revision 1.31 2002-07-29 21:28:16 florian
  811. * several fixes to get further with linux/ppc system unit compilation
  812. Revision 1.30 2002/07/29 09:23:11 jonas
  813. * fixed some datastructures > 2GB
  814. Revision 1.29 2002/07/28 21:39:28 florian
  815. * made abs a compiler proc if it is generic
  816. Revision 1.28 2002/07/28 20:43:47 florian
  817. * several fixes for linux/powerpc
  818. * several fixes to MT
  819. Revision 1.27 2002/06/16 08:19:03 carl
  820. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  821. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  822. Revision 1.25 2002/05/16 19:58:05 carl
  823. * generic constructor implemented
  824. Revision 1.24 2002/03/30 13:08:54 carl
  825. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  826. Revision 1.23 2002/01/25 17:38:55 peter
  827. * add internconst for all overloaded types of Odd/Abs/Sqr
  828. Revision 1.22 2002/01/24 12:33:53 jonas
  829. * adapted ranges of native types to int64 (e.g. high cardinal is no
  830. longer longint($ffffffff), but just $fffffff in psystem)
  831. * small additional fix in 64bit rangecheck code generation for 32 bit
  832. processors
  833. * adaption of ranges required the matching talgorithm used for selecting
  834. which overloaded procedure to call to be adapted. It should now always
  835. select the closest match for ordinal parameters.
  836. + inttostr(qword) in sysstr.inc/sysstrh.inc
  837. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  838. fixes were required to be able to add them)
  839. * is_in_limit() moved from ncal to types unit, should always be used
  840. instead of direct comparisons of low/high values of orddefs because
  841. qword is a special case
  842. Revision 1.21 2001/09/03 13:27:43 jonas
  843. * compilerproc implementation of set addition/substraction/...
  844. * changed the declaration of some set helpers somewhat to accomodate the
  845. above change
  846. * i386 still uses the old code for comparisons of sets, because its
  847. helpers return the results in the flags
  848. * dummy tc_normal_2_small_set type conversion because I need the original
  849. resulttype of the set add nodes
  850. NOTE: you have to start a cycle with 1.0.5!
  851. Revision 1.20 2001/08/30 15:43:15 jonas
  852. * converted adding/comparing of strings to compileproc. Note that due
  853. to the way the shortstring helpers for i386 are written, they are
  854. still handled by the old code (reason: fpc_shortstr_compare returns
  855. results in the flags instead of in eax and fpc_shortstr_concat
  856. has wierd parameter conventions). The compilerproc stuff should work
  857. fine with the generic implementations though.
  858. * removed some nested comments warnings
  859. Revision 1.19 2001/08/29 19:49:04 jonas
  860. * some fixes in compilerprocs for chararray to string conversions
  861. * conversion from string to chararray is now also done via compilerprocs
  862. Revision 1.18 2001/08/28 13:24:47 jonas
  863. + compilerproc implementation of most string-related type conversions
  864. - removed all code from the compiler which has been replaced by
  865. compilerproc implementations (using (ifdef hascompilerproc) is not
  866. necessary in the compiler)
  867. Revision 1.17 2001/08/01 15:00:10 jonas
  868. + "compproc" helpers
  869. * renamed several helpers so that their name is the same as their
  870. "public alias", which should facilitate the conversion of processor
  871. specific code in the code generator to processor independent code
  872. * some small fixes to the val_ansistring and val_widestring helpers
  873. (always immediately exit if the source string is longer than 255
  874. chars)
  875. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  876. still nil (used to crash, now return resp -1 and 0)
  877. Revision 1.16 2001/07/31 19:36:51 peter
  878. * small cleanup of commented code (merged)
  879. Revision 1.15 2001/07/29 13:49:15 peter
  880. * m68k updates merged
  881. Revision 1.14 2001/07/08 21:00:18 peter
  882. * various widestring updates, it works now mostly without charset
  883. mapping supported
  884. Revision 1.13 2001/05/28 20:43:17 peter
  885. * more saveregisters added (merged)
  886. Revision 1.12 2001/05/18 22:59:59 peter
  887. * merged fixes branch fixes
  888. Revision 1.11 2001/05/16 17:44:25 jonas
  889. + odd() for cardinal, int64 and qword (merged)
  890. Revision 1.10 2001/05/09 19:57:07 peter
  891. *** empty log message ***
  892. Revision 1.9 2001/04/21 12:16:28 peter
  893. * int_str cardinal fix (merged)
  894. Revision 1.8 2001/04/13 18:06:28 peter
  895. * removed rtllite define
  896. Revision 1.7 2001/03/05 17:10:40 jonas
  897. * changed typecast in FPC_STR_TO_CHARARRAY so that no temp ansistring is
  898. generated anymore (merged)
  899. Revision 1.6 2001/03/03 12:41:22 jonas
  900. * simplified and optimized range checking code, FPC_BOUNDCHECK is no longer necessary
  901. Revision 1.5 2000/10/01 13:17:35 michael
  902. + Merged from fixbranch
  903. Revision 1.4 2000/08/09 11:29:01 jonas
  904. Revision 1.1.2.2 2000/10/01 13:14:50 michael
  905. + Corrected and (hopefully) improved compare0
  906. Revision 1.1.2.1 2000/08/09 11:21:32 jonas
  907. + FPC_STR_TO_CHARARRAY routine necessary for string -> chararray
  908. conversions fix (merged fropm fixes branch)
  909. Revision 1.3 2000/07/14 10:33:10 michael
  910. + Conditionals fixed
  911. Revision 1.2 2000/07/13 11:33:43 michael
  912. + removed logs
  913. }