n68kmat.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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(tnotnode)
  24. procedure pass_generate_code;override;
  25. end;
  26. tm68kmoddivnode = class(tcgmoddivnode)
  27. procedure emit_div_reg_reg(signed: boolean;denum,num : tregister);override;
  28. procedure emit_mod_reg_reg(signed: boolean;denum,num : tregister);override;
  29. end;
  30. tm68kshlshrnode = class(tshlshrnode)
  31. procedure pass_generate_code;override;
  32. { everything will be handled in pass_2 }
  33. function first_shlshr64bitint: tnode; override;
  34. end;
  35. implementation
  36. uses
  37. globtype,systems,
  38. cutils,verbose,globals,
  39. symconst,symdef,aasmbase,aasmtai,aasmdata,aasmcpu,
  40. pass_1,pass_2,procinfo,
  41. ncon,
  42. cpuinfo,paramgr,defutil,parabase,
  43. tgobj,ncgutil,cgobj,cgutils,rgobj,rgcpu,cgcpu,cg64f32;
  44. {*****************************************************************************
  45. TM68KNOTNODE
  46. *****************************************************************************}
  47. procedure tm68knotnode.pass_generate_code;
  48. var
  49. hl : tasmlabel;
  50. opsize : tcgsize;
  51. begin
  52. opsize:=def_cgsize(resultdef);
  53. if is_boolean(resultdef) then
  54. begin
  55. { the second pass could change the location of left }
  56. { if it is a register variable, so we've to do }
  57. { this before the case statement }
  58. if left.location.loc<>LOC_JUMP then
  59. secondpass(left);
  60. case left.location.loc of
  61. LOC_JUMP :
  62. begin
  63. location_reset(location,LOC_JUMP,OS_NO);
  64. hl:=current_procinfo.CurrTrueLabel;
  65. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  66. current_procinfo.CurrFalseLabel:=hl;
  67. secondpass(left);
  68. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  69. hl:=current_procinfo.CurrTrueLabel;
  70. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  71. current_procinfo.CurrFalseLabel:=hl;
  72. end;
  73. LOC_FLAGS :
  74. begin
  75. location_copy(location,left.location);
  76. // location_release(current_asmdata.CurrAsmList,left.location);
  77. inverse_flags(location.resflags);
  78. end;
  79. LOC_CONSTANT,
  80. LOC_REGISTER,
  81. LOC_CREGISTER,
  82. LOC_REFERENCE,
  83. LOC_CREFERENCE :
  84. begin
  85. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(resultdef),true);
  86. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,tcgsize2opsize[opsize],left.location.register));
  87. // location_release(current_asmdata.CurrAsmList,left.location);
  88. location_reset(location,LOC_FLAGS,OS_NO);
  89. location.resflags:=F_E;
  90. end;
  91. else
  92. internalerror(200203224);
  93. end;
  94. end
  95. else if is_64bitint(left.resultdef) then
  96. begin
  97. secondpass(left);
  98. location_copy(location,left.location);
  99. location_force_reg(current_asmdata.CurrAsmList,location,OS_64,false);
  100. cg64.a_op64_loc_reg(current_asmdata.CurrAsmList,OP_NOT,OS_64,location,
  101. joinreg64(location.register64.reglo,location.register64.reghi));
  102. end
  103. else
  104. begin
  105. secondpass(left);
  106. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),false);
  107. location_copy(location,left.location);
  108. if location.loc=LOC_CREGISTER then
  109. location.register := cg.getintregister(current_asmdata.CurrAsmList,opsize);
  110. { perform the NOT operation }
  111. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,opsize,location.register,left.location.register);
  112. end;
  113. end;
  114. {*****************************************************************************
  115. TM68KMODDIVNODE
  116. *****************************************************************************}
  117. procedure tm68kmoddivnode.emit_div_reg_reg(signed: boolean;denum,num : tregister);
  118. var
  119. continuelabel : tasmlabel;
  120. reg_d0,reg_d1 : tregister;
  121. paraloc1 : tcgpara;
  122. begin
  123. { no RTL call, so inline a zero denominator verification }
  124. if current_settings.cputype <> cpu_MC68000 then
  125. begin
  126. { verify if denominator is zero }
  127. current_asmdata.getjumplabel(continuelabel);
  128. { compare against zero, if not zero continue }
  129. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_S32,OC_NE,0,denum,continuelabel);
  130. // paraloc1.init;
  131. // cg.a_param_const(current_asmdata.CurrAsmList,OS_S32,200,paramanager.getintparaloc(pocall_default,1,paraloc1));
  132. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_HANDLEERROR',false);
  133. cg.a_label(current_asmdata.CurrAsmList, continuelabel);
  134. if signed then
  135. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_DIVS,S_L,denum,num))
  136. else
  137. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_DIVU,S_L,denum,num));
  138. { result should be in denuminator }
  139. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,num,denum);
  140. end
  141. else
  142. begin
  143. { On MC68000/68010 mw must pass through RTL routines }
  144. reg_d0:=NR_D0;
  145. cg.getcpuregister(current_asmdata.CurrAsmList,NR_D0);
  146. reg_d1:=NR_D1;
  147. cg.getcpuregister(current_asmdata.CurrAsmList,NR_D1);
  148. { put numerator in d0 }
  149. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,num,reg_d0);
  150. { put denum in D1 }
  151. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,denum,reg_d1);
  152. if signed then
  153. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DIV_LONGINT',false)
  154. else
  155. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DIV_CARDINAL',false);
  156. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,reg_d0,denum);
  157. cg.ungetcpuregister(current_asmdata.CurrAsmList,reg_d0);
  158. cg.ungetcpuregister(current_asmdata.CurrAsmList,reg_d1);
  159. end;
  160. end;
  161. procedure tm68kmoddivnode.emit_mod_reg_reg(signed: boolean;denum,num : tregister);
  162. var tmpreg : tregister;
  163. continuelabel : tasmlabel;
  164. signlabel : tasmlabel;
  165. reg_d0,reg_d1 : tregister;
  166. begin
  167. // writeln('emit mod reg reg');
  168. { no RTL call, so inline a zero denominator verification }
  169. if current_settings.cputype <> cpu_MC68000 then
  170. begin
  171. { verify if denominator is zero }
  172. current_asmdata.getjumplabel(continuelabel);
  173. { compare against zero, if not zero continue }
  174. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_S32,OC_NE,0,denum,continuelabel);
  175. // cg.a_param_const(current_asmdata.CurrAsmList, OS_S32,200,paramanager.getintparaloc(pocall_default,1));
  176. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_HANDLEERROR',false);
  177. cg.a_label(current_asmdata.CurrAsmList, continuelabel);
  178. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  179. { we have to prepare the high register with the }
  180. { correct sign. i.e we clear it, check if the low dword reg }
  181. { which will participate in the division is signed, if so we}
  182. { we extend the sign to the high doword register by inverting }
  183. { all the bits. }
  184. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_CLR,S_L,tmpreg));
  185. current_asmdata.getjumplabel(signlabel);
  186. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,tmpreg));
  187. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_S32,OC_A,0,tmpreg,signlabel);
  188. { its a negative value, therefore change sign }
  189. cg.a_label(current_asmdata.CurrAsmList,signlabel);
  190. { tmpreg:num / denum }
  191. if signed then
  192. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIVSL,S_L,denum,tmpreg,num))
  193. else
  194. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_DIVUL,S_L,denum,tmpreg,num));
  195. { remainder in tmpreg }
  196. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,tmpreg,denum);
  197. // cg.ungetcpuregister(current_asmdata.CurrAsmList,tmpreg);
  198. end
  199. else
  200. begin
  201. { On MC68000/68010 mw must pass through RTL routines }
  202. Reg_d0:=NR_D0;
  203. cg.getcpuregister(current_asmdata.CurrAsmList,NR_D0);
  204. Reg_d1:=NR_D1;
  205. cg.getcpuregister(current_asmdata.CurrAsmList,NR_D1);
  206. { put numerator in d0 }
  207. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,num,Reg_D0);
  208. { put denum in D1 }
  209. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,denum,Reg_D1);
  210. if signed then
  211. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_MOD_LONGINT',false)
  212. else
  213. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_MOD_CARDINAL',false);
  214. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,Reg_D0,denum);
  215. cg.ungetcpuregister(current_asmdata.CurrAsmList,Reg_D0);
  216. cg.ungetcpuregister(current_asmdata.CurrAsmList,Reg_D1);
  217. end;
  218. // writeln('exits');
  219. end;
  220. {*****************************************************************************
  221. TM68KSHLRSHRNODE
  222. *****************************************************************************}
  223. function tm68kShlShrNode.first_shlshr64bitint:TNode;
  224. begin
  225. { 2nd pass is our friend }
  226. result := nil;
  227. end;
  228. { TODO: FIX ME!!! shlshrnode needs review}
  229. procedure tm68kshlshrnode.pass_generate_code;
  230. var
  231. hregister,resultreg,hregister1,
  232. hreg64hi,hreg64lo : tregister;
  233. op : topcg;
  234. shiftval: aint;
  235. begin
  236. secondpass(left);
  237. secondpass(right);
  238. if is_64bit(left.resultdef) then
  239. begin
  240. location_reset(location,LOC_REGISTER,OS_64);
  241. { load left operator in a register }
  242. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_64,false);
  243. hreg64hi:=left.location.register64.reghi;
  244. hreg64lo:=left.location.register64.reglo;
  245. shiftval := tordconstnode(right).value.svalue;
  246. shiftval := shiftval and 63;
  247. if shiftval > 31 then
  248. begin
  249. if nodetype = shln then
  250. begin
  251. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64hi);
  252. if (shiftval and 31) <> 0 then
  253. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval and 31,hreg64lo,hreg64lo);
  254. end
  255. else
  256. begin
  257. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64lo);
  258. if (shiftval and 31) <> 0 then
  259. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval and 31,hreg64hi,hreg64hi);
  260. end;
  261. location.register64.reglo:=hreg64hi;
  262. location.register64.reghi:=hreg64lo;
  263. end
  264. else
  265. begin
  266. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  267. if nodetype = shln then
  268. begin
  269. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,32-shiftval,hreg64lo,hregister);
  270. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64hi,hreg64hi);
  271. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64hi,hreg64hi);
  272. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64lo,hreg64lo);
  273. end
  274. else
  275. begin
  276. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,32-shiftval,hreg64hi,hregister);
  277. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64lo,hreg64lo);
  278. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64lo,hreg64lo);
  279. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64hi,hreg64hi);
  280. end;
  281. location.register64.reghi:=hreg64hi;
  282. location.register64.reglo:=hreg64lo;
  283. end;
  284. end
  285. else
  286. begin
  287. { load left operators in a register }
  288. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),true);
  289. location_copy(location,left.location);
  290. resultreg := location.register;
  291. hregister1 := location.register;
  292. if (location.loc = LOC_CREGISTER) then
  293. begin
  294. location.loc := LOC_REGISTER;
  295. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  296. location.register := resultreg;
  297. end;
  298. { determine operator }
  299. if nodetype=shln then
  300. op:=OP_SHL
  301. else
  302. op:=OP_SHR;
  303. { shifting by a constant directly coded: }
  304. if (right.nodetype=ordconstn) then
  305. begin
  306. if tordconstnode(right).value.svalue and 31<>0 then
  307. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,OS_32,tordconstnode(right).value.svalue and 31,hregister1,resultreg)
  308. end
  309. else
  310. begin
  311. { load shift count in a register if necessary }
  312. location_force_reg(current_asmdata.CurrAsmList,right.location,def_cgsize(right.resultdef),true);
  313. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,OS_32,right.location.register,hregister1,resultreg);
  314. end;
  315. end;
  316. end;
  317. begin
  318. cnotnode:=tm68knotnode;
  319. cmoddivnode:=tm68kmoddivnode;
  320. cshlshrnode:=tm68kshlshrnode;
  321. end.