cginfo.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit exports some types and constants for the code generation
  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. {# This unit exports some types which are used across the code generator }
  19. unit cginfo;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses cpuinfo,symconst;
  23. type
  24. { Location types where value can be stored }
  25. TCGLoc=(
  26. LOC_INVALID, { added for tracking problems}
  27. LOC_VOID, { no value is available }
  28. LOC_CONSTANT, { constant value }
  29. LOC_JUMP, { boolean results only, jump to false or true label }
  30. LOC_FLAGS, { boolean results only, flags are set }
  31. LOC_CREFERENCE, { in memory constant value reference (cannot change) }
  32. LOC_REFERENCE, { in memory value }
  33. LOC_REGISTER, { in a processor register }
  34. LOC_CREGISTER, { Constant register which shouldn't be modified }
  35. LOC_FPUREGISTER, { FPU stack }
  36. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  37. LOC_MMXREGISTER, { MMX register }
  38. { MMX register variable }
  39. LOC_CMMXREGISTER,
  40. LOC_SSEREGISTER,
  41. LOC_CSSEREGISTER,
  42. { multimedia register }
  43. LOC_MMREGISTER,
  44. { Constant multimedia reg which shouldn't be modified }
  45. LOC_CMMREGISTER
  46. );
  47. {# Generic opcodes, which must be supported by all processors
  48. }
  49. topcg =
  50. (
  51. OP_NONE,
  52. OP_ADD, { simple addition }
  53. OP_AND, { simple logical and }
  54. OP_DIV, { simple unsigned division }
  55. OP_IDIV, { simple signed division }
  56. OP_IMUL, { simple signed multiply }
  57. OP_MUL, { simple unsigned multiply }
  58. OP_NEG, { simple negate }
  59. OP_NOT, { simple logical not }
  60. OP_OR, { simple logical or }
  61. OP_SAR, { arithmetic shift-right }
  62. OP_SHL, { logical shift left }
  63. OP_SHR, { logical shift right }
  64. OP_SUB, { simple subtraction }
  65. OP_XOR { simple exclusive or }
  66. );
  67. {# Generic flag values - used for jump locations }
  68. TOpCmp =
  69. (
  70. OC_NONE,
  71. OC_EQ, { equality comparison }
  72. OC_GT, { greater than (signed) }
  73. OC_LT, { less than (signed) }
  74. OC_GTE, { greater or equal than (signed) }
  75. OC_LTE, { less or equal than (signed) }
  76. OC_NE, { not equal }
  77. OC_BE, { less or equal than (unsigned) }
  78. OC_B, { less than (unsigned) }
  79. OC_AE, { greater or equal than (unsigned) }
  80. OC_A { greater than (unsigned) }
  81. );
  82. { OS_NO is also used memory references with large data that can
  83. not be loaded in a register directly }
  84. TCgSize = (OS_NO,
  85. { integer registers }
  86. OS_8,OS_16,OS_32,OS_64,OS_S8,OS_S16,OS_S32,OS_S64,
  87. { single,double,extended,comp,float128 }
  88. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  89. { multi-media sizes: split in byte, word, dword, ... }
  90. { entities, then the signed counterparts }
  91. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_MS8,OS_MS16,OS_MS32,
  92. OS_MS64,OS_MS128);
  93. { Register types }
  94. TRegisterType = (
  95. R_INVALIDREGISTER, { = 0 }
  96. R_INTREGISTER, { = 1 }
  97. R_FPUREGISTER, { = 2 }
  98. { used by Intel only }
  99. R_MMXREGISTER, { = 3 }
  100. R_MMREGISTER, { = 4 }
  101. R_SPECIALREGISTER, { = 5 }
  102. R_ADDRESSREGISTER { = 6 }
  103. );
  104. { Sub registers }
  105. TSubRegister = (
  106. R_SUBNONE, { = 0; no sub register possible }
  107. R_SUBL, { = 1; 8 bits, Like AL }
  108. R_SUBH, { = 2; 8 bits, Like AH }
  109. R_SUBW, { = 3; 16 bits, Like AX }
  110. R_SUBD, { = 4; 32 bits, Like EAX }
  111. R_SUBQ { = 5; 64 bits, Like RAX }
  112. );
  113. TSuperRegister = type byte;
  114. {
  115. The new register coding:
  116. SuperRegister (bits 0..7)
  117. Unused (bits 8..15)
  118. Subregister (bits 16..23)
  119. Register type (bits 24..31)
  120. }
  121. TRegister = type cardinal;
  122. TRegisterRec=packed record
  123. {$ifdef FPC_BIG_ENDIAN}
  124. regtype : Tregistertype;
  125. subreg : Tsubregister;
  126. unused : byte;
  127. supreg : Tsuperregister;
  128. {$else FPC_BIG_ENDIAN}
  129. supreg : Tsuperregister;
  130. unused : byte;
  131. subreg : Tsubregister;
  132. regtype : Tregistertype;
  133. {$endif FPC_BIG_ENDIAN}
  134. end;
  135. { A type to store register locations for 64 Bit values. }
  136. {$ifdef cpu64bit}
  137. tregister64 = tregister;
  138. {$else cpu64bit}
  139. tregister64 = packed record
  140. reglo,reghi : tregister;
  141. end;
  142. {$endif cpu64bit}
  143. { Set type definition for registers }
  144. tsuperregisterset = set of tsuperregister;
  145. { Temp types }
  146. ttemptype = (tt_none,
  147. tt_free,tt_normal,tt_persistent,
  148. tt_noreuse,tt_freenoreuse,
  149. tt_ansistring,tt_freeansistring,
  150. tt_widestring,tt_freewidestring,
  151. tt_interfacecom,tt_freeinterfacecom);
  152. ttemptypeset = set of ttemptype;
  153. const
  154. { Invalid register number }
  155. RS_INVALID = $ff;
  156. tcgsize2size : Array[tcgsize] of integer =
  157. { integer values }
  158. (0,1,2,4,8,1,2,4,8,
  159. { floating point values }
  160. 4,8,EXTENDED_SIZE,8,16,
  161. { multimedia values }
  162. 1,2,4,8,16,1,2,4,8,16);
  163. tfloat2tcgsize: array[tfloattype] of tcgsize =
  164. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  165. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  166. (s32real,s64real,s80real,s64comp);
  167. { Table to convert tcgsize variables to the correspondending
  168. unsigned types }
  169. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  170. OS_8,OS_16,OS_32,OS_64,OS_8,OS_16,OS_32,OS_64,
  171. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  172. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  173. OS_M64,OS_M128);
  174. tcgloc2str : array[TCGLoc] of string[11] = (
  175. 'LOC_INVALID',
  176. 'LOC_VOID',
  177. 'LOC_CONST',
  178. 'LOC_JUMP',
  179. 'LOC_FLAGS',
  180. 'LOC_CREF',
  181. 'LOC_REF',
  182. 'LOC_REG',
  183. 'LOC_CREG',
  184. 'LOC_FPUREG',
  185. 'LOC_CFPUREG',
  186. 'LOC_MMXREG',
  187. 'LOC_CMMXREG',
  188. 'LOC_SSEREG',
  189. 'LOC_CSSEREG',
  190. 'LOC_MMREG',
  191. 'LOC_CMMREG');
  192. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  193. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  194. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  195. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  196. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  197. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  198. function generic_regname(r:tregister):string;
  199. implementation
  200. uses
  201. verbose;
  202. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  203. begin
  204. tregisterrec(result).regtype:=rt;
  205. tregisterrec(result).unused:=0;
  206. tregisterrec(result).supreg:=sr;
  207. tregisterrec(result).subreg:=sb;
  208. end;
  209. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  210. begin
  211. result:=tregisterrec(r).subreg;
  212. end;
  213. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  214. begin
  215. result:=tregisterrec(r).supreg;
  216. end;
  217. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  218. begin
  219. result:=tregisterrec(r).regtype;
  220. end;
  221. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  222. begin
  223. tregisterrec(r).subreg:=sr;
  224. end;
  225. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  226. begin
  227. tregisterrec(r).supreg:=sr;
  228. end;
  229. function generic_regname(r:tregister):string;
  230. var
  231. t,sub : char;
  232. nr : string[12];
  233. begin
  234. case getregtype(r) of
  235. R_INTREGISTER:
  236. t:='i';
  237. R_FPUREGISTER:
  238. t:='f';
  239. R_MMXREGISTER:
  240. t:='x';
  241. R_MMREGISTER:
  242. t:='m';
  243. else
  244. begin
  245. result:='INVALID';
  246. exit;
  247. end;
  248. end;
  249. str(getsupreg(r),nr);
  250. case getsubreg(r) of
  251. R_SUBNONE:
  252. sub:=' ';
  253. R_SUBL:
  254. sub:='l';
  255. R_SUBH:
  256. sub:='h';
  257. R_SUBW:
  258. sub:='w';
  259. R_SUBD:
  260. sub:='d';
  261. R_SUBQ:
  262. sub:='q';
  263. else
  264. internalerror(200308252);
  265. end;
  266. if sub<>' ' then
  267. result:=t+'reg'+nr+sub
  268. else
  269. result:=t+'reg'+nr;
  270. end;
  271. end.
  272. {
  273. $Log$
  274. Revision 1.27 2003-09-23 17:56:05 peter
  275. * locals and paras are allocated in the code generation
  276. * tvarsym.localloc contains the location of para/local when
  277. generating code for the current procedure
  278. Revision 1.26 2003/09/14 19:30:58 daniel
  279. * Fixed endian problem in Tregisterrec record
  280. Revision 1.25 2003/09/04 21:07:03 florian
  281. * ARM compiler compiles again
  282. Revision 1.24 2003/09/03 21:06:19 peter
  283. * write INVALID as register name instead of IE
  284. Revision 1.23 2003/09/03 16:29:37 peter
  285. * superregisters also from .dat file
  286. Revision 1.22 2003/09/03 15:55:00 peter
  287. * NEWRA branch merged
  288. Revision 1.21.2.4 2003/09/01 21:02:55 peter
  289. * sparc updates for new tregister
  290. Revision 1.21.2.3 2003/08/29 17:28:59 peter
  291. * next batch of updates
  292. Revision 1.21.2.2 2003/08/28 18:35:07 peter
  293. * tregister changed to cardinal
  294. Revision 1.21.2.1 2003/08/27 19:55:54 peter
  295. * first tregister patch
  296. Revision 1.21 2003/04/25 20:59:33 peter
  297. * removed funcretn,funcretsym, function result is now in varsym
  298. and aliases for result and function name are added using absolutesym
  299. * vs_hidden parameter for funcret passed in parameter
  300. * vs_hidden fixes
  301. * writenode changed to printnode and released from extdebug
  302. * -vp option added to generate a tree.log with the nodetree
  303. * nicer printnode for statements, callnode
  304. Revision 1.20 2003/04/23 12:35:34 florian
  305. * fixed several issues with powerpc
  306. + applied a patch from Jonas for nested function calls (PowerPC only)
  307. * ...
  308. Revision 1.19 2003/04/22 23:50:22 peter
  309. * firstpass uses expectloc
  310. * checks if there are differences between the expectloc and
  311. location.loc from secondpass in EXTDEBUG
  312. Revision 1.18 2003/01/09 22:00:53 florian
  313. * fixed some PowerPC issues
  314. Revision 1.17 2003/01/05 13:36:53 florian
  315. * x86-64 compiles
  316. + very basic support for float128 type (x86-64 only)
  317. Revision 1.16 2002/09/07 15:25:01 peter
  318. * old logs removed and tabs fixed
  319. Revision 1.15 2002/08/05 18:27:48 carl
  320. + more more more documentation
  321. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  322. Revision 1.14 2002/08/04 19:06:41 carl
  323. + added generic exception support (still does not work!)
  324. + more documentation
  325. Revision 1.13 2002/07/07 09:52:32 florian
  326. * powerpc target fixed, very simple units can be compiled
  327. * some basic stuff for better callparanode handling, far from being finished
  328. Revision 1.12 2002/07/01 16:23:52 peter
  329. * cg64 patch
  330. * basics for currency
  331. * asnode updates for class and interface (not finished)
  332. Revision 1.11 2002/05/27 19:16:08 carl
  333. + added comments to virtual comparison flags
  334. Revision 1.10 2002/05/18 13:34:05 peter
  335. * readded missing revisions
  336. Revision 1.9 2002/05/16 19:46:35 carl
  337. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  338. + try to fix temp allocation (still in ifdef)
  339. + generic constructor calls
  340. + start of tassembler / tmodulebase class cleanup
  341. Revision 1.7 2002/05/13 19:54:36 peter
  342. * removed n386ld and n386util units
  343. * maybe_save/maybe_restore added instead of the old maybe_push
  344. Revision 1.6 2002/05/06 19:48:26 carl
  345. + added more patches from Mazen for SPARC port
  346. Revision 1.5 2002/04/21 19:46:52 carl
  347. + added patch for SPARC from Mazen (to move to cpuinfo)
  348. Revision 1.4 2002/04/21 15:26:15 carl
  349. * move stuff to cpuinfo and cpubase
  350. + documented
  351. Revision 1.3 2002/04/20 21:32:23 carl
  352. + generic FPC_CHECKPOINTER
  353. + first parameter offset in stack now portable
  354. * rename some constants
  355. + move some cpu stuff to other units
  356. - remove unused constents
  357. * fix stacksize for some targets
  358. * fix generic size problems which depend now on EXTEND_SIZE constant
  359. Revision 1.2 2002/04/19 15:46:01 peter
  360. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  361. in most cases and not written to the ppu
  362. * add mangeledname_prefix() routine to generate the prefix of
  363. manglednames depending on the current procedure, object and module
  364. * removed static procprefix since the mangledname is now build only
  365. on demand from tprocdef.mangledname
  366. Revision 1.1 2002/04/02 18:09:47 jonas
  367. + initial implementation (Peter forgot to commit it)
  368. }