n386ic.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Kovacs Attila Zoltan
  4. Generate i386 assembly wrapper code interface implementor objects
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n386ic;
  19. interface
  20. uses
  21. aasm,
  22. symbase,symtype,symtable,symdef,symsym;
  23. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: pprocdef; const labelname: string; ioffset: longint);
  24. implementation
  25. uses
  26. systems,
  27. verbose, globals,
  28. symconst,
  29. temp_gen,
  30. cpubase,
  31. cgai386, tgcpu;
  32. {
  33. possible calling conventions:
  34. default stdcall cdecl pascal popstack register saveregisters
  35. default(0): OK OK OK(1) OK OK(1) OK OK
  36. virtual(2): OK OK OK(3) OK OK(3) OK OK(4)
  37. (0):
  38. set self parameter to correct value
  39. jmp mangledname
  40. (1): The code is the following
  41. set self parameter to correct value
  42. call mangledname
  43. set self parameter to interface value
  44. (2): The wrapper code use %eax to reach the virtual method address
  45. set self to correct value
  46. move self,%eax
  47. mov 0(%eax),%eax ; load vmt
  48. jmp vmtoffs(%eax) ; method offs
  49. (3): The wrapper code use %eax to reach the virtual method address
  50. set self to correct value
  51. move self,%eax
  52. mov 0(%eax),%eax ; load vmt
  53. jmp vmtoffs(%eax) ; method offs
  54. set self parameter to interface value
  55. (4): Virtual use eax to reach the method address so the following code be generated:
  56. set self to correct value
  57. push %ebx ; allocate space for function address
  58. push %eax
  59. mov self,%eax
  60. mov 0(%eax),%eax ; load vmt
  61. mov vmtoffs(%eax),eax ; method offs
  62. mov %eax,4(%esp)
  63. pop %eax
  64. ret 0; jmp the address
  65. }
  66. function getselfoffsetfromsp(procdef: pprocdef): longint;
  67. begin
  68. if not assigned(procdef^.parast^.symindex^.first) then
  69. getselfoffsetfromsp:=4
  70. else
  71. if psym(procdef^.parast^.symindex^.first)^.typ=varsym then
  72. getselfoffsetfromsp:=pvarsym(procdef^.parast^.symindex^.first)^.address+4
  73. else
  74. Internalerror(2000061310);
  75. end;
  76. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: pprocdef; const labelname: string; ioffset: longint);
  77. procedure checkvirtual;
  78. begin
  79. if (procdef^.extnumber=-1) then
  80. Internalerror(200006139);
  81. end;
  82. procedure adjustselfvalue(ioffset: longint);
  83. begin
  84. { sub $ioffset,offset(%esp) }
  85. emit_const_ref(A_SUB,S_L,ioffset,new_reference(R_ESP,getselfoffsetfromsp(procdef)));
  86. end;
  87. procedure getselftoeax(offs: longint);
  88. begin
  89. { mov offset(%esp),%eax }
  90. emit_ref_reg(A_MOV,S_L,new_reference(R_ESP,getselfoffsetfromsp(procdef)),R_EAX);
  91. end;
  92. procedure loadvmttoeax;
  93. begin
  94. checkvirtual;
  95. { mov 0(%eax),%eax ; load vmt}
  96. emit_ref_reg(A_MOV,S_L,new_reference(R_EAX,0),R_EAX);
  97. end;
  98. procedure op_oneaxmethodaddr(op: TAsmOp);
  99. begin
  100. { call/jmp vmtoffs(%eax) ; method offs }
  101. emit_ref(op,S_L,new_reference(R_EAX,procdef^._class^.vmtmethodoffset(procdef^.extnumber)));
  102. end;
  103. procedure loadmethodoffstoeax;
  104. begin
  105. { mov vmtoffs(%eax),%eax ; method offs }
  106. emit_ref_reg(A_MOV,S_L,new_reference(R_EAX,procdef^._class^.vmtmethodoffset(procdef^.extnumber)),R_EAX);
  107. end;
  108. var
  109. oldexprasmlist: TAAsmoutput;
  110. lab : pasmsymbol;
  111. begin
  112. if procdef^.proctypeoption<>potype_none then
  113. Internalerror(200006137);
  114. if not assigned(procdef^._class) or
  115. (procdef^.procoptions*[po_containsself, po_classmethod, po_staticmethod,
  116. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  117. Internalerror(200006138);
  118. oldexprasmlist:=exprasmlist;
  119. exprasmlist:=asmlist;
  120. exprasmList.concat(Tai_symbol.Createname(labelname,0));
  121. { set param1 interface to self }
  122. adjustselfvalue(ioffset);
  123. { case 1 or 2 }
  124. if (pocall_clearstack in procdef^.proccalloptions) then
  125. begin
  126. if po_virtualmethod in procdef^.procoptions then
  127. begin { case 2 }
  128. getselftoeax(0);
  129. loadvmttoeax;
  130. op_oneaxmethodaddr(A_CALL);
  131. end
  132. else { case 1 }
  133. begin
  134. emitcall(procdef^.mangledname);
  135. end;
  136. { restore param1 value self to interface }
  137. adjustselfvalue(-ioffset);
  138. end
  139. { case 3 }
  140. else if [po_virtualmethod,po_saveregisters]*procdef^.procoptions=[po_virtualmethod,po_saveregisters] then
  141. begin
  142. emit_reg(A_PUSH,S_L,R_EBX); { allocate space for address}
  143. emit_reg(A_PUSH,S_L,R_EAX);
  144. getselftoeax(8);
  145. loadvmttoeax;
  146. loadmethodoffstoeax;
  147. { mov %eax,4(%esp) }
  148. emit_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,4));
  149. { pop %eax }
  150. emit_reg(A_POP,S_L,R_EAX);
  151. { ret ; jump to the address }
  152. emit_none(A_RET,S_L);
  153. end
  154. { case 4 }
  155. else if po_virtualmethod in procdef^.procoptions then
  156. begin
  157. getselftoeax(0);
  158. loadvmttoeax;
  159. op_oneaxmethodaddr(A_JMP);
  160. end
  161. { case 0 }
  162. else
  163. begin
  164. lab:=newasmsymbol(procdef^.mangledname);
  165. emit_sym(A_JMP,S_NO,lab);
  166. end;
  167. exprasmlist:=oldexprasmlist;
  168. end;
  169. end.
  170. {
  171. $Log$
  172. Revision 1.4 2000-12-25 00:07:33 peter
  173. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  174. tlinkedlist objects)
  175. Revision 1.3 2000/11/29 00:30:47 florian
  176. * unused units removed from uses clause
  177. * some changes for widestrings
  178. Revision 1.2 2000/11/12 23:24:15 florian
  179. * interfaces are basically running
  180. Revision 1.1 2000/11/04 14:25:23 florian
  181. + merged Attila's changes for interfaces, not tested yet
  182. Revision 1.1.2.2 2000/06/15 15:05:30 kaz
  183. * An minor bug fix
  184. Revision 1.1.2.1 2000/06/15 06:26:34 kaz
  185. * Initial version
  186. }