n386cal.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i386 assembler for in call nodes
  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. unit n386cal;
  18. {$i fpcdefs.inc}
  19. interface
  20. { $define AnsiStrRef}
  21. uses
  22. nx86cal,ncal;
  23. type
  24. ti386callnode = class(tx86callnode)
  25. protected
  26. procedure gen_syscall_para(para: tcallparanode); override;
  27. procedure pop_parasize(pop_size:longint);override;
  28. procedure extra_interrupt_code;override;
  29. public
  30. procedure do_syscall;override;
  31. end;
  32. implementation
  33. uses
  34. globtype,systems,
  35. cutils,verbose,globals,
  36. cgbase,cgutils,
  37. cpubase,paramgr,
  38. aasmtai,aasmdata,aasmcpu,
  39. nbas,nmem,nld,ncnv,
  40. parabase,
  41. symdef,symsym,symcpu,symconst,
  42. cga,cgobj,cpuinfo;
  43. {*****************************************************************************
  44. TI386CALLNODE
  45. *****************************************************************************}
  46. procedure ti386callnode.do_syscall;
  47. var
  48. tmpref: treference;
  49. begin
  50. case target_info.system of
  51. system_i386_aros:
  52. begin
  53. if ([po_syscall_baselast, po_syscall_basereg] * tprocdef(procdefinition).procoptions) <> [] then
  54. begin
  55. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('AROS SysCall')));
  56. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  57. get_syscall_call_ref(tmpref,NR_EAX);
  58. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_CALL,S_NO,tmpref));
  59. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  60. exit;
  61. end;
  62. internalerror(2016090104);
  63. end;
  64. else
  65. internalerror(2014081801);
  66. end;
  67. end;
  68. procedure ti386callnode.gen_syscall_para(para: tcallparanode);
  69. begin
  70. { lib parameter has no special type but proccalloptions must be a syscall }
  71. para.left:=cloadnode.create(tcpuprocdef(procdefinition).libsym,tcpuprocdef(procdefinition).libsym.owner);
  72. end;
  73. procedure ti386callnode.extra_interrupt_code;
  74. begin
  75. if not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  76. begin
  77. emit_none(A_PUSHF,S_L);
  78. emit_reg(A_PUSH,S_L,NR_CS);
  79. end;
  80. end;
  81. procedure ti386callnode.pop_parasize(pop_size:longint);
  82. var
  83. hreg : tregister;
  84. begin
  85. if (paramanager.use_fixed_stack) then
  86. begin
  87. { very weird: in this case the callee does a "ret $4" and the }
  88. { caller immediately a "subl $4,%esp". Possibly this is for }
  89. { use_fixed_stack code to be able to transparently call }
  90. { old-style code (JM) }
  91. dec(pop_size,pushedparasize);
  92. if (pop_size < 0) then
  93. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_SUB,S_L,-pop_size,NR_ESP));
  94. exit;
  95. end;
  96. { on win32, the caller is responsible for removing the funcret }
  97. { pointer from the stack, unlike on Linux. Don't know about }
  98. { elsewhere (except Darwin, handled above), but since the default }
  99. { was "callee removes funcret pointer from stack" until now, we'll }
  100. { keep that default for everyone else (ncgcal decreases popsize by }
  101. { sizeof(aint) in case of ret_in_param()) }
  102. { This is only correct if the hidden funcret parameter
  103. is not passed as a register.
  104. As it is inserted in parast after all other hidden parameters,
  105. it is always the first parameter (apart from hidden parentfp,
  106. but this one is never put into a register (vs_nonregable set)
  107. so funcret is always in EAX for register calling }
  108. if ((target_info.system = system_i386_win32) and
  109. not (target_info.abi=abi_old_win32_gnu)) and
  110. paramanager.ret_in_param(procdefinition.returndef,procdefinition) and
  111. not ((procdefinition.proccalloption=pocall_register) or
  112. ((procdefinition.proccalloption=pocall_internproc) and
  113. (pocall_default=pocall_register))) then
  114. inc(pop_size,sizeof(aint));
  115. { better than an add on all processors }
  116. if pop_size=4 then
  117. begin
  118. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  119. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  120. end
  121. { the pentium has two pipes and pop reg is pairable }
  122. { but the registers must be different! }
  123. else
  124. if (pop_size=8) and
  125. not(cs_opt_size in current_settings.optimizerswitches) and
  126. (current_settings.optimizecputype=cpu_Pentium) then
  127. begin
  128. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  129. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  130. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  131. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  132. end
  133. else
  134. if pop_size<>0 then
  135. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_ADD,S_L,pop_size,NR_ESP));
  136. end;
  137. begin
  138. ccallnode:=ti386callnode;
  139. end.