2
0

cgcpu.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the SPARC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,parabase,
  22. cgbase,cgutils,cgobj,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. cpubase,cpuinfo,
  25. node,symconst,SymType,symdef,
  26. rgcpu,
  27. cgsparc;
  28. type
  29. TCGSparc64=class(TCGSparcGen)
  30. procedure a_load_reg_reg(list : TAsmList; fromsize,tosize : tcgsize; reg1,reg2 : tregister);override;
  31. procedure a_load_ref_reg_unaligned(list : TAsmList; fromsize,tosize : tcgsize; const ref : treference; register : tregister);override;
  32. procedure a_load_reg_ref_unaligned(list : TAsmList; fromsize,tosize : tcgsize; register : tregister; const ref : treference);override;
  33. procedure a_load_const_reg(list : TAsmList; size : TCGSize; a : tcgint; reg : TRegister);override;
  34. end;
  35. procedure create_codegen;
  36. implementation
  37. uses
  38. verbose,
  39. systems;
  40. procedure TCGSparc64.a_load_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1,reg2:tregister);
  41. var
  42. instr : taicpu;
  43. begin
  44. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  45. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  46. (fromsize <> tosize)) or
  47. { needs to mask out the sign in the top 16 bits }
  48. ((fromsize = OS_S8) and
  49. (tosize = OS_16)) then
  50. case tosize of
  51. OS_8 :
  52. list.concat(taicpu.op_reg_const_reg(A_AND,reg1,$ff,reg2));
  53. OS_16 :
  54. begin
  55. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,48,reg2));
  56. list.concat(taicpu.op_reg_const_reg(A_SRLX,reg2,48,reg2));
  57. end;
  58. OS_32 :
  59. begin
  60. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,32,reg2));
  61. list.concat(taicpu.op_reg_const_reg(A_SRLX,reg2,32,reg2));
  62. end;
  63. OS_S32 :
  64. begin
  65. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,32,reg2));
  66. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,32,reg2));
  67. end;
  68. OS_64,
  69. OS_S64 :
  70. begin
  71. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  72. list.Concat(instr);
  73. { Notify the register allocator that we have written a move instruction so
  74. it can try to eliminate it. }
  75. add_move_instruction(instr);
  76. end;
  77. OS_S8 :
  78. begin
  79. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,56,reg2));
  80. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,56,reg2));
  81. end;
  82. OS_S16 :
  83. begin
  84. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,48,reg2));
  85. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,48,reg2));
  86. end;
  87. else
  88. internalerror(2017060501);
  89. end
  90. else
  91. begin
  92. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  93. list.Concat(instr);
  94. { Notify the register allocator that we have written a move instruction so
  95. it can try to eliminate it. }
  96. add_move_instruction(instr);
  97. end;
  98. end;
  99. procedure TCGSparc64.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; register: tregister);
  100. var
  101. href: treference;
  102. hreg1, hreg2, tmpreg: tregister;
  103. begin
  104. if fromsize in [OS_64,OS_S64] then
  105. begin
  106. { split into two 32 bit loads }
  107. hreg1:=getintregister(list,OS_32);
  108. hreg2:=getintregister(list,OS_32);
  109. a_load_ref_reg(list,OS_32,OS_32,ref,hreg1);
  110. href:=ref;
  111. inc(href.offset,4);
  112. a_load_ref_reg(list,OS_32,OS_32,href,hreg2);
  113. a_op_const_reg_reg(list,OP_SHL,OS_64,32,hreg1,register);
  114. a_op_reg_reg_reg(list,OP_OR,OS_64,hreg2,register,register);
  115. end
  116. else
  117. inherited;
  118. end;
  119. procedure TCGSparc64.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  120. var
  121. href: treference;
  122. hreg1: tregister;
  123. begin
  124. if fromsize in [OS_64,OS_S64] then
  125. begin
  126. { split into two 32 bit stores }
  127. href:=ref;
  128. if not(TCGSparc64(cg).IsSimpleRef(href)) then
  129. begin
  130. hreg1:=getintregister(list,OS_ADDR);
  131. a_loadaddr_ref_reg(list,href,hreg1);
  132. reference_reset_base(href,hreg1,0,href.alignment,href.volatility);
  133. end;
  134. inc(href.offset,4);
  135. a_load_reg_ref(list,OS_32,OS_32,register,href);
  136. hreg1:=getintregister(list,OS_32);
  137. a_op_const_reg_reg(list,OP_SHR,OS_64,32,register,hreg1);
  138. dec(href.offset,4);
  139. a_load_reg_ref(list,OS_32,OS_32,hreg1,href);
  140. end
  141. else
  142. inherited;
  143. end;
  144. procedure TCGSparc64.a_load_const_reg(list : TAsmList;size : TCGSize;a : tcgint;reg : TRegister);
  145. var
  146. hreg : TRegister;
  147. begin
  148. { we don't use the set instruction here because it could be evalutated to two
  149. instructions which would cause problems with the delay slot (FK) }
  150. if a=0 then
  151. list.concat(taicpu.op_reg(A_CLR,reg))
  152. else if (a>=simm13lo) and (a<=simm13hi) then
  153. list.concat(taicpu.op_const_reg(A_MOV,a,reg))
  154. else if (a>=0) and (a<=$ffffffff) then
  155. begin
  156. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 10,reg));
  157. if (aint(a) and aint($3ff))<>0 then
  158. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  159. end
  160. else if (a>=-4294967296) and (a<=-1) then
  161. begin
  162. list.concat(taicpu.op_const_reg(A_SETHI,(not(aint(a)) shr 10) and $3fffff,reg));
  163. if (aint(a) and aint($3ff)) or aint($1c00)<>0 then
  164. list.concat(taicpu.op_reg_const_reg(A_XOR,reg,(aint(a) and aint($3ff)) or aint($1c00),reg));
  165. end
  166. else
  167. begin
  168. hreg:=getintregister(list,OS_64);
  169. list.concat(taicpu.op_const_reg(A_SETHI,(aint(a) shr 10) and $3fffff,reg));
  170. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 42,hreg));
  171. if ((aint(a) shr 32) and aint($3ff))<>0 then
  172. list.concat(taicpu.op_reg_const_reg(A_OR,hreg,(aint(a) shr 32) and aint($3ff),hreg));
  173. if (aint(a) and aint($3ff))<>0 then
  174. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  175. a_op_const_reg_reg(list,OP_SHL,OS_64,32,hreg,hreg);
  176. list.concat(taicpu.op_reg_reg_reg(A_OR,reg,hreg,reg));
  177. end;
  178. end;
  179. procedure create_codegen;
  180. begin
  181. cg:=TCgSparc64.Create;
  182. if target_info.system=system_sparc64_linux then
  183. TCgSparc64(cg).use_unlimited_pic_mode:=true
  184. else
  185. TCgSparc64(cg).use_unlimited_pic_mode:=false;
  186. cg128:=tcg128.create;
  187. end;
  188. end.