n68kmat.pas 13 KB

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