cpupi.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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) then
  58. allocate_push_parasize(32);
  59. if not(po_assembler in procdef.procoptions) and
  60. (tg.direction > 0) then
  61. { maxpushedparasize already contains 32 bytes of spilling area }
  62. tg.setfirsttemp(tg.direction*maxpushedparasize);
  63. end
  64. else
  65. tg.setfirsttemp(tg.direction*maxpushedparasize);
  66. end;
  67. procedure tx86_64procinfo.generate_parameter_info;
  68. begin
  69. inherited generate_parameter_info;
  70. if target_info.system=system_x86_64_win64 then
  71. para_stack_size:=0;
  72. end;
  73. function tx86_64procinfo.calc_stackframe_size:longint;
  74. begin
  75. maxpushedparasize:=align(maxpushedparasize,max(current_settings.alignment.localalignmin,16));
  76. { Note 1: when tg.direction>0, tg.lasttemp is already offset by maxpushedparasize
  77. (because tg.setfirsttemp also sets lasttemp)
  78. Note 2: Align to 8 bytes here. The final 16-byte alignment is handled in
  79. tcgx86.g_proc_entry, which considers saved rbp and the misalignment
  80. caused by the call itself. }
  81. if (tg.direction>0) then
  82. result:=Align(tg.lasttemp,8)
  83. else
  84. result:=Align(tg.direction*tg.lasttemp+maxpushedparasize,8);
  85. end;
  86. procedure tx86_64procinfo.add_finally_scope(startlabel,endlabel,handler:TAsmSymbol;implicit:Boolean);
  87. begin
  88. unwindflags:=unwindflags or 2;
  89. if implicit then { also needs catch functionality }
  90. unwindflags:=unwindflags or 1;
  91. inc(scopecount);
  92. if scopes=nil then
  93. scopes:=TAsmList.Create;
  94. if implicit then
  95. scopes.concat(tai_const.create_32bit(SCOPE_IMPLICIT))
  96. else
  97. scopes.concat(tai_const.create_32bit(SCOPE_FINALLY));
  98. scopes.concat(tai_const.create_rva_sym(startlabel));
  99. scopes.concat(tai_const.create_rva_sym(endlabel));
  100. scopes.concat(tai_const.create_rva_sym(handler));
  101. end;
  102. procedure tx86_64procinfo.add_except_scope(trylabel,exceptlabel,endlabel,filter:TAsmSymbol);
  103. begin
  104. unwindflags:=unwindflags or 3;
  105. inc(scopecount);
  106. if scopes=nil then
  107. scopes:=TAsmList.Create;
  108. if Assigned(filter) then
  109. scopes.concat(tai_const.create_rva_sym(filter))
  110. else
  111. scopes.concat(tai_const.create_32bit(SCOPE_CATCHALL));
  112. scopes.concat(tai_const.create_rva_sym(trylabel));
  113. scopes.concat(tai_const.create_rva_sym(exceptlabel));
  114. scopes.concat(tai_const.create_rva_sym(endlabel));
  115. end;
  116. procedure tx86_64procinfo.dump_scopes(list: TAsmList);
  117. var
  118. hdir: tai_seh_directive;
  119. begin
  120. if (scopecount=0) then
  121. exit;
  122. hdir:=cai_seh_directive.create_name(ash_handler,'__FPC_specific_handler');
  123. hdir.data.flags:=unwindflags;
  124. list.concat(hdir);
  125. list.concat(cai_seh_directive.create(ash_handlerdata));
  126. list.concat(tai_const.create_32bit(scopecount));
  127. list.concatlist(scopes);
  128. { return to text, required for GAS compatibility }
  129. { This creates a tai_align which is redundant here (although harmless) }
  130. new_section(list,sec_code,lower(procdef.mangledname),0);
  131. end;
  132. destructor tx86_64procinfo.destroy;
  133. begin
  134. scopes.free;
  135. inherited destroy;
  136. end;
  137. begin
  138. cprocinfo:=tx86_64procinfo;
  139. end.