nppcobj.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 nppcobj;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. systems,
  24. verbose,globals,globtype,
  25. aasmbase,aasmtai,aasmcpu,
  26. symconst,symdef,
  27. fmodule,
  28. nobj,
  29. cpuinfo,cpubase,
  30. cgobj;
  31. type
  32. tppcclassheader=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 register saveregisters
  39. default(0): OK OK OK(1) OK OK OK
  40. virtual(2): OK OK OK(3) OK 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. procedure tppcclassheader.cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);
  71. procedure loadvmttor0;
  72. var
  73. href : treference;
  74. begin
  75. reference_reset_base(href,NR_R0,0);
  76. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,NR_R0);
  77. end;
  78. procedure op_onr0methodaddr;
  79. var
  80. href : treference;
  81. begin
  82. if (procdef.extnumber=-1) then
  83. Internalerror(200006139);
  84. { call/jmp vmtoffs(%eax) ; method offs }
  85. reference_reset_base(href,NR_R0,procdef._class.vmtmethodoffset(procdef.extnumber));
  86. {$warning FIX ME}
  87. //!!!! cg.a_jmp_ref(asmlist,href);
  88. end;
  89. var
  90. oldexprasmlist: TAAsmoutput;
  91. lab : tasmsymbol;
  92. make_global : boolean;
  93. href : treference;
  94. begin
  95. if procdef.proctypeoption<>potype_none then
  96. Internalerror(200006137);
  97. if not assigned(procdef._class) or
  98. (procdef.procoptions*[po_classmethod, po_staticmethod,
  99. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  100. Internalerror(200006138);
  101. if procdef.owner.symtabletype<>objectsymtable then
  102. Internalerror(200109191);
  103. oldexprasmlist:=exprasmlist;
  104. exprasmlist:=asmlist;
  105. make_global:=false;
  106. if (not current_module.is_unit) or
  107. (cs_create_smart in aktmoduleswitches) or
  108. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  109. make_global:=true;
  110. if make_global then
  111. exprasmList.concat(Tai_symbol.Createname_global(labelname,0))
  112. else
  113. exprasmList.concat(Tai_symbol.Createname(labelname,0));
  114. { set param1 interface to self }
  115. adjustselfvalue(procdef,ioffset);
  116. { case 4 }
  117. if po_virtualmethod in procdef.procoptions then
  118. begin
  119. loadvmttor0;
  120. op_onr0methodaddr;
  121. end
  122. { case 0 }
  123. else
  124. asmlist.concat(taicpu.op_sym(A_B,objectlibrary.newasmsymbol(procdef.mangledname)));
  125. exprasmlist:=oldexprasmlist;
  126. end;
  127. initialization
  128. cclassheader:=tppcclassheader;
  129. end.
  130. {
  131. $Log$
  132. Revision 1.1 2003-12-10 01:10:47 florian
  133. + initial interface support added
  134. }