n68kmat.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate 680x0 assembler for math nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n68kmat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat,ncgmat,cpubase,cgbase;
  23. type
  24. tm68knotnode = class(tnotnode)
  25. procedure pass_2;override;
  26. end;
  27. tm68kmoddivnode = class(tcgmoddivnode)
  28. procedure emit_div_reg_reg(signed: boolean;denum,num : tregister);override;
  29. procedure emit_mod_reg_reg(signed: boolean;denum,num : tregister);override;
  30. end;
  31. implementation
  32. uses
  33. globtype,systems,
  34. cutils,verbose,globals,
  35. symconst,symdef,aasmbase,aasmtai,aasmcpu,
  36. pass_1,pass_2,
  37. ncon,
  38. cpuinfo,paramgr,defutil,parabase,
  39. tgobj,ncgutil,cgobj,cgutils,rgobj,rgcpu,cgcpu,cg64f32;
  40. {*****************************************************************************
  41. TM68KNOTNODE
  42. *****************************************************************************}
  43. procedure tm68knotnode.pass_2;
  44. var
  45. hl : tasmlabel;
  46. opsize : tcgsize;
  47. begin
  48. opsize:=def_cgsize(resulttype.def);
  49. if is_boolean(resulttype.def) then
  50. begin
  51. { the second pass could change the location of left }
  52. { if it is a register variable, so we've to do }
  53. { this before the case statement }
  54. if left.location.loc<>LOC_JUMP then
  55. secondpass(left);
  56. case left.location.loc of
  57. LOC_JUMP :
  58. begin
  59. location_reset(location,LOC_JUMP,OS_NO);
  60. hl:=truelabel;
  61. truelabel:=falselabel;
  62. falselabel:=hl;
  63. secondpass(left);
  64. maketojumpbool(exprasmlist,left,lr_load_regvars);
  65. hl:=truelabel;
  66. truelabel:=falselabel;
  67. falselabel:=hl;
  68. end;
  69. LOC_FLAGS :
  70. begin
  71. location_copy(location,left.location);
  72. // location_release(exprasmlist,left.location);
  73. inverse_flags(location.resflags);
  74. end;
  75. LOC_CONSTANT,
  76. LOC_REGISTER,
  77. LOC_CREGISTER,
  78. LOC_REFERENCE,
  79. LOC_CREFERENCE :
  80. begin
  81. location_force_reg(exprasmlist,left.location,def_cgsize(resulttype.def),true);
  82. exprasmlist.concat(taicpu.op_reg(A_TST,tcgsize2opsize[opsize],left.location.register));
  83. // location_release(exprasmlist,left.location);
  84. location_reset(location,LOC_FLAGS,OS_NO);
  85. location.resflags:=F_E;
  86. end;
  87. else
  88. internalerror(200203224);
  89. end;
  90. end
  91. else if is_64bitint(left.resulttype.def) then
  92. begin
  93. secondpass(left);
  94. location_copy(location,left.location);
  95. location_force_reg(exprasmlist,location,OS_64,false);
  96. cg64.a_op64_loc_reg(exprasmlist,OP_NOT,location,
  97. joinreg64(location.register64.reglo,location.register64.reghi));
  98. end
  99. else
  100. begin
  101. secondpass(left);
  102. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  103. location_copy(location,left.location);
  104. if location.loc=LOC_CREGISTER then
  105. location.register := cg.getintregister(exprasmlist,opsize);
  106. { perform the NOT operation }
  107. cg.a_op_reg_reg(exprasmlist,OP_NOT,opsize,location.register,left.location.register);
  108. end;
  109. end;
  110. {*****************************************************************************
  111. TM68KMODDIVNODE
  112. *****************************************************************************}
  113. procedure tm68kmoddivnode.emit_div_reg_reg(signed: boolean;denum,num : tregister);
  114. var
  115. continuelabel : tasmlabel;
  116. reg_d0,reg_d1 : tregister;
  117. paraloc1 : tcgpara;
  118. begin
  119. { no RTL call, so inline a zero denominator verification }
  120. if aktoptprocessor <> MC68000 then
  121. begin
  122. { verify if denominator is zero }
  123. objectlibrary.getlabel(continuelabel);
  124. { compare against zero, if not zero continue }
  125. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_NE,0,denum,continuelabel);
  126. // paraloc1.init;
  127. // cg.a_param_const(exprasmlist,OS_S32,200,paramanager.getintparaloc(pocall_default,1,paraloc1));
  128. cg.a_call_name(exprasmlist,'FPC_HANDLEERROR');
  129. cg.a_label(exprasmlist, continuelabel);
  130. if signed then
  131. exprasmlist.concat(taicpu.op_reg_reg(A_DIVS,S_L,denum,num))
  132. else
  133. exprasmlist.concat(taicpu.op_reg_reg(A_DIVU,S_L,denum,num));
  134. { result should be in denuminator }
  135. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,num,denum);
  136. end
  137. else
  138. begin
  139. { On MC68000/68010 mw must pass through RTL routines }
  140. reg_d0:=NR_D0;
  141. cg.getcpuregister(exprasmlist,NR_D0);
  142. reg_d1:=NR_D1;
  143. cg.getcpuregister(exprasmlist,NR_D1);
  144. { put numerator in d0 }
  145. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,num,reg_d0);
  146. { put denum in D1 }
  147. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,denum,reg_d1);
  148. if signed then
  149. cg.a_call_name(exprasmlist,'FPC_DIV_LONGINT')
  150. else
  151. cg.a_call_name(exprasmlist,'FPC_DIV_CARDINAL');
  152. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,reg_d0,denum);
  153. cg.ungetcpuregister(exprasmlist,reg_d0);
  154. cg.ungetcpuregister(exprasmlist,reg_d1);
  155. end;
  156. end;
  157. procedure tm68kmoddivnode.emit_mod_reg_reg(signed: boolean;denum,num : tregister);
  158. var tmpreg : tregister;
  159. continuelabel : tasmlabel;
  160. signlabel : tasmlabel;
  161. reg_d0,reg_d1 : tregister;
  162. begin
  163. { no RTL call, so inline a zero denominator verification }
  164. if aktoptprocessor <> MC68000 then
  165. begin
  166. { verify if denominator is zero }
  167. objectlibrary.getlabel(continuelabel);
  168. { compare against zero, if not zero continue }
  169. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_NE,0,denum,continuelabel);
  170. // cg.a_param_const(exprasmlist, OS_S32,200,paramanager.getintparaloc(pocall_default,1));
  171. cg.a_call_name(exprasmlist,'FPC_HANDLEERROR');
  172. cg.a_label(exprasmlist, continuelabel);
  173. tmpreg:=cg.getintregister(exprasmlist,OS_INT);
  174. { we have to prepare the high register with the }
  175. { correct sign. i.e we clear it, check if the low dword reg }
  176. { which will participate in the division is signed, if so we}
  177. { we extend the sign to the high doword register by inverting }
  178. { all the bits. }
  179. exprasmlist.concat(taicpu.op_reg(A_CLR,S_L,tmpreg));
  180. objectlibrary.getlabel(signlabel);
  181. exprasmlist.concat(taicpu.op_reg(A_TST,S_L,tmpreg));
  182. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_A,0,tmpreg,signlabel);
  183. { its a negative value, therefore change sign }
  184. cg.a_label(exprasmlist,signlabel);
  185. { tmpreg:num / denum }
  186. if signed then
  187. exprasmlist.concat(taicpu.op_reg_reg_reg(A_DIVSL,S_L,denum,tmpreg,num))
  188. else
  189. exprasmlist.concat(taicpu.op_reg_reg_reg(A_DIVUL,S_L,denum,tmpreg,num));
  190. { remainder in tmpreg }
  191. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,tmpreg,denum);
  192. cg.ungetcpuregister(exprasmlist,tmpreg);
  193. end
  194. else
  195. begin
  196. { On MC68000/68010 mw must pass through RTL routines }
  197. Reg_d0:=NR_D0;
  198. cg.getcpuregister(exprasmlist,NR_D0);
  199. Reg_d1:=NR_D1;
  200. cg.getcpuregister(exprasmlist,NR_D1);
  201. { put numerator in d0 }
  202. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,num,Reg_D0);
  203. { put denum in D1 }
  204. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,denum,Reg_D1);
  205. if signed then
  206. cg.a_call_name(exprasmlist,'FPC_MOD_LONGINT')
  207. else
  208. cg.a_call_name(exprasmlist,'FPC_MOD_CARDINAL');
  209. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,Reg_D0,denum);
  210. cg.ungetcpuregister(exprasmlist,Reg_D0);
  211. cg.ungetcpuregister(exprasmlist,Reg_D1);
  212. end;
  213. end;
  214. begin
  215. cnotnode:=tm68knotnode;
  216. cmoddivnode:=tm68kmoddivnode;
  217. end.
  218. {
  219. $Log$
  220. Revision 1.12 2005-01-08 04:11:26 karoly
  221. * made m68k to compile again
  222. Revision 1.11 2004/10/31 21:45:03 peter
  223. * generic tlocation
  224. * move tlocation to cgutils
  225. Revision 1.10 2004/09/25 14:23:54 peter
  226. * ungetregister is now only used for cpuregisters, renamed to
  227. ungetcpuregister
  228. * renamed (get|unget)explicitregister(s) to ..cpuregister
  229. * removed location-release/reference_release
  230. Revision 1.9 2004/06/20 08:55:31 florian
  231. * logs truncated
  232. Revision 1.8 2004/04/25 21:26:16 florian
  233. * some m68k stuff fixed
  234. }