cpupi.pas 11 KB

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