n386obj.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 n386obj;
  19. {$i defines.inc }
  20. interface
  21. implementation
  22. uses
  23. systems,
  24. verbose,globals,
  25. aasm,
  26. symconst,symbase,symtype,symtable,symdef,symsym,
  27. nobj,
  28. temp_gen,
  29. cpubase,
  30. cgai386, tgcpu;
  31. type
  32. ti386classheader=class(tclassheader)
  33. protected
  34. procedure cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  35. end;
  36. {
  37. possible calling conventions:
  38. default stdcall cdecl pascal popstack register saveregisters
  39. default(0): OK OK OK(1) OK OK(1) OK OK
  40. virtual(2): OK OK OK(3) OK OK(3) OK OK(4)
  41. (0):
  42. set self parameter to correct value
  43. jmp mangledname
  44. (1): The code is the following
  45. set self parameter to correct value
  46. call mangledname
  47. set self parameter to interface value
  48. (2): The wrapper code use %eax to reach the virtual method address
  49. set self to correct value
  50. move self,%eax
  51. mov 0(%eax),%eax ; load vmt
  52. jmp vmtoffs(%eax) ; method offs
  53. (3): The wrapper code use %eax to reach the virtual method address
  54. set self to correct value
  55. move self,%eax
  56. mov 0(%eax),%eax ; load vmt
  57. jmp vmtoffs(%eax) ; method offs
  58. set self parameter to interface value
  59. (4): Virtual use eax to reach the method address so the following code be generated:
  60. set self to correct value
  61. push %ebx ; allocate space for function address
  62. push %eax
  63. mov self,%eax
  64. mov 0(%eax),%eax ; load vmt
  65. mov vmtoffs(%eax),eax ; method offs
  66. mov %eax,4(%esp)
  67. pop %eax
  68. ret 0; jmp the address
  69. }
  70. function getselfoffsetfromsp(procdef: tprocdef): longint;
  71. begin
  72. if not assigned(procdef.parast.symindex.first) then
  73. getselfoffsetfromsp:=4
  74. else
  75. if tsym(procdef.parast.symindex.first).typ=varsym then
  76. getselfoffsetfromsp:=tvarsym(procdef.parast.symindex.first).address+4
  77. else
  78. Internalerror(2000061310);
  79. end;
  80. procedure ti386classheader.cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);
  81. procedure checkvirtual;
  82. begin
  83. if (procdef.extnumber=-1) then
  84. Internalerror(200006139);
  85. end;
  86. procedure adjustselfvalue(ioffset: longint);
  87. begin
  88. { sub $ioffset,offset(%esp) }
  89. emit_const_ref(A_SUB,S_L,ioffset,new_reference(R_ESP,getselfoffsetfromsp(procdef)));
  90. end;
  91. procedure getselftoeax(offs: longint);
  92. begin
  93. { mov offset(%esp),%eax }
  94. emit_ref_reg(A_MOV,S_L,new_reference(R_ESP,getselfoffsetfromsp(procdef)),R_EAX);
  95. end;
  96. procedure loadvmttoeax;
  97. begin
  98. checkvirtual;
  99. { mov 0(%eax),%eax ; load vmt}
  100. emit_ref_reg(A_MOV,S_L,new_reference(R_EAX,0),R_EAX);
  101. end;
  102. procedure op_oneaxmethodaddr(op: TAsmOp);
  103. begin
  104. { call/jmp vmtoffs(%eax) ; method offs }
  105. emit_ref(op,S_L,new_reference(R_EAX,procdef._class.vmtmethodoffset(procdef.extnumber)));
  106. end;
  107. procedure loadmethodoffstoeax;
  108. begin
  109. { mov vmtoffs(%eax),%eax ; method offs }
  110. emit_ref_reg(A_MOV,S_L,new_reference(R_EAX,procdef._class.vmtmethodoffset(procdef.extnumber)),R_EAX);
  111. end;
  112. var
  113. oldexprasmlist: TAAsmoutput;
  114. lab : tasmsymbol;
  115. begin
  116. if procdef.proctypeoption<>potype_none then
  117. Internalerror(200006137);
  118. if not assigned(procdef._class) or
  119. (procdef.procoptions*[po_containsself, po_classmethod, po_staticmethod,
  120. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  121. Internalerror(200006138);
  122. oldexprasmlist:=exprasmlist;
  123. exprasmlist:=asmlist;
  124. exprasmList.concat(Tai_symbol.Createname(labelname,0));
  125. { set param1 interface to self }
  126. adjustselfvalue(ioffset);
  127. { case 1 or 2 }
  128. if (pocall_clearstack in procdef.proccalloptions) then
  129. begin
  130. if po_virtualmethod in procdef.procoptions then
  131. begin { case 2 }
  132. getselftoeax(0);
  133. loadvmttoeax;
  134. op_oneaxmethodaddr(A_CALL);
  135. end
  136. else { case 1 }
  137. begin
  138. emitcall(procdef.mangledname);
  139. end;
  140. { restore param1 value self to interface }
  141. adjustselfvalue(-ioffset);
  142. end
  143. { case 3 }
  144. else if [po_virtualmethod,po_saveregisters]*procdef.procoptions=[po_virtualmethod,po_saveregisters] then
  145. begin
  146. emit_reg(A_PUSH,S_L,R_EBX); { allocate space for address}
  147. emit_reg(A_PUSH,S_L,R_EAX);
  148. getselftoeax(8);
  149. loadvmttoeax;
  150. loadmethodoffstoeax;
  151. { mov %eax,4(%esp) }
  152. emit_reg_ref(A_MOV,S_L,R_EAX,new_reference(R_ESP,4));
  153. { pop %eax }
  154. emit_reg(A_POP,S_L,R_EAX);
  155. { ret ; jump to the address }
  156. emit_none(A_RET,S_L);
  157. end
  158. { case 4 }
  159. else if po_virtualmethod in procdef.procoptions then
  160. begin
  161. getselftoeax(0);
  162. loadvmttoeax;
  163. op_oneaxmethodaddr(A_JMP);
  164. end
  165. { case 0 }
  166. else
  167. begin
  168. lab:=newasmsymbol(procdef.mangledname);
  169. emit_sym(A_JMP,S_NO,lab);
  170. end;
  171. exprasmlist:=oldexprasmlist;
  172. end;
  173. initialization
  174. cclassheader:=ti386classheader;
  175. end.
  176. {
  177. $Log$
  178. Revision 1.1 2001-04-21 13:37:17 peter
  179. * made tclassheader using class of to implement cpu dependent code
  180. Revision 1.5 2001/04/13 01:22:19 peter
  181. * symtable change to classes
  182. * range check generation and errors fixed, make cycle DEBUG=1 works
  183. * memory leaks fixed
  184. Revision 1.4 2000/12/25 00:07:33 peter
  185. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  186. tlinkedlist objects)
  187. Revision 1.3 2000/11/29 00:30:47 florian
  188. * unused units removed from uses clause
  189. * some changes for widestrings
  190. Revision 1.2 2000/11/12 23:24:15 florian
  191. * interfaces are basically running
  192. Revision 1.1 2000/11/04 14:25:23 florian
  193. + merged Attila's changes for interfaces, not tested yet
  194. Revision 1.1.2.2 2000/06/15 15:05:30 kaz
  195. * An minor bug fix
  196. Revision 1.1.2.1 2000/06/15 06:26:34 kaz
  197. * Initial version
  198. }