n386obj.pas 7.3 KB

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