ncpumat.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. {
  2. David Zhang 2007/01/15
  3. $Id: ncpumat.pas,v 1.23 2005/02/14 17:13:10 peter Exp $
  4. Copyright (c) 1998-2002 by Florian Klaempfl
  5. Generate MIPSel assembler for math nodes
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ncpumat;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. node, nmat, ncgmat, cgbase;
  24. type
  25. tMIPSELmoddivnode = class(tmoddivnode)
  26. procedure pass_generate_code;override;
  27. end;
  28. tMIPSELshlshrnode = class(tshlshrnode)
  29. procedure pass_generate_code;override;
  30. { everything will be handled in pass_2 }
  31. function first_shlshr64bitint: tnode; override;
  32. end;
  33. tMIPSELnotnode = class(tcgnotnode)
  34. procedure second_boolean; override;
  35. end;
  36. TMIPSunaryminusnode = class(tcgunaryminusnode)
  37. procedure emit_float_sign_change(r: tregister; _size : tcgsize);override;
  38. end;
  39. implementation
  40. uses
  41. globtype, systems,
  42. cutils, verbose, globals,
  43. symconst, symdef,
  44. aasmbase, aasmcpu, aasmtai, aasmdata,
  45. defutil,
  46. procinfo,
  47. cgobj, hlcgobj, pass_2,
  48. ncon,
  49. cpubase,
  50. ncgutil, cgcpu, cgutils;
  51. {*****************************************************************************
  52. TMipselMODDIVNODE
  53. *****************************************************************************}
  54. procedure tMIPSELmoddivnode.pass_generate_code;
  55. var
  56. power: longint;
  57. tmpreg, numerator, divider, resultreg: tregister;
  58. begin
  59. secondpass(left);
  60. secondpass(right);
  61. location_copy(location, left.location);
  62. { put numerator in register }
  63. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location, left.resultdef, left.resultdef, True);
  64. location_copy(location, left.location);
  65. numerator := location.Register;
  66. if (nodetype = modn) then
  67. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT)
  68. else
  69. begin
  70. if (location.loc = LOC_CREGISTER) then
  71. begin
  72. location.loc := LOC_REGISTER;
  73. location.Register := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  74. end;
  75. resultreg := location.Register;
  76. end;
  77. if (nodetype = divn) and
  78. (right.nodetype = ordconstn) and
  79. ispowerof2(tordconstnode(right).Value.svalue, power) then
  80. begin
  81. tmpreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  82. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, 31, numerator, tmpreg);
  83. { if signed, tmpreg=right value-1, otherwise 0 }
  84. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, tordconstnode(right).Value.svalue - 1, tmpreg);
  85. { add to the left value }
  86. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, tmpreg, numerator);
  87. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, aword(power), numerator, resultreg);
  88. end
  89. else
  90. begin
  91. { load divider in a register if necessary }
  92. hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location,
  93. right.resultdef, right.resultdef, True);
  94. divider := right.location.Register;
  95. if (nodetype = modn) then
  96. begin
  97. if is_signed(right.resultdef) then
  98. begin
  99. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_REM, resultreg, numerator, divider));
  100. end
  101. else
  102. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_REMU, resultreg, numerator, divider));
  103. end
  104. else
  105. begin
  106. if is_signed({left.resultdef}right.resultdef) then
  107. begin
  108. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIV, resultreg, numerator, divider));
  109. end
  110. else
  111. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIVU, resultreg, numerator, divider));
  112. end;
  113. end;
  114. { set result location }
  115. location.loc := LOC_REGISTER;
  116. location.Register := resultreg;
  117. end;
  118. {*****************************************************************************
  119. TMIPSelSHLRSHRNODE
  120. *****************************************************************************}
  121. function TMIPSELShlShrNode.first_shlshr64bitint: TNode;
  122. begin
  123. { 64bit without constants need a helper }
  124. if is_64bit(left.resultdef) and
  125. (right.nodetype <> ordconstn) then
  126. begin
  127. Result := inherited first_shlshr64bitint;
  128. exit;
  129. end;
  130. Result := nil;
  131. end;
  132. procedure tMIPSELshlshrnode.pass_generate_code;
  133. var
  134. hregister, resultreg, hregister1, hreg64hi, hreg64lo: tregister;
  135. op: topcg;
  136. shiftval: aword;
  137. begin
  138. { 64bit without constants need a helper, and is
  139. already replaced in pass1 }
  140. if is_64bit(left.resultdef) and
  141. (right.nodetype <> ordconstn) then
  142. internalerror(200405301);
  143. secondpass(left);
  144. secondpass(right);
  145. if is_64bit(left.resultdef) then
  146. begin
  147. location_reset(location, LOC_REGISTER, OS_64);
  148. { load left operator in a register }
  149. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location, left.resultdef, u64inttype, False);
  150. hreg64hi := left.location.register64.reghi;
  151. hreg64lo := left.location.register64.reglo;
  152. shiftval := tordconstnode(right).Value.svalue and 63;
  153. if shiftval > 31 then
  154. begin
  155. if nodetype = shln then
  156. begin
  157. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_32, 0, hreg64hi);
  158. if (shiftval and 31) <> 0 then
  159. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval and 31, hreg64lo, hreg64lo);
  160. end
  161. else
  162. begin
  163. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_32, 0, hreg64lo);
  164. if (shiftval and 31) <> 0 then
  165. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval and 31, hreg64hi, hreg64hi);
  166. end;
  167. location.register64.reglo := hreg64hi;
  168. location.register64.reghi := hreg64lo;
  169. end
  170. else
  171. begin
  172. if shiftval <> 0 then
  173. begin
  174. hregister := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  175. if nodetype = shln then
  176. begin
  177. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, 32 - shiftval, hreg64lo, hregister);
  178. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval, hreg64hi, hreg64hi);
  179. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, hreg64hi, hreg64hi);
  180. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval, hreg64lo, hreg64lo);
  181. end
  182. else
  183. begin
  184. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, 32 - shiftval, hreg64hi, hregister);
  185. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval, hreg64lo, hreg64lo);
  186. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, hreg64lo, hreg64lo);
  187. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval, hreg64hi, hreg64hi);
  188. end;
  189. end;
  190. location.register64.reghi := hreg64hi;
  191. location.register64.reglo := hreg64lo;
  192. end;
  193. end
  194. else
  195. begin
  196. { load left operators in a register }
  197. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location, left.resultdef, left.resultdef, True);
  198. location_copy(location, left.location);
  199. resultreg := location.Register;
  200. hregister1 := location.Register;
  201. if (location.loc = LOC_CREGISTER) then
  202. begin
  203. location.loc := LOC_REGISTER;
  204. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  205. location.Register := resultreg;
  206. end;
  207. { determine operator }
  208. if nodetype = shln then
  209. op := OP_SHL
  210. else
  211. op := OP_SHR;
  212. { shifting by a constant directly coded: }
  213. if (right.nodetype = ordconstn) then
  214. begin
  215. if tordconstnode(right).Value.svalue and 31 <> 0 then
  216. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, tordconstnode(right).Value.svalue and 31, hregister1, resultreg);
  217. end
  218. else
  219. begin
  220. { load shift count in a register if necessary }
  221. hlcg.location_force_reg(current_asmdata.CurrAsmList, right.location, right.resultdef, right.resultdef, True);
  222. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, op, OS_32, right.location.Register, hregister1, resultreg);
  223. end;
  224. end;
  225. end;
  226. {*****************************************************************************
  227. TMIPSelNOTNODE
  228. *****************************************************************************}
  229. procedure tMIPSELnotnode.second_boolean;
  230. var
  231. hl: tasmlabel;
  232. tmpreg : TRegister;
  233. r64: TRegister64;
  234. begin
  235. { if the location is LOC_JUMP, we do the secondpass after the
  236. labels are allocated
  237. }
  238. if left.expectloc = LOC_JUMP then
  239. begin
  240. hl := current_procinfo.CurrTrueLabel;
  241. current_procinfo.CurrTrueLabel := current_procinfo.CurrFalseLabel;
  242. current_procinfo.CurrFalseLabel := hl;
  243. secondpass(left);
  244. maketojumpbool(current_asmdata.CurrAsmList, left, lr_load_regvars);
  245. hl := current_procinfo.CurrTrueLabel;
  246. current_procinfo.CurrTrueLabel := current_procinfo.CurrFalseLabel;
  247. current_procinfo.CurrFalseLabel := hl;
  248. location.loc := LOC_JUMP;
  249. end
  250. else
  251. begin
  252. secondpass(left);
  253. case left.location.loc of
  254. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE,
  255. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF:
  256. begin
  257. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location, left.resultdef, left.resultdef, True);
  258. if is_64bit(resultdef) then
  259. begin
  260. r64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  261. r64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  262. { OR low and high parts together }
  263. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR,r64.reglo,left.location.register64.reglo,left.location.register64.reghi));
  264. { x=0 <=> unsigned(x)<1 }
  265. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SLTIU,r64.reglo,r64.reglo,1));
  266. if is_cbool(resultdef) then
  267. begin
  268. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_S32,r64.reglo,r64.reglo);
  269. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,r64.reglo,r64.reghi);
  270. end
  271. else
  272. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,r64.reghi);
  273. location_reset(location,LOC_REGISTER,OS_64);
  274. location.Register64:=r64;
  275. end
  276. else
  277. begin
  278. tmpreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  279. { x=0 <=> unsigned(x)<1 }
  280. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SLTIU, tmpreg, left.location.Register, 1));
  281. if is_cbool(resultdef) then
  282. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_S32,tmpreg,tmpreg);
  283. location_reset(location, LOC_REGISTER, OS_INT);
  284. location.Register := tmpreg;
  285. end;
  286. end;
  287. else
  288. internalerror(2003042401);
  289. end;
  290. end;
  291. end;
  292. {*****************************************************************************
  293. TMIPSunaryminusnode
  294. *****************************************************************************}
  295. procedure TMIPSunaryminusnode.emit_float_sign_change(r: tregister; _size : tcgsize);
  296. begin
  297. case _size of
  298. OS_F32:
  299. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG_s,r,r));
  300. OS_F64:
  301. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG_d,r,r));
  302. else
  303. internalerror(2013030501);
  304. end;
  305. end;
  306. begin
  307. cmoddivnode := tMIPSELmoddivnode;
  308. cshlshrnode := tMIPSELshlshrnode;
  309. cnotnode := tMIPSELnotnode;
  310. cunaryminusnode := TMIPSunaryminusnode;
  311. end.