cginfo.pas 14 KB

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