cpupi.pas 6.1 KB

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