cginfo.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. R_MMXREGISTER, { = 3 }
  99. R_KNIREGISTER, { = 4 }
  100. R_SPECIALREGISTER, { = 5 }
  101. R_ADDRESSREGISTER { = 6 }
  102. );
  103. { Sub registers }
  104. TSubRegister = (
  105. R_SUBNONE, { = 0; no sub register possible }
  106. R_SUBL, { = 1; 8 bits, Like AL }
  107. R_SUBH, { = 2; 8 bits, Like AH }
  108. R_SUBW, { = 3; 16 bits, Like AX }
  109. R_SUBD, { = 4; 32 bits, Like EAX }
  110. R_SUBQ { = 5; 64 bits, Like RAX }
  111. );
  112. TSuperRegister = type byte;
  113. {
  114. The new register coding:
  115. SuperRegister (bits 0..7)
  116. Unused (bits 8..15)
  117. Subregister (bits 16..23)
  118. Register type (bits 24..31)
  119. }
  120. TRegister = type cardinal;
  121. TRegisterRec=packed record
  122. {$ifdef FPC_ENDIAN_BIG}
  123. regtype : Tregistertype;
  124. subreg : Tsubregister;
  125. unused : byte;
  126. supreg : Tsuperregister;
  127. {$else FPC_ENDIAN_BIG}
  128. supreg : Tsuperregister;
  129. unused : byte;
  130. subreg : Tsubregister;
  131. regtype : Tregistertype;
  132. {$endif FPC_ENDIAN_BIG}
  133. end;
  134. { A type to store register locations for 64 Bit values. }
  135. {$ifdef cpu64bit}
  136. tregister64 = tregister;
  137. {$else cpu64bit}
  138. tregister64 = packed record
  139. reglo,reghi : tregister;
  140. end;
  141. {$endif cpu64bit}
  142. { Set type definition for registers }
  143. tsuperregisterset = set of tsuperregister;
  144. const
  145. tcgsize2size : Array[tcgsize] of integer =
  146. { integer values }
  147. (0,1,2,4,8,1,2,4,8,
  148. { floating point values }
  149. 4,8,EXTENDED_SIZE,8,16,
  150. { multimedia values }
  151. 1,2,4,8,16,1,2,4,8,16);
  152. tfloat2tcgsize: array[tfloattype] of tcgsize =
  153. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  154. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  155. (s32real,s64real,s80real,s64comp);
  156. { Table to convert tcgsize variables to the correspondending
  157. unsigned types }
  158. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  159. OS_8,OS_16,OS_32,OS_64,OS_8,OS_16,OS_32,OS_64,
  160. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  161. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  162. OS_M64,OS_M128);
  163. tcgloc2str : array[TCGLoc] of string[11] = (
  164. 'LOC_INVALID',
  165. 'LOC_VOID',
  166. 'LOC_CONST',
  167. 'LOC_JUMP',
  168. 'LOC_FLAGS',
  169. 'LOC_CREF',
  170. 'LOC_REF',
  171. 'LOC_REG',
  172. 'LOC_CREG',
  173. 'LOC_FPUREG',
  174. 'LOC_CFPUREG',
  175. 'LOC_MMXREG',
  176. 'LOC_CMMXREG',
  177. 'LOC_SSEREG',
  178. 'LOC_CSSEREG',
  179. 'LOC_MMREG',
  180. 'LOC_CMMREG');
  181. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  182. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  183. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  184. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  185. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  186. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  187. function generic_regname(r:tregister):string;
  188. implementation
  189. uses
  190. verbose;
  191. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  192. begin
  193. tregisterrec(result).regtype:=rt;
  194. tregisterrec(result).unused:=0;
  195. tregisterrec(result).supreg:=sr;
  196. tregisterrec(result).subreg:=sb;
  197. end;
  198. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  199. begin
  200. result:=tregisterrec(r).subreg;
  201. end;
  202. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  203. begin
  204. result:=tregisterrec(r).supreg;
  205. end;
  206. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  207. begin
  208. result:=tregisterrec(r).regtype;
  209. end;
  210. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  211. begin
  212. tregisterrec(r).subreg:=sr;
  213. end;
  214. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  215. begin
  216. tregisterrec(r).supreg:=sr;
  217. end;
  218. function generic_regname(r:tregister):string;
  219. var
  220. t,sub : char;
  221. nr : string[12];
  222. begin
  223. case getregtype(r) of
  224. R_INTREGISTER:
  225. t:='i';
  226. R_FPUREGISTER:
  227. t:='f';
  228. R_MMXREGISTER:
  229. t:='m';
  230. R_KNIREGISTER:
  231. t:='k';
  232. else
  233. internalerror(200308251);
  234. end;
  235. str(getsupreg(r),nr);
  236. case getsubreg(r) of
  237. R_SUBNONE:
  238. sub:=' ';
  239. R_SUBL:
  240. sub:='l';
  241. R_SUBH:
  242. sub:='h';
  243. R_SUBW:
  244. sub:='w';
  245. R_SUBD:
  246. sub:='d';
  247. R_SUBQ:
  248. sub:='q';
  249. else
  250. internalerror(200308252);
  251. end;
  252. if sub<>' ' then
  253. result:=t+'reg'+nr+sub
  254. else
  255. result:=t+'reg'+nr;
  256. end;
  257. end.
  258. {
  259. $Log$
  260. Revision 1.22 2003-09-03 15:55:00 peter
  261. * NEWRA branch merged
  262. Revision 1.21.2.4 2003/09/01 21:02:55 peter
  263. * sparc updates for new tregister
  264. Revision 1.21.2.3 2003/08/29 17:28:59 peter
  265. * next batch of updates
  266. Revision 1.21.2.2 2003/08/28 18:35:07 peter
  267. * tregister changed to cardinal
  268. Revision 1.21.2.1 2003/08/27 19:55:54 peter
  269. * first tregister patch
  270. Revision 1.21 2003/04/25 20:59:33 peter
  271. * removed funcretn,funcretsym, function result is now in varsym
  272. and aliases for result and function name are added using absolutesym
  273. * vs_hidden parameter for funcret passed in parameter
  274. * vs_hidden fixes
  275. * writenode changed to printnode and released from extdebug
  276. * -vp option added to generate a tree.log with the nodetree
  277. * nicer printnode for statements, callnode
  278. Revision 1.20 2003/04/23 12:35:34 florian
  279. * fixed several issues with powerpc
  280. + applied a patch from Jonas for nested function calls (PowerPC only)
  281. * ...
  282. Revision 1.19 2003/04/22 23:50:22 peter
  283. * firstpass uses expectloc
  284. * checks if there are differences between the expectloc and
  285. location.loc from secondpass in EXTDEBUG
  286. Revision 1.18 2003/01/09 22:00:53 florian
  287. * fixed some PowerPC issues
  288. Revision 1.17 2003/01/05 13:36:53 florian
  289. * x86-64 compiles
  290. + very basic support for float128 type (x86-64 only)
  291. Revision 1.16 2002/09/07 15:25:01 peter
  292. * old logs removed and tabs fixed
  293. Revision 1.15 2002/08/05 18:27:48 carl
  294. + more more more documentation
  295. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  296. Revision 1.14 2002/08/04 19:06:41 carl
  297. + added generic exception support (still does not work!)
  298. + more documentation
  299. Revision 1.13 2002/07/07 09:52:32 florian
  300. * powerpc target fixed, very simple units can be compiled
  301. * some basic stuff for better callparanode handling, far from being finished
  302. Revision 1.12 2002/07/01 16:23:52 peter
  303. * cg64 patch
  304. * basics for currency
  305. * asnode updates for class and interface (not finished)
  306. Revision 1.11 2002/05/27 19:16:08 carl
  307. + added comments to virtual comparison flags
  308. Revision 1.10 2002/05/18 13:34:05 peter
  309. * readded missing revisions
  310. Revision 1.9 2002/05/16 19:46:35 carl
  311. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  312. + try to fix temp allocation (still in ifdef)
  313. + generic constructor calls
  314. + start of tassembler / tmodulebase class cleanup
  315. Revision 1.7 2002/05/13 19:54:36 peter
  316. * removed n386ld and n386util units
  317. * maybe_save/maybe_restore added instead of the old maybe_push
  318. Revision 1.6 2002/05/06 19:48:26 carl
  319. + added more patches from Mazen for SPARC port
  320. Revision 1.5 2002/04/21 19:46:52 carl
  321. + added patch for SPARC from Mazen (to move to cpuinfo)
  322. Revision 1.4 2002/04/21 15:26:15 carl
  323. * move stuff to cpuinfo and cpubase
  324. + documented
  325. Revision 1.3 2002/04/20 21:32:23 carl
  326. + generic FPC_CHECKPOINTER
  327. + first parameter offset in stack now portable
  328. * rename some constants
  329. + move some cpu stuff to other units
  330. - remove unused constents
  331. * fix stacksize for some targets
  332. * fix generic size problems which depend now on EXTEND_SIZE constant
  333. Revision 1.2 2002/04/19 15:46:01 peter
  334. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  335. in most cases and not written to the ppu
  336. * add mangeledname_prefix() routine to generate the prefix of
  337. manglednames depending on the current procedure, object and module
  338. * removed static procprefix since the mangledname is now build only
  339. on demand from tprocdef.mangledname
  340. Revision 1.1 2002/04/02 18:09:47 jonas
  341. + initial implementation (Peter forgot to commit it)
  342. }