n8086cal.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. node,
  23. parabase,
  24. nx86cal,cgutils;
  25. type
  26. ti8086callnode = class(tx86callnode)
  27. protected
  28. procedure pop_parasize(pop_size:longint);override;
  29. procedure extra_interrupt_code;override;
  30. procedure extra_call_ref_code(var ref: treference);override;
  31. function do_call_ref(ref: treference): tcgpara;override;
  32. function can_call_ref(var ref: treference): boolean; override;
  33. public
  34. function pass_1: tnode; override;
  35. end;
  36. implementation
  37. uses
  38. globtype,systems,
  39. cutils,verbose,globals,
  40. htypechk,pass_1,
  41. cgbase,
  42. cpubase,paramgr,
  43. aasmtai,aasmdata,aasmcpu,
  44. ncal,nbas,nmem,nld,ncnv,
  45. hlcgobj,
  46. symcpu,
  47. cga,cgobj,cgx86,cpuinfo;
  48. {*****************************************************************************
  49. TI8086CALLNODE
  50. *****************************************************************************}
  51. procedure ti8086callnode.extra_interrupt_code;
  52. begin
  53. emit_none(A_PUSHF,S_W);
  54. if current_settings.x86memorymodel in x86_near_code_models then
  55. emit_reg(A_PUSH,S_W,NR_CS);
  56. end;
  57. procedure ti8086callnode.pop_parasize(pop_size:longint);
  58. var
  59. hreg : tregister;
  60. begin
  61. if (paramanager.use_fixed_stack) then
  62. begin
  63. { very weird: in this case the callee does a "ret $4" and the }
  64. { caller immediately a "subl $4,%esp". Possibly this is for }
  65. { use_fixed_stack code to be able to transparently call }
  66. { old-style code (JM) }
  67. dec(pop_size,pushedparasize);
  68. if (pop_size < 0) then
  69. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_SUB,S_W,-pop_size,NR_SP));
  70. exit;
  71. end;
  72. { better than an add on all processors }
  73. if pop_size=2 then
  74. begin
  75. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  76. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_POP,S_W,hreg));
  77. end
  78. { the pentium has two pipes and pop reg is pairable }
  79. { but the registers must be different! }
  80. else
  81. if pop_size<>0 then
  82. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_ADD,S_W,pop_size,NR_SP));
  83. end;
  84. function ti8086callnode.do_call_ref(ref: treference): tcgpara;
  85. begin
  86. if is_proc_far(procdefinition) then
  87. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_CALL,S_FAR,ref))
  88. else
  89. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  90. result:=hlcg.get_call_result_cgpara(procdefinition,typedef)
  91. end;
  92. procedure ti8086callnode.extra_call_ref_code(var ref: treference);
  93. begin
  94. { Preload ref base and index to BX and SI to help the register allocator }
  95. if getsupreg(ref.base)>=first_int_imreg then
  96. begin
  97. if procdefinition.proccalloption=pocall_register then
  98. begin
  99. { BX can't be used as ref base in case of the register calling convention }
  100. cg.getcpuregister(current_asmdata.CurrAsmList,NR_SI);
  101. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,ref.base,NR_SI);
  102. if ref.index<>NR_NO then
  103. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_16,ref.index,NR_SI);
  104. ref.base:=NR_NO;
  105. ref.index:=NR_SI;
  106. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_SI);
  107. end
  108. else
  109. begin
  110. cg.getcpuregister(current_asmdata.CurrAsmList,NR_BX);
  111. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,ref.base,NR_BX);
  112. ref.base:=NR_BX;
  113. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_BX);
  114. end;
  115. end;
  116. if getsupreg(ref.index)>=first_int_imreg then
  117. begin
  118. cg.getcpuregister(current_asmdata.CurrAsmList,NR_SI);
  119. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,ref.index,NR_SI);
  120. ref.index:=NR_SI;
  121. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_SI);
  122. end;
  123. end;
  124. function ti8086callnode.can_call_ref(var ref: treference): boolean;
  125. begin
  126. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,ref);
  127. result:=true;
  128. end;
  129. function ti8086callnode.pass_1: tnode;
  130. begin
  131. { If a far procvar is called, it must be in a memory location.
  132. There is no CALL reg1:reg2 instruction. }
  133. if (right<>nil) then
  134. if is_proc_far(procdefinition) then
  135. begin
  136. make_not_regable(right,[]);
  137. firstpass(right);
  138. if not (right.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  139. begin
  140. { Use a temp if the procvar still not a reference }
  141. load_in_temp(right);
  142. make_not_regable(right,[]);
  143. end;
  144. end;
  145. Result:=inherited pass_1;
  146. end;
  147. begin
  148. ccallnode:=ti8086callnode;
  149. end.