cgcpu.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_const_reg(list : TAsmList; size : TCGSize; a : tcgint; reg : TRegister);override;
  33. end;
  34. procedure create_codegen;
  35. implementation
  36. uses
  37. verbose,
  38. systems;
  39. procedure TCGSparc64.a_load_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1,reg2:tregister);
  40. var
  41. instr : taicpu;
  42. begin
  43. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  44. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  45. (fromsize <> tosize)) or
  46. { needs to mask out the sign in the top 16 bits }
  47. ((fromsize = OS_S8) and
  48. (tosize = OS_16)) then
  49. case tosize of
  50. OS_8 :
  51. list.concat(taicpu.op_reg_const_reg(A_AND,reg1,$ff,reg2));
  52. OS_16 :
  53. begin
  54. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,48,reg2));
  55. list.concat(taicpu.op_reg_const_reg(A_SRLX,reg2,48,reg2));
  56. end;
  57. OS_32 :
  58. begin
  59. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,32,reg2));
  60. list.concat(taicpu.op_reg_const_reg(A_SRLX,reg2,32,reg2));
  61. end;
  62. OS_S32 :
  63. begin
  64. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,32,reg2));
  65. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,32,reg2));
  66. end;
  67. OS_64,
  68. OS_S64 :
  69. begin
  70. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  71. list.Concat(instr);
  72. { Notify the register allocator that we have written a move instruction so
  73. it can try to eliminate it. }
  74. add_move_instruction(instr);
  75. end;
  76. OS_S8 :
  77. begin
  78. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,56,reg2));
  79. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,56,reg2));
  80. end;
  81. OS_S16 :
  82. begin
  83. list.concat(taicpu.op_reg_const_reg(A_SLLX,reg1,48,reg2));
  84. list.concat(taicpu.op_reg_const_reg(A_SRAX,reg2,48,reg2));
  85. end;
  86. else
  87. internalerror(2017060501);
  88. end
  89. else
  90. begin
  91. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  92. list.Concat(instr);
  93. { Notify the register allocator that we have written a move instruction so
  94. it can try to eliminate it. }
  95. add_move_instruction(instr);
  96. end;
  97. end;
  98. procedure TCGSparc64.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; register: tregister);
  99. var
  100. href: treference;
  101. hreg1, hreg2, tmpreg: tregister;
  102. begin
  103. if fromsize in [OS_64,OS_S64] then
  104. begin
  105. { split into two 32 bit loads }
  106. hreg1:=getintregister(list,OS_32);
  107. hreg2:=getintregister(list,OS_32);
  108. a_load_ref_reg(list,OS_32,OS_32,ref,hreg1);
  109. href:=ref;
  110. inc(href.offset,4);
  111. a_load_ref_reg(list,OS_32,OS_32,href,hreg2);
  112. a_op_const_reg_reg(list,OP_SHL,OS_64,32,hreg1,register);
  113. a_op_reg_reg_reg(list,OP_OR,OS_64,hreg2,register,register);
  114. end
  115. else
  116. inherited;
  117. end;
  118. procedure TCGSparc64.a_load_const_reg(list : TAsmList;size : TCGSize;a : tcgint;reg : TRegister);
  119. var
  120. hreg : TRegister;
  121. begin
  122. { we don't use the set instruction here because it could be evalutated to two
  123. instructions which would cause problems with the delay slot (FK) }
  124. if a=0 then
  125. list.concat(taicpu.op_reg(A_CLR,reg))
  126. else if (a>=simm13lo) and (a<=simm13hi) then
  127. list.concat(taicpu.op_const_reg(A_MOV,a,reg))
  128. else if (a>=0) and (a<=$ffffffff) then
  129. begin
  130. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 10,reg));
  131. if (aint(a) and aint($3ff))<>0 then
  132. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  133. end
  134. else if (a>=-4294967296) and (a<=-1) then
  135. begin
  136. list.concat(taicpu.op_const_reg(A_SETHI,(not(aint(a)) shr 10) and $3fffff,reg));
  137. if (aint(a) and aint($3ff)) or aint($1c00)<>0 then
  138. list.concat(taicpu.op_reg_const_reg(A_XOR,reg,(aint(a) and aint($3ff)) or aint($1c00),reg));
  139. end
  140. else
  141. begin
  142. hreg:=getintregister(list,OS_64);
  143. list.concat(taicpu.op_const_reg(A_SETHI,(aint(a) shr 10) and $3fffff,reg));
  144. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 42,hreg));
  145. if ((aint(a) shr 32) and aint($3ff))<>0 then
  146. list.concat(taicpu.op_reg_const_reg(A_OR,hreg,(aint(a) shr 32) and aint($3ff),hreg));
  147. if (aint(a) and aint($3ff))<>0 then
  148. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  149. a_op_const_reg_reg(list,OP_SHL,OS_64,32,hreg,hreg);
  150. list.concat(taicpu.op_reg_reg_reg(A_OR,reg,hreg,reg));
  151. end;
  152. end;
  153. procedure create_codegen;
  154. begin
  155. cg:=TCgSparc64.Create;
  156. if target_info.system=system_sparc_linux then
  157. TCgSparc64(cg).use_unlimited_pic_mode:=true
  158. else
  159. TCgSparc64(cg).use_unlimited_pic_mode:=false;
  160. cg128:=tcg128.create;
  161. end;
  162. end.