n68kmat.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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,cginfo;
  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. cgbase,pass_1,pass_2,
  37. ncon,
  38. cpuinfo,paramgr,defutil,
  39. tgobj,ncgutil,cgobj,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.registerlow,location.registerhigh));
  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 := rg.getregisterint(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. begin
  118. { no RTL call, so inline a zero denominator verification }
  119. if aktoptprocessor <> MC68000 then
  120. begin
  121. { verify if denominator is zero }
  122. objectlibrary.getlabel(continuelabel);
  123. { compare against zero, if not zero continue }
  124. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_NE,0,denum,continuelabel);
  125. cg.a_param_const(exprasmlist, OS_S32,200,paramanager.getintparaloc(exprasmlist,1));
  126. cg.a_call_name(exprasmlist,'FPC_HANDLEERROR');
  127. paramanager.freeintparaloc(exprasmlist,1);
  128. cg.a_label(exprasmlist, continuelabel);
  129. if signed then
  130. exprasmlist.concat(taicpu.op_reg_reg(A_DIVS,S_L,denum,num))
  131. else
  132. exprasmlist.concat(taicpu.op_reg_reg(A_DIVU,S_L,denum,num));
  133. { result should be in denuminator }
  134. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,num,denum);
  135. end
  136. else
  137. begin
  138. { On MC68000/68010 mw must pass through RTL routines }
  139. reg_d0:=rg.getexplicitregisterint(exprasmlist,NR_D0);
  140. reg_d1:=rg.getexplicitregisterint(exprasmlist,NR_D1);
  141. { put numerator in d0 }
  142. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,num,reg_d0);
  143. { put denum in D1 }
  144. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,denum,reg_d1);
  145. if signed then
  146. cg.a_call_name(exprasmlist,'FPC_DIV_LONGINT')
  147. else
  148. cg.a_call_name(exprasmlist,'FPC_DIV_CARDINAL');
  149. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,reg_d0,denum);
  150. rg.ungetregisterint(exprasmlist,reg_d0);
  151. rg.ungetregisterint(exprasmlist,reg_d1);
  152. end;
  153. end;
  154. procedure tm68kmoddivnode.emit_mod_reg_reg(signed: boolean;denum,num : tregister);
  155. var tmpreg : tregister;
  156. continuelabel : tasmlabel;
  157. signlabel : tasmlabel;
  158. reg_d0,reg_d1 : tregister;
  159. begin
  160. { no RTL call, so inline a zero denominator verification }
  161. if aktoptprocessor <> MC68000 then
  162. begin
  163. { verify if denominator is zero }
  164. objectlibrary.getlabel(continuelabel);
  165. { compare against zero, if not zero continue }
  166. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_NE,0,denum,continuelabel);
  167. cg.a_param_const(exprasmlist, OS_S32,200,paramanager.getintparaloc(exprasmlist,1));
  168. cg.a_call_name(exprasmlist,'FPC_HANDLEERROR');
  169. paramanager.freeintparaloc(exprasmlist,1);
  170. cg.a_label(exprasmlist, continuelabel);
  171. tmpreg := cg.get_scratch_reg_int(exprasmlist,OS_INT);
  172. { we have to prepare the high register with the }
  173. { correct sign. i.e we clear it, check if the low dword reg }
  174. { which will participate in the division is signed, if so we}
  175. { we extend the sign to the high doword register by inverting }
  176. { all the bits. }
  177. exprasmlist.concat(taicpu.op_reg(A_CLR,S_L,tmpreg));
  178. objectlibrary.getlabel(signlabel);
  179. exprasmlist.concat(taicpu.op_reg(A_TST,S_L,tmpreg));
  180. cg.a_cmp_const_reg_label(exprasmlist,OS_S32,OC_A,0,tmpreg,signlabel);
  181. { its a negative value, therefore change sign }
  182. cg.a_label(exprasmlist,signlabel);
  183. { tmpreg:num / denum }
  184. if signed then
  185. exprasmlist.concat(taicpu.op_reg_reg_reg(A_DIVSL,S_L,denum,tmpreg,num))
  186. else
  187. exprasmlist.concat(taicpu.op_reg_reg_reg(A_DIVUL,S_L,denum,tmpreg,num));
  188. { remainder in tmpreg }
  189. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,tmpreg,denum);
  190. cg.free_scratch_reg(exprasmlist,tmpreg);
  191. end
  192. else
  193. begin
  194. { On MC68000/68010 mw must pass through RTL routines }
  195. Reg_d0:=rg.getexplicitregisterint(exprasmlist,NR_D0);
  196. Reg_d1:=rg.getexplicitregisterint(exprasmlist,NR_D1);
  197. { put numerator in d0 }
  198. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,num,Reg_D0);
  199. { put denum in D1 }
  200. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,denum,Reg_D1);
  201. if signed then
  202. cg.a_call_name(exprasmlist,'FPC_MOD_LONGINT')
  203. else
  204. cg.a_call_name(exprasmlist,'FPC_MOD_CARDINAL');
  205. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,Reg_D0,denum);
  206. rg.ungetregisterint(exprasmlist,Reg_D0);
  207. rg.ungetregisterint(exprasmlist,Reg_D1);
  208. end;
  209. end;
  210. begin
  211. cnotnode:=tm68knotnode;
  212. cmoddivnode:=tm68kmoddivnode;
  213. end.
  214. {
  215. $Log$
  216. Revision 1.7 2003-06-07 18:57:04 jonas
  217. + added freeintparaloc
  218. * ppc get/freeintparaloc now check whether the parameter regs are
  219. properly allocated/deallocated (and get an extra list para)
  220. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  221. * fixed lot of missing pi_do_call's
  222. Revision 1.6 2003/02/19 22:00:16 daniel
  223. * Code generator converted to new register notation
  224. - Horribily outdated todo.txt removed
  225. Revision 1.5 2003/02/02 19:25:54 carl
  226. * Several bugfixes for m68k target (register alloc., opcode emission)
  227. + VIS target
  228. + Generic add more complete (still not verified)
  229. Revision 1.4 2002/09/07 15:25:13 peter
  230. * old logs removed and tabs fixed
  231. Revision 1.3 2002/08/15 15:15:55 carl
  232. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  233. * more generic nodes for maths
  234. * several fixes for better m68k support
  235. Revision 1.2 2002/08/15 08:13:54 carl
  236. - a_load_sym_ofs_reg removed
  237. * loadvmt now calls loadaddr_ref_reg instead
  238. Revision 1.1 2002/08/14 19:16:34 carl
  239. + m68k type conversion nodes
  240. + started some mathematical nodes
  241. * out of bound references should now be handled correctly
  242. }