cgcpu.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. list.concat(taicpu.op_reg_reg_reg(A_SRA,reg1,NR_G0,reg2));
  65. OS_64,
  66. OS_S64 :
  67. begin
  68. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  69. list.Concat(instr);
  70. { Notify the register allocator that we have written a move instruction so
  71. it can try to eliminate it. }
  72. add_move_instruction(instr);
  73. end;
  74. OS_S8 :
  75. begin
  76. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,56,reg2));
  77. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,56,reg2));
  78. end;
  79. OS_S16 :
  80. begin
  81. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,48,reg2));
  82. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,48,reg2));
  83. end;
  84. else
  85. internalerror(2017060501);
  86. end
  87. else
  88. begin
  89. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  90. list.Concat(instr);
  91. { Notify the register allocator that we have written a move instruction so
  92. it can try to eliminate it. }
  93. add_move_instruction(instr);
  94. end;
  95. end;
  96. procedure TCGSparc64.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; register: tregister);
  97. var
  98. href: treference;
  99. hreg1, hreg2, tmpreg: tregister;
  100. begin
  101. if fromsize in [OS_64,OS_S64] then
  102. begin
  103. { split into two 32 bit loads }
  104. hreg1:=getintregister(list,OS_32);
  105. hreg2:=getintregister(list,OS_32);
  106. a_load_ref_reg(list,OS_32,OS_32,ref,hreg1);
  107. href:=ref;
  108. inc(href.offset,4);
  109. a_load_ref_reg(list,OS_32,OS_32,href,hreg2);
  110. a_op_const_reg_reg(list,OP_SHL,OS_64,32,hreg1,register);
  111. a_op_reg_reg_reg(list,OP_OR,OS_64,hreg2,register,register);
  112. end
  113. else
  114. inherited;
  115. end;
  116. procedure TCGSparc64.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  117. var
  118. href: treference;
  119. hreg1: tregister;
  120. begin
  121. if fromsize in [OS_64,OS_S64] then
  122. begin
  123. { split into two 32 bit stores }
  124. href:=ref;
  125. if not(TCGSparc64(cg).IsSimpleRef(href)) then
  126. begin
  127. hreg1:=getintregister(list,OS_ADDR);
  128. a_loadaddr_ref_reg(list,href,hreg1);
  129. reference_reset_base(href,hreg1,0,href.temppos,href.alignment,href.volatility);
  130. end;
  131. inc(href.offset,4);
  132. a_load_reg_ref(list,OS_32,OS_32,register,href);
  133. hreg1:=getintregister(list,OS_32);
  134. a_op_const_reg_reg(list,OP_SHR,OS_64,32,register,hreg1);
  135. dec(href.offset,4);
  136. a_load_reg_ref(list,OS_32,OS_32,hreg1,href);
  137. end
  138. else
  139. inherited;
  140. end;
  141. procedure TCGSparc64.a_load_const_reg(list : TAsmList;size : TCGSize;a : tcgint;reg : TRegister);
  142. var
  143. hreg : TRegister;
  144. begin
  145. { we don't use the set instruction here because it could be evalutated to two
  146. instructions which would cause problems with the delay slot (FK) }
  147. if a=0 then
  148. list.concat(taicpu.op_reg(A_CLR,reg))
  149. else if (a>=simm13lo) and (a<=simm13hi) then
  150. list.concat(taicpu.op_const_reg(A_MOV,a,reg))
  151. else if (a>=0) and (a<=$ffffffff) then
  152. begin
  153. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 10,reg));
  154. if (aint(a) and aint($3ff))<>0 then
  155. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  156. end
  157. else if (a>=-4294967296) and (a<=-1) then
  158. begin
  159. list.concat(taicpu.op_const_reg(A_SETHI,(not(aint(a)) shr 10) and $3fffff,reg));
  160. if (aint(a) and aint($3ff)) or aint($1c00)<>0 then
  161. list.concat(taicpu.op_reg_const_reg(A_XOR,reg,(aint(a) and aint($3ff)) or aint($1c00),reg));
  162. end
  163. else
  164. begin
  165. hreg:=getintregister(list,OS_64);
  166. list.concat(taicpu.op_const_reg(A_SETHI,(aint(a) shr 10) and $3fffff,reg));
  167. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 42,hreg));
  168. if ((aint(a) shr 32) and aint($3ff))<>0 then
  169. list.concat(taicpu.op_reg_const_reg(A_OR,hreg,(aint(a) shr 32) and aint($3ff),hreg));
  170. if (aint(a) and aint($3ff))<>0 then
  171. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  172. a_op_const_reg_reg(list,OP_SHL,OS_64,32,hreg,hreg);
  173. list.concat(taicpu.op_reg_reg_reg(A_OR,reg,hreg,reg));
  174. end;
  175. end;
  176. procedure create_codegen;
  177. begin
  178. cg:=TCgSparc64.Create;
  179. if target_info.system=system_sparc64_linux then
  180. TCgSparc64(cg).use_unlimited_pic_mode:=true
  181. else
  182. TCgSparc64(cg).use_unlimited_pic_mode:=false;
  183. cg128:=tcg128.create;
  184. end;
  185. end.