cgbase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. );
  115. TSuperRegister = type byte;
  116. {
  117. The new register coding:
  118. SuperRegister (bits 0..7)
  119. Unused (bits 8..15)
  120. Subregister (bits 16..23)
  121. Register type (bits 24..31)
  122. }
  123. TRegister = type cardinal;
  124. TRegisterRec=packed record
  125. {$ifdef FPC_BIG_ENDIAN}
  126. regtype : Tregistertype;
  127. subreg : Tsubregister;
  128. unused : byte;
  129. supreg : Tsuperregister;
  130. {$else FPC_BIG_ENDIAN}
  131. supreg : Tsuperregister;
  132. unused : byte;
  133. subreg : Tsubregister;
  134. regtype : Tregistertype;
  135. {$endif FPC_BIG_ENDIAN}
  136. end;
  137. { A type to store register locations for 64 Bit values. }
  138. {$ifdef cpu64bit}
  139. tregister64 = tregister;
  140. {$else cpu64bit}
  141. tregister64 = packed record
  142. reglo,reghi : tregister;
  143. end;
  144. {$endif cpu64bit}
  145. { Set type definition for registers }
  146. tsuperregisterset = set of tsuperregister;
  147. { Temp types }
  148. ttemptype = (tt_none,
  149. tt_free,tt_normal,tt_persistent,
  150. tt_noreuse,tt_freenoreuse,
  151. tt_ansistring,tt_freeansistring,
  152. tt_widestring,tt_freewidestring,
  153. tt_interfacecom,tt_freeinterfacecom);
  154. ttemptypeset = set of ttemptype;
  155. const
  156. { Invalid register number }
  157. RS_INVALID = $ff;
  158. tcgsize2size : Array[tcgsize] of integer =
  159. { integer values }
  160. (0,1,2,4,8,1,2,4,8,
  161. { floating point values }
  162. 4,8,EXTENDED_SIZE,8,16,
  163. { multimedia values }
  164. 1,2,4,8,16,1,2,4,8,16);
  165. tfloat2tcgsize: array[tfloattype] of tcgsize =
  166. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  167. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  168. (s32real,s64real,s80real,s64comp);
  169. { Table to convert tcgsize variables to the correspondending
  170. unsigned types }
  171. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  172. OS_8,OS_16,OS_32,OS_64,OS_8,OS_16,OS_32,OS_64,
  173. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  174. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  175. OS_M64,OS_M128);
  176. tcgloc2str : array[TCGLoc] of string[11] = (
  177. 'LOC_INVALID',
  178. 'LOC_VOID',
  179. 'LOC_CONST',
  180. 'LOC_JUMP',
  181. 'LOC_FLAGS',
  182. 'LOC_CREF',
  183. 'LOC_REF',
  184. 'LOC_REG',
  185. 'LOC_CREG',
  186. 'LOC_FPUREG',
  187. 'LOC_CFPUREG',
  188. 'LOC_MMXREG',
  189. 'LOC_CMMXREG',
  190. 'LOC_SSEREG',
  191. 'LOC_CSSEREG',
  192. 'LOC_MMREG',
  193. 'LOC_CMMREG');
  194. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  195. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  196. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  197. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  198. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  199. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  200. function generic_regname(r:tregister):string;
  201. {# From a constant numeric value, return the abstract code generator
  202. size.
  203. }
  204. function int_cgsize(const a: aword): tcgsize;
  205. { return the inverse condition of opcmp }
  206. function inverse_opcmp(opcmp: topcmp): topcmp;
  207. { return whether op is commutative }
  208. function commutativeop(op: topcg): boolean;
  209. implementation
  210. uses
  211. verbose;
  212. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  213. begin
  214. tregisterrec(result).regtype:=rt;
  215. tregisterrec(result).unused:=0;
  216. tregisterrec(result).supreg:=sr;
  217. tregisterrec(result).subreg:=sb;
  218. end;
  219. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  220. begin
  221. result:=tregisterrec(r).subreg;
  222. end;
  223. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  224. begin
  225. result:=tregisterrec(r).supreg;
  226. end;
  227. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  228. begin
  229. result:=tregisterrec(r).regtype;
  230. end;
  231. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  232. begin
  233. tregisterrec(r).subreg:=sr;
  234. end;
  235. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  236. begin
  237. tregisterrec(r).supreg:=sr;
  238. end;
  239. function generic_regname(r:tregister):string;
  240. var
  241. t,sub : char;
  242. nr : string[12];
  243. begin
  244. case getregtype(r) of
  245. R_INTREGISTER:
  246. t:='i';
  247. R_FPUREGISTER:
  248. t:='f';
  249. R_MMXREGISTER:
  250. t:='x';
  251. R_MMREGISTER:
  252. t:='m';
  253. else
  254. begin
  255. result:='INVALID';
  256. exit;
  257. end;
  258. end;
  259. str(getsupreg(r),nr);
  260. case getsubreg(r) of
  261. R_SUBNONE:
  262. sub:=' ';
  263. R_SUBL:
  264. sub:='l';
  265. R_SUBH:
  266. sub:='h';
  267. R_SUBW:
  268. sub:='w';
  269. R_SUBD:
  270. sub:='d';
  271. R_SUBQ:
  272. sub:='q';
  273. else
  274. internalerror(200308252);
  275. end;
  276. if sub<>' ' then
  277. result:=t+'reg'+nr+sub
  278. else
  279. result:=t+'reg'+nr;
  280. end;
  281. function int_cgsize(const a: aword): tcgsize;
  282. begin
  283. if a > 8 then
  284. begin
  285. int_cgsize := OS_NO;
  286. exit;
  287. end;
  288. case byte(a) of
  289. 1 :
  290. result := OS_8;
  291. 2 :
  292. result := OS_16;
  293. 3,4 :
  294. result := OS_32;
  295. 5..8 :
  296. result := OS_64;
  297. end;
  298. end;
  299. function inverse_opcmp(opcmp: topcmp): topcmp;
  300. const
  301. list: array[TOpCmp] of TOpCmp =
  302. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  303. OC_B,OC_BE);
  304. begin
  305. inverse_opcmp := list[opcmp];
  306. end;
  307. function commutativeop(op: topcg): boolean;
  308. const
  309. list: array[topcg] of boolean =
  310. (true,true,true,false,false,true,true,false,false,
  311. true,false,false,false,false,true);
  312. begin
  313. commutativeop := list[op];
  314. end;
  315. end.
  316. {
  317. $Log$
  318. Revision 1.67 2003-10-01 20:34:48 peter
  319. * procinfo unit contains tprocinfo
  320. * cginfo renamed to cgbase
  321. * moved cgmessage to verbose
  322. * fixed ppc and sparc compiles
  323. }