cgbase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Some basic 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 cgbase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. cpuinfo,
  24. symconst;
  25. type
  26. { Location types where value can be stored }
  27. TCGLoc=(
  28. LOC_INVALID, { added for tracking problems}
  29. LOC_VOID, { no value is available }
  30. LOC_CONSTANT, { constant value }
  31. LOC_JUMP, { boolean results only, jump to false or true label }
  32. LOC_FLAGS, { boolean results only, flags are set }
  33. LOC_CREFERENCE, { in memory constant value reference (cannot change) }
  34. LOC_REFERENCE, { in memory value }
  35. LOC_REGISTER, { in a processor register }
  36. LOC_CREGISTER, { Constant register which shouldn't be modified }
  37. LOC_FPUREGISTER, { FPU stack }
  38. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  39. LOC_MMXREGISTER, { MMX register }
  40. { MMX register variable }
  41. LOC_CMMXREGISTER,
  42. LOC_SSEREGISTER,
  43. LOC_CSSEREGISTER,
  44. { multimedia register }
  45. LOC_MMREGISTER,
  46. { Constant multimedia reg which shouldn't be modified }
  47. LOC_CMMREGISTER
  48. );
  49. {# Generic opcodes, which must be supported by all processors
  50. }
  51. topcg =
  52. (
  53. OP_NONE,
  54. OP_ADD, { simple addition }
  55. OP_AND, { simple logical and }
  56. OP_DIV, { simple unsigned division }
  57. OP_IDIV, { simple signed division }
  58. OP_IMUL, { simple signed multiply }
  59. OP_MUL, { simple unsigned multiply }
  60. OP_NEG, { simple negate }
  61. OP_NOT, { simple logical not }
  62. OP_OR, { simple logical or }
  63. OP_SAR, { arithmetic shift-right }
  64. OP_SHL, { logical shift left }
  65. OP_SHR, { logical shift right }
  66. OP_SUB, { simple subtraction }
  67. OP_XOR { simple exclusive or }
  68. );
  69. {# Generic flag values - used for jump locations }
  70. TOpCmp =
  71. (
  72. OC_NONE,
  73. OC_EQ, { equality comparison }
  74. OC_GT, { greater than (signed) }
  75. OC_LT, { less than (signed) }
  76. OC_GTE, { greater or equal than (signed) }
  77. OC_LTE, { less or equal than (signed) }
  78. OC_NE, { not equal }
  79. OC_BE, { less or equal than (unsigned) }
  80. OC_B, { less than (unsigned) }
  81. OC_AE, { greater or equal than (unsigned) }
  82. OC_A { greater than (unsigned) }
  83. );
  84. { OS_NO is also used memory references with large data that can
  85. not be loaded in a register directly }
  86. TCgSize = (OS_NO,
  87. { integer registers }
  88. OS_8,OS_16,OS_32,OS_64,OS_S8,OS_S16,OS_S32,OS_S64,
  89. { single,double,extended,comp,float128 }
  90. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  91. { multi-media sizes: split in byte, word, dword, ... }
  92. { entities, then the signed counterparts }
  93. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_MS8,OS_MS16,OS_MS32,
  94. OS_MS64,OS_MS128);
  95. { Register types }
  96. TRegisterType = (
  97. R_INVALIDREGISTER, { = 0 }
  98. R_INTREGISTER, { = 1 }
  99. R_FPUREGISTER, { = 2 }
  100. { used by Intel only }
  101. R_MMXREGISTER, { = 3 }
  102. R_MMREGISTER, { = 4 }
  103. R_SPECIALREGISTER, { = 5 }
  104. R_ADDRESSREGISTER { = 6 }
  105. );
  106. { Sub registers }
  107. TSubRegister = (
  108. R_SUBNONE, { = 0; no sub register possible }
  109. R_SUBL, { = 1; 8 bits, Like AL }
  110. R_SUBH, { = 2; 8 bits, Like AH }
  111. R_SUBW, { = 3; 16 bits, Like AX }
  112. R_SUBD, { = 4; 32 bits, Like EAX }
  113. R_SUBQ, { = 5; 64 bits, Like RAX }
  114. R_SUBF64 { = 6; 64 bits float that allocates 2 FPU registers }
  115. );
  116. TSuperRegister = type byte;
  117. {
  118. The new register coding:
  119. SuperRegister (bits 0..7)
  120. Unused (bits 8..15)
  121. Subregister (bits 16..23)
  122. Register type (bits 24..31)
  123. }
  124. TRegister = type cardinal;
  125. TRegisterRec=packed record
  126. {$ifdef FPC_BIG_ENDIAN}
  127. regtype : Tregistertype;
  128. subreg : Tsubregister;
  129. unused : byte;
  130. supreg : Tsuperregister;
  131. {$else FPC_BIG_ENDIAN}
  132. supreg : Tsuperregister;
  133. unused : byte;
  134. subreg : Tsubregister;
  135. regtype : Tregistertype;
  136. {$endif FPC_BIG_ENDIAN}
  137. end;
  138. { A type to store register locations for 64 Bit values. }
  139. {$ifdef cpu64bit}
  140. tregister64 = tregister;
  141. {$else cpu64bit}
  142. tregister64 = packed record
  143. reglo,reghi : tregister;
  144. end;
  145. {$endif cpu64bit}
  146. { Set type definition for registers }
  147. tsuperregisterset = set of tsuperregister;
  148. { Temp types }
  149. ttemptype = (tt_none,
  150. tt_free,tt_normal,tt_persistent,
  151. tt_noreuse,tt_freenoreuse,
  152. tt_ansistring,tt_freeansistring,
  153. tt_widestring,tt_freewidestring,
  154. tt_interfacecom,tt_freeinterfacecom);
  155. ttemptypeset = set of ttemptype;
  156. const
  157. { alias for easier understanding }
  158. R_SSEREGISTER = R_MMREGISTER;
  159. { Invalid register number }
  160. RS_INVALID = $ff;
  161. tcgsize2size : Array[tcgsize] of integer =
  162. { integer values }
  163. (0,1,2,4,8,1,2,4,8,
  164. { floating point values }
  165. 4,8,EXTENDED_SIZE,8,16,
  166. { multimedia values }
  167. 1,2,4,8,16,1,2,4,8,16);
  168. tfloat2tcgsize: array[tfloattype] of tcgsize =
  169. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  170. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  171. (s32real,s64real,s80real,s64comp);
  172. { Table to convert tcgsize variables to the correspondending
  173. unsigned types }
  174. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  175. OS_8,OS_16,OS_32,OS_64,OS_8,OS_16,OS_32,OS_64,
  176. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  177. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  178. OS_M64,OS_M128);
  179. tcgloc2str : array[TCGLoc] of string[11] = (
  180. 'LOC_INVALID',
  181. 'LOC_VOID',
  182. 'LOC_CONST',
  183. 'LOC_JUMP',
  184. 'LOC_FLAGS',
  185. 'LOC_CREF',
  186. 'LOC_REF',
  187. 'LOC_REG',
  188. 'LOC_CREG',
  189. 'LOC_FPUREG',
  190. 'LOC_CFPUREG',
  191. 'LOC_MMXREG',
  192. 'LOC_CMMXREG',
  193. 'LOC_SSEREG',
  194. 'LOC_CSSEREG',
  195. 'LOC_MMREG',
  196. 'LOC_CMMREG');
  197. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  198. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  199. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  200. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  201. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  202. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  203. function generic_regname(r:tregister):string;
  204. {# From a constant numeric value, return the abstract code generator
  205. size.
  206. }
  207. function int_cgsize(const a: aword): tcgsize;
  208. { return the inverse condition of opcmp }
  209. function inverse_opcmp(opcmp: topcmp): topcmp;
  210. { return whether op is commutative }
  211. function commutativeop(op: topcg): boolean;
  212. implementation
  213. uses
  214. verbose;
  215. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  216. begin
  217. tregisterrec(result).regtype:=rt;
  218. tregisterrec(result).unused:=0;
  219. tregisterrec(result).supreg:=sr;
  220. tregisterrec(result).subreg:=sb;
  221. end;
  222. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  223. begin
  224. result:=tregisterrec(r).subreg;
  225. end;
  226. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  227. begin
  228. result:=tregisterrec(r).supreg;
  229. end;
  230. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  231. begin
  232. result:=tregisterrec(r).regtype;
  233. end;
  234. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  235. begin
  236. tregisterrec(r).subreg:=sr;
  237. end;
  238. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  239. begin
  240. tregisterrec(r).supreg:=sr;
  241. end;
  242. function generic_regname(r:tregister):string;
  243. var
  244. t,sub : char;
  245. nr : string[12];
  246. begin
  247. case getregtype(r) of
  248. R_INTREGISTER:
  249. t:='i';
  250. R_FPUREGISTER:
  251. t:='f';
  252. R_MMXREGISTER:
  253. t:='x';
  254. R_MMREGISTER:
  255. t:='m';
  256. else
  257. begin
  258. result:='INVALID';
  259. exit;
  260. end;
  261. end;
  262. str(getsupreg(r),nr);
  263. case getsubreg(r) of
  264. R_SUBNONE:
  265. sub:=' ';
  266. R_SUBL:
  267. sub:='l';
  268. R_SUBH:
  269. sub:='h';
  270. R_SUBW:
  271. sub:='w';
  272. R_SUBD:
  273. sub:='d';
  274. R_SUBQ:
  275. sub:='q';
  276. else
  277. internalerror(200308252);
  278. end;
  279. if sub<>' ' then
  280. result:=t+'reg'+nr+sub
  281. else
  282. result:=t+'reg'+nr;
  283. end;
  284. function int_cgsize(const a: aword): tcgsize;
  285. begin
  286. if a > 8 then
  287. begin
  288. int_cgsize := OS_NO;
  289. exit;
  290. end;
  291. case byte(a) of
  292. 1 :
  293. result := OS_8;
  294. 2 :
  295. result := OS_16;
  296. 3,4 :
  297. result := OS_32;
  298. 5..8 :
  299. result := OS_64;
  300. end;
  301. end;
  302. function inverse_opcmp(opcmp: topcmp): topcmp;
  303. const
  304. list: array[TOpCmp] of TOpCmp =
  305. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  306. OC_B,OC_BE);
  307. begin
  308. inverse_opcmp := list[opcmp];
  309. end;
  310. function commutativeop(op: topcg): boolean;
  311. const
  312. list: array[topcg] of boolean =
  313. (true,true,true,false,false,true,true,false,false,
  314. true,false,false,false,false,true);
  315. begin
  316. commutativeop := list[op];
  317. end;
  318. end.
  319. {
  320. $Log$
  321. Revision 1.69 2003-10-11 16:06:42 florian
  322. * fixed some MMX<->SSE
  323. * started to fix ppc, needs an overhaul
  324. + stabs info improve for spilling, not sure if it works correctly/completly
  325. - MMX_SUPPORT removed from Makefile.fpc
  326. Revision 1.68 2003/10/09 21:31:37 daniel
  327. * Register allocator splitted, ans abstract now
  328. Revision 1.67 2003/10/01 20:34:48 peter
  329. * procinfo unit contains tprocinfo
  330. * cginfo renamed to cgbase
  331. * moved cgmessage to verbose
  332. * fixed ppc and sparc compiles
  333. }