n68kmat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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);
  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. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,tcgsize2opsize[opsize],left.location.register));
  99. end;
  100. location_reset(location,LOC_FLAGS,OS_NO);
  101. location.resflags:=F_E;
  102. end;
  103. else
  104. internalerror(200203223);
  105. end;
  106. end;
  107. end;
  108. {*****************************************************************************
  109. TM68KMODDIVNODE
  110. *****************************************************************************}
  111. function tm68kmoddivnode.first_moddivint: tnode;
  112. begin
  113. if current_settings.cputype=cpu_MC68020 then
  114. result:=nil
  115. else
  116. result:=inherited first_moddivint;
  117. end;
  118. procedure tm68kmoddivnode.emit_div_reg_reg(signed: boolean;denum,num : tregister);
  119. begin
  120. if current_settings.cputype=cpu_MC68020 then
  121. begin
  122. if signed then
  123. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_DIVS,S_L,denum,num))
  124. else
  125. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_DIVU,S_L,denum,num));
  126. end
  127. else
  128. InternalError(2014062801);
  129. end;
  130. procedure tm68kmoddivnode.emit_mod_reg_reg(signed: boolean;denum,num : tregister);
  131. var
  132. tmpreg : 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. TM68KUNARYMINUSNODE
  150. *****************************************************************************}
  151. procedure tm68kunaryminusnode.second_float;
  152. var
  153. href: treference;
  154. begin
  155. secondpass(left);
  156. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  157. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('unaryminus second_float called!')));
  158. case left.location.loc of
  159. LOC_REFERENCE,
  160. LOC_CREFERENCE :
  161. begin
  162. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  163. href:=left.location.reference;
  164. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  165. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FNEG,tcgsize2opsize[left.location.size],href,location.register));
  166. end;
  167. LOC_FPUREGISTER:
  168. begin
  169. location.register:=left.location.register;
  170. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_FNEG,S_FX,location.register));
  171. end;
  172. LOC_CFPUREGISTER:
  173. begin
  174. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  175. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEG,S_FX,left.location.register,location.register));
  176. end;
  177. else
  178. internalerror(200306021);
  179. end;
  180. end;
  181. {*****************************************************************************
  182. TM68KSHLRSHRNODE
  183. *****************************************************************************}
  184. function tm68kShlShrNode.first_shlshr64bitint:TNode;
  185. begin
  186. if is_64bit(left.resultdef) and not (right.nodetype=ordconstn) then
  187. { for 64bit shifts with anything but constants we use rtl helpers }
  188. result:=inherited
  189. else
  190. { 2nd pass is our friend }
  191. result := nil;
  192. end;
  193. { TODO: FIX ME!!! shlshrnode needs review}
  194. procedure tm68kshlshrnode.pass_generate_code;
  195. var
  196. hregister,resultreg,hregister1,
  197. hreg64hi,hreg64lo : tregister;
  198. op : topcg;
  199. shiftval: aint;
  200. begin
  201. secondpass(left);
  202. secondpass(right);
  203. if is_64bit(left.resultdef) then
  204. begin
  205. location_reset(location,LOC_REGISTER,OS_64);
  206. { load left operator in a register }
  207. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u64inttype,false);
  208. hreg64hi:=left.location.register64.reghi;
  209. hreg64lo:=left.location.register64.reglo;
  210. shiftval := tordconstnode(right).value.svalue;
  211. shiftval := shiftval and 63;
  212. if shiftval > 31 then
  213. begin
  214. if nodetype = shln then
  215. begin
  216. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64hi);
  217. if (shiftval and 31) <> 0 then
  218. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval and 31,hreg64lo,hreg64lo);
  219. end
  220. else
  221. begin
  222. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64lo);
  223. if (shiftval and 31) <> 0 then
  224. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval and 31,hreg64hi,hreg64hi);
  225. end;
  226. location.register64.reglo:=hreg64hi;
  227. location.register64.reghi:=hreg64lo;
  228. end
  229. else
  230. begin
  231. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  232. if nodetype = shln then
  233. begin
  234. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,32-shiftval,hreg64lo,hregister);
  235. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64hi,hreg64hi);
  236. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64hi,hreg64hi);
  237. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64lo,hreg64lo);
  238. end
  239. else
  240. begin
  241. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,32-shiftval,hreg64hi,hregister);
  242. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64lo,hreg64lo);
  243. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64lo,hreg64lo);
  244. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64hi,hreg64hi);
  245. end;
  246. location.register64.reghi:=hreg64hi;
  247. location.register64.reglo:=hreg64lo;
  248. end;
  249. end
  250. else
  251. begin
  252. { load left operators in a register }
  253. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  254. location_copy(location,left.location);
  255. resultreg := location.register;
  256. hregister1 := location.register;
  257. if (location.loc = LOC_CREGISTER) then
  258. begin
  259. location.loc := LOC_REGISTER;
  260. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  261. location.register := resultreg;
  262. end;
  263. { determine operator }
  264. if nodetype=shln then
  265. op:=OP_SHL
  266. else
  267. op:=OP_SHR;
  268. { shifting by a constant directly coded: }
  269. if (right.nodetype=ordconstn) then
  270. begin
  271. if tordconstnode(right).value.svalue and 31<>0 then
  272. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,OS_32,tordconstnode(right).value.svalue and 31,hregister1,resultreg)
  273. end
  274. else
  275. begin
  276. { load shift count in a register if necessary }
  277. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  278. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,OS_32,right.location.register,hregister1,resultreg);
  279. end;
  280. end;
  281. end;
  282. begin
  283. cnotnode:=tm68knotnode;
  284. cmoddivnode:=tm68kmoddivnode;
  285. cunaryminusnode:=tm68kunaryminusnode;
  286. cshlshrnode:=tm68kshlshrnode;
  287. end.