n8086cal.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i8086 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 n8086cal;
  18. {$i fpcdefs.inc}
  19. interface
  20. { $define AnsiStrRef}
  21. uses
  22. parabase,
  23. nx86cal,cgutils;
  24. type
  25. ti8086callnode = class(tx86callnode)
  26. protected
  27. procedure pop_parasize(pop_size:longint);override;
  28. procedure extra_interrupt_code;override;
  29. procedure extra_call_ref_code(var ref: treference);override;
  30. function do_call_ref(ref: treference): tcgpara;override;
  31. end;
  32. implementation
  33. uses
  34. globtype,systems,
  35. cutils,verbose,globals,
  36. cgbase,
  37. cpubase,paramgr,
  38. aasmtai,aasmdata,aasmcpu,
  39. ncal,nbas,nmem,nld,ncnv,
  40. hlcgobj,
  41. symcpu,
  42. cga,cgobj,cgx86,cpuinfo;
  43. {*****************************************************************************
  44. TI8086CALLNODE
  45. *****************************************************************************}
  46. procedure ti8086callnode.extra_interrupt_code;
  47. begin
  48. emit_none(A_PUSHF,S_W);
  49. if current_settings.x86memorymodel in x86_near_code_models then
  50. emit_reg(A_PUSH,S_W,NR_CS);
  51. end;
  52. procedure ti8086callnode.pop_parasize(pop_size:longint);
  53. var
  54. hreg : tregister;
  55. begin
  56. if (paramanager.use_fixed_stack) then
  57. begin
  58. { very weird: in this case the callee does a "ret $4" and the }
  59. { caller immediately a "subl $4,%esp". Possibly this is for }
  60. { use_fixed_stack code to be able to transparently call }
  61. { old-style code (JM) }
  62. dec(pop_size,pushedparasize);
  63. if (pop_size < 0) then
  64. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_SUB,S_W,-pop_size,NR_SP));
  65. exit;
  66. end;
  67. { better than an add on all processors }
  68. if pop_size=2 then
  69. begin
  70. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  71. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_W,hreg));
  72. end
  73. { the pentium has two pipes and pop reg is pairable }
  74. { but the registers must be different! }
  75. else
  76. if pop_size<>0 then
  77. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_ADD,S_W,pop_size,NR_SP));
  78. end;
  79. procedure ti8086callnode.extra_call_ref_code(var ref: treference);
  80. begin
  81. if (ref.base<>NR_NO) and (ref.base<>NR_BP) then
  82. begin
  83. cg.getcpuregister(current_asmdata.CurrAsmList,NR_BX);
  84. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,ref.base,NR_BX);
  85. ref.base:=NR_BX;
  86. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_BX);
  87. end;
  88. if ref.index<>NR_NO then
  89. begin
  90. cg.getcpuregister(current_asmdata.CurrAsmList,NR_SI);
  91. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,ref.index,NR_SI);
  92. ref.index:=NR_SI;
  93. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_SI);
  94. end;
  95. end;
  96. function ti8086callnode.do_call_ref(ref: treference): tcgpara;
  97. begin
  98. if is_proc_far(procdefinition) then
  99. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_CALL,S_FAR,ref))
  100. else
  101. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  102. result:=hlcg.get_call_result_cgpara(procdefinition,typedef)
  103. end;
  104. begin
  105. ccallnode:=ti8086callnode;
  106. end.