n68kmat.pas 15 KB

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