nppcobj.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Kovacs Attila Zoltan
  4. Generate powerpc 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. cgutils,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 loadvmttor11;
  72. var
  73. href : treference;
  74. begin
  75. reference_reset_base(href,NR_R3,0);
  76. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,NR_R11);
  77. end;
  78. procedure op_onr11methodaddr;
  79. var
  80. href : treference;
  81. begin
  82. if (procdef.extnumber=$ffff) then
  83. Internalerror(200006139);
  84. { call/jmp vmtoffs(%eax) ; method offs }
  85. reference_reset_base(href,NR_R11,procdef._class.vmtmethodoffset(procdef.extnumber));
  86. if not((longint(href.offset) >= low(smallint)) and
  87. (longint(href.offset) <= high(smallint))) then
  88. begin
  89. asmlist.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  90. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  91. href.offset := smallint(href.offset and $ffff);
  92. end;
  93. asmlist.concat(taicpu.op_reg_ref(A_LWZ,NR_R11,href));
  94. asmlist.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  95. asmlist.concat(taicpu.op_none(A_BCTR));
  96. end;
  97. var
  98. oldexprasmlist: TAAsmoutput;
  99. lab : tasmsymbol;
  100. make_global : boolean;
  101. href : treference;
  102. begin
  103. if procdef.proctypeoption<>potype_none then
  104. Internalerror(200006137);
  105. if not assigned(procdef._class) or
  106. (procdef.procoptions*[po_classmethod, po_staticmethod,
  107. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  108. Internalerror(200006138);
  109. if procdef.owner.symtabletype<>objectsymtable then
  110. Internalerror(200109191);
  111. oldexprasmlist:=exprasmlist;
  112. exprasmlist:=asmlist;
  113. make_global:=false;
  114. if (not current_module.is_unit) or
  115. (cs_create_smart in aktmoduleswitches) or
  116. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  117. make_global:=true;
  118. if make_global then
  119. exprasmList.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  120. else
  121. exprasmList.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  122. { set param1 interface to self }
  123. adjustselfvalue(procdef,ioffset);
  124. { case 4 }
  125. if po_virtualmethod in procdef.procoptions then
  126. begin
  127. loadvmttor11;
  128. op_onr11methodaddr;
  129. end
  130. { case 0 }
  131. else
  132. asmlist.concat(taicpu.op_sym(A_B,objectlibrary.newasmsymbol(procdef.mangledname,AB_EXTERNAL,AT_FUNCTION)));
  133. exprasmList.concat(Tai_symbol_end.Createname(labelname));
  134. exprasmlist:=oldexprasmlist;
  135. end;
  136. initialization
  137. cclassheader:=tppcclassheader;
  138. end.
  139. {
  140. $Log$
  141. Revision 1.7 2004-06-20 08:55:32 florian
  142. * logs truncated
  143. Revision 1.6 2004/03/02 00:36:33 olle
  144. * big transformation of Tai_[const_]Symbol.Create[data]name*
  145. Revision 1.5 2004/02/27 13:42:56 olle
  146. + added Tai_symbol_end
  147. Revision 1.4 2004/02/27 10:21:05 florian
  148. * top_symbol killed
  149. + refaddr to treference added
  150. + refsymbol to treference added
  151. * top_local stuff moved to an extra record to save memory
  152. + aint introduced
  153. * tppufile.get/putint64/aint implemented
  154. }