n68kmat.pas 13 KB

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