cpubase.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Contains the base types for the PowerPC
  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 contains the base types for the PowerPC
  19. }
  20. unit cpubase;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. strings,cutils,cclasses,aasmbase,cpuinfo,cginfo;
  25. {*****************************************************************************
  26. Assembler Opcodes
  27. *****************************************************************************}
  28. type
  29. TAsmOp=(A_None,
  30. { normal opcodes }
  31. a_add, a_add_, a_addo, a_addo_, a_addc, a_addc_, a_addco, a_addco_,
  32. a_adde, a_adde_, a_addeo, a_addeo_, a_addi, a_addic, a_addic_, a_addis,
  33. a_addme, a_addme_, a_addmeo, a_addmeo_, a_addze, a_addze_, a_addzeo,
  34. a_addzeo_, a_and, a_and_, a_andc, a_andc_, a_andi_, a_andis_, a_b,
  35. a_ba, a_bl, a_bla, a_bc, a_bca, a_bcl, a_bcla, a_bcctr, a_bcctrl, a_bclr,
  36. a_bclrl, a_cmp, a_cmpi, a_cmpl, a_cmpli, a_cntlzw, a_cntlzw_, a_crand,
  37. a_crandc, a_creqv, a_crnand, a_crnor, a_cror, a_crorc, a_crxor, a_dcba,
  38. a_dcbf, a_dcbi, a_dcbst, a_dcbt, a_divw, a_divw_, a_divwo, a_divwo_,
  39. a_divwu, a_divwu_, a_divwuo, a_divwuo_, a_eciwx, a_ecowx, a_eieio, a_eqv,
  40. a_eqv_, a_extsb, a_extsb_, a_extsh, a_extsh_, a_fabs, a_fabs_, a_fadd,
  41. a_fadd_, a_fadds, a_fadds_, a_fcmpo, a_fcmpu, a_fctiw, a_fctw_, a_fctwz,
  42. a_fctwz_, a_fdiv, a_fdiv_, a_fdivs, a_fdivs_, a_fmadd, a_fmadd_, a_fmadds,
  43. a_fmadds_, a_fmr, a_fmsub, a_fmsub_, a_fmsubs, a_fmsubs_, a_fmul, a_fmul_,
  44. a_fmuls, a_fmuls_, a_fnabs, a_fnabs_, a_fneg, a_fneg_, a_fnmadd,
  45. a_fnmadd_, a_fnmadds, a_fnmadds_, a_fnmsub, a_fnmsub_, a_fnmsubs,
  46. a_fnmsubs_, a_fres, a_fres_, a_frsp, a_frsp_, a_frsqrte, a_frsqrte_,
  47. a_fsel, a_fsel_, a_fsqrt, a_fsqrt_, a_fsqrts, a_fsqrts_, a_fsub, a_fsub_,
  48. a_fsubs, a_fsubs_, a_icbi, a_isync, a_lbz, a_lbzu, a_lbzux, a_lbzx,
  49. a_lfd, a_lfdu, a_lfdux, a_lfdx, a_lfs, a_lfsu, a_lfsux, a_lfsx, a_lha,
  50. a_lhau, a_lhaux, a_lhax, a_hbrx, a_lhz, a_lhzu, a_lhzux, a_lhzx, a_lmw,
  51. a_lswi, a_lswx, a_lwarx, a_lwbrx, a_lwz, a_lwzu, a_lwzux, a_lwzx, a_mcrf,
  52. a_mcrfs, a_mcrxr, a_lcrxe, a_mfcr, a_mffs, a_maffs_, a_mfmsr, a_mfspr, a_mfsr,
  53. a_mfsrin, a_mftb, a_mtfcrf, a_a_mtfd0, a_mtfsb1, a_mtfsf, a_mtfsf_,
  54. a_mtfsfi, a_mtfsfi_, a_mtmsr, a_mtspr, a_mtsr, a_mtsrin, a_mulhw,
  55. a_mulhw_, a_mulhwu, a_mulhwu_, a_mulli, a_mullw, a_mullw_, a_mullwo,
  56. a_mullwo_, a_nand, a_nand_, a_neg, a_neg_, a_nego, a_nego_, a_nor, a_nor_,
  57. a_or, a_or_, a_orc, a_orc_, a_ori, a_oris, a_rfi, a_rlwimi, a_rlwimi_,
  58. a_rlwinm, a_rlwinm_, a_rlwnm, a_sc, a_slw, a_slw_, a_sraw, a_sraw_,
  59. a_srawi, a_srawi_,a_srw, a_srw_, a_stb, a_stbu, a_stbux, a_stbx, a_stfd,
  60. a_stfdu, a_stfdux, a_stfdx, a_stfiwx, a_stfs, a_stfsu, a_stfsux, a_stfsx,
  61. a_sth, a_sthbrx, a_sthu, a_sthux, a_sthx, a_stmw, a_stswi, a_stswx, a_stw,
  62. a_stwbrx, a_stwx_, a_stwu, a_stwux, a_stwx, a_subf, a_subf_, a_subfo,
  63. a_subfo_, a_subfc, a_subfc_, a_subfco, a_subfco_, a_subfe, a_subfe_,
  64. a_subfeo, a_subfeo_, a_subfic, a_subfme, a_subfme_, a_subfmeo, a_subfmeo_,
  65. a_subfze, a_subfze_, a_subfzeo, a_subfzeo_, a_sync, a_tlbia, a_tlbie,
  66. a_tlbsync, a_tw, a_twi, a_xor, a_xor_, a_xori, a_xoris,
  67. { simplified mnemonics }
  68. a_subi, a_subis, a_subic, a_subic_, a_sub, a_sub_, a_subo, a_subo_,
  69. a_subc, a_subc_, a_subco, a_subco_, a_cmpwi, a_cmpw, a_cmplwi, a_cmplw,
  70. a_extlwi, a_extlwi_, a_extrwi, a_extrwi_, a_inslwi, a_inslwi_, a_insrwi,
  71. a_insrwi_, a_rotlwi, a_rotlwi_, a_rotlw, a_rotlw_, a_slwi, a_slwi_,
  72. a_srwi, a_srwi_, a_clrlwi, a_clrlwi_, a_clrrwi, a_clrrwi_, a_clrslwi,
  73. a_clrslwi_, a_blr, a_bctr, a_blrl, a_bctrl, a_crset, a_crclr, a_crmove,
  74. a_crnot, a_mt {move to special prupose reg}, a_mf {move from special purpose reg},
  75. a_nop, a_li, a_lis, a_la, a_mr, a_mr_, a_not, a_mtcr, a_mtlr, a_mflr,
  76. a_mtctr, a_mfctr);
  77. {# This should define the array of instructions as string }
  78. op2strtable=array[tasmop] of string[8];
  79. Const
  80. {# First value of opcode enumeration }
  81. firstop = low(tasmop);
  82. {# Last value of opcode enumeration }
  83. lastop = high(tasmop);
  84. {*****************************************************************************
  85. Registers
  86. *****************************************************************************}
  87. type
  88. Toldregister = (R_NO,
  89. R_0,R_1,R_2,R_3,R_4,R_5,R_6,R_7,R_8,R_9,R_10,R_11,R_12,R_13,R_14,R_15,R_16,
  90. R_17,R_18,R_19,R_20,R_21,R_22,R_23,R_24,R_25,R_26,R_27,R_28,R_29,R_30,R_31,
  91. R_F0,R_F1,R_F2,R_F3,R_F4,R_F5,R_F6,R_F7,R_F8,R_F9,R_F10,R_F11,R_F12,
  92. R_F13,R_F14,R_F15,R_F16,R_F17, R_F18,R_F19,R_F20,R_F21,R_F22, R_F23,R_F24,
  93. R_F25,R_F26,R_F27,R_F28,R_F29,R_F30,R_F31,
  94. R_M0,R_M1,R_M2,R_M3,R_M4,R_M5,R_M6,R_M7,R_M8,R_M9,R_M10,R_M11,R_M12,
  95. R_M13,R_M14,R_M15,R_M16,R_M17,R_M18,R_M19,R_M20,R_M21,R_M22, R_M23,R_M24,
  96. R_M25,R_M26,R_M27,R_M28,R_M29,R_M30,R_M31,
  97. R_CR,R_CR0,R_CR1,R_CR2,R_CR3,R_CR4,R_CR5,R_CR6,R_CR7,
  98. R_XER,R_LR,R_CTR,R_FPSCR,
  99. R_INTREGISTER {Only for use by the register allocator.}
  100. );
  101. Tnewregister=word;
  102. Tsuperregister=byte;
  103. Tsubregister=byte;
  104. Tregister=record
  105. enum:Toldregister;
  106. number:Tnewregister;
  107. end;
  108. {# Set type definition for registers }
  109. tregisterset = set of Toldregister;
  110. Tsupregset=set of Tsuperregister;
  111. { A type to store register locations for 64 Bit values. }
  112. tregister64 = packed record
  113. reglo,reghi : tregister;
  114. end;
  115. { alias for compact code }
  116. treg64 = tregister64;
  117. Const
  118. {# First register in the tregister enumeration }
  119. firstreg = low(Toldregister);
  120. {# Last register in the tregister enumeration }
  121. lastreg = R_FPSCR;
  122. type
  123. {# Type definition for the array of string of register nnames }
  124. treg2strtable = array[firstreg..lastreg] of string[5];
  125. const
  126. R_SPR1 = R_XER;
  127. R_SPR8 = R_LR;
  128. R_SPR9 = R_CTR;
  129. R_TOC = R_2;
  130. { CR0 = 0;
  131. CR1 = 4;
  132. CR2 = 8;
  133. CR3 = 12;
  134. CR4 = 16;
  135. CR5 = 20;
  136. CR6 = 24;
  137. CR7 = 28;
  138. LT = 0;
  139. GT = 1;
  140. EQ = 2;
  141. SO = 3;
  142. FX = 4;
  143. FEX = 5;
  144. VX = 6;
  145. OX = 7;}
  146. mot_reg2str : treg2strtable = ('',
  147. 'r0','r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','r12','r13',
  148. 'r14','r15','r16','r17','r18','r19','r20','r21','r22','r23','r24','r25',
  149. 'r26','r27','r28','r29','r30','r31',
  150. 'F0','F1','F2','F3','F4','F5','F6','F7', 'F8','F9','F10','F11','F12',
  151. 'F13','F14','F15','F16','F17', 'F18','F19','F20','F21','F22', 'F23','F24',
  152. 'F25','F26','F27','F28','F29','F30','F31',
  153. 'M0','M1','M2','M3','M4','M5','M6','M7','M8','M9','M10','M11','M12',
  154. 'M13','M14','M15','M16','M17','M18','M19','M20','M21','M22', 'M23','M24',
  155. 'M25','M26','M27','M28','M29','M30','M31',
  156. 'CR','CR0','CR1','CR2','CR3','CR4','CR5','CR6','CR7',
  157. 'XER','LR','CTR','FPSCR'
  158. );
  159. std_reg2str : treg2strtable = ('',
  160. 'r0','r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','r12','r13',
  161. 'r14','r15','r16','r17','r18','r19','r20','r21','r22','r23','r24','r25',
  162. 'r26','r27','r28','r29','r30','r31',
  163. 'F0','F1','F2','F3','F4','F5','F6','F7', 'F8','F9','F10','F11','F12',
  164. 'F13','F14','F15','F16','F17', 'F18','F19','F20','F21','F22', 'F23','F24',
  165. 'F25','F26','F27','F28','F29','F30','F31',
  166. 'M0','M1','M2','M3','M4','M5','M6','M7','M8','M9','M10','M11','M12',
  167. 'M13','M14','M15','M16','M17','M18','M19','M20','M21','M22', 'M23','M24',
  168. 'M25','M26','M27','M28','M29','M30','M31',
  169. 'CR','CR0','CR1','CR2','CR3','CR4','CR5','CR6','CR7',
  170. 'XER','LR','CTR','FPSCR'
  171. );
  172. {New register coding:}
  173. {Special registers:}
  174. const
  175. NR_NO = $0000; {Invalid register}
  176. {Normal registers:}
  177. {General purpose registers:}
  178. NR_R0 = $0100; NR_R1 = $0200; NR_R2 = $0300;
  179. NR_R3 = $0400; NR_R4 = $0500; NR_R5 = $0600;
  180. NR_R6 = $0700; NR_R7 = $0800; NR_R8 = $0900;
  181. NR_R9 = $0A00; NR_R10 = $0B00; NR_R11 = $0C00;
  182. NR_R12 = $0D00; NR_R13 = $0E00; NR_R14 = $0F00;
  183. NR_R15 = $1000; NR_R16 = $1100; NR_R17 = $1200;
  184. NR_R18 = $1300; NR_R19 = $1400; NR_R20 = $1500;
  185. NR_R21 = $1600; NR_R22 = $1700; NR_R23 = $1800;
  186. NR_R24 = $1900; NR_R25 = $1A00; NR_R26 = $1B00;
  187. NR_R27 = $1C00; NR_R28 = $1D00; NR_R29 = $1E00;
  188. NR_R30 = $1F00; NR_R31 = $2000;
  189. {Super registers:}
  190. RS_R0 = $01; RS_R1 = $02; RS_R2 = $03;
  191. RS_R3 = $04; RS_R4 = $05; RS_R5 = $06;
  192. RS_R6 = $07; RS_R7 = $08; RS_R8 = $09;
  193. RS_R9 = $0A; RS_R10 = $0B; RS_R11 = $0C;
  194. RS_R12 = $0D; RS_R13 = $0E; RS_R14 = $0F;
  195. RS_R15 = $10; RS_R16 = $11; RS_R17 = $12;
  196. RS_R18 = $13; RS_R19 = $14; RS_R20 = $15;
  197. RS_R21 = $16; RS_R22 = $17; RS_R23 = $18;
  198. RS_R24 = $19; RS_R25 = $1A; RS_R26 = $1B;
  199. RS_R27 = $1C; RS_R28 = $1D; RS_R29 = $1E;
  200. RS_R30 = $1F; RS_R31 = $20;
  201. first_supreg = $00;
  202. last_supreg = $20;
  203. {Subregisters, situation unknown!!.}
  204. R_SUBWHOLE=$00;
  205. R_SUBL=$00;
  206. {*****************************************************************************
  207. Conditions
  208. *****************************************************************************}
  209. type
  210. TAsmCondFlag = (C_None { unconditional jumps },
  211. { conditions when not using ctr decrement etc }
  212. C_LT,C_LE,C_EQ,C_GE,C_GT,C_NL,C_NE,C_NG,C_SO,C_NS,C_UN,C_NU,
  213. { conditions when using ctr decrement etc }
  214. C_T,C_F,C_DNZ,C_DNZT,C_DNZF,C_DZ,C_DZT,C_DZF);
  215. const
  216. { these are in the XER, but when moved to CR_x they correspond with the }
  217. { bits below (still needs to be verified!!!) }
  218. C_OV = C_EQ;
  219. C_CA = C_GT;
  220. type
  221. TAsmCond = packed record
  222. case simple: boolean of
  223. false: (BO, BI: byte);
  224. true: (
  225. cond: TAsmCondFlag;
  226. case byte of
  227. 0: ();
  228. { specifies in which part of the cr the bit has to be }
  229. { tested for blt,bgt,beq,..,bnu }
  230. 1: (cr: R_CR0..R_CR7);
  231. { specifies the bit to test for bt,bf,bdz,..,bdzf }
  232. 2: (crbit: byte)
  233. );
  234. end;
  235. const
  236. AsmCondFlag2BO: Array[C_T..C_DZF] of Byte =
  237. (12,4,16,8,0,18,10,2);
  238. AsmCondFlag2BI: Array[C_LT..C_NU] of Byte =
  239. (0,1,2,0,1,0,2,1,3,3,3,3);
  240. AsmCondFlagTF: Array[TAsmCondFlag] of Boolean =
  241. (false,true,false,true,false,true,false,false,false,true,false,true,false,
  242. true,false,false,true,false,false,true,false);
  243. AsmCondFlag2Str: Array[TAsmCondFlag] of string[4] = ({cf_none}'',
  244. { conditions when not using ctr decrement etc}
  245. 'lt','le','eq','ge','gt','nl','ne','ng','so','ns','un','nu',
  246. 't','f','dnz','dzt','dnzf','dz','dzt','dzf');
  247. const
  248. CondAsmOps=3;
  249. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  250. A_BC, A_TW, A_TWI
  251. );
  252. {*****************************************************************************
  253. Flags
  254. *****************************************************************************}
  255. type
  256. TResFlagsEnum = (F_EQ,F_NE,F_LT,F_LE,F_GT,F_GE,F_SO,F_FX,F_FEX,F_VX,F_OX);
  257. TResFlags = record
  258. cr: R_CR0..R_CR7;
  259. flag: TResFlagsEnum;
  260. end;
  261. (*
  262. const
  263. { arrays for boolean location conversions }
  264. flag_2_cond : array[TResFlags] of TAsmCond =
  265. (C_E,C_NE,C_LT,C_LE,C_GT,C_GE,???????????????);
  266. *)
  267. {*****************************************************************************
  268. Reference
  269. *****************************************************************************}
  270. type
  271. trefoptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  272. { since we have only 16 offsets, we need to be able to specify the high }
  273. { and low 16 bits of the address of a symbol }
  274. trefsymaddr = (refs_full,refs_ha,refs_l);
  275. { reference record }
  276. preference = ^treference;
  277. treference = packed record
  278. { base register, R_NO if none }
  279. base,
  280. { index register, R_NO if none }
  281. index : tregister;
  282. { offset, 0 if none }
  283. offset : longint;
  284. { symbol this reference refers to, nil if none }
  285. symbol : tasmsymbol;
  286. { used in conjunction with symbols and offsets: refs_full means }
  287. { means a full 32bit reference, refs_ha means the upper 16 bits }
  288. { and refs_l the lower 16 bits of the address }
  289. symaddr : trefsymaddr;
  290. { changed when inlining and possibly in other cases, don't }
  291. { set manually }
  292. offsetfixup : longint;
  293. { used in conjunction with the previous field }
  294. options : trefoptions;
  295. { alignment this reference is guaranteed to have }
  296. alignment : byte;
  297. end;
  298. { reference record }
  299. pparareference = ^tparareference;
  300. tparareference = packed record
  301. index : tregister;
  302. offset : aword;
  303. end;
  304. const
  305. symaddr2str: array[trefsymaddr] of string[3] = ('','@ha','@l');
  306. const
  307. { MacOS only. Whether the direct data area (TOC) directly contain
  308. global variables. Otherwise it contains pointers to global variables. }
  309. macos_direct_globals = false;
  310. {*****************************************************************************
  311. Operand
  312. *****************************************************************************}
  313. type
  314. toptype=(top_none,top_reg,top_ref,top_const,top_symbol,top_bool);
  315. toper=record
  316. ot : longint;
  317. case typ : toptype of
  318. top_none : ();
  319. top_reg : (reg:tregister);
  320. top_ref : (ref:^treference);
  321. top_const : (val:aword);
  322. top_symbol : (sym:tasmsymbol;symofs:longint);
  323. top_bool : (b: boolean);
  324. end;
  325. {*****************************************************************************
  326. Operand Sizes
  327. *****************************************************************************}
  328. {*****************************************************************************
  329. Generic Location
  330. *****************************************************************************}
  331. type
  332. TLoc=(
  333. { added for tracking problems}
  334. LOC_INVALID,
  335. { ordinal constant }
  336. LOC_CONSTANT,
  337. { in a processor register }
  338. LOC_REGISTER,
  339. { Constant register which shouldn't be modified }
  340. LOC_CREGISTER,
  341. { FPU register}
  342. LOC_FPUREGISTER,
  343. { Constant FPU register which shouldn't be modified }
  344. LOC_CFPUREGISTER,
  345. { multimedia register }
  346. LOC_MMREGISTER,
  347. { Constant multimedia reg which shouldn't be modified }
  348. LOC_CMMREGISTER,
  349. { in memory }
  350. LOC_REFERENCE,
  351. { in memory (constant) }
  352. LOC_CREFERENCE,
  353. { boolean results only, jump to false or true label }
  354. LOC_JUMP,
  355. { boolean results only, flags are set }
  356. LOC_FLAGS
  357. );
  358. { tparamlocation describes where a parameter for a procedure is stored.
  359. References are given from the caller's point of view. The usual
  360. TLocation isn't used, because contains a lot of unnessary fields.
  361. }
  362. tparalocation = packed record
  363. size : TCGSize;
  364. { The location type where the parameter is passed, usually
  365. LOC_REFERENCE,LOC_REGISTER or LOC_FPUREGISTER
  366. }
  367. loc : TLoc;
  368. { The stack pointer must be decreased by this value before
  369. the parameter is copied to the given destination.
  370. This allows to "encode" pushes with tparalocation.
  371. On the PowerPC, this field is unsed but it is there
  372. because several generic code accesses it.
  373. }
  374. sp_fixup : longint;
  375. case TLoc of
  376. LOC_REFERENCE : (reference : tparareference);
  377. LOC_FPUREGISTER, LOC_CFPUREGISTER, LOC_MMREGISTER, LOC_CMMREGISTER,
  378. LOC_REGISTER,LOC_CREGISTER : (
  379. case longint of
  380. 1 : (register,registerhigh : tregister);
  381. { overlay a registerlow }
  382. 2 : (registerlow : tregister);
  383. { overlay a 64 Bit register type }
  384. 3 : (reg64 : tregister64);
  385. 4 : (register64 : tregister64);
  386. );
  387. end;
  388. treglocation = packed record
  389. case longint of
  390. 1 : (register,registerhigh : tregister);
  391. { overlay a registerlow }
  392. 2 : (registerlow : tregister);
  393. { overlay a 64 Bit register type }
  394. 3 : (reg64 : tregister64);
  395. 4 : (register64 : tregister64);
  396. end;
  397. tlocation = packed record
  398. size : TCGSize;
  399. loc : tloc;
  400. case tloc of
  401. LOC_CREFERENCE,LOC_REFERENCE : (reference : treference);
  402. LOC_CONSTANT : (
  403. case longint of
  404. 1 : (value : AWord);
  405. { can't do this, this layout depends on the host cpu. Use }
  406. { lo(valueqword)/hi(valueqword) instead (JM) }
  407. { 2 : (valuelow, valuehigh:AWord); }
  408. { overlay a complete 64 Bit value }
  409. 3 : (valueqword : qword);
  410. );
  411. LOC_FPUREGISTER, LOC_CFPUREGISTER, LOC_MMREGISTER, LOC_CMMREGISTER,
  412. LOC_REGISTER,LOC_CREGISTER : (
  413. case longint of
  414. 1 : (registerlow,registerhigh : tregister);
  415. 2 : (register : tregister);
  416. { overlay a 64 Bit register type }
  417. 3 : (reg64 : tregister64);
  418. 4 : (register64 : tregister64);
  419. );
  420. LOC_FLAGS : (resflags : tresflags);
  421. end;
  422. {*****************************************************************************
  423. Constants
  424. *****************************************************************************}
  425. const
  426. max_operands = 5;
  427. lvaluelocations = [LOC_REFERENCE, LOC_CREGISTER, LOC_CFPUREGISTER,
  428. LOC_CMMREGISTER];
  429. {# Constant defining possibly all registers which might require saving }
  430. {$warning FIX ME !!!!!!!!! }
  431. ALL_REGISTERS = [R_0..R_FPSCR];
  432. ALL_INTREGISTERS = [1..255];
  433. general_registers = [R_0..R_31];
  434. general_superregisters = [RS_R0..RS_R31];
  435. {# low and high of the available maximum width integer general purpose }
  436. { registers }
  437. LoGPReg = R_0;
  438. HiGPReg = R_31;
  439. {# low and high of every possible width general purpose register (same as }
  440. { above on most architctures apart from the 80x86) }
  441. LoReg = R_0;
  442. HiReg = R_31;
  443. {# Table of registers which can be allocated by the code generator
  444. internally, when generating the code.
  445. }
  446. { legend: }
  447. { xxxregs = set of all possibly used registers of that type in the code }
  448. { generator }
  449. { usableregsxxx = set of all 32bit components of registers that can be }
  450. { possible allocated to a regvar or using getregisterxxx (this }
  451. { excludes registers which can be only used for parameter }
  452. { passing on ABI's that define this) }
  453. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  454. maxintregs = 18;
  455. intregs = [R_0..R_31];
  456. usableregsint = [RS_R13..RS_R27];
  457. c_countusableregsint = 18;
  458. maxfpuregs = 31-14+1;
  459. fpuregs = [R_F0..R_F31];
  460. usableregsfpu = [R_F14..R_F31];
  461. c_countusableregsfpu = 31-14+1;
  462. mmregs = [R_M0..R_M31];
  463. usableregsmm = [R_M14..R_M31];
  464. c_countusableregsmm = 31-14+1;
  465. { no distinction on this platform }
  466. maxaddrregs = 0;
  467. addrregs = [];
  468. usableregsaddr = [];
  469. c_countusableregsaddr = 0;
  470. firstsaveintreg = RS_R13;
  471. lastsaveintreg = RS_R27;
  472. firstsavefpureg = R_F14;
  473. lastsavefpureg = R_F31;
  474. { no altivec support yet. Need to override tcgobj.a_loadmm_* first in tcgppc }
  475. firstsavemmreg = R_NO;
  476. lastsavemmreg = R_NO;
  477. maxvarregs = 17;
  478. varregs : Array [1..maxvarregs] of Toldregister =
  479. (R_14,R_15,R_16,R_17,R_18,R_19,R_20,R_21,R_22,R_23,R_24,R_25,
  480. R_26,R_27,R_28,R_29,R_30);
  481. maxfpuvarregs = 31-14+1;
  482. fpuvarregs : Array [1..maxfpuvarregs] of Toldregister =
  483. (R_F14,R_F15,R_F16,R_F17,R_F18,R_F19,R_F20,R_F21,R_F22,R_F23,
  484. R_F24,R_F25,R_F26,R_F27,R_F28,R_F29,R_F30,R_F31);
  485. max_param_regs_int = 8;
  486. param_regs_int: Array[1..max_param_regs_int] of Toldregister =
  487. (R_3,R_4,R_5,R_6,R_7,R_8,R_9,R_10);
  488. max_param_regs_fpu = 13;
  489. param_regs_fpu: Array[1..max_param_regs_fpu] of Toldregister =
  490. (R_F1,R_F2,R_F3,R_F4,R_F5,R_F6,R_F7,R_F8,R_F9,R_F10,R_F11,R_F12,R_F13);
  491. max_param_regs_mm = 13;
  492. param_regs_mm: Array[1..max_param_regs_mm] of Toldregister =
  493. (R_M1,R_M2,R_M3,R_M4,R_M5,R_M6,R_M7,R_M8,R_M9,R_M10,R_M11,R_M12,R_M13);
  494. {# Registers which are defined as scratch and no need to save across
  495. routine calls or in assembler blocks.
  496. }
  497. max_scratch_regs = 3;
  498. scratch_regs: Array[1..max_scratch_regs] of Tsuperregister = (RS_R28,RS_R29,RS_R30);
  499. {*****************************************************************************
  500. Default generic sizes
  501. *****************************************************************************}
  502. {# Defines the default address size for a processor, }
  503. OS_ADDR = OS_32;
  504. {# the natural int size for a processor, }
  505. OS_INT = OS_32;
  506. {# the maximum float size for a processor, }
  507. OS_FLOAT = OS_F64;
  508. {# the size of a vector register for a processor }
  509. OS_VECTOR = OS_M128;
  510. {*****************************************************************************
  511. GDB Information
  512. *****************************************************************************}
  513. {# Register indexes for stabs information, when some
  514. parameters or variables are stored in registers.
  515. Taken from rs6000.h (DBX_REGISTER_NUMBER)
  516. from GCC 3.x source code. PowerPC has 1:1 mapping
  517. according to the order of the registers defined
  518. in GCC
  519. }
  520. stab_regindex : array[firstreg..lastreg] of shortint =
  521. (
  522. { R_NO }
  523. -1,
  524. { R0..R7 }
  525. 0,1,2,3,4,5,6,7,
  526. { R8..R15 }
  527. 8,9,10,11,12,13,14,15,
  528. { R16..R23 }
  529. 16,17,18,19,20,21,22,23,
  530. { R24..R32 }
  531. 24,25,26,27,28,29,30,31,
  532. { F0..F7 }
  533. 32,33,34,35,36,37,38,39,
  534. { F8..F15 }
  535. 40,41,42,43,44,45,46,47,
  536. { F16..F23 }
  537. 48,49,50,51,52,53,54,55,
  538. { F24..F31 }
  539. 56,57,58,59,60,61,62,63,
  540. { M0..M7 Multimedia registers are not supported by GCC }
  541. -1,-1,-1,-1,-1,-1,-1,-1,
  542. { M8..M15 }
  543. -1,-1,-1,-1,-1,-1,-1,-1,
  544. { M16..M23 }
  545. -1,-1,-1,-1,-1,-1,-1,-1,
  546. { M24..M31 }
  547. -1,-1,-1,-1,-1,-1,-1,-1,
  548. { CR }
  549. -1,
  550. { CR0..CR7 }
  551. 68,69,70,71,72,73,74,75,
  552. { XER }
  553. 76,
  554. { LR }
  555. 65,
  556. { CTR }
  557. 66,
  558. { FPSCR }
  559. -1
  560. );
  561. {*****************************************************************************
  562. Generic Register names
  563. *****************************************************************************}
  564. {# Stack pointer register }
  565. stack_pointer_reg = R_1;
  566. NR_STACK_POINTER_REG = NR_R1;
  567. RS_STACK_POINTER_REG = RS_R1;
  568. {# Frame pointer register }
  569. frame_pointer_reg = stack_pointer_reg;
  570. NR_FRAME_POINTER_REG = NR_STACK_POINTER_REG;
  571. RS_FRAME_POINTER_REG = RS_STACK_POINTER_REG;
  572. {# Self pointer register : contains the instance address of an
  573. object or class. }
  574. self_pointer_reg = R_9;
  575. NR_SELF_POINTER_REG = NR_R9;
  576. RS_SELF_POINTER_REG = RS_R9;
  577. {# Register for addressing absolute data in a position independant way,
  578. such as in PIC code. The exact meaning is ABI specific. For
  579. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  580. Taken from GCC rs6000.h
  581. }
  582. {$warning As indicated in rs6000.h, but can't find it anywhere else!}
  583. pic_offset_reg = R_30;
  584. {# Results are returned in this register (32-bit values) }
  585. accumulator = R_3;
  586. NR_ACCUMULATOR = NR_R3;
  587. RS_ACCUMULATOR = RS_R3;
  588. {the return_result_reg, is used inside the called function to store its return
  589. value when that is a scalar value otherwise a pointer to the address of the
  590. result is placed inside it}
  591. return_result_reg = accumulator;
  592. NR_RETURN_RESULT_REG = NR_ACCUMULATOR;
  593. RS_RETURN_RESULT_REG = RS_ACCUMULATOR;
  594. {the function_result_reg contains the function result after a call to a scalar
  595. function othewise it contains a pointer to the returned result}
  596. function_result_reg = accumulator;
  597. {# Hi-Results are returned in this register (64-bit value high register) }
  598. accumulatorhigh = R_4;
  599. NR_ACCUMULATORHIGH = NR_R4;
  600. RS_ACCUMULATORHIGH = RS_R4;
  601. { WARNING: don't change to R_ST0!! See comments above implementation of }
  602. { a_loadfpu* methods in rgcpu (JM) }
  603. fpu_result_reg = R_F1;
  604. mmresultreg = R_M0;
  605. {*****************************************************************************
  606. GCC /ABI linking information
  607. *****************************************************************************}
  608. {# Registers which must be saved when calling a routine declared as
  609. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  610. saved should be the ones as defined in the target ABI and / or GCC.
  611. This value can be deduced from CALLED_USED_REGISTERS array in the
  612. GCC source.
  613. }
  614. std_saved_registers = [RS_R13..RS_R29];
  615. {# Required parameter alignment when calling a routine declared as
  616. stdcall and cdecl. The alignment value should be the one defined
  617. by GCC or the target ABI.
  618. The value of this constant is equal to the constant
  619. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  620. }
  621. std_param_align = 4; { for 32-bit version only }
  622. {*****************************************************************************
  623. CPU Dependent Constants
  624. *****************************************************************************}
  625. LinkageAreaSize = 24;
  626. { offset in the linkage area for the saved stack pointer }
  627. LA_SP = 0;
  628. { offset in the linkage area for the saved conditional register}
  629. LA_CR = 4;
  630. { offset in the linkage area for the saved link register}
  631. LA_LR = 8;
  632. { offset in the linkage area for the saved RTOC register}
  633. LA_RTOC = 20;
  634. {*****************************************************************************
  635. Helpers
  636. *****************************************************************************}
  637. function is_calljmp(o:tasmop):boolean;
  638. procedure inverse_flags(var r : TResFlags);
  639. procedure inverse_cond(const c: TAsmCond;var r : TAsmCond);
  640. function flags_to_cond(const f: TResFlags) : TAsmCond;
  641. procedure create_cond_imm(BO,BI:byte;var r : TAsmCond);
  642. procedure create_cond_norm(cond: TAsmCondFlag; cr: byte;var r : TasmCond);
  643. procedure convert_register_to_enum(var r:Tregister);
  644. function cgsize2subreg(s:Tcgsize):Tsubregister;
  645. implementation
  646. uses
  647. verbose;
  648. {*****************************************************************************
  649. Helpers
  650. *****************************************************************************}
  651. function is_calljmp(o:tasmop):boolean;
  652. begin
  653. is_calljmp:=false;
  654. case o of
  655. A_B,A_BA,A_BL,A_BLA,A_BC,A_BCA,A_BCL,A_BCLA,A_BCCTR,A_BCCTRL,A_BCLR,
  656. A_BCLRL,A_TW,A_TWI: is_calljmp:=true;
  657. end;
  658. end;
  659. procedure inverse_flags(var r: TResFlags);
  660. const
  661. inv_flags: array[F_EQ..F_GE] of TResFlagsEnum =
  662. (F_NE,F_EQ,F_GE,F_GE,F_LE,F_LT);
  663. begin
  664. r.flag := inv_flags[r.flag];
  665. end;
  666. procedure inverse_cond(const c: TAsmCond;var r : TAsmCond);
  667. const
  668. inv_condflags:array[TAsmCondFlag] of TAsmCondFlag=(C_None,
  669. C_GE,C_GT,C_NE,C_LT,C_LE,C_LT,C_EQ,C_GT,C_NS,C_SO,C_NU,C_UN,
  670. C_F,C_T,C_DNZ,C_DNZF,C_DNZT,C_DZ,C_DZF,C_DZT);
  671. begin
  672. r := c;
  673. r.cond := inv_condflags[c.cond];
  674. end;
  675. function flags_to_cond(const f: TResFlags) : TAsmCond;
  676. const
  677. flag_2_cond: array[F_EQ..F_SO] of TAsmCondFlag =
  678. (C_EQ,C_NE,C_LT,C_LE,C_GT,C_GE,C_SO);
  679. begin
  680. if f.flag > high(flag_2_cond) then
  681. internalerror(200112301);
  682. result.simple := true;
  683. result.cr := f.cr;
  684. result.cond := flag_2_cond[f.flag];
  685. end;
  686. procedure create_cond_imm(BO,BI:byte;var r : TAsmCond);
  687. begin
  688. r.simple := false;
  689. r.bo := bo;
  690. r.bi := bi;
  691. end;
  692. procedure create_cond_norm(cond: TAsmCondFlag; cr: byte;var r : TasmCond);
  693. begin
  694. r.simple := true;
  695. r.cond := cond;
  696. case cond of
  697. C_NONE:;
  698. C_T..C_DZF: r.crbit := cr
  699. else r.cr := Toldregister(ord(R_CR0)+cr);
  700. end;
  701. end;
  702. procedure convert_register_to_enum(var r:Tregister);
  703. begin
  704. if r.enum = R_INTREGISTER then
  705. case r.number of
  706. NR_NO: r.enum:= R_NO;
  707. NR_R0: r.enum:= R_0;
  708. NR_R1: r.enum:= R_1;
  709. NR_R2: r.enum:= R_2;
  710. NR_R3: r.enum:= R_3;
  711. NR_R4: r.enum:= R_4;
  712. NR_R5: r.enum:= R_5;
  713. NR_R6: r.enum:= R_6;
  714. NR_R7: r.enum:= R_7;
  715. NR_R8: r.enum:= R_8;
  716. NR_R9: r.enum:= R_9;
  717. NR_R10: r.enum:= R_10;
  718. NR_R11: r.enum:= R_11;
  719. NR_R12: r.enum:= R_12;
  720. NR_R13: r.enum:= R_13;
  721. NR_R14: r.enum:= R_14;
  722. NR_R15: r.enum:= R_15;
  723. NR_R16: r.enum:= R_16;
  724. NR_R17: r.enum:= R_17;
  725. NR_R18: r.enum:= R_18;
  726. NR_R19: r.enum:= R_19;
  727. NR_R20: r.enum:= R_20;
  728. NR_R21: r.enum:= R_21;
  729. NR_R22: r.enum:= R_22;
  730. NR_R23: r.enum:= R_23;
  731. NR_R24: r.enum:= R_24;
  732. NR_R25: r.enum:= R_25;
  733. NR_R26: r.enum:= R_26;
  734. NR_R27: r.enum:= R_27;
  735. NR_R28: r.enum:= R_28;
  736. NR_R29: r.enum:= R_29;
  737. NR_R30: r.enum:= R_30;
  738. NR_R31: r.enum:= R_31;
  739. else
  740. internalerror(200301082);
  741. end;
  742. end;
  743. function cgsize2subreg(s:Tcgsize):Tsubregister;
  744. begin
  745. cgsize2subreg:=R_SUBWHOLE;
  746. end;
  747. end.
  748. {
  749. $Log$
  750. Revision 1.44 2003-02-19 22:00:16 daniel
  751. * Code generator converted to new register notation
  752. - Horribily outdated todo.txt removed
  753. Revision 1.43 2003/02/02 19:25:54 carl
  754. * Several bugfixes for m68k target (register alloc., opcode emission)
  755. + VIS target
  756. + Generic add more complete (still not verified)
  757. Revision 1.42 2003/01/16 11:31:28 olle
  758. + added new register constants
  759. + implemented register convertion proc
  760. Revision 1.41 2003/01/13 17:17:50 olle
  761. * changed global var access, TOC now contain pointers to globals
  762. * fixed handling of function pointers
  763. Revision 1.40 2003/01/09 15:49:56 daniel
  764. * Added register conversion
  765. Revision 1.39 2003/01/08 18:43:58 daniel
  766. * Tregister changed into a record
  767. Revision 1.38 2002/11/25 17:43:27 peter
  768. * splitted defbase in defutil,symutil,defcmp
  769. * merged isconvertable and is_equal into compare_defs(_ext)
  770. * made operator search faster by walking the list only once
  771. Revision 1.37 2002/11/24 14:28:56 jonas
  772. + some comments describing the fields of treference
  773. Revision 1.36 2002/11/17 18:26:16 mazen
  774. * fixed a compilation bug accmulator-->accumulator, in definition of return_result_reg
  775. Revision 1.35 2002/11/17 17:49:09 mazen
  776. + return_result_reg and function_result_reg are now used, in all plateforms, to pass functions result between called function and its caller. See the explanation of each one
  777. Revision 1.34 2002/09/17 18:54:06 jonas
  778. * a_load_reg_reg() now has two size parameters: source and dest. This
  779. allows some optimizations on architectures that don't encode the
  780. register size in the register name.
  781. Revision 1.33 2002/09/07 17:54:59 florian
  782. * first part of PowerPC fixes
  783. Revision 1.32 2002/09/07 15:25:14 peter
  784. * old logs removed and tabs fixed
  785. Revision 1.31 2002/09/01 21:04:49 florian
  786. * several powerpc related stuff fixed
  787. Revision 1.30 2002/08/18 22:16:15 florian
  788. + the ppc gas assembler writer adds now registers aliases
  789. to the assembler file
  790. Revision 1.29 2002/08/18 21:36:42 florian
  791. + handling of local variables in direct reader implemented
  792. Revision 1.28 2002/08/14 18:41:47 jonas
  793. - remove valuelow/valuehigh fields from tlocation, because they depend
  794. on the endianess of the host operating system -> difficult to get
  795. right. Use lo/hi(location.valueqword) instead (remember to use
  796. valueqword and not value!!)
  797. Revision 1.27 2002/08/13 21:40:58 florian
  798. * more fixes for ppc calling conventions
  799. Revision 1.26 2002/08/12 15:08:44 carl
  800. + stab register indexes for powerpc (moved from gdb to cpubase)
  801. + tprocessor enumeration moved to cpuinfo
  802. + linker in target_info is now a class
  803. * many many updates for m68k (will soon start to compile)
  804. - removed some ifdef or correct them for correct cpu
  805. Revision 1.25 2002/08/10 17:15:06 jonas
  806. * endianess fix
  807. Revision 1.24 2002/08/06 20:55:24 florian
  808. * first part of ppc calling conventions fix
  809. Revision 1.23 2002/08/04 12:57:56 jonas
  810. * more misc. fixes, mostly constant-related
  811. Revision 1.22 2002/07/27 19:57:18 jonas
  812. * some typo corrections in the instruction tables
  813. * renamed the m* registers to v*
  814. Revision 1.21 2002/07/26 12:30:51 jonas
  815. * fixed typo in instruction table (_subco_ -> a_subco)
  816. Revision 1.20 2002/07/25 18:04:10 carl
  817. + FPURESULTREG -> FPU_RESULT_REG
  818. Revision 1.19 2002/07/13 19:38:44 florian
  819. * some more generic calling stuff fixed
  820. Revision 1.18 2002/07/11 14:41:34 florian
  821. * start of the new generic parameter handling
  822. Revision 1.17 2002/07/11 07:35:36 jonas
  823. * some available registers fixes
  824. Revision 1.16 2002/07/09 19:45:01 jonas
  825. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  826. * small fixes in the assembler writer
  827. * changed scratch registers, because they were used by the linker (r11
  828. and r12) and by the abi under linux (r31)
  829. Revision 1.15 2002/07/07 09:44:31 florian
  830. * powerpc target fixed, very simple units can be compiled
  831. Revision 1.14 2002/05/18 13:34:26 peter
  832. * readded missing revisions
  833. Revision 1.12 2002/05/14 19:35:01 peter
  834. * removed old logs and updated copyright year
  835. Revision 1.11 2002/05/14 17:28:10 peter
  836. * synchronized cpubase between powerpc and i386
  837. * moved more tables from cpubase to cpuasm
  838. * tai_align_abstract moved to tainst, cpuasm must define
  839. the tai_align class now, which may be empty
  840. }