narmobj.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Kovacs Attila Zoltan
  4. Generate arm 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 narmobj;
  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. tarmclassheader=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 tarmclassheader.cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);
  71. procedure loadvmttor12;
  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_R12);
  77. end;
  78. procedure op_onr12methodaddr;
  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_R12,procdef._class.vmtmethodoffset(procdef.extnumber));
  86. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,NR_R12);
  87. { using a_call here causes an internalerror because pi_do_call
  88. isn't set properly }
  89. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  90. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  91. end;
  92. var
  93. oldexprasmlist: TAAsmoutput;
  94. lab : tasmsymbol;
  95. make_global : boolean;
  96. href : treference;
  97. begin
  98. if procdef.proctypeoption<>potype_none then
  99. Internalerror(200006137);
  100. if not assigned(procdef._class) or
  101. (procdef.procoptions*[po_classmethod, po_staticmethod,
  102. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  103. Internalerror(200006138);
  104. if procdef.owner.symtabletype<>objectsymtable then
  105. Internalerror(200109191);
  106. oldexprasmlist:=exprasmlist;
  107. exprasmlist:=asmlist;
  108. make_global:=false;
  109. if (not current_module.is_unit) or
  110. (cs_create_smart in aktmoduleswitches) or
  111. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  112. make_global:=true;
  113. if make_global then
  114. exprasmList.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  115. else
  116. exprasmList.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  117. { set param1 interface to self }
  118. adjustselfvalue(procdef,ioffset);
  119. { case 4 }
  120. if po_virtualmethod in procdef.procoptions then
  121. begin
  122. loadvmttor12;
  123. op_onr12methodaddr;
  124. end
  125. { case 0 }
  126. else
  127. asmlist.concat(taicpu.op_sym(A_B,objectlibrary.newasmsymbol(procdef.mangledname,AB_EXTERNAL,AT_FUNCTION)));
  128. exprasmList.concat(Tai_symbol_end.Createname(labelname));
  129. exprasmlist:=oldexprasmlist;
  130. end;
  131. initialization
  132. cclassheader:=tarmclassheader;
  133. end.
  134. {
  135. $Log$
  136. Revision 1.1 2004-03-21 22:40:15 florian
  137. + added interface support for the arm
  138. * added FPC_REQUIRES_PROPER_ALIGNMENT define for targets which require proper alignment
  139. }