generic.inc 27 KB

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