psystem.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Load the system unit, create required defs for systemunit
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  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. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit psystem;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. symbase;
  23. procedure insertinternsyms(p : tsymtable);
  24. procedure insert_intern_types(p : tsymtable);
  25. procedure readconstdefs;
  26. procedure createconstdefs;
  27. implementation
  28. uses
  29. globals,
  30. symconst,symtype,symsym,symdef,symtable,
  31. ninl;
  32. procedure insertinternsyms(p : tsymtable);
  33. {
  34. all intern procedures for the system unit
  35. }
  36. begin
  37. p.insert(tsyssym.create('Concat',in_concat_x));
  38. p.insert(tsyssym.create('Write',in_write_x));
  39. p.insert(tsyssym.create('WriteLn',in_writeln_x));
  40. p.insert(tsyssym.create('Assigned',in_assigned_x));
  41. p.insert(tsyssym.create('Read',in_read_x));
  42. p.insert(tsyssym.create('ReadLn',in_readln_x));
  43. p.insert(tsyssym.create('Ofs',in_ofs_x));
  44. p.insert(tsyssym.create('SizeOf',in_sizeof_x));
  45. p.insert(tsyssym.create('TypeOf',in_typeof_x));
  46. p.insert(tsyssym.create('Low',in_low_x));
  47. p.insert(tsyssym.create('High',in_high_x));
  48. p.insert(tsyssym.create('Seg',in_seg_x));
  49. p.insert(tsyssym.create('Ord',in_ord_x));
  50. p.insert(tsyssym.create('Pred',in_pred_x));
  51. p.insert(tsyssym.create('Succ',in_succ_x));
  52. p.insert(tsyssym.create('Exclude',in_exclude_x_y));
  53. p.insert(tsyssym.create('Include',in_include_x_y));
  54. p.insert(tsyssym.create('Break',in_break));
  55. p.insert(tsyssym.create('Continue',in_continue));
  56. p.insert(tsyssym.create('Dec',in_dec_x));
  57. p.insert(tsyssym.create('Inc',in_inc_x));
  58. p.insert(tsyssym.create('Str',in_str_x_string));
  59. p.insert(tsyssym.create('Assert',in_assert_x_y));
  60. p.insert(tsyssym.create('Val',in_val_x));
  61. p.insert(tsyssym.create('Addr',in_addr_x));
  62. p.insert(tsyssym.create('TypeInfo',in_typeinfo_x));
  63. p.insert(tsyssym.create('SetLength',in_setlength_x));
  64. p.insert(tsyssym.create('Finalize',in_finalize_x));
  65. p.insert(tsyssym.create('Length',in_length_x));
  66. p.insert(tsyssym.create('New',in_new_x));
  67. p.insert(tsyssym.create('Dispose',in_dispose_x));
  68. end;
  69. procedure insert_intern_types(p : tsymtable);
  70. {
  71. all the types inserted into the system unit
  72. }
  73. function addtype(const s:string;const t:ttype):ttypesym;
  74. begin
  75. result:=ttypesym.create(s,t);
  76. p.insert(result);
  77. { add init/final table if required }
  78. if t.def.needs_inittable then
  79. generate_inittable(result);
  80. end;
  81. procedure adddef(const s:string;def:tdef);
  82. var
  83. t : ttype;
  84. begin
  85. t.setdef(def);
  86. p.insert(ttypesym.create(s,t));
  87. end;
  88. var
  89. { several defs to simulate more or less C++ objects for GDB }
  90. vmttype,
  91. vmtarraytype : ttype;
  92. vmtsymtable : tsymtable;
  93. begin
  94. { Normal types }
  95. addtype('Single',s32floattype);
  96. addtype('Double',s64floattype);
  97. addtype('Extended',s80floattype);
  98. addtype('Real',s64floattype);
  99. {$ifdef i386}
  100. adddef('Comp',tfloatdef.create(s64comp));
  101. {$endif}
  102. addtype('Pointer',voidpointertype);
  103. addtype('FarPointer',voidfarpointertype);
  104. addtype('ShortString',cshortstringtype);
  105. addtype('LongString',clongstringtype);
  106. addtype('AnsiString',cansistringtype);
  107. addtype('WideString',cwidestringtype);
  108. addtype('Boolean',booltype);
  109. addtype('ByteBool',booltype);
  110. adddef('WordBool',torddef.create(bool16bit,0,1));
  111. adddef('LongBool',torddef.create(bool32bit,0,1));
  112. addtype('Char',cchartype);
  113. addtype('WideChar',cwidechartype);
  114. adddef('Text',tfiledef.createtext);
  115. addtype('Cardinal',u32bittype);
  116. addtype('QWord',cu64bittype);
  117. addtype('Int64',cs64bittype);
  118. adddef('TypedFile',tfiledef.createtyped(voidtype));
  119. addtype('Variant',cvarianttype);
  120. { Internal types }
  121. addtype('$formal',cformaltype);
  122. addtype('$void',voidtype);
  123. addtype('$byte',u8bittype);
  124. addtype('$word',u16bittype);
  125. addtype('$ulong',u32bittype);
  126. addtype('$longint',s32bittype);
  127. addtype('$qword',cu64bittype);
  128. addtype('$int64',cs64bittype);
  129. addtype('$char',cchartype);
  130. addtype('$widechar',cwidechartype);
  131. addtype('$shortstring',cshortstringtype);
  132. addtype('$longstring',clongstringtype);
  133. addtype('$ansistring',cansistringtype);
  134. addtype('$widestring',cwidestringtype);
  135. addtype('$openshortstring',openshortstringtype);
  136. addtype('$boolean',booltype);
  137. addtype('$void_pointer',voidpointertype);
  138. addtype('$char_pointer',charpointertype);
  139. addtype('$void_farpointer',voidfarpointertype);
  140. addtype('$openchararray',openchararraytype);
  141. addtype('$file',cfiletype);
  142. addtype('$variant',cvarianttype);
  143. addtype('$s32real',s32floattype);
  144. addtype('$s64real',s64floattype);
  145. addtype('$s80real',s80floattype);
  146. { Add a type for virtual method tables }
  147. vmtsymtable:=trecordsymtable.create;
  148. vmttype.setdef(trecorddef.create(vmtsymtable));
  149. pvmttype.setdef(tpointerdef.create(vmttype));
  150. vmtsymtable.insert(tvarsym.create('$parent',pvmttype));
  151. vmtsymtable.insert(tvarsym.create('$length',s32bittype));
  152. vmtsymtable.insert(tvarsym.create('$mlength',s32bittype));
  153. vmtarraytype.setdef(tarraydef.create(0,1,s32bittype));
  154. tarraydef(vmtarraytype.def).elementtype:=voidpointertype;
  155. vmtsymtable.insert(tvarsym.create('$__pfn',vmtarraytype));
  156. addtype('$__vtbl_ptr_type',vmttype);
  157. addtype('$pvmt',pvmttype);
  158. vmtarraytype.setdef(tarraydef.create(0,1,s32bittype));
  159. tarraydef(vmtarraytype.def).elementtype:=pvmttype;
  160. addtype('$vtblarray',vmtarraytype);
  161. { Add functions that require compiler magic }
  162. insertinternsyms(p);
  163. end;
  164. procedure readconstdefs;
  165. {
  166. Load all default definitions for consts from the system unit
  167. }
  168. begin
  169. globaldef('byte',u8bittype);
  170. globaldef('word',u16bittype);
  171. globaldef('ulong',u32bittype);
  172. globaldef('longint',s32bittype);
  173. globaldef('qword',cu64bittype);
  174. globaldef('int64',cs64bittype);
  175. globaldef('formal',cformaltype);
  176. globaldef('void',voidtype);
  177. globaldef('char',cchartype);
  178. globaldef('widechar',cwidechartype);
  179. globaldef('shortstring',cshortstringtype);
  180. globaldef('longstring',clongstringtype);
  181. globaldef('ansistring',cansistringtype);
  182. globaldef('widestring',cwidestringtype);
  183. globaldef('openshortstring',openshortstringtype);
  184. globaldef('openchararray',openchararraytype);
  185. globaldef('s32real',s32floattype);
  186. globaldef('s64real',s64floattype);
  187. globaldef('s80real',s80floattype);
  188. globaldef('boolean',booltype);
  189. globaldef('void_pointer',voidpointertype);
  190. globaldef('char_pointer',charpointertype);
  191. globaldef('void_farpointer',voidfarpointertype);
  192. globaldef('file',cfiletype);
  193. globaldef('pvmt',pvmttype);
  194. globaldef('variant',cvarianttype);
  195. end;
  196. procedure createconstdefs;
  197. {
  198. Create all default definitions for consts for the system unit
  199. }
  200. var
  201. oldregisterdef : boolean;
  202. begin
  203. { create definitions for constants }
  204. oldregisterdef:=registerdef;
  205. registerdef:=false;
  206. cformaltype.setdef(tformaldef.create);
  207. voidtype.setdef(torddef.create(uvoid,0,0));
  208. u8bittype.setdef(torddef.create(u8bit,0,255));
  209. u16bittype.setdef(torddef.create(u16bit,0,65535));
  210. u32bittype.setdef(torddef.create(u32bit,0,high(cardinal)));
  211. s32bittype.setdef(torddef.create(s32bit,low(longint),high(longint)));
  212. cu64bittype.setdef(torddef.create(u64bit,low(qword),TConstExprInt(high(qword))));
  213. cs64bittype.setdef(torddef.create(s64bit,low(int64),high(int64)));
  214. booltype.setdef(torddef.create(bool8bit,0,1));
  215. cchartype.setdef(torddef.create(uchar,0,255));
  216. cwidechartype.setdef(torddef.create(uwidechar,0,65535));
  217. cshortstringtype.setdef(tstringdef.createshort(255));
  218. { should we give a length to the default long and ansi string definition ?? }
  219. clongstringtype.setdef(tstringdef.createlong(-1));
  220. cansistringtype.setdef(tstringdef.createansi(-1));
  221. cwidestringtype.setdef(tstringdef.createwide(-1));
  222. { length=0 for shortstring is open string (needed for readln(string) }
  223. openshortstringtype.setdef(tstringdef.createshort(0));
  224. openchararraytype.setdef(tarraydef.create(0,-1,s32bittype));
  225. tarraydef(openchararraytype.def).elementtype:=cchartype;
  226. {$ifdef i386}
  227. s32floattype.setdef(tfloatdef.create(s32real));
  228. s64floattype.setdef(tfloatdef.create(s64real));
  229. s80floattype.setdef(tfloatdef.create(s80real));
  230. {$endif}
  231. {$ifdef m68k}
  232. s32floattype.setdef(tfloatdef.create(s32real));
  233. if (cs_fp_emulation in aktmoduleswitches) then
  234. begin
  235. s64floattype.setdef(tfloatdef.create(s32real));
  236. s80floattype.setdef(tfloatdef.create(s32real)))
  237. end
  238. else
  239. begin
  240. s64floattype.setdef(tfloatdef.create(s64real));
  241. s80floattype.setdef(tfloatdef.create(s80real));
  242. end;
  243. {$endif}
  244. { some other definitions }
  245. voidpointertype.setdef(tpointerdef.create(voidtype));
  246. charpointertype.setdef(tpointerdef.create(cchartype));
  247. voidfarpointertype.setdef(tpointerdef.createfar(voidtype));
  248. cfiletype.setdef(tfiledef.createuntyped);
  249. cvarianttype.setdef(tvariantdef.create);
  250. registerdef:=oldregisterdef;
  251. end;
  252. end.
  253. {
  254. $Log$
  255. Revision 1.23 2002-05-12 16:53:09 peter
  256. * moved entry and exitcode to ncgutil and cgobj
  257. * foreach gets extra argument for passing local data to the
  258. iterator function
  259. * -CR checks also class typecasts at runtime by changing them
  260. into as
  261. * fixed compiler to cycle with the -CR option
  262. * fixed stabs with elf writer, finally the global variables can
  263. be watched
  264. * removed a lot of routines from cga unit and replaced them by
  265. calls to cgobj
  266. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  267. u32bit then the other is typecasted also to u32bit without giving
  268. a rangecheck warning/error.
  269. * fixed pascal calling method with reversing also the high tree in
  270. the parast, detected by tcalcst3 test
  271. Revision 1.22 2002/01/24 12:33:53 jonas
  272. * adapted ranges of native types to int64 (e.g. high cardinal is no
  273. longer longint($ffffffff), but just $fffffff in psystem)
  274. * small additional fix in 64bit rangecheck code generation for 32 bit
  275. processors
  276. * adaption of ranges required the matching talgorithm used for selecting
  277. which overloaded procedure to call to be adapted. It should now always
  278. select the closest match for ordinal parameters.
  279. + inttostr(qword) in sysstr.inc/sysstrh.inc
  280. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  281. fixes were required to be able to add them)
  282. * is_in_limit() moved from ncal to types unit, should always be used
  283. instead of direct comparisons of low/high values of orddefs because
  284. qword is a special case
  285. Revision 1.21 2001/11/18 18:43:14 peter
  286. * overloading supported in child classes
  287. * fixed parsing of classes with private and virtual and overloaded
  288. so it is compatible with delphi
  289. Revision 1.20 2001/10/24 11:51:39 marco
  290. * Make new/dispose system functions instead of keywords
  291. Revision 1.19 2001/08/30 20:13:53 peter
  292. * rtti/init table updates
  293. * rttisym for reusable global rtti/init info
  294. * support published for interfaces
  295. Revision 1.18 2001/07/30 20:59:27 peter
  296. * m68k updates from v10 merged
  297. Revision 1.17 2001/07/09 21:15:41 peter
  298. * Length made internal
  299. * Add array support for Length
  300. Revision 1.16 2001/05/09 19:58:45 peter
  301. * m68k doesn't support double (merged)
  302. Revision 1.15 2001/04/13 01:22:13 peter
  303. * symtable change to classes
  304. * range check generation and errors fixed, make cycle DEBUG=1 works
  305. * memory leaks fixed
  306. Revision 1.14 2001/04/02 21:20:34 peter
  307. * resulttype rewrite
  308. Revision 1.13 2001/03/25 12:40:00 florian
  309. * cwidechar was loaded with a chardef, fixed
  310. Revision 1.12 2001/03/22 00:10:58 florian
  311. + basic variant type support in the compiler
  312. Revision 1.11 2000/12/07 17:19:43 jonas
  313. * new constant handling: from now on, hex constants >$7fffffff are
  314. parsed as unsigned constants (otherwise, $80000000 got sign extended
  315. and became $ffffffff80000000), all constants in the longint range
  316. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  317. are cardinals and the rest are int64's.
  318. * added lots of longint typecast to prevent range check errors in the
  319. compiler and rtl
  320. * type casts of symbolic ordinal constants are now preserved
  321. * fixed bug where the original resulttype.def wasn't restored correctly
  322. after doing a 64bit rangecheck
  323. Revision 1.10 2000/11/29 00:30:38 florian
  324. * unused units removed from uses clause
  325. * some changes for widestrings
  326. Revision 1.9 2000/11/09 17:46:56 florian
  327. * System.TypeInfo fixed
  328. + System.Finalize implemented
  329. + some new keywords for interface support added
  330. Revision 1.8 2000/10/31 22:02:51 peter
  331. * symtable splitted, no real code changes
  332. Revision 1.7 2000/10/21 18:16:12 florian
  333. * a lot of changes:
  334. - basic dyn. array support
  335. - basic C++ support
  336. - some work for interfaces done
  337. ....
  338. Revision 1.6 2000/10/14 10:14:52 peter
  339. * moehrendorf oct 2000 rewrite
  340. Revision 1.5 2000/09/24 15:06:24 peter
  341. * use defines.inc
  342. Revision 1.4 2000/08/27 20:19:39 peter
  343. * store strings with case in ppu, when an internal symbol is created
  344. a '$' is prefixed so it's not automatic uppercased
  345. Revision 1.3 2000/08/16 13:06:06 florian
  346. + support of 64 bit integer constants
  347. Revision 1.2 2000/07/13 11:32:47 michael
  348. + removed logs
  349. }