ncpumat.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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;
  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. implementation
  37. uses
  38. globtype, systems,
  39. cutils, verbose, globals,
  40. symconst,
  41. aasmbase, aasmcpu, aasmtai, aasmdata,
  42. defutil,
  43. procinfo,
  44. cgbase, cgobj, pass_2,
  45. ncon,
  46. cpubase,
  47. ncgutil, cgcpu, cgutils;
  48. {*****************************************************************************
  49. TMipselMODDIVNODE
  50. *****************************************************************************}
  51. procedure tMIPSELmoddivnode.pass_generate_code;
  52. var
  53. power: longint;
  54. tmpreg, numerator, divider, resultreg: tregister;
  55. begin
  56. secondpass(left);
  57. secondpass(right);
  58. location_copy(location, left.location);
  59. { put numerator in register }
  60. location_force_reg(current_asmdata.CurrAsmList, left.location, def_cgsize(left.resultdef), True);
  61. location_copy(location, left.location);
  62. numerator := location.Register;
  63. if (nodetype = modn) then
  64. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT)
  65. else
  66. begin
  67. if (location.loc = LOC_CREGISTER) then
  68. begin
  69. location.loc := LOC_REGISTER;
  70. location.Register := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  71. end;
  72. resultreg := location.Register;
  73. end;
  74. if (nodetype = divn) and
  75. (right.nodetype = ordconstn) and
  76. ispowerof2(tordconstnode(right).Value.svalue, power) then
  77. begin
  78. tmpreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  79. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, 31, numerator, tmpreg);
  80. { if signed, tmpreg=right value-1, otherwise 0 }
  81. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, tordconstnode(right).Value.svalue - 1, tmpreg);
  82. { add to the left value }
  83. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, tmpreg, numerator);
  84. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, aword(power), numerator, resultreg);
  85. end
  86. else
  87. begin
  88. { load divider in a register if necessary }
  89. location_force_reg(current_asmdata.CurrAsmList, right.location,
  90. def_cgsize(right.resultdef), True);
  91. divider := right.location.Register;
  92. if (nodetype = modn) then
  93. begin
  94. if is_signed(right.resultdef) then
  95. begin
  96. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_REM, resultreg, numerator, divider));
  97. end
  98. else
  99. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_REMU, resultreg, numerator, divider));
  100. end
  101. else
  102. begin
  103. if is_signed({left.resultdef}right.resultdef) then
  104. begin
  105. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIV, resultreg, numerator, divider));
  106. end
  107. else
  108. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIVU, resultreg, numerator, divider));
  109. end;
  110. end;
  111. { set result location }
  112. location.loc := LOC_REGISTER;
  113. location.Register := resultreg;
  114. end;
  115. {*****************************************************************************
  116. TMIPSelSHLRSHRNODE
  117. *****************************************************************************}
  118. function TMIPSELShlShrNode.first_shlshr64bitint: TNode;
  119. begin
  120. { 64bit without constants need a helper }
  121. if is_64bit(left.resultdef) and
  122. (right.nodetype <> ordconstn) then
  123. begin
  124. Result := inherited first_shlshr64bitint;
  125. exit;
  126. end;
  127. Result := nil;
  128. end;
  129. procedure tMIPSELshlshrnode.pass_generate_code;
  130. var
  131. hregister, resultreg, hregister1, hreg64hi, hreg64lo: tregister;
  132. op: topcg;
  133. shiftval: aword;
  134. begin
  135. { 64bit without constants need a helper, and is
  136. already replaced in pass1 }
  137. if is_64bit(left.resultdef) and
  138. (right.nodetype <> ordconstn) then
  139. internalerror(200405301);
  140. secondpass(left);
  141. secondpass(right);
  142. if is_64bit(left.resultdef) then
  143. begin
  144. location_reset(location, LOC_REGISTER, OS_64);
  145. { load left operator in a register }
  146. location_force_reg(current_asmdata.CurrAsmList, left.location, OS_64, False);
  147. hreg64hi := left.location.register64.reghi;
  148. hreg64lo := left.location.register64.reglo;
  149. shiftval := tordconstnode(right).Value.svalue and 63;
  150. if shiftval > 31 then
  151. begin
  152. if nodetype = shln then
  153. begin
  154. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_32, 0, hreg64hi);
  155. if (shiftval and 31) <> 0 then
  156. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval and 31, hreg64lo, hreg64lo);
  157. end
  158. else
  159. begin
  160. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_32, 0, hreg64lo);
  161. if (shiftval and 31) <> 0 then
  162. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval and 31, hreg64hi, hreg64hi);
  163. end;
  164. location.register64.reglo := hreg64hi;
  165. location.register64.reghi := hreg64lo;
  166. end
  167. else
  168. begin
  169. hregister := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  170. if nodetype = shln then
  171. begin
  172. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, 32 - shiftval, hreg64lo, hregister);
  173. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval, hreg64hi, hreg64hi);
  174. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, hreg64hi, hreg64hi);
  175. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval, hreg64lo, hreg64lo);
  176. end
  177. else
  178. begin
  179. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, 32 - shiftval, hreg64hi, hregister);
  180. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval, hreg64lo, hreg64lo);
  181. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, hreg64lo, hreg64lo);
  182. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval, hreg64hi, hreg64hi);
  183. end;
  184. location.register64.reghi := hreg64hi;
  185. location.register64.reglo := hreg64lo;
  186. end;
  187. end
  188. else
  189. begin
  190. { load left operators in a register }
  191. location_force_reg(current_asmdata.CurrAsmList, left.location, def_cgsize(left.resultdef), True);
  192. location_copy(location, left.location);
  193. resultreg := location.Register;
  194. hregister1 := location.Register;
  195. if (location.loc = LOC_CREGISTER) then
  196. begin
  197. location.loc := LOC_REGISTER;
  198. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList, OS_INT);
  199. location.Register := resultreg;
  200. end;
  201. { determine operator }
  202. if nodetype = shln then
  203. op := OP_SHL
  204. else
  205. op := OP_SHR;
  206. { shifting by a constant directly coded: }
  207. if (right.nodetype = ordconstn) then
  208. begin
  209. if tordconstnode(right).Value.svalue and 31 <> 0 then
  210. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, tordconstnode(right).Value.svalue and 31, hregister1, resultreg);
  211. end
  212. else
  213. begin
  214. { load shift count in a register if necessary }
  215. location_force_reg(current_asmdata.CurrAsmList, right.location, def_cgsize(right.resultdef), True);
  216. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, op, OS_32, right.location.Register, hregister1, resultreg);
  217. end;
  218. end;
  219. end;
  220. {*****************************************************************************
  221. TMIPSelNOTNODE
  222. *****************************************************************************}
  223. procedure tMIPSELnotnode.second_boolean;
  224. var
  225. hl: tasmlabel;
  226. begin
  227. { if the location is LOC_JUMP, we do the secondpass after the
  228. labels are allocated
  229. }
  230. if left.expectloc = LOC_JUMP then
  231. begin
  232. hl := current_procinfo.CurrTrueLabel;
  233. current_procinfo.CurrTrueLabel := current_procinfo.CurrFalseLabel;
  234. current_procinfo.CurrFalseLabel := hl;
  235. secondpass(left);
  236. maketojumpbool(current_asmdata.CurrAsmList, left, lr_load_regvars);
  237. hl := current_procinfo.CurrTrueLabel;
  238. current_procinfo.CurrTrueLabel := current_procinfo.CurrFalseLabel;
  239. current_procinfo.CurrFalseLabel := hl;
  240. location.loc := LOC_JUMP;
  241. end
  242. else
  243. begin
  244. secondpass(left);
  245. case left.location.loc of
  246. LOC_FLAGS:
  247. begin
  248. internalerror(2007011501);
  249. end;
  250. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE:
  251. begin
  252. location_force_reg(current_asmdata.CurrAsmList, left.location, def_cgsize(left.resultdef), True);
  253. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SEQ, NR_TCR0, left.location.Register, NR_R0));
  254. location_reset(location, LOC_REGISTER, OS_INT);
  255. location.Register := NR_TCR0;
  256. end;
  257. else
  258. internalerror(2003042401);
  259. end;
  260. end;
  261. end;
  262. begin
  263. cmoddivnode := tMIPSELmoddivnode;
  264. cshlshrnode := tMIPSELshlshrnode;
  265. cnotnode := tMIPSELnotnode;
  266. end.