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