n68kmat.pas 15 KB

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