n386cal.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. emit_none(A_PUSHF,S_L);
  77. emit_reg(A_PUSH,S_L,NR_CS);
  78. end;
  79. procedure ti386callnode.pop_parasize(pop_size:longint);
  80. var
  81. hreg : tregister;
  82. href : treference;
  83. begin
  84. if paramanager.use_fixed_stack then
  85. begin
  86. { very weird: in this case the callee does a "ret $4" and the }
  87. { caller immediately a "subl $4,%esp". Possibly this is for }
  88. { use_fixed_stack code to be able to transparently call }
  89. { old-style code (JM) }
  90. dec(pop_size,pushedparasize);
  91. if pop_size<0 then
  92. begin
  93. reference_reset_base(href,NR_STACK_POINTER_REG,pop_size,ctempposinvalid,0,[]);
  94. { it is better to use lea here }
  95. current_asmdata.CurrAsmList.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_ESP));
  96. end;
  97. exit;
  98. end;
  99. { on win32, the caller is responsible for removing the funcret }
  100. { pointer from the stack, unlike on Linux. Don't know about }
  101. { elsewhere (except Darwin, handled above), but since the default }
  102. { was "callee removes funcret pointer from stack" until now, we'll }
  103. { keep that default for everyone else (ncgcal decreases popsize by }
  104. { sizeof(aint) in case of ret_in_param()) }
  105. { This is only correct if the hidden funcret parameter
  106. is not passed as a register.
  107. As it is inserted in parast after all other hidden parameters,
  108. it is always the first parameter (apart from hidden parentfp,
  109. but this one is never put into a register (vs_nonregable set)
  110. so funcret is always in EAX for register calling }
  111. if ((target_info.system = system_i386_win32) and
  112. not (target_info.abi=abi_old_win32_gnu)) and
  113. paramanager.ret_in_param(procdefinition.returndef,procdefinition) and
  114. not ((procdefinition.proccalloption=pocall_register) or
  115. ((procdefinition.proccalloption=pocall_internproc) and
  116. (pocall_default=pocall_register))) then
  117. inc(pop_size,sizeof(aint));
  118. { better than an add on all processors }
  119. if pop_size=4 then
  120. begin
  121. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  122. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  123. end
  124. { the pentium has two pipes and pop reg is pairable }
  125. { but the registers must be different! }
  126. else
  127. if (pop_size=8) and
  128. not(cs_opt_size in current_settings.optimizerswitches) and
  129. (current_settings.optimizecputype=cpu_Pentium) then
  130. begin
  131. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  132. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  133. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  134. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_L,hreg));
  135. end
  136. else
  137. if pop_size<>0 then
  138. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_ADD,S_L,pop_size,NR_ESP));
  139. end;
  140. begin
  141. ccallnode:=ti386callnode;
  142. end.