cpupi.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. {
  2. Copyright (c) 2002-2006 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. psub,procinfo,aasmbase,aasmdata;
  23. type
  24. tx86_64procinfo = class(tcgprocinfo)
  25. private
  26. scopes: TAsmList;
  27. scopecount: longint;
  28. unwindflags: byte;
  29. public
  30. procedure set_first_temp_offset;override;
  31. procedure generate_parameter_info;override;
  32. function calc_stackframe_size:longint;override;
  33. procedure add_finally_scope(startlabel,endlabel,handler:TAsmSymbol;implicit:Boolean);
  34. procedure add_except_scope(trylabel,exceptlabel,endlabel,filter:TAsmSymbol);
  35. procedure dump_scopes(list:TAsmList);
  36. destructor destroy;override;
  37. end;
  38. implementation
  39. uses
  40. systems,
  41. globtype,
  42. globals,
  43. cutils,
  44. symconst,
  45. aasmtai,
  46. tgobj;
  47. const
  48. SCOPE_FINALLY=0;
  49. SCOPE_CATCHALL=1;
  50. SCOPE_IMPLICIT=2;
  51. procedure tx86_64procinfo.set_first_temp_offset;
  52. begin
  53. if target_info.system=system_x86_64_win64 then
  54. begin
  55. { Fixes the case when there are calls done by low-level means
  56. (cg.a_call_name) but no child callnode }
  57. if (pi_do_call in flags) and
  58. not (po_nostackframe in procdef.procoptions) then
  59. allocate_push_parasize(32);
  60. if not(po_assembler in procdef.procoptions) and
  61. (tg.direction > 0) then
  62. { maxpushedparasize already contains 32 bytes of spilling area }
  63. tg.setfirsttemp(tg.direction*maxpushedparasize);
  64. end
  65. else
  66. tg.setfirsttemp(tg.direction*maxpushedparasize);
  67. end;
  68. procedure tx86_64procinfo.generate_parameter_info;
  69. begin
  70. inherited generate_parameter_info;
  71. if target_info.system=system_x86_64_win64 then
  72. para_stack_size:=0;
  73. end;
  74. function tx86_64procinfo.calc_stackframe_size:longint;
  75. begin
  76. maxpushedparasize:=align(maxpushedparasize,max(current_settings.alignment.localalignmin,16));
  77. { Note 1: when tg.direction>0, tg.lasttemp is already offset by maxpushedparasize
  78. (because tg.setfirsttemp also sets lasttemp)
  79. Note 2: Align to 8 bytes here. The final 16-byte alignment is handled in
  80. tcgx86.g_proc_entry, which considers saved rbp and the misalignment
  81. caused by the call itself. }
  82. if (tg.direction>0) then
  83. result:=Align(tg.lasttemp,8)
  84. else
  85. result:=Align(tg.direction*tg.lasttemp+maxpushedparasize,8);
  86. end;
  87. procedure tx86_64procinfo.add_finally_scope(startlabel,endlabel,handler:TAsmSymbol;implicit:Boolean);
  88. begin
  89. unwindflags:=unwindflags or 2;
  90. if implicit then { also needs catch functionality }
  91. unwindflags:=unwindflags or 1;
  92. inc(scopecount);
  93. if scopes=nil then
  94. scopes:=TAsmList.Create;
  95. if implicit then
  96. scopes.concat(tai_const.create_32bit(SCOPE_IMPLICIT))
  97. else
  98. scopes.concat(tai_const.create_32bit(SCOPE_FINALLY));
  99. scopes.concat(tai_const.create_rva_sym(startlabel));
  100. scopes.concat(tai_const.create_rva_sym(endlabel));
  101. scopes.concat(tai_const.create_rva_sym(handler));
  102. end;
  103. procedure tx86_64procinfo.add_except_scope(trylabel,exceptlabel,endlabel,filter:TAsmSymbol);
  104. begin
  105. unwindflags:=unwindflags or 3;
  106. inc(scopecount);
  107. if scopes=nil then
  108. scopes:=TAsmList.Create;
  109. if Assigned(filter) then
  110. scopes.concat(tai_const.create_rva_sym(filter))
  111. else
  112. scopes.concat(tai_const.create_32bit(SCOPE_CATCHALL));
  113. scopes.concat(tai_const.create_rva_sym(trylabel));
  114. scopes.concat(tai_const.create_rva_sym(exceptlabel));
  115. scopes.concat(tai_const.create_rva_sym(endlabel));
  116. end;
  117. procedure tx86_64procinfo.dump_scopes(list: TAsmList);
  118. var
  119. hdir: tai_seh_directive;
  120. begin
  121. if (scopecount=0) then
  122. exit;
  123. hdir:=cai_seh_directive.create_name(ash_handler,'__FPC_specific_handler');
  124. hdir.data.flags:=unwindflags;
  125. list.concat(hdir);
  126. list.concat(cai_seh_directive.create(ash_handlerdata));
  127. list.concat(tai_const.create_32bit(scopecount));
  128. list.concatlist(scopes);
  129. { return to text, required for GAS compatibility }
  130. { This creates a tai_align which is redundant here (although harmless) }
  131. new_section(list,sec_code,lower(procdef.mangledname),0);
  132. end;
  133. destructor tx86_64procinfo.destroy;
  134. begin
  135. scopes.free;
  136. inherited destroy;
  137. end;
  138. begin
  139. cprocinfo:=tx86_64procinfo;
  140. end.