cpupi.pas 6.1 KB

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