n386cal.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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,cgx86,
  43. cpuinfo;
  44. {*****************************************************************************
  45. TI386CALLNODE
  46. *****************************************************************************}
  47. procedure ti386callnode.do_syscall;
  48. var
  49. tmpref: treference;
  50. begin
  51. case target_info.system of
  52. system_i386_aros:
  53. begin
  54. if ([po_syscall_baselast, po_syscall_basereg] * tprocdef(procdefinition).procoptions) <> [] then
  55. begin
  56. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('AROS SysCall')));
  57. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  58. get_syscall_call_ref(tmpref,NR_EAX);
  59. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_CALL,S_NO,tmpref));
  60. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  61. exit;
  62. end;
  63. internalerror(2016090104);
  64. end;
  65. else
  66. internalerror(2014081801);
  67. end;
  68. end;
  69. procedure ti386callnode.gen_syscall_para(para: tcallparanode);
  70. begin
  71. { lib parameter has no special type but proccalloptions must be a syscall }
  72. para.left:=cloadnode.create(tcpuprocdef(procdefinition).libsym,tcpuprocdef(procdefinition).libsym.owner);
  73. end;
  74. procedure ti386callnode.extra_interrupt_code;
  75. begin
  76. if not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  77. begin
  78. emit_none(A_PUSHF,S_L);
  79. emit_reg(A_PUSH,S_L,NR_CS);
  80. end;
  81. end;
  82. procedure ti386callnode.pop_parasize(pop_size:longint);
  83. var
  84. hreg : tregister;
  85. href : treference;
  86. begin
  87. if paramanager.use_fixed_stack then
  88. begin
  89. { very weird: in this case the callee does a "ret $4" and the }
  90. { caller immediately a "subl $4,%esp". Possibly this is for }
  91. { use_fixed_stack code to be able to transparently call }
  92. { old-style code (JM) }
  93. dec(pop_size,pushedparasize);
  94. if pop_size<0 then
  95. begin
  96. reference_reset_base(href,NR_STACK_POINTER_REG,pop_size,ctempposinvalid,0,[]);
  97. { it is better to use lea here }
  98. current_asmdata.CurrAsmList.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_ESP));
  99. end;
  100. exit;
  101. end;
  102. { on win32, the caller is responsible for removing the funcret }
  103. { pointer from the stack, unlike on Linux. Don't know about }
  104. { elsewhere (except Darwin, handled above), but since the default }
  105. { was "callee removes funcret pointer from stack" until now, we'll }
  106. { keep that default for everyone else (ncgcal decreases popsize by }
  107. { sizeof(aint) in case of ret_in_param()) }
  108. { This is only correct if the hidden funcret parameter
  109. is not passed as a register.
  110. As it is inserted in parast after all other hidden parameters,
  111. it is always the first parameter (apart from hidden parentfp,
  112. but this one is never put into a register (vs_nonregable set)
  113. so funcret is always in EAX for register calling }
  114. if ((target_info.system = system_i386_win32) and
  115. not (target_info.abi=abi_old_win32_gnu)) and
  116. paramanager.ret_in_param(procdefinition.returndef,procdefinition) and
  117. not ((procdefinition.proccalloption=pocall_register) or
  118. ((procdefinition.proccalloption=pocall_internproc) and
  119. (pocall_default=pocall_register))) then
  120. inc(pop_size,sizeof(aint));
  121. { better than an add on all processors }
  122. if pop_size=4 then
  123. begin
  124. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  125. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  126. end
  127. { the pentium has two pipes and pop reg is pairable }
  128. { but the registers must be different! }
  129. else
  130. if (pop_size=8) and
  131. not(cs_opt_size in current_settings.optimizerswitches) and
  132. (current_settings.optimizecputype=cpu_Pentium) then
  133. begin
  134. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  135. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  136. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  137. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  138. end
  139. else
  140. if pop_size<>0 then
  141. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_ADD,S_L,pop_size,NR_ESP));
  142. end;
  143. begin
  144. ccallnode:=ti386callnode;
  145. end.