tgen68k.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl, Carl Eric Codere
  4. This unit handles the temporary variables stuff for m68k
  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 tgen68k;
  19. interface
  20. uses
  21. cobjects,globals,tree,hcodegen,verbose,files,aasm
  22. {$ifdef m68k}
  23. ,m68k
  24. {$endif}
  25. ;
  26. type
  27. tregisterset = set of tregister;
  28. tpushed = array[R_D0..R_A6] of boolean;
  29. const
  30. { D2 to D5 usable as scratch registers }
  31. usablereg32 : byte = 4;
  32. { A2 to A4 usable as address registers }
  33. usableaddress: byte = 3;
  34. { FP2 to FP7 usable as FPU registers }
  35. usablefloatreg : byte = 6;
  36. function getregister32 : tregister;
  37. procedure ungetregister32(r : tregister);
  38. { return a free 32-bit address register }
  39. function getaddressreg: tregister;
  40. procedure ungetregister(r : tregister);
  41. procedure cleartempgen;
  42. function getfloatreg: tregister;
  43. { returns a free floating point register }
  44. { used in real, fpu mode, otherwise we }
  45. { must use standard register allocation }
  46. procedure del_reference(const ref : treference);
  47. procedure del_locref(const location : tlocation);
  48. { pushs and restores registers }
  49. procedure pushusedregisters(var pushed : tpushed;b : word);
  50. procedure popusedregisters(const pushed : tpushed);
  51. var
  52. unused,usableregs : tregisterset;
  53. c_usableregs : longint;
  54. usedinproc : word;
  55. { count, how much a register must be pushed if it is used as register }
  56. { variable }
  57. reg_pushes : array[R_D0..R_A6] of longint;
  58. is_reg_var : array[R_D0..R_A6] of boolean;
  59. implementation
  60. procedure pushusedregisters(var pushed : tpushed;b : word);
  61. var
  62. r : tregister;
  63. begin
  64. { the following registers can be pushed }
  65. { D0, D1, D2, D3, D4, D5, D6, D7, A0 }
  66. { A1, A2, A3, A4 }
  67. for r:=R_D2 to R_A4 do
  68. begin
  69. pushed[r]:=false;
  70. { if the register is used by the calling subroutine }
  71. if ((b and ($800 shr word(r)))<>0) then
  72. begin
  73. { and is present in use }
  74. if not(r in unused) then
  75. begin
  76. { then save it }
  77. { then save it on the stack }
  78. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,r,R_SPPUSH)));
  79. { here was a big problem !!!!!}
  80. { you cannot do that for a register that is
  81. globally assigned to a var
  82. this also means that you must push it much more
  83. often, but there must be a better way
  84. maybe by putting the value back to the stack !! }
  85. if not(is_reg_var[r]) then
  86. unused:=unused+[r];
  87. pushed[r]:=true;
  88. end;
  89. end;
  90. end;
  91. end;
  92. procedure popusedregisters(const pushed : tpushed);
  93. var
  94. r : tregister;
  95. begin
  96. for r:=R_A4 downto R_D2 do
  97. if pushed[r] then
  98. begin
  99. exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_SPPULL,r)));
  100. unused:=unused-[r];
  101. end;
  102. end;
  103. procedure ungetregister(r : tregister);
  104. begin
  105. ungetregister32(r)
  106. end;
  107. procedure del_reference(const ref : treference);
  108. begin
  109. if ref.isintvalue then
  110. exit;
  111. ungetregister(ref.base);
  112. ungetregister32(ref.index);
  113. end;
  114. procedure del_locref(const location : tlocation);
  115. begin
  116. if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  117. exit;
  118. if location.reference.isintvalue then
  119. exit;
  120. ungetregister(location.reference.base);
  121. ungetregister32(location.reference.index);
  122. end;
  123. procedure ungetregister32(r : tregister);
  124. begin
  125. if r in [R_D2,R_D3,R_D4,R_D5,R_D7] then
  126. begin
  127. unused:=unused+[r];
  128. inc(usablereg32);
  129. end
  130. else
  131. if r in [R_FP2,R_FP3,R_FP4,R_FP5,R_FP6,R_FP7] then
  132. begin
  133. unused:=unused+[r];
  134. inc(usablefloatreg);
  135. end
  136. else
  137. if r in [R_A2,R_A3,R_A4,R_A6,R_SP] then
  138. begin
  139. unused:=unused+[r];
  140. inc(usableaddress);
  141. {$ifdef EXTDEBUG}
  142. end
  143. else
  144. begin
  145. if not (r in [R_NO]) then
  146. begin
  147. Comment(V_Debug,'ungetregister32() deallocation of reserved register.');
  148. end;
  149. end;
  150. {$ELSE}
  151. end;
  152. {$ENDIF}
  153. end;
  154. function getfloatreg: tregister;
  155. { returns a free floating point register }
  156. { used in real, fpu mode, otherwise we }
  157. { must use standard register allocation }
  158. var
  159. i:tregister;
  160. begin
  161. dec(usablefloatreg);
  162. if usablefloatreg = 0 then
  163. Message(cg_f_internal_error_in_getfloatreg);
  164. for i:=R_FP2 to R_FP7 do
  165. begin
  166. if i in unused then
  167. begin
  168. unused := unused-[i];
  169. getfloatreg := i;
  170. exit;
  171. end;
  172. end;
  173. { if we are here, then there was an allocation failure }
  174. Message(cg_f_internal_error_in_getfloatreg);
  175. end;
  176. function getaddressreg: tregister;
  177. begin
  178. dec(usableaddress);
  179. if R_A2 in unused then
  180. begin
  181. unused:=unused-[R_A2];
  182. usedinproc:=usedinproc or ($800 shr word(R_A2));
  183. getaddressreg:=R_A2;
  184. end
  185. else
  186. if R_A3 in unused then
  187. begin
  188. unused:=unused-[R_A3];
  189. usedinproc:=usedinproc or ($800 shr word(R_A3));
  190. getaddressreg:=R_A3;
  191. end
  192. else
  193. if R_A4 in unused then
  194. begin
  195. unused:=unused-[R_A4];
  196. usedinproc:=usedinproc or ($800 shr word(R_A4));
  197. getaddressreg:=R_A4;
  198. end
  199. else
  200. begin
  201. internalerror(10);
  202. end;
  203. end;
  204. function getregister32 : tregister;
  205. begin
  206. dec(usablereg32);
  207. if R_D2 in unused then
  208. begin
  209. unused:=unused-[R_D2];
  210. usedinproc:=usedinproc or ($800 shr word(R_D2));
  211. getregister32:=R_D2;
  212. end
  213. else if R_D3 in unused then
  214. begin
  215. unused:=unused-[R_D3];
  216. usedinproc:=usedinproc or ($800 shr word(R_D3));
  217. getregister32:=R_D3;
  218. end
  219. else if R_D4 in unused then
  220. begin
  221. unused:=unused-[R_D4];
  222. usedinproc:=usedinproc or ($800 shr word(R_D4));
  223. getregister32:=R_D4;
  224. end
  225. else if R_D5 in unused then
  226. begin
  227. unused:=unused-[R_D5];
  228. usedinproc:=usedinproc or ($800 shr word(R_D5));
  229. getregister32:=R_D5;
  230. end
  231. else if R_D7 in unused then
  232. begin
  233. unused:=unused-[R_D7];
  234. usedinproc:=usedinproc or ($800 shr word(R_D7));
  235. getregister32:=R_D7;
  236. end
  237. else
  238. begin
  239. internalerror(10);
  240. end;
  241. end;
  242. procedure cleartempgen;
  243. begin
  244. unused:=usableregs;
  245. usablereg32:=c_usableregs;
  246. end;
  247. begin
  248. { contains both information on Address registers and data registers }
  249. { even if they are allocated separately. }
  250. usableregs:=[R_D0,R_D1,R_D2,R_D3,R_D4,R_D5,R_D6,R_D7,R_A0,R_A1,R_A2,R_A3,R_A4,
  251. R_FP0,R_FP1,R_FP2,R_FP3,R_FP4,R_FP5,R_FP6,R_FP7];
  252. c_usableregs:=4;
  253. end.
  254. {
  255. $Log$
  256. Revision 1.2 1998-06-08 13:13:46 pierre
  257. + temporary variables now in temp_gen.pas unit
  258. because it is processor independent
  259. * mppc68k.bat modified to undefine i386 and support_mmx
  260. (which are defaults for i386)
  261. Revision 1.1.1.1 1998/03/25 11:18:15 root
  262. * Restored version
  263. Revision 1.12 1998/03/22 12:45:38 florian
  264. * changes of Carl-Eric to m68k target commit:
  265. - wrong nodes because of the new string cg in intel, I had to create
  266. this under m68k also ... had to work it out to fix potential alignment
  267. problems --> this removes the crash of the m68k compiler.
  268. - added absolute addressing in m68k assembler (required for Amiga startup)
  269. - fixed alignment problems (because of byte return values, alignment
  270. would not be always valid) -- is this ok if i change the offset if odd in
  271. setfirsttemp ?? -- it seems ok...
  272. Revision 1.11 1998/03/10 04:21:15 carl
  273. * fixed extdebug problems
  274. Revision 1.10 1998/03/10 01:17:30 peter
  275. * all files have the same header
  276. * messages are fully implemented, EXTDEBUG uses Comment()
  277. + AG... files for the Assembler generation
  278. Revision 1.9 1998/03/06 00:53:00 peter
  279. * replaced all old messages from errore.msg, only ExtDebug and some
  280. Comment() calls are left
  281. * fixed options.pas
  282. Revision 1.8 1998/03/02 01:49:35 peter
  283. * renamed target_DOS to target_GO32V1
  284. + new verbose system, merged old errors and verbose units into one new
  285. verbose.pas, so errors.pas is obsolete
  286. Revision 1.7 1998/02/13 10:35:51 daniel
  287. * Made Motorola version compilable.
  288. * Fixed optimizer
  289. Revision 1.6 1998/01/11 03:40:16 carl
  290. + added fpu register allocation
  291. Revision 1.3 1997/12/09 14:13:07 carl
  292. * bugfix of free register list.
  293. Revision 1.2 1997/11/28 18:14:49 pierre
  294. working version with several bug fixes
  295. Revision 1.1.1.1 1997/11/27 08:33:03 michael
  296. FPC Compiler CVS start
  297. Pre-CVS log:
  298. + feature added
  299. - removed
  300. * bug fixed or changed
  301. History (started with version 0.9.0):
  302. 7th december 1996:
  303. * some code from Pierre Muller inserted
  304. makes the use of the stack more efficient
  305. 5th september 1997:
  306. + Converted for Motorola MC68000 output (C. E. Codere)
  307. 24nd september 1997:
  308. + Reserved register list modified. (CEC)
  309. 26 september 1997:
  310. + Converted to work with v093 (CEC)
  311. * Knowing that base is in address register, modified routines
  312. accordingly. (CEC)
  313. 27 september 1997:
  314. + pushusedregisters now pushes only non-scratch registers.
  315. 2nd october 1997:
  316. + added strict error checking when extdebug defined.
  317. 23 october 1997:
  318. - it seems that sp, and the base pointer can be freed in ungetregister,
  319. removed warning accordingly. (CEC).
  320. * bugfix of address register in usableregs set. (They were not defined...) (CEC).
  321. * other stupid bug! When I changed the register conventions, I forgot to change
  322. getaddressreg to reflect those changes!! (CEC).
  323. }