n68kmat.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate 680x0 assembler for math nodes
  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 n68kmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat,cpubase,cgbase;
  22. type
  23. tm68knotnode = class(tcgnotnode)
  24. procedure second_boolean;override;
  25. end;
  26. tm68kmoddivnode = class(tcgmoddivnode)
  27. public
  28. function first_moddivint: tnode;override;
  29. procedure emit_div_reg_reg(signed: boolean;denum,num : tregister);override;
  30. procedure emit_mod_reg_reg(signed: boolean;denum,num : tregister);override;
  31. end;
  32. tm68kshlshrnode = class(tshlshrnode)
  33. procedure pass_generate_code;override;
  34. { everything will be handled in pass_2 }
  35. function first_shlshr64bitint: tnode; override;
  36. end;
  37. implementation
  38. uses
  39. globtype,systems,
  40. cutils,verbose,globals,
  41. symconst,symdef,symtable,aasmbase,aasmtai,aasmdata,aasmcpu,
  42. pass_1,pass_2,procinfo,
  43. ncon,
  44. cpuinfo,paramgr,defutil,parabase,
  45. tgobj,ncgutil,cgobj,hlcgobj,cgutils,rgobj,rgcpu,cgcpu,cg64f32;
  46. {*****************************************************************************
  47. TM68KNOTNODE
  48. *****************************************************************************}
  49. procedure tm68knotnode.second_boolean;
  50. var
  51. hreg: tregister;
  52. opsize : tcgsize;
  53. loc : tcgloc;
  54. begin
  55. if not handle_locjump then
  56. begin
  57. secondpass(left);
  58. opsize:=def_cgsize(resultdef);
  59. case left.location.loc of
  60. LOC_FLAGS :
  61. begin
  62. location_copy(location,left.location);
  63. inverse_flags(location.resflags);
  64. end;
  65. LOC_REFERENCE,
  66. LOC_CREFERENCE:
  67. begin
  68. tcg68k(cg).fixref(current_asmdata.CurrAsmList,left.location.reference);
  69. if is_64bit(resultdef) then
  70. begin
  71. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  72. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  73. inc(left.location.reference.offset,4);
  74. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.reference,hreg);
  75. end
  76. else
  77. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,tcgsize2opsize[opsize],left.location.reference));
  78. location_reset(location,LOC_FLAGS,OS_NO);
  79. location.resflags:=F_E;
  80. end;
  81. LOC_REGISTER,
  82. LOC_CREGISTER,
  83. LOC_SUBSETREG,
  84. LOC_CSUBSETREG,
  85. LOC_SUBSETREF,
  86. LOC_CSUBSETREF:
  87. begin
  88. if is_64bit(resultdef) then
  89. begin
  90. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  91. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_OR,S_L,left.location.register64.reghi,left.location.register64.reglo));
  92. end
  93. else
  94. begin
  95. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  96. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,tcgsize2opsize[opsize],left.location.register));
  97. end;
  98. location_reset(location,LOC_FLAGS,OS_NO);
  99. location.resflags:=F_E;
  100. end;
  101. else
  102. internalerror(200203223);
  103. end;
  104. end;
  105. end;
  106. {*****************************************************************************
  107. TM68KMODDIVNODE
  108. *****************************************************************************}
  109. function tm68kmoddivnode.first_moddivint: tnode;
  110. begin
  111. if current_settings.cputype=cpu_MC68020 then
  112. result:=nil
  113. else
  114. result:=inherited first_moddivint;
  115. end;
  116. procedure tm68kmoddivnode.emit_div_reg_reg(signed: boolean;denum,num : tregister);
  117. begin
  118. if current_settings.cputype=cpu_MC68020 then
  119. begin
  120. if signed then
  121. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_DIVS,S_L,denum,num))
  122. else
  123. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_DIVU,S_L,denum,num));
  124. end
  125. else
  126. InternalError(2014062801);
  127. end;
  128. procedure tm68kmoddivnode.emit_mod_reg_reg(signed: boolean;denum,num : tregister);
  129. var tmpreg : tregister;
  130. continuelabel : tasmlabel;
  131. signlabel : tasmlabel;
  132. reg_d0,reg_d1 : tregister;
  133. begin
  134. if current_settings.cputype=cpu_MC68020 then
  135. begin
  136. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  137. { copy the numerator to the tmpreg, so we can use it as quotient, which
  138. means we'll get the remainder immediately in the numerator }
  139. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,num,tmpreg);
  140. if signed then
  141. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIVSL,S_L,denum,num,tmpreg))
  142. else
  143. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIVUL,S_L,denum,num,tmpreg));
  144. end
  145. else
  146. InternalError(2014062802);
  147. end;
  148. {*****************************************************************************
  149. TM68KSHLRSHRNODE
  150. *****************************************************************************}
  151. function tm68kShlShrNode.first_shlshr64bitint:TNode;
  152. begin
  153. if is_64bit(left.resultdef) and not (right.nodetype=ordconstn) then
  154. { for 64bit shifts with anything but constants we use rtl helpers }
  155. result:=inherited
  156. else
  157. { 2nd pass is our friend }
  158. result := nil;
  159. end;
  160. { TODO: FIX ME!!! shlshrnode needs review}
  161. procedure tm68kshlshrnode.pass_generate_code;
  162. var
  163. hregister,resultreg,hregister1,
  164. hreg64hi,hreg64lo : tregister;
  165. op : topcg;
  166. shiftval: aint;
  167. begin
  168. secondpass(left);
  169. secondpass(right);
  170. if is_64bit(left.resultdef) then
  171. begin
  172. location_reset(location,LOC_REGISTER,OS_64);
  173. { load left operator in a register }
  174. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u64inttype,false);
  175. hreg64hi:=left.location.register64.reghi;
  176. hreg64lo:=left.location.register64.reglo;
  177. shiftval := tordconstnode(right).value.svalue;
  178. shiftval := shiftval and 63;
  179. if shiftval > 31 then
  180. begin
  181. if nodetype = shln then
  182. begin
  183. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64hi);
  184. if (shiftval and 31) <> 0 then
  185. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval and 31,hreg64lo,hreg64lo);
  186. end
  187. else
  188. begin
  189. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64lo);
  190. if (shiftval and 31) <> 0 then
  191. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval and 31,hreg64hi,hreg64hi);
  192. end;
  193. location.register64.reglo:=hreg64hi;
  194. location.register64.reghi:=hreg64lo;
  195. end
  196. else
  197. begin
  198. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  199. if nodetype = shln then
  200. begin
  201. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,32-shiftval,hreg64lo,hregister);
  202. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64hi,hreg64hi);
  203. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64hi,hreg64hi);
  204. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64lo,hreg64lo);
  205. end
  206. else
  207. begin
  208. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,32-shiftval,hreg64hi,hregister);
  209. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64lo,hreg64lo);
  210. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64lo,hreg64lo);
  211. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64hi,hreg64hi);
  212. end;
  213. location.register64.reghi:=hreg64hi;
  214. location.register64.reglo:=hreg64lo;
  215. end;
  216. end
  217. else
  218. begin
  219. { load left operators in a register }
  220. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  221. location_copy(location,left.location);
  222. resultreg := location.register;
  223. hregister1 := location.register;
  224. if (location.loc = LOC_CREGISTER) then
  225. begin
  226. location.loc := LOC_REGISTER;
  227. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  228. location.register := resultreg;
  229. end;
  230. { determine operator }
  231. if nodetype=shln then
  232. op:=OP_SHL
  233. else
  234. op:=OP_SHR;
  235. { shifting by a constant directly coded: }
  236. if (right.nodetype=ordconstn) then
  237. begin
  238. if tordconstnode(right).value.svalue and 31<>0 then
  239. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,OS_32,tordconstnode(right).value.svalue and 31,hregister1,resultreg)
  240. end
  241. else
  242. begin
  243. { load shift count in a register if necessary }
  244. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  245. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,OS_32,right.location.register,hregister1,resultreg);
  246. end;
  247. end;
  248. end;
  249. begin
  250. cnotnode:=tm68knotnode;
  251. cmoddivnode:=tm68kmoddivnode;
  252. cshlshrnode:=tm68kshlshrnode;
  253. end.