cpupi.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. This unit contains the CPU specific part of tprocinfo
  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. { This unit contains the CPU specific part of tprocinfo. }
  18. unit cpupi;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,cutils,
  23. procinfo,cpuinfo,psub,cgbase,
  24. aasmdata;
  25. type
  26. tarmprocinfo = class(tcgprocinfo)
  27. { for arm thumb, we need to know the stackframe size before
  28. starting procedure compilation, so this contains the stack frame size, the compiler
  29. should assume
  30. if this size is too little the procedure must be compiled again with a larger value }
  31. stackframesize,
  32. floatregstart : aint;
  33. stackpaddingreg: TSuperRegister;
  34. // procedure handle_body_start;override;
  35. // procedure after_pass1;override;
  36. procedure set_first_temp_offset;override;
  37. function calc_stackframe_size:longint;override;
  38. procedure init_framepointer; override;
  39. procedure generate_parameter_info;override;
  40. procedure allocate_got_register(list : TAsmList);override;
  41. end;
  42. implementation
  43. uses
  44. globals,systems,
  45. cpubase,
  46. tgobj,
  47. symconst,symtype,symsym,paramgr,
  48. cgutils,
  49. cgobj,
  50. defutil;
  51. procedure tarmprocinfo.set_first_temp_offset;
  52. var
  53. localsize : aint;
  54. i : longint;
  55. begin
  56. { We allocate enough space to save all registers because we can't determine
  57. the necessary space because the used registers aren't known before
  58. secondpass is run. Even worse, patching
  59. the local offsets after generating the code could cause trouble because
  60. "shifter" constants could change to non-"shifter" constants. This
  61. is especially a problem when taking the address of a local. For now,
  62. this extra memory should hurt less than generating all local contants with offsets
  63. >256 as non shifter constants }
  64. if (po_nostackframe in procdef.procoptions) then
  65. begin
  66. { maxpushedparasize sghould be zero,
  67. if not we will get an error later. }
  68. tg.setfirsttemp(maxpushedparasize);
  69. exit;
  70. end;
  71. if tg.direction = -1 then
  72. begin
  73. if (target_info.system<>system_arm_darwin) then
  74. { Non-Darwin, worst case: r4-r10,r11,r13,r14,r15 is saved -> -28-16, but we
  75. always adjust the frame pointer to point to the first stored
  76. register (= last register in list above) -> + 4 }
  77. tg.setfirsttemp(-28-16)
  78. else
  79. { on Darwin first r4-r7,r14 are saved, then r7 is adjusted to
  80. point to the saved r7, and next r8,r10,r11 gets saved -> -24
  81. (r4-r6 and r8,r10,r11) }
  82. tg.setfirsttemp(-24)
  83. end
  84. else
  85. tg.setfirsttemp(maxpushedparasize);
  86. { estimate stack frame size }
  87. if GenerateThumbCode then
  88. begin
  89. stackframesize:=maxpushedparasize+32;
  90. localsize:=0;
  91. for i:=0 to procdef.localst.SymList.Count-1 do
  92. if tsym(procdef.localst.SymList[i]).typ=localvarsym then
  93. inc(localsize,tabstractnormalvarsym(procdef.localst.SymList[i]).getsize);
  94. inc(stackframesize,localsize);
  95. localsize:=0;
  96. for i:=0 to procdef.parast.SymList.Count-1 do
  97. if tsym(procdef.parast.SymList[i]).typ=paravarsym then
  98. if is_open_string(tabstractnormalvarsym(procdef.parast.SymList[i]).vardef) then
  99. inc(localsize,256)
  100. else
  101. inc(localsize,tabstractnormalvarsym(procdef.parast.SymList[i]).getsize);
  102. inc(stackframesize,localsize);
  103. if pi_needs_implicit_finally in flags then
  104. inc(stackframesize,40);
  105. if pi_uses_exceptions in flags then
  106. inc(stackframesize,40);
  107. if procdef.proctypeoption in [potype_constructor] then
  108. inc(stackframesize,40*2);
  109. inc(stackframesize,estimatedtempsize);
  110. stackframesize:=Align(stackframesize,8);
  111. end;
  112. end;
  113. function tarmprocinfo.calc_stackframe_size:longint;
  114. var
  115. firstfloatreg,lastfloatreg,
  116. r : byte;
  117. floatsavesize : aword;
  118. regs: tcpuregisterset;
  119. begin
  120. if GenerateThumbCode then
  121. result:=stackframesize
  122. else
  123. begin
  124. maxpushedparasize:=align(maxpushedparasize,max(current_settings.alignment.localalignmin,4));
  125. floatsavesize:=0;
  126. case current_settings.fputype of
  127. fpu_fpa,
  128. fpu_fpa10,
  129. fpu_fpa11:
  130. begin
  131. { save floating point registers? }
  132. firstfloatreg:=RS_NO;
  133. regs:=cg.rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  134. for r:=RS_F0 to RS_F7 do
  135. if r in regs then
  136. begin
  137. if firstfloatreg=RS_NO then
  138. firstfloatreg:=r;
  139. lastfloatreg:=r;
  140. end;
  141. if firstfloatreg<>RS_NO then
  142. floatsavesize:=(lastfloatreg-firstfloatreg+1)*12;
  143. end;
  144. fpu_vfpv2,
  145. fpu_vfpv3,
  146. fpu_vfpv3_d16:
  147. begin
  148. floatsavesize:=0;
  149. regs:=cg.rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  150. for r:=RS_D0 to RS_D31 do
  151. if r in regs then
  152. inc(floatsavesize,8);
  153. end;
  154. fpu_fpv4_s16:
  155. begin
  156. floatsavesize:=0;
  157. regs:=cg.rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  158. for r:=RS_D0 to RS_D15 do
  159. if r in regs then
  160. inc(floatsavesize,8);
  161. end;
  162. end;
  163. floatsavesize:=align(floatsavesize,max(current_settings.alignment.localalignmin,4));
  164. result:=Align(tg.direction*tg.lasttemp,max(current_settings.alignment.localalignmin,4))+maxpushedparasize+aint(floatsavesize);
  165. { Note: in cgcpu "-floatregstart" is subtracted -> reason based on
  166. "adding floatregstart" to avoid double negation
  167. tg.direction=1 -> no framepointer ->
  168. 1) save used int registers
  169. 2) allocate stacksize (= subtracting result, which is positive,
  170. from the stackpointer)
  171. 3) add floatregstart to the stackpointer to get the offset where
  172. to store the floating point registers (-> floatregstart
  173. should be positive)
  174. 4) store the floating point registers from this offset with IA
  175. (i.e., this offset and higher addresses -> offset should
  176. point to lower end of area)
  177. -> newsp+(result) points to lower end of saved int registers area
  178. -> newsp+(result-floatsavesize) points to lower end of float reg
  179. saving area
  180. tg.direction=-1 -> with framepointer ->
  181. 1) save stack pointer in framepointer
  182. 2) save used int registers using stackpointer
  183. 3) allocate stacksize (= subtracting result, which is positive,
  184. from the stack pointer)
  185. 4) add floatregstart" to the framepointer to get the offset
  186. where to store the floating point registers (-> floatregstart
  187. should be negative)
  188. 5) store the floating point registers from this offset with IA
  189. (i.e., this offset and higher addresses -> offset should
  190. point to lower end of area)
  191. o in this case, firsttemp starts right after the saved int
  192. registers area (or a bit further, because it's calculated for
  193. the worst-case scenario, when all non-volative integer
  194. registers have to be saved) -> we store the floating point
  195. registers between the last temp and the parameter pushing area
  196. -> fp+(-result) points to the top of the stack (= end of
  197. parameter pushing area)
  198. -> fp+(-result+maxpushedparasize) points to the start of the
  199. parameter pushing area = lower end of float reg saving area
  200. }
  201. if tg.direction=1 then
  202. floatregstart:=result-aint(floatsavesize)
  203. else
  204. floatregstart:=-result+maxpushedparasize;
  205. end;
  206. end;
  207. procedure tarmprocinfo.init_framepointer;
  208. begin
  209. if (target_info.system in systems_darwin) or GenerateThumbCode then
  210. begin
  211. RS_FRAME_POINTER_REG:=RS_R7;
  212. NR_FRAME_POINTER_REG:=NR_R7;
  213. end
  214. else
  215. begin
  216. RS_FRAME_POINTER_REG:=RS_R11;
  217. NR_FRAME_POINTER_REG:=NR_R11;
  218. end;
  219. end;
  220. procedure tarmprocinfo.generate_parameter_info;
  221. begin
  222. procdef.total_stackframe_size:=stackframesize;
  223. inherited generate_parameter_info;
  224. end;
  225. procedure tarmprocinfo.allocate_got_register(list: TAsmList);
  226. begin
  227. { darwin doesn't use a got }
  228. if tf_pic_uses_got in target_info.flags then
  229. got := cg.getaddressregister(list);
  230. end;
  231. begin
  232. cprocinfo:=tarmprocinfo;
  233. end.