2
0

psystem.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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. procedure registernodes;
  28. implementation
  29. uses
  30. globals,globtype,
  31. symconst,symtype,symsym,symdef,symtable,
  32. node,nbas,nflw,nset,ncon,ncnv,nld,nmem,ncal,nmat,nadd,ninl,nopt;
  33. procedure insertinternsyms(p : tsymtable);
  34. {
  35. all intern procedures for the system unit
  36. }
  37. begin
  38. p.insert(tsyssym.create('Concat',in_concat_x));
  39. p.insert(tsyssym.create('Write',in_write_x));
  40. p.insert(tsyssym.create('WriteLn',in_writeln_x));
  41. p.insert(tsyssym.create('Assigned',in_assigned_x));
  42. p.insert(tsyssym.create('Read',in_read_x));
  43. p.insert(tsyssym.create('ReadLn',in_readln_x));
  44. p.insert(tsyssym.create('Ofs',in_ofs_x));
  45. p.insert(tsyssym.create('SizeOf',in_sizeof_x));
  46. p.insert(tsyssym.create('TypeOf',in_typeof_x));
  47. p.insert(tsyssym.create('Low',in_low_x));
  48. p.insert(tsyssym.create('High',in_high_x));
  49. p.insert(tsyssym.create('Seg',in_seg_x));
  50. p.insert(tsyssym.create('Ord',in_ord_x));
  51. p.insert(tsyssym.create('Pred',in_pred_x));
  52. p.insert(tsyssym.create('Succ',in_succ_x));
  53. p.insert(tsyssym.create('Exclude',in_exclude_x_y));
  54. p.insert(tsyssym.create('Include',in_include_x_y));
  55. p.insert(tsyssym.create('Break',in_break));
  56. p.insert(tsyssym.create('Exit',in_exit));
  57. p.insert(tsyssym.create('Continue',in_continue));
  58. p.insert(tsyssym.create('Dec',in_dec_x));
  59. p.insert(tsyssym.create('Inc',in_inc_x));
  60. p.insert(tsyssym.create('Str',in_str_x_string));
  61. p.insert(tsyssym.create('Assert',in_assert_x_y));
  62. p.insert(tsyssym.create('Val',in_val_x));
  63. p.insert(tsyssym.create('Addr',in_addr_x));
  64. p.insert(tsyssym.create('TypeInfo',in_typeinfo_x));
  65. p.insert(tsyssym.create('SetLength',in_setlength_x));
  66. p.insert(tsyssym.create('Finalize',in_finalize_x));
  67. p.insert(tsyssym.create('Length',in_length_x));
  68. p.insert(tsyssym.create('New',in_new_x));
  69. p.insert(tsyssym.create('Dispose',in_dispose_x));
  70. end;
  71. procedure insert_intern_types(p : tsymtable);
  72. {
  73. all the types inserted into the system unit
  74. }
  75. function addtype(const s:string;const t:ttype):ttypesym;
  76. begin
  77. result:=ttypesym.create(s,t);
  78. p.insert(result);
  79. { add init/final table if required }
  80. if t.def.needs_inittable then
  81. generate_inittable(result);
  82. end;
  83. procedure adddef(const s:string;def:tdef);
  84. var
  85. t : ttype;
  86. begin
  87. t.setdef(def);
  88. p.insert(ttypesym.create(s,t));
  89. end;
  90. var
  91. { several defs to simulate more or less C++ objects for GDB }
  92. vmttype,
  93. vmtarraytype : ttype;
  94. vmtsymtable : tsymtable;
  95. begin
  96. { Normal types }
  97. if (cs_fp_emulation in aktmoduleswitches) then
  98. begin
  99. addtype('Single',s32floattype);
  100. { extended size is the best real type for the target }
  101. addtype('Real',s32floattype);
  102. pbestrealtype:=@s32floattype;
  103. end
  104. else
  105. begin
  106. addtype('Single',s32floattype);
  107. addtype('Double',s64floattype);
  108. { extended size is the best real type for the target }
  109. addtype('Extended',pbestrealtype^);
  110. addtype('Real',s64floattype);
  111. end;
  112. {$ifdef x86}
  113. adddef('Comp',tfloatdef.create(s64comp));
  114. {$endif x86}
  115. addtype('Currency',s64currencytype);
  116. addtype('Pointer',voidpointertype);
  117. addtype('FarPointer',voidfarpointertype);
  118. addtype('ShortString',cshortstringtype);
  119. addtype('LongString',clongstringtype);
  120. addtype('AnsiString',cansistringtype);
  121. addtype('WideString',cwidestringtype);
  122. addtype('Boolean',booltype);
  123. addtype('ByteBool',booltype);
  124. adddef('WordBool',torddef.create(bool16bit,0,1));
  125. adddef('LongBool',torddef.create(bool32bit,0,1));
  126. addtype('Char',cchartype);
  127. addtype('WideChar',cwidechartype);
  128. adddef('Text',tfiledef.createtext);
  129. addtype('Cardinal',u32bittype);
  130. addtype('QWord',cu64bittype);
  131. addtype('Int64',cs64bittype);
  132. adddef('TypedFile',tfiledef.createtyped(voidtype));
  133. addtype('Variant',cvarianttype);
  134. { Internal types }
  135. addtype('$formal',cformaltype);
  136. addtype('$void',voidtype);
  137. addtype('$byte',u8bittype);
  138. addtype('$word',u16bittype);
  139. addtype('$ulong',u32bittype);
  140. addtype('$longint',s32bittype);
  141. addtype('$qword',cu64bittype);
  142. addtype('$int64',cs64bittype);
  143. addtype('$char',cchartype);
  144. addtype('$widechar',cwidechartype);
  145. addtype('$shortstring',cshortstringtype);
  146. addtype('$longstring',clongstringtype);
  147. addtype('$ansistring',cansistringtype);
  148. addtype('$widestring',cwidestringtype);
  149. addtype('$openshortstring',openshortstringtype);
  150. addtype('$boolean',booltype);
  151. addtype('$void_pointer',voidpointertype);
  152. addtype('$char_pointer',charpointertype);
  153. addtype('$void_farpointer',voidfarpointertype);
  154. addtype('$openchararray',openchararraytype);
  155. addtype('$file',cfiletype);
  156. addtype('$variant',cvarianttype);
  157. addtype('$s32real',s32floattype);
  158. addtype('$s64real',s64floattype);
  159. addtype('$s80real',s80floattype);
  160. addtype('$s64currency',s64currencytype);
  161. { Add a type for virtual method tables }
  162. vmtsymtable:=trecordsymtable.create;
  163. vmttype.setdef(trecorddef.create(vmtsymtable));
  164. pvmttype.setdef(tpointerdef.create(vmttype));
  165. vmtsymtable.insert(tvarsym.create('$parent',pvmttype));
  166. vmtsymtable.insert(tvarsym.create('$length',s32bittype));
  167. vmtsymtable.insert(tvarsym.create('$mlength',s32bittype));
  168. vmtarraytype.setdef(tarraydef.create(0,1,s32bittype));
  169. tarraydef(vmtarraytype.def).elementtype:=voidpointertype;
  170. vmtsymtable.insert(tvarsym.create('$__pfn',vmtarraytype));
  171. addtype('$__vtbl_ptr_type',vmttype);
  172. addtype('$pvmt',pvmttype);
  173. vmtarraytype.setdef(tarraydef.create(0,1,s32bittype));
  174. tarraydef(vmtarraytype.def).elementtype:=pvmttype;
  175. addtype('$vtblarray',vmtarraytype);
  176. { Add functions that require compiler magic }
  177. insertinternsyms(p);
  178. end;
  179. procedure readconstdefs;
  180. {
  181. Load all default definitions for consts from the system unit
  182. }
  183. begin
  184. globaldef('byte',u8bittype);
  185. globaldef('word',u16bittype);
  186. globaldef('ulong',u32bittype);
  187. globaldef('longint',s32bittype);
  188. globaldef('qword',cu64bittype);
  189. globaldef('int64',cs64bittype);
  190. globaldef('formal',cformaltype);
  191. globaldef('void',voidtype);
  192. globaldef('char',cchartype);
  193. globaldef('widechar',cwidechartype);
  194. globaldef('shortstring',cshortstringtype);
  195. globaldef('longstring',clongstringtype);
  196. globaldef('ansistring',cansistringtype);
  197. globaldef('widestring',cwidestringtype);
  198. globaldef('openshortstring',openshortstringtype);
  199. globaldef('openchararray',openchararraytype);
  200. globaldef('s32real',s32floattype);
  201. globaldef('s64real',s64floattype);
  202. globaldef('s80real',s80floattype);
  203. globaldef('s64currency',s64currencytype);
  204. globaldef('boolean',booltype);
  205. globaldef('void_pointer',voidpointertype);
  206. globaldef('char_pointer',charpointertype);
  207. globaldef('void_farpointer',voidfarpointertype);
  208. globaldef('file',cfiletype);
  209. globaldef('pvmt',pvmttype);
  210. globaldef('variant',cvarianttype);
  211. {$ifdef i386}
  212. ordpointertype:=u32bittype;
  213. {$endif i386}
  214. {$ifdef x86_64}
  215. ordpointertype:=cu64bittype;
  216. {$endif x86_64}
  217. {$ifdef powerpc}
  218. ordpointertype:=u32bittype;
  219. {$endif powerpc}
  220. {$ifdef sparc}
  221. ordpointertype:=u32bittype;
  222. {$endif sparc}
  223. {$ifdef m68k}
  224. ordpointertype:=u32bittype;
  225. {$endif}
  226. end;
  227. procedure createconstdefs;
  228. {
  229. Create all default definitions for consts for the system unit
  230. }
  231. var
  232. oldregisterdef : boolean;
  233. begin
  234. { create definitions for constants }
  235. oldregisterdef:=registerdef;
  236. registerdef:=false;
  237. cformaltype.setdef(tformaldef.create);
  238. voidtype.setdef(torddef.create(uvoid,0,0));
  239. u8bittype.setdef(torddef.create(u8bit,0,255));
  240. u16bittype.setdef(torddef.create(u16bit,0,65535));
  241. u32bittype.setdef(torddef.create(u32bit,0,high(cardinal)));
  242. s32bittype.setdef(torddef.create(s32bit,low(longint),high(longint)));
  243. cu64bittype.setdef(torddef.create(u64bit,low(qword),TConstExprInt(high(qword))));
  244. cs64bittype.setdef(torddef.create(s64bit,low(int64),high(int64)));
  245. booltype.setdef(torddef.create(bool8bit,0,1));
  246. cchartype.setdef(torddef.create(uchar,0,255));
  247. cwidechartype.setdef(torddef.create(uwidechar,0,65535));
  248. cshortstringtype.setdef(tstringdef.createshort(255));
  249. { should we give a length to the default long and ansi string definition ?? }
  250. clongstringtype.setdef(tstringdef.createlong(-1));
  251. cansistringtype.setdef(tstringdef.createansi(-1));
  252. cwidestringtype.setdef(tstringdef.createwide(-1));
  253. { length=0 for shortstring is open string (needed for readln(string) }
  254. openshortstringtype.setdef(tstringdef.createshort(0));
  255. openchararraytype.setdef(tarraydef.create(0,-1,s32bittype));
  256. tarraydef(openchararraytype.def).elementtype:=cchartype;
  257. {$ifdef x86}
  258. {$ifdef i386}
  259. ordpointertype:=u32bittype;
  260. {$endif i386}
  261. {$ifdef x86_64}
  262. ordpointertype:=cu64bittype;
  263. {$endif x86_64}
  264. s32floattype.setdef(tfloatdef.create(s32real));
  265. s64floattype.setdef(tfloatdef.create(s64real));
  266. s80floattype.setdef(tfloatdef.create(s80real));
  267. {$endif x86}
  268. {$ifdef powerpc}
  269. ordpointertype:=u32bittype;
  270. s32floattype.setdef(tfloatdef.create(s32real));
  271. s64floattype.setdef(tfloatdef.create(s64real));
  272. s80floattype.setdef(tfloatdef.create(s80real));
  273. {$endif powerpc}
  274. {$ifdef sparc}
  275. ordpointertype:=u32bittype;
  276. s32floattype.setdef(tfloatdef.create(s32real));
  277. s64floattype.setdef(tfloatdef.create(s64real));
  278. s80floattype.setdef(tfloatdef.create(s80real));
  279. {$endif sparc}
  280. {$ifdef m68k}
  281. ordpointertype:=u32bittype;
  282. s32floattype.setdef(tfloatdef.create(s32real));
  283. s64floattype.setdef(tfloatdef.create(s64real));
  284. s80floattype.setdef(tfloatdef.create(s80real));
  285. {$endif}
  286. s64currencytype.setdef(tfloatdef.create(s64currency));
  287. { some other definitions }
  288. voidpointertype.setdef(tpointerdef.create(voidtype));
  289. charpointertype.setdef(tpointerdef.create(cchartype));
  290. voidfarpointertype.setdef(tpointerdef.createfar(voidtype));
  291. cfiletype.setdef(tfiledef.createuntyped);
  292. cvarianttype.setdef(tvariantdef.create);
  293. registerdef:=oldregisterdef;
  294. end;
  295. procedure registernodes;
  296. {
  297. Register all possible nodes in the nodeclass array that
  298. will be used for loading the nodes from a ppu
  299. }
  300. begin
  301. nodeclass[addn]:=caddnode;
  302. nodeclass[muln]:=caddnode;
  303. nodeclass[subn]:=caddnode;
  304. nodeclass[divn]:=cmoddivnode;
  305. nodeclass[symdifn]:=caddnode;
  306. nodeclass[modn]:=cmoddivnode;
  307. nodeclass[assignn]:=cassignmentnode;
  308. nodeclass[loadn]:=cloadnode;
  309. nodeclass[rangen]:=crangenode;
  310. nodeclass[ltn]:=caddnode;
  311. nodeclass[lten]:=caddnode;
  312. nodeclass[gtn]:=caddnode;
  313. nodeclass[gten]:=caddnode;
  314. nodeclass[equaln]:=caddnode;
  315. nodeclass[unequaln]:=caddnode;
  316. nodeclass[inn]:=cinnode;
  317. nodeclass[orn]:=caddnode;
  318. nodeclass[xorn]:=caddnode;
  319. nodeclass[shrn]:=cshlshrnode;
  320. nodeclass[shln]:=cshlshrnode;
  321. nodeclass[slashn]:=caddnode;
  322. nodeclass[andn]:=caddnode;
  323. nodeclass[subscriptn]:=csubscriptnode;
  324. nodeclass[derefn]:=cderefnode;
  325. nodeclass[addrn]:=caddrnode;
  326. nodeclass[doubleaddrn]:=cdoubleaddrnode;
  327. nodeclass[ordconstn]:=cordconstnode;
  328. nodeclass[typeconvn]:=ctypeconvnode;
  329. nodeclass[calln]:=ccallnode;
  330. nodeclass[callparan]:=ccallparanode;
  331. nodeclass[realconstn]:=crealconstnode;
  332. nodeclass[unaryminusn]:=cunaryminusnode;
  333. nodeclass[asmn]:=casmnode;
  334. nodeclass[vecn]:=cvecnode;
  335. nodeclass[pointerconstn]:=cpointerconstnode;
  336. nodeclass[stringconstn]:=cstringconstnode;
  337. nodeclass[funcretn]:=cfuncretnode;
  338. nodeclass[selfn]:=cselfnode;
  339. nodeclass[notn]:=cnotnode;
  340. nodeclass[inlinen]:=cinlinenode;
  341. nodeclass[niln]:=cnilnode;
  342. nodeclass[errorn]:=cerrornode;
  343. nodeclass[typen]:=ctypenode;
  344. nodeclass[hnewn]:=chnewnode;
  345. nodeclass[hdisposen]:=chdisposenode;
  346. nodeclass[setelementn]:=csetelementnode;
  347. nodeclass[setconstn]:=csetconstnode;
  348. nodeclass[blockn]:=cblocknode;
  349. nodeclass[statementn]:=cstatementnode;
  350. nodeclass[ifn]:=cifnode;
  351. nodeclass[breakn]:=cbreaknode;
  352. nodeclass[continuen]:=ccontinuenode;
  353. nodeclass[whilerepeatn]:=cwhilerepeatnode;
  354. nodeclass[forn]:=cfornode;
  355. nodeclass[exitn]:=cexitnode;
  356. nodeclass[withn]:=cwithnode;
  357. nodeclass[casen]:=ccasenode;
  358. nodeclass[labeln]:=clabelnode;
  359. nodeclass[goton]:=cgotonode;
  360. nodeclass[tryexceptn]:=ctryexceptnode;
  361. nodeclass[raisen]:=craisenode;
  362. nodeclass[tryfinallyn]:=ctryfinallynode;
  363. nodeclass[onn]:=connode;
  364. nodeclass[isn]:=cisnode;
  365. nodeclass[asn]:=casnode;
  366. nodeclass[caretn]:=caddnode;
  367. nodeclass[failn]:=cfailnode;
  368. nodeclass[starstarn]:=caddnode;
  369. nodeclass[procinlinen]:=cprocinlinenode;
  370. nodeclass[arrayconstructorn]:=carrayconstructornode;
  371. nodeclass[arrayconstructorrangen]:=carrayconstructorrangenode;
  372. nodeclass[tempcreaten]:=ctempcreatenode;
  373. nodeclass[temprefn]:=ctemprefnode;
  374. nodeclass[tempdeleten]:=ctempdeletenode;
  375. nodeclass[addoptn]:=caddnode;
  376. nodeclass[nothingn]:=cnothingnode;
  377. nodeclass[loadvmtn]:=cloadvmtnode;
  378. nodeclass[guidconstn]:=cguidconstnode;
  379. nodeclass[rttin]:=crttinode;
  380. end;
  381. end.
  382. {
  383. $Log$
  384. Revision 1.36 2002-08-15 19:10:35 peter
  385. * first things tai,tnode storing in ppu
  386. Revision 1.35 2002/08/14 19:14:39 carl
  387. + fpu emulation support (generic and untested)
  388. Revision 1.34 2002/08/13 18:01:52 carl
  389. * rename swatoperands to swapoperands
  390. + m68k first compilable version (still needs a lot of testing):
  391. assembler generator, system information , inline
  392. assembler reader.
  393. Revision 1.33 2002/08/11 15:28:00 florian
  394. + support of explicit type case <any ordinal type>->pointer
  395. (delphi mode only)
  396. Revision 1.32 2002/07/25 17:54:24 carl
  397. + Extended is now CPU dependant (equal to bestrealtype)
  398. Revision 1.30 2002/07/07 09:52:32 florian
  399. * powerpc target fixed, very simple units can be compiled
  400. * some basic stuff for better callparanode handling, far from being finished
  401. Revision 1.29 2002/07/06 20:18:47 carl
  402. + more SPARC patches from Mazen
  403. Revision 1.28 2002/07/04 20:43:02 florian
  404. * first x86-64 patches
  405. Revision 1.27 2002/07/01 16:23:54 peter
  406. * cg64 patch
  407. * basics for currency
  408. * asnode updates for class and interface (not finished)
  409. Revision 1.26 2002/05/18 13:34:16 peter
  410. * readded missing revisions
  411. Revision 1.25 2002/05/16 19:46:44 carl
  412. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  413. + try to fix temp allocation (still in ifdef)
  414. + generic constructor calls
  415. + start of tassembler / tmodulebase class cleanup
  416. Revision 1.23 2002/05/12 16:53:09 peter
  417. * moved entry and exitcode to ncgutil and cgobj
  418. * foreach gets extra argument for passing local data to the
  419. iterator function
  420. * -CR checks also class typecasts at runtime by changing them
  421. into as
  422. * fixed compiler to cycle with the -CR option
  423. * fixed stabs with elf writer, finally the global variables can
  424. be watched
  425. * removed a lot of routines from cga unit and replaced them by
  426. calls to cgobj
  427. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  428. u32bit then the other is typecasted also to u32bit without giving
  429. a rangecheck warning/error.
  430. * fixed pascal calling method with reversing also the high tree in
  431. the parast, detected by tcalcst3 test
  432. Revision 1.22 2002/01/24 12:33:53 jonas
  433. * adapted ranges of native types to int64 (e.g. high cardinal is no
  434. longer longint($ffffffff), but just $fffffff in psystem)
  435. * small additional fix in 64bit rangecheck code generation for 32 bit
  436. processors
  437. * adaption of ranges required the matching talgorithm used for selecting
  438. which overloaded procedure to call to be adapted. It should now always
  439. select the closest match for ordinal parameters.
  440. + inttostr(qword) in sysstr.inc/sysstrh.inc
  441. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  442. fixes were required to be able to add them)
  443. * is_in_limit() moved from ncal to types unit, should always be used
  444. instead of direct comparisons of low/high values of orddefs because
  445. qword is a special case
  446. }