generic.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 ndef 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] 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 ndef 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 ndef 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] of longint;
  58. wordarray = array [0..maxlongint] 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 ndef 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] 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. longintarray(X)[I-1]:=Value;
  82. Dec(I);
  83. end;
  84. end;
  85. end;
  86. {$endif ndef FPC_SYSTEM_HAS_FILLDWORD}
  87. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR}
  88. function IndexChar(Const buf;len:longint;b:char):longint;
  89. begin
  90. IndexChar:=IndexByte(Buf,Len,byte(B));
  91. end;
  92. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR}
  93. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  94. function IndexByte(Const buf;len:longint;b:byte):longint;
  95. type
  96. bytearray = array [0..maxlongint] of byte;
  97. var
  98. I : longint;
  99. begin
  100. I:=0;
  101. while (bytearray(buf)[I]<>b) and (I<Len) do
  102. inc(I);
  103. if (i=Len) then
  104. i:=-1; {Can't use 0, since it is a possible value}
  105. IndexByte:=I;
  106. end;
  107. {$endif ndef FPC_SYSTEM_HAS_INDEXBYTE}
  108. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  109. function Indexword(Const buf;len:longint;b:word):longint;
  110. type
  111. wordarray = array [0..maxlongint] of word;
  112. var
  113. I : longint;
  114. begin
  115. I:=0;
  116. while (wordarray(buf)[I]<>b) and (I<Len) do
  117. inc(I);
  118. if (i=Len) then
  119. i:=-1; {Can't use 0, since it is a possible value for index}
  120. Indexword:=I;
  121. end;
  122. {$endif ndef FPC_SYSTEM_HAS_INDEXWORD}
  123. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  124. function IndexDWord(Const buf;len:longint;b:DWord):longint;
  125. type
  126. longintarray = array [0..maxlongint] of longint;
  127. var
  128. I : longint;
  129. begin
  130. I:=0;
  131. while (longintarray(buf)[I]<>b) and (I<Len) do inc(I);
  132. if (i=Len) then
  133. i:=-1; {Can't use 0, since it is a possible value for index}
  134. IndexDWord:=I;
  135. end;
  136. {$endif ndef FPC_SYSTEM_HAS_INDEXDWORD}
  137. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR}
  138. function CompareChar(Const buf1,buf2;len:longint):longint;
  139. begin
  140. CompareChar:=CompareByte(buf1,buf2,len);
  141. end;
  142. {$endif ndef FPC_SYSTEM_HAS_COMPARECHAR}
  143. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  144. function CompareByte(Const buf1,buf2;len:longint):longint;
  145. type
  146. bytearray = array [0..maxlongint] of byte;
  147. var
  148. I,J : longint;
  149. begin
  150. I:=0;
  151. if (Len<>0) and (@Buf1<>@Buf2) then
  152. begin
  153. while (bytearray(Buf1)[I]=bytearray(Buf2)[I]) and (I<Len) do
  154. inc(I);
  155. if I=Len then {No difference}
  156. I:=0
  157. else
  158. begin
  159. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  160. if I>0 then
  161. I:=1
  162. else
  163. if I<0 then
  164. I:=-1;
  165. end;
  166. end;
  167. CompareByte:=I;
  168. end;
  169. {$endif ndef FPC_SYSTEM_HAS_COMPAREBYTE}
  170. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  171. function CompareWord(Const buf1,buf2;len:longint):longint;
  172. type
  173. wordarray = array [0..maxlongint] of word;
  174. var
  175. I,J : longint;
  176. begin
  177. I:=0;
  178. if (Len<>0) and (@Buf1<>@Buf2) then
  179. begin
  180. while (wordarray(Buf1)[I]=wordarray(Buf2)[I]) and (I<Len) do
  181. inc(I);
  182. if I=Len then {No difference}
  183. I:=0
  184. else
  185. begin
  186. I:=wordarray(Buf1)[I]-wordarray(Buf2)[I];
  187. if I>0 then
  188. I:=1
  189. else
  190. if I<0 then
  191. I:=-1;
  192. end;
  193. end;
  194. CompareWord:=I;
  195. end;
  196. {$endif ndef FPC_SYSTEM_HAS_COMPAREWORD}
  197. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  198. function CompareDWord(Const buf1,buf2;len:longint):longint;
  199. type
  200. longintarray = array [0..maxlongint] of longint;
  201. var
  202. I,J : longint;
  203. begin
  204. I:=0;
  205. if (Len<>0) and (@Buf1<>@Buf2) then
  206. begin
  207. while (longintarray(Buf1)[I]=longintarray(Buf2)[I]) and (I<Len) do
  208. inc(I);
  209. if I=Len then {No difference}
  210. I:=0
  211. else
  212. begin
  213. I:=longintarray(Buf1)[I]-longintarray(Buf2)[I];
  214. if I>0 then
  215. I:=1
  216. else
  217. if I<0 then
  218. I:=-1;
  219. end;
  220. end;
  221. CompareDWord:=I;
  222. end;
  223. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  224. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  225. procedure MoveChar0(Const buf1;var buf2;len:longint);
  226. var
  227. I : longint;
  228. begin
  229. if Len<> 0 then
  230. begin
  231. I:=IndexByte(Buf1,Len,0);
  232. if I<>0 then
  233. Move(Buf1,Buf2,I);
  234. end;
  235. end;
  236. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  237. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  238. function IndexChar0(Const buf;len:longint;b:Char):longint;
  239. var
  240. I : longint;
  241. begin
  242. if Len<>0 then
  243. begin
  244. I:=IndexByte(Buf,Len,0);
  245. IndexChar0:=IndexByte(Buf,I,0);
  246. end
  247. else
  248. IndexChar0:=0;
  249. end;
  250. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  251. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  252. function CompareChar0(Const buf1,buf2;len:longint):longint;
  253. type
  254. bytearray = array [0..maxlongint] of byte;
  255. Var i : longint;
  256. begin
  257. I:=0;
  258. if (Len<>0) and (@Buf1<>@Buf2) then
  259. begin
  260. while (I<Len) And
  261. ((Pbyte(@Buf1)[i]<>0) and (PByte(@buf2)[i]<>0)) and
  262. (pbyte(@Buf1)[I]=pbyte(@Buf2)[I]) do
  263. inc(I);
  264. if (I=Len) or
  265. (PByte(@Buf1)[i]=0) or
  266. (PByte(@buf2)[I]=0) then {No difference or 0 reached }
  267. I:=0
  268. else
  269. begin
  270. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  271. if I>0 then
  272. I:=1
  273. else
  274. if I<0 then
  275. I:=-1;
  276. end;
  277. end;
  278. CompareChar0:=I;
  279. end;
  280. {$endif ndef FPC_SYSTEM_HAS_COMPARECHAR0}
  281. {****************************************************************************
  282. Object Helpers
  283. ****************************************************************************}
  284. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  285. { Generic code does not set the register used for self !
  286. So this needs to be done by the compiler after calling
  287. FPC_HELP_CONSTRUCTOR : generic allways means aa little less efficient (PM) }
  288. { I don't think we really need to save any registers here }
  289. { since this is called at the start of the constructor (CEC) }
  290. function fpc_help_constructor(var _self : pointer; var vmt : pointer; vmt_pos : cardinal) : pointer; [public,alias:'FPC_HELP_CONSTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  291. type
  292. ppointer = ^pointer;
  293. pvmt = ^tvmt;
  294. tvmt = packed record
  295. size,msize : longint;
  296. parent : pointer;
  297. end;
  298. var
  299. objectsize : longint;
  300. vmtcopy : pointer;
  301. begin
  302. if vmt=nil then
  303. begin
  304. fpc_help_constructor:=_self;
  305. exit;
  306. end;
  307. vmtcopy:=vmt;
  308. objectsize:=pvmt(vmtcopy)^.size;
  309. if _self=nil then
  310. begin
  311. getmem(_self,objectsize);
  312. longint(vmt):=-1; { needed for fail }
  313. end;
  314. if _self<>nil then
  315. begin
  316. fillchar(_self^,objectsize,#0);
  317. ppointer(_self+vmt_pos)^:=vmtcopy;
  318. end;
  319. fpc_help_constructor:=_self;
  320. end;
  321. {$endif ndef 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);saveregisters;[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 ndef 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);safecall; [public,alias:'FPC_HELP_FAIL'];
  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. end;
  377. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  378. {$ifndef FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  379. { the constructor receives as first parameter a pointer }
  380. { to the vmt or nil, if called when class already instanciated }
  381. { RETURNS SELF }
  382. { IMPORTANT: SELF REGISTER should be pre-loaded before call to }
  383. { constructor for this to work! }
  384. function fpc_new_class(_vmt: pointer; _self : pointer):pointer;saveregisters;[public,alias:'FPC_NEW_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  385. begin
  386. if _vmt <> nil then
  387. begin
  388. fpc_new_class := tclass(_vmt).NewInstance;
  389. end
  390. else
  391. begin
  392. { calling when class already instanciated }
  393. { then simply returna a boolean value <> 0 }
  394. fpc_new_class := _self;
  395. end;
  396. end;
  397. {$endif FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  398. {$ifndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  399. procedure fpc_dispose_class(_self: tobject; flag : longint);saveregisters;[public,alias:'FPC_DISPOSE_CLASS'];
  400. begin
  401. if (_self <> nil) and (flag = 1) then
  402. _self.FreeInstance;
  403. end;
  404. {$endif ndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  405. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  406. procedure fpc_check_object(vmt : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  407. type
  408. pvmt = ^tvmt;
  409. tvmt = packed record
  410. size,msize : longint;
  411. parent : pointer;
  412. end;
  413. begin
  414. (* if (vmt=nil) or
  415. (pvmt(vmt)^.size=0) or
  416. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  417. RunError(210);*)
  418. end;
  419. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  420. { checks for a correct vmt pointer }
  421. { deeper check to see if the current object is }
  422. { really related to the true }
  423. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  424. procedure fpc_check_object_ext(vmt, expvmt : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  425. type
  426. pvmt = ^tvmt;
  427. tvmt = packed record
  428. size,msize : longint;
  429. parent : pointer;
  430. end;
  431. begin
  432. if (vmt=nil) or
  433. (pvmt(vmt)^.size=0) or
  434. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  435. RunError(210);
  436. while assigned(vmt) do
  437. if vmt=expvmt then
  438. exit
  439. else
  440. vmt:=pvmt(vmt)^.parent;
  441. RunError(220);
  442. end;
  443. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  444. {****************************************************************************
  445. String
  446. ****************************************************************************}
  447. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  448. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  449. var
  450. slen : byte;
  451. begin
  452. { these are shortstrings, not pointers! (JM)
  453. if dstr=nil then
  454. exit;
  455. if sstr=nil then
  456. begin
  457. if dstr<>nil then
  458. pstring(dstr)^[0]:=#0;
  459. exit;
  460. end;
  461. }
  462. slen:=length(sstr);
  463. if slen<len then
  464. len:=slen;
  465. { don't forget the length character }
  466. if len <> 0 then
  467. move(sstr[0],result[0],len+1);
  468. end;
  469. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY'];
  470. var
  471. slen : byte;
  472. type
  473. pstring = ^string;
  474. begin
  475. { these are shortstrings, not pointers! (JM)
  476. if dstr=nil then
  477. exit;
  478. if sstr=nil then
  479. begin
  480. if dstr<>nil then
  481. pstring(dstr)^[0]:=#0;
  482. exit;
  483. end;
  484. }
  485. slen:=length(pstring(sstr)^);
  486. if slen<len then
  487. len:=slen;
  488. { don't forget the length character }
  489. if len <> 0 then
  490. move(sstr^,dstr^,len+1);
  491. { already done by the move above (JM)
  492. pstring(dstr)^[0]:=chr(len);
  493. }
  494. end;
  495. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COPY}
  496. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  497. { note: this routine is *DIFFERENT* from the routine in i386.inc and as such you }
  498. { cannot use it with the i386 compiler, unless you remove the }
  499. { ti386addnode.first_string method (JM) }
  500. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT'];
  501. var
  502. s1l, s2l : byte;
  503. begin
  504. { these are shortstrings, they can't be nil! (JM)
  505. if (s1=nil) or (s2=nil) then
  506. exit;
  507. }
  508. s1l:=length(s1);
  509. s2l:=length(s2);
  510. if s1l+s2l>255 then
  511. s2l:=255-s1l;
  512. fpc_shortstr_concat := s1;
  513. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  514. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  515. end;
  516. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  517. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  518. function fpc_shortstr_compare(const rightstr,leftstr:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  519. var
  520. s1,s2,max,i : byte;
  521. d : longint;
  522. begin
  523. s1:=length(rightstr);
  524. s2:=length(leftstr);
  525. if s1<s2 then
  526. max:=s1
  527. else
  528. max:=s2;
  529. for i:=1 to max do
  530. begin
  531. d:=byte(leftstr[i])-byte(rightstr[i]);
  532. if d>0 then
  533. exit(1)
  534. else if d<0 then
  535. exit(-1);
  536. end;
  537. if s1>s2 then
  538. exit(1)
  539. else if s1<s2 then
  540. exit(-1)
  541. else
  542. exit(0);
  543. end;
  544. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  545. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  546. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  547. var
  548. l : longint;
  549. s: shortstring;
  550. begin
  551. if p=nil then
  552. l:=0
  553. else
  554. l:=strlen(p);
  555. if l>255 then
  556. l:=255;
  557. if l>0 then
  558. move(p^,s[1],l);
  559. s[0]:=chr(l);
  560. fpc_pchar_to_shortstr := s;
  561. end;
  562. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  563. { also add a strpas alias for internal use in the system unit (JM) }
  564. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  565. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  566. {$ifdef hascompilerproc}
  567. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  568. var
  569. l: longint;
  570. {$else hascompilerproc}
  571. function fpc_chararray_to_shortstr(p:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  572. {$endif hascompilerproc}
  573. begin
  574. {$ifdef hascompilerproc}
  575. l := high(arr)+1;
  576. {$endif hascompilerproc}
  577. if l>=256 then
  578. l:=255
  579. else if l<0 then
  580. l:=0;
  581. move(arr[0],fpc_chararray_to_shortstr[1],l);
  582. fpc_chararray_to_shortstr[0]:=chr(l);
  583. end;
  584. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  585. {$ifdef hascompilerproc}
  586. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  587. { inside the compiler, the resulttype is modified to that of the actual }
  588. { chararray we're converting to (JM) }
  589. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  590. var
  591. len: longint;
  592. begin
  593. len := length(src);
  594. if len > arraysize then
  595. len := arraysize;
  596. { make sure we don't access char 1 if length is 0 (JM) }
  597. if len > 0 then
  598. move(src[1],fpc_shortstr_to_chararray[0],len);
  599. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  600. end;
  601. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  602. {$else hascompilerproc}
  603. {$ifopt r+}
  604. {$define rangeon}
  605. {$r-}
  606. {$endif}
  607. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  608. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  609. type
  610. plongint = ^longint;
  611. var
  612. len: longint;
  613. begin
  614. case strtyp of
  615. { shortstring }
  616. 0:
  617. begin
  618. len := byte(src[0]);
  619. inc(src);
  620. end;
  621. {$ifdef SUPPORT_ANSISTRING}
  622. { ansistring}
  623. 1: len := length(ansistring(pointer(src)));
  624. {$endif SUPPORT_ANSISTRING}
  625. { longstring }
  626. 2:;
  627. { widestring }
  628. 3: ;
  629. end;
  630. if len > arraysize then
  631. len := arraysize;
  632. { make sure we don't dereference src if it can be nil (JM) }
  633. if len > 0 then
  634. move(src^,dest^,len);
  635. fillchar(dest[len],arraysize-len,0);
  636. end;
  637. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  638. {$ifdef rangeon}
  639. {$r+}
  640. {undef rangeon}
  641. {$endif rangeon}
  642. {$endif hascompilerproc}
  643. {$ifndef FPC_SYSTEM_HAS_STRLEN}
  644. function strlen(p:pchar):longint;
  645. var i : longint;
  646. begin
  647. i:=0;
  648. while p[i]<>#0 do inc(i);
  649. exit(i);
  650. end;
  651. {$endif ndef FPC_SYSTEM_HAS_STRLEN}
  652. {****************************************************************************
  653. Caller/StackFrame Helpers
  654. ****************************************************************************}
  655. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  656. {$error Get_frame must be defined for each processor }
  657. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  658. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  659. {$error Get_caller_addr must be defined for each processor }
  660. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  661. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  662. {$error Get_caller_frame must be defined for each processor }
  663. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  664. {****************************************************************************
  665. Math
  666. ****************************************************************************}
  667. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  668. function abs(l:longint):longint;[internconst:in_const_abs];
  669. begin
  670. if l<0 then
  671. abs:=-l
  672. else
  673. abs:=l;
  674. end;
  675. {$endif ndef FPC_SYSTEM_HAS_ABS_LONGINT}
  676. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  677. function odd(l:longint):boolean;[internconst:in_const_odd];
  678. begin
  679. odd:=boolean(l and 1);
  680. end;
  681. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  682. {$ifndef FPC_SYSTEM_HAS_ODD_CARDINAL}
  683. function odd(l:cardinal):boolean;[internconst:in_const_odd];
  684. begin
  685. odd:=boolean(l and 1);
  686. end;
  687. {$endif ndef FPC_SYSTEM_HAS_ODD_CARDINAL}
  688. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  689. function odd(l:int64):boolean;[internconst:in_const_odd];
  690. begin
  691. odd:=boolean(longint(l) and 1);
  692. end;
  693. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  694. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  695. function odd(l:qword):boolean;[internconst:in_const_odd];
  696. begin
  697. odd:=boolean(longint(l) and 1);
  698. end;
  699. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  700. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  701. function sqr(l:longint):longint;[internconst:in_const_sqr];
  702. begin
  703. sqr:=l*l;
  704. end;
  705. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  706. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  707. function abs(l: Int64): Int64;[internconst:in_const_abs];
  708. begin
  709. if l < 0 then
  710. abs := -l
  711. else
  712. abs := l;
  713. end;
  714. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  715. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  716. function sqr(l: Int64): Int64;[internconst:in_const_sqr];
  717. begin
  718. sqr := l*l;
  719. end;
  720. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  721. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  722. function sqr(l: QWord): QWord;[internconst:in_const_sqr];
  723. begin
  724. sqr := l*l;
  725. end;
  726. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  727. {$ifndef FPC_SYSTEM_HAS_SPTR}
  728. {$error Sptr must be defined for each processor }
  729. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  730. {****************************************************************************
  731. Str()
  732. ****************************************************************************}
  733. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  734. procedure int_str(l : longint;var s : string);
  735. var
  736. value: longint;
  737. negative: boolean;
  738. begin
  739. negative := false;
  740. s:='';
  741. { Workaround: }
  742. if l=$80000000 then
  743. begin
  744. s:='-2147483648';
  745. exit;
  746. end;
  747. { handle case where l = 0 }
  748. if l = 0 then
  749. begin
  750. s:='0';
  751. exit;
  752. end;
  753. If l < 0 then
  754. begin
  755. negative := true;
  756. value:=abs(l);
  757. end
  758. else
  759. value:=l;
  760. { handle non-zero case }
  761. while value>0 do
  762. begin
  763. s:=char((value mod 10)+ord('0'))+s;
  764. value := value div 10;
  765. end;
  766. if negative then
  767. s := '-' + s;
  768. end;
  769. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  770. {$ifndef FPC_SYSTEM_HAS_INT_STR_CARDINAL}
  771. procedure int_str(l : cardinal;var s : string);
  772. begin
  773. s:='';
  774. if l = 0 then
  775. begin
  776. s := '0';
  777. exit;
  778. end;
  779. while l>0 do
  780. begin
  781. s:=char(ord('0')+(l mod 10))+s;
  782. l:=l div 10;
  783. end;
  784. end;
  785. {$endif ndef FPC_SYSTEM_HAS_INT_STR_CARDINAL}
  786. {****************************************************************************
  787. Bounds Check
  788. ****************************************************************************}
  789. {$ifndef NOBOUNDCHECK}
  790. {$ifndef FPC_SYSTEM_HAS_FPC_BOUNDCHECK}
  791. procedure int_boundcheck(l : longint; range : pointer);[public,alias: 'FPC_BOUNDCHECK'];
  792. type
  793. prange = ^trange;
  794. trange = packed record
  795. min,max : longint;
  796. end;
  797. begin
  798. if (l < prange(range)^.min) or
  799. (l > prange(range)^.max) then
  800. HandleError(201);
  801. end;
  802. {$endif ndef FPC_SYSTEM_HAS_FPC_BOUNDCHECK}
  803. {$endif NOBOUNDCHECK}
  804. {****************************************************************************
  805. IoCheck
  806. ****************************************************************************}
  807. {
  808. $Log$
  809. Revision 1.27 2002-06-16 08:19:03 carl
  810. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  811. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  812. Revision 1.25 2002/05/16 19:58:05 carl
  813. * generic constructor implemented
  814. Revision 1.24 2002/03/30 13:08:54 carl
  815. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  816. Revision 1.23 2002/01/25 17:38:55 peter
  817. * add internconst for all overloaded types of Odd/Abs/Sqr
  818. Revision 1.22 2002/01/24 12:33:53 jonas
  819. * adapted ranges of native types to int64 (e.g. high cardinal is no
  820. longer longint($ffffffff), but just $fffffff in psystem)
  821. * small additional fix in 64bit rangecheck code generation for 32 bit
  822. processors
  823. * adaption of ranges required the matching talgorithm used for selecting
  824. which overloaded procedure to call to be adapted. It should now always
  825. select the closest match for ordinal parameters.
  826. + inttostr(qword) in sysstr.inc/sysstrh.inc
  827. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  828. fixes were required to be able to add them)
  829. * is_in_limit() moved from ncal to types unit, should always be used
  830. instead of direct comparisons of low/high values of orddefs because
  831. qword is a special case
  832. Revision 1.21 2001/09/03 13:27:43 jonas
  833. * compilerproc implementation of set addition/substraction/...
  834. * changed the declaration of some set helpers somewhat to accomodate the
  835. above change
  836. * i386 still uses the old code for comparisons of sets, because its
  837. helpers return the results in the flags
  838. * dummy tc_normal_2_small_set type conversion because I need the original
  839. resulttype of the set add nodes
  840. NOTE: you have to start a cycle with 1.0.5!
  841. Revision 1.20 2001/08/30 15:43:15 jonas
  842. * converted adding/comparing of strings to compileproc. Note that due
  843. to the way the shortstring helpers for i386 are written, they are
  844. still handled by the old code (reason: fpc_shortstr_compare returns
  845. results in the flags instead of in eax and fpc_shortstr_concat
  846. has wierd parameter conventions). The compilerproc stuff should work
  847. fine with the generic implementations though.
  848. * removed some nested comments warnings
  849. Revision 1.19 2001/08/29 19:49:04 jonas
  850. * some fixes in compilerprocs for chararray to string conversions
  851. * conversion from string to chararray is now also done via compilerprocs
  852. Revision 1.18 2001/08/28 13:24:47 jonas
  853. + compilerproc implementation of most string-related type conversions
  854. - removed all code from the compiler which has been replaced by
  855. compilerproc implementations (using (ifdef hascompilerproc) is not
  856. necessary in the compiler)
  857. Revision 1.17 2001/08/01 15:00:10 jonas
  858. + "compproc" helpers
  859. * renamed several helpers so that their name is the same as their
  860. "public alias", which should facilitate the conversion of processor
  861. specific code in the code generator to processor independent code
  862. * some small fixes to the val_ansistring and val_widestring helpers
  863. (always immediately exit if the source string is longer than 255
  864. chars)
  865. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  866. still nil (used to crash, now return resp -1 and 0)
  867. Revision 1.16 2001/07/31 19:36:51 peter
  868. * small cleanup of commented code (merged)
  869. Revision 1.15 2001/07/29 13:49:15 peter
  870. * m68k updates merged
  871. Revision 1.14 2001/07/08 21:00:18 peter
  872. * various widestring updates, it works now mostly without charset
  873. mapping supported
  874. Revision 1.13 2001/05/28 20:43:17 peter
  875. * more saveregisters added (merged)
  876. Revision 1.12 2001/05/18 22:59:59 peter
  877. * merged fixes branch fixes
  878. Revision 1.11 2001/05/16 17:44:25 jonas
  879. + odd() for cardinal, int64 and qword (merged)
  880. Revision 1.10 2001/05/09 19:57:07 peter
  881. *** empty log message ***
  882. Revision 1.9 2001/04/21 12:16:28 peter
  883. * int_str cardinal fix (merged)
  884. Revision 1.8 2001/04/13 18:06:28 peter
  885. * removed rtllite define
  886. Revision 1.7 2001/03/05 17:10:40 jonas
  887. * changed typecast in FPC_STR_TO_CHARARRAY so that no temp ansistring is
  888. generated anymore (merged)
  889. Revision 1.6 2001/03/03 12:41:22 jonas
  890. * simplified and optimized range checking code, FPC_BOUNDCHECK is no longer necessary
  891. Revision 1.5 2000/10/01 13:17:35 michael
  892. + Merged from fixbranch
  893. Revision 1.4 2000/08/09 11:29:01 jonas
  894. Revision 1.1.2.2 2000/10/01 13:14:50 michael
  895. + Corrected and (hopefully) improved compare0
  896. Revision 1.1.2.1 2000/08/09 11:21:32 jonas
  897. + FPC_STR_TO_CHARARRAY routine necessary for string -> chararray
  898. conversions fix (merged fropm fixes branch)
  899. Revision 1.3 2000/07/14 10:33:10 michael
  900. + Conditionals fixed
  901. Revision 1.2 2000/07/13 11:33:43 michael
  902. + removed logs
  903. }