cpupi.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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;
  24. type
  25. tarmprocinfo = class(tcgprocinfo)
  26. { for arm thumb, we need to know the stackframe size before
  27. starting procedure compilation, so this contains the stack frame size, the compiler
  28. should assume
  29. if this size is too little the procedure must be compiled again with a larger value }
  30. stackframesize,
  31. floatregstart : aint;
  32. // procedure handle_body_start;override;
  33. // procedure after_pass1;override;
  34. procedure set_first_temp_offset;override;
  35. function calc_stackframe_size:longint;override;
  36. procedure init_framepointer; override;
  37. procedure generate_parameter_info;override;
  38. end;
  39. implementation
  40. uses
  41. globals,systems,
  42. cpubase,
  43. tgobj,
  44. symconst,symtype,symsym,paramgr,
  45. cgbase,cgutils,
  46. cgobj,
  47. defutil;
  48. procedure tarmprocinfo.set_first_temp_offset;
  49. var
  50. localsize : aint;
  51. i : longint;
  52. begin
  53. { We allocate enough space to save all registers because we can't determine
  54. the necessary space because the used registers aren't known before
  55. secondpass is run. Even worse, patching
  56. the local offsets after generating the code could cause trouble because
  57. "shifter" constants could change to non-"shifter" constants. This
  58. is especially a problem when taking the address of a local. For now,
  59. this extra memory should hurt less than generating all local contants with offsets
  60. >256 as non shifter constants }
  61. if (po_nostackframe in procdef.procoptions) then
  62. begin
  63. { maxpushedparasize sghould be zero,
  64. if not we will get an error later. }
  65. tg.setfirsttemp(maxpushedparasize);
  66. exit;
  67. end;
  68. if tg.direction = -1 then
  69. begin
  70. if (target_info.system<>system_arm_darwin) then
  71. { Non-Darwin, worst case: r4-r10,r11,r13,r14,r15 is saved -> -28-16, but we
  72. always adjust the frame pointer to point to the first stored
  73. register (= last register in list above) -> + 4 }
  74. tg.setfirsttemp(-28-16)
  75. else
  76. { on Darwin first r4-r7,r14 are saved, then r7 is adjusted to
  77. point to the saved r7, and next r8,r10,r11 gets saved -> -24
  78. (r4-r6 and r8,r10,r11) }
  79. tg.setfirsttemp(-24)
  80. end
  81. else
  82. tg.setfirsttemp(maxpushedparasize);
  83. { estimate stack frame size }
  84. if current_settings.cputype in cpu_thumb then
  85. begin
  86. stackframesize:=maxpushedparasize+32;
  87. localsize:=0;
  88. for i:=0 to procdef.localst.SymList.Count-1 do
  89. if tsym(procdef.localst.SymList[i]).typ=localvarsym then
  90. inc(localsize,tabstractnormalvarsym(procdef.localst.SymList[i]).getsize);
  91. inc(stackframesize,localsize);
  92. localsize:=0;
  93. for i:=0 to procdef.parast.SymList.Count-1 do
  94. if tsym(procdef.parast.SymList[i]).typ=paravarsym then
  95. if is_open_string(tabstractnormalvarsym(procdef.parast.SymList[i]).vardef) then
  96. inc(localsize,256)
  97. else
  98. inc(localsize,tabstractnormalvarsym(procdef.parast.SymList[i]).getsize);
  99. inc(stackframesize,localsize);
  100. if pi_needs_implicit_finally in flags then
  101. inc(stackframesize,40);
  102. if pi_uses_exceptions in flags then
  103. inc(stackframesize,40);
  104. if procdef.proctypeoption in [potype_constructor] then
  105. inc(stackframesize,40*2);
  106. inc(stackframesize,estimatedtempsize);
  107. stackframesize:=Align(stackframesize,8);
  108. end;
  109. end;
  110. function tarmprocinfo.calc_stackframe_size:longint;
  111. var
  112. firstfloatreg,lastfloatreg,
  113. r : byte;
  114. floatsavesize : aword;
  115. regs: tcpuregisterset;
  116. begin
  117. if current_settings.cputype in cpu_thumb then
  118. result:=stackframesize
  119. else
  120. begin
  121. maxpushedparasize:=align(maxpushedparasize,max(current_settings.alignment.localalignmin,4));
  122. floatsavesize:=0;
  123. case current_settings.fputype of
  124. fpu_fpa,
  125. fpu_fpa10,
  126. fpu_fpa11:
  127. begin
  128. { save floating point registers? }
  129. firstfloatreg:=RS_NO;
  130. regs:=cg.rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  131. for r:=RS_F0 to RS_F7 do
  132. if r in regs then
  133. begin
  134. if firstfloatreg=RS_NO then
  135. firstfloatreg:=r;
  136. lastfloatreg:=r;
  137. end;
  138. if firstfloatreg<>RS_NO then
  139. floatsavesize:=(lastfloatreg-firstfloatreg+1)*12;
  140. end;
  141. fpu_vfpv2,
  142. fpu_vfpv3,
  143. fpu_vfpv3_d16:
  144. begin
  145. floatsavesize:=0;
  146. regs:=cg.rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  147. for r:=RS_D0 to RS_D31 do
  148. if r in regs then
  149. inc(floatsavesize,8);
  150. end;
  151. fpu_fpv4_s16:
  152. begin
  153. floatsavesize:=0;
  154. regs:=cg.rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  155. for r:=RS_D0 to RS_D15 do
  156. if r in regs then
  157. inc(floatsavesize,8);
  158. end;
  159. end;
  160. floatsavesize:=align(floatsavesize,max(current_settings.alignment.localalignmin,4));
  161. result:=Align(tg.direction*tg.lasttemp,max(current_settings.alignment.localalignmin,4))+maxpushedparasize+aint(floatsavesize);
  162. floatregstart:=tg.direction*result+maxpushedparasize;
  163. if tg.direction=1 then
  164. dec(floatregstart,floatsavesize);
  165. end;
  166. end;
  167. procedure tarmprocinfo.init_framepointer;
  168. begin
  169. if (target_info.system in systems_darwin) or (current_settings.cputype in cpu_thumb) then
  170. begin
  171. RS_FRAME_POINTER_REG:=RS_R7;
  172. NR_FRAME_POINTER_REG:=NR_R7;
  173. end
  174. else
  175. begin
  176. RS_FRAME_POINTER_REG:=RS_R11;
  177. NR_FRAME_POINTER_REG:=NR_R11;
  178. end;
  179. end;
  180. procedure tarmprocinfo.generate_parameter_info;
  181. begin
  182. procdef.total_stackframe_size:=stackframesize;
  183. inherited generate_parameter_info;
  184. end;
  185. begin
  186. cprocinfo:=tarmprocinfo;
  187. end.