cgcpu.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. cg64f32,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. cpubase,cpuinfo,
  26. node,symconst,SymType,symdef,
  27. rgcpu,
  28. cgsparc;
  29. type
  30. TCGSparc=class(TCGSparcGen)
  31. procedure a_load_reg_reg(list : TAsmList; fromsize,tosize : tcgsize; reg1,reg2 : tregister);override;
  32. procedure a_load_const_reg(list : TAsmList; size : TCGSize; a : tcgint; reg : TRegister);override;
  33. end;
  34. TCg64Sparc=class(tcg64f32)
  35. private
  36. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp;checkoverflow : boolean);
  37. public
  38. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);override;
  39. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);override;
  40. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);override;
  41. procedure a_op64_reg_reg(list:TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst:TRegister64);override;
  42. procedure a_op64_const_reg(list:TAsmList;op:TOpCG;size : tcgsize;value:int64;regdst:TRegister64);override;
  43. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  44. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  45. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  46. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  47. end;
  48. procedure create_codegen;
  49. implementation
  50. uses
  51. verbose,
  52. systems;
  53. procedure TCGSparc.a_load_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1,reg2:tregister);
  54. var
  55. instr : taicpu;
  56. begin
  57. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  58. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  59. (fromsize <> tosize)) or
  60. { needs to mask out the sign in the top 16 bits }
  61. ((fromsize = OS_S8) and
  62. (tosize = OS_16)) then
  63. case tosize of
  64. OS_8 :
  65. list.concat(taicpu.op_reg_const_reg(A_AND,reg1,$ff,reg2));
  66. OS_16 :
  67. begin
  68. list.concat(taicpu.op_reg_const_reg(A_SLL,reg1,16,reg2));
  69. list.concat(taicpu.op_reg_const_reg(A_SRL,reg2,16,reg2));
  70. end;
  71. OS_32,
  72. OS_S32 :
  73. begin
  74. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  75. list.Concat(instr);
  76. { Notify the register allocator that we have written a move instruction so
  77. it can try to eliminate it. }
  78. add_move_instruction(instr);
  79. end;
  80. OS_S8 :
  81. begin
  82. list.concat(taicpu.op_reg_const_reg(A_SLL,reg1,24,reg2));
  83. list.concat(taicpu.op_reg_const_reg(A_SRA,reg2,24,reg2));
  84. end;
  85. OS_S16 :
  86. begin
  87. list.concat(taicpu.op_reg_const_reg(A_SLL,reg1,16,reg2));
  88. list.concat(taicpu.op_reg_const_reg(A_SRA,reg2,16,reg2));
  89. end;
  90. else
  91. internalerror(2002090901);
  92. end
  93. else
  94. begin
  95. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  96. list.Concat(instr);
  97. { Notify the register allocator that we have written a move instruction so
  98. it can try to eliminate it. }
  99. add_move_instruction(instr);
  100. end;
  101. end;
  102. procedure TCGSparc.a_load_const_reg(list : TAsmList;size : TCGSize;a : tcgint;reg : TRegister);
  103. begin
  104. { we don't use the set instruction here because it could be evalutated to two
  105. instructions which would cause problems with the delay slot (FK) }
  106. if (a=0) then
  107. list.concat(taicpu.op_reg(A_CLR,reg))
  108. else if (a>=simm13lo) and (a<=simm13hi) then
  109. list.concat(taicpu.op_const_reg(A_MOV,a,reg))
  110. else
  111. begin
  112. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 10,reg));
  113. if (aint(a) and aint($3ff))<>0 then
  114. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  115. end;
  116. end;
  117. {****************************************************************************
  118. TCG64Sparc
  119. ****************************************************************************}
  120. procedure tcg64sparc.a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);
  121. var
  122. tmpref: treference;
  123. begin
  124. { Override this function to prevent loading the reference twice }
  125. tmpref:=ref;
  126. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  127. inc(tmpref.offset,4);
  128. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,tmpref);
  129. end;
  130. procedure tcg64sparc.a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);
  131. var
  132. tmpref: treference;
  133. begin
  134. { Override this function to prevent loading the reference twice }
  135. tmpref:=ref;
  136. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  137. inc(tmpref.offset,4);
  138. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  139. end;
  140. procedure tcg64sparc.a_load64_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  141. var
  142. hreg64 : tregister64;
  143. begin
  144. { Override this function to prevent loading the reference twice.
  145. Use here some extra registers, but those are optimized away by the RA }
  146. hreg64.reglo:=cg.GetIntRegister(list,OS_32);
  147. hreg64.reghi:=cg.GetIntRegister(list,OS_32);
  148. a_load64_ref_reg(list,r,hreg64);
  149. a_load64_reg_cgpara(list,hreg64,paraloc);
  150. end;
  151. procedure TCg64Sparc.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp;checkoverflow : boolean);
  152. begin
  153. case op of
  154. OP_ADD :
  155. begin
  156. op1:=A_ADDCC;
  157. if checkoverflow then
  158. op2:=A_ADDXCC
  159. else
  160. op2:=A_ADDX;
  161. end;
  162. OP_SUB :
  163. begin
  164. op1:=A_SUBCC;
  165. if checkoverflow then
  166. op2:=A_SUBXCC
  167. else
  168. op2:=A_SUBX;
  169. end;
  170. OP_XOR :
  171. begin
  172. op1:=A_XOR;
  173. op2:=A_XOR;
  174. end;
  175. OP_OR :
  176. begin
  177. op1:=A_OR;
  178. op2:=A_OR;
  179. end;
  180. OP_AND :
  181. begin
  182. op1:=A_AND;
  183. op2:=A_AND;
  184. end;
  185. else
  186. internalerror(2002032409);
  187. end;
  188. end;
  189. procedure TCg64Sparc.a_op64_reg_reg(list:TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst:TRegister64);
  190. begin
  191. case op of
  192. OP_NEG :
  193. begin
  194. { Use the simple code: y=0-z }
  195. list.concat(taicpu.op_reg_reg_reg(A_SUBcc,NR_G0,regsrc.reglo,regdst.reglo));
  196. list.concat(taicpu.op_reg_reg_reg(A_SUBX,NR_G0,regsrc.reghi,regdst.reghi));
  197. end;
  198. OP_NOT :
  199. begin
  200. list.concat(taicpu.op_reg_reg_reg(A_XNOR,regsrc.reglo,NR_G0,regdst.reglo));
  201. list.concat(taicpu.op_reg_reg_reg(A_XNOR,regsrc.reghi,NR_G0,regdst.reghi));
  202. end;
  203. else
  204. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  205. end;
  206. end;
  207. procedure TCg64Sparc.a_op64_const_reg(list:TAsmList;op:TOpCG;size : tcgsize;value:int64;regdst:TRegister64);
  208. begin
  209. a_op64_const_reg_reg(list,op,size,value,regdst,regdst);
  210. end;
  211. procedure tcg64sparc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  212. var
  213. l : tlocation;
  214. begin
  215. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,l);
  216. end;
  217. procedure tcg64sparc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  218. var
  219. l : tlocation;
  220. begin
  221. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,l);
  222. end;
  223. procedure tcg64sparc.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  224. var
  225. op1,op2:TAsmOp;
  226. begin
  227. case op of
  228. OP_NEG,
  229. OP_NOT :
  230. internalerror(2003060102);
  231. OP_AND,OP_OR,OP_XOR:
  232. begin
  233. cg.a_op_const_reg_reg(list,op,OS_INT,tcgint(lo(value)),regsrc.reglo,regdst.reglo);
  234. cg.a_op_const_reg_reg(list,op,OS_INT,tcgint(hi(value)),regsrc.reghi,regdst.reghi);
  235. end;
  236. else
  237. get_64bit_ops(op,op1,op2,setflags);
  238. tcgsparc(cg).handle_reg_const_reg(list,op1,regsrc.reglo,tcgint(lo(value)),regdst.reglo);
  239. tcgsparc(cg).handle_reg_const_reg(list,op2,regsrc.reghi,tcgint(hi(value)),regdst.reghi);
  240. end;
  241. end;
  242. procedure tcg64sparc.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  243. var
  244. op1,op2:TAsmOp;
  245. begin
  246. case op of
  247. OP_NEG,
  248. OP_NOT :
  249. internalerror(2003060103);
  250. else
  251. ;
  252. end;
  253. get_64bit_ops(op,op1,op2,setflags);
  254. list.concat(taicpu.op_reg_reg_reg(op1,regsrc2.reglo,regsrc1.reglo,regdst.reglo));
  255. list.concat(taicpu.op_reg_reg_reg(op2,regsrc2.reghi,regsrc1.reghi,regdst.reghi));
  256. end;
  257. procedure create_codegen;
  258. begin
  259. cg:=TCgSparc.Create;
  260. if target_info.system=system_sparc_linux then
  261. TCgSparc(cg).use_unlimited_pic_mode:=true
  262. else
  263. TCgSparc(cg).use_unlimited_pic_mode:=false;
  264. cg64:=TCg64Sparc.Create;
  265. end;
  266. end.