n68kmat.pas 14 KB

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