n68kmat.pas 16 KB

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