n386obj.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 fpcdefs.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. cpubase,
  30. cga,tgobj,rgobj,cgobj;
  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. var
  88. href : treference;
  89. begin
  90. { sub $ioffset,offset(%esp) }
  91. reference_reset_base(href,R_ESP,getselfoffsetfromsp(procdef));
  92. emit_const_ref(A_SUB,S_L,ioffset,href);
  93. end;
  94. procedure getselftoeax(offs: longint);
  95. var
  96. href : treference;
  97. begin
  98. { mov offset(%esp),%eax }
  99. reference_reset_base(href,R_ESP,getselfoffsetfromsp(procdef));
  100. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,R_EAX);
  101. end;
  102. procedure loadvmttoeax;
  103. var
  104. href : treference;
  105. begin
  106. checkvirtual;
  107. { mov 0(%eax),%eax ; load vmt}
  108. reference_reset_base(href,R_EAX,0);
  109. emit_ref_reg(A_MOV,S_L,href,R_EAX);
  110. end;
  111. procedure op_oneaxmethodaddr(op: TAsmOp);
  112. var
  113. href : treference;
  114. begin
  115. { call/jmp vmtoffs(%eax) ; method offs }
  116. reference_reset_base(href,R_EAX,procdef._class.vmtmethodoffset(procdef.extnumber));
  117. emit_ref(op,S_L,href);
  118. end;
  119. procedure loadmethodoffstoeax;
  120. var
  121. href : treference;
  122. begin
  123. { mov vmtoffs(%eax),%eax ; method offs }
  124. reference_reset_base(href,R_EAX,procdef._class.vmtmethodoffset(procdef.extnumber));
  125. emit_ref_reg(A_MOV,S_L,href,R_EAX);
  126. end;
  127. var
  128. oldexprasmlist: TAAsmoutput;
  129. lab : tasmsymbol;
  130. make_global : boolean;
  131. href : treference;
  132. begin
  133. if procdef.proctypeoption<>potype_none then
  134. Internalerror(200006137);
  135. if not assigned(procdef._class) or
  136. (procdef.procoptions*[po_containsself, po_classmethod, po_staticmethod,
  137. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  138. Internalerror(200006138);
  139. if procdef.owner.symtabletype<>objectsymtable then
  140. Internalerror(200109191);
  141. oldexprasmlist:=exprasmlist;
  142. exprasmlist:=asmlist;
  143. make_global:=false;
  144. if (not current_module.is_unit) or
  145. (cs_create_smart in aktmoduleswitches) or
  146. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  147. make_global:=true;
  148. if make_global then
  149. exprasmList.concat(Tai_symbol.Createname_global(labelname,0))
  150. else
  151. exprasmList.concat(Tai_symbol.Createname(labelname,0));
  152. { set param1 interface to self }
  153. adjustselfvalue(ioffset);
  154. { case 1 or 2 }
  155. if (po_clearstack in procdef.procoptions) then
  156. begin
  157. if po_virtualmethod in procdef.procoptions then
  158. begin { case 2 }
  159. getselftoeax(0);
  160. loadvmttoeax;
  161. op_oneaxmethodaddr(A_CALL);
  162. end
  163. else { case 1 }
  164. cg.a_call_name(exprasmlist,procdef.mangledname);
  165. { restore param1 value self to interface }
  166. adjustselfvalue(-ioffset);
  167. end
  168. { case 3 }
  169. else if [po_virtualmethod,po_saveregisters]*procdef.procoptions=[po_virtualmethod,po_saveregisters] then
  170. begin
  171. emit_reg(A_PUSH,S_L,R_EBX); { allocate space for address}
  172. emit_reg(A_PUSH,S_L,R_EAX);
  173. getselftoeax(8);
  174. loadvmttoeax;
  175. loadmethodoffstoeax;
  176. { mov %eax,4(%esp) }
  177. reference_reset_base(href,R_ESP,4);
  178. emit_reg_ref(A_MOV,S_L,R_EAX,href);
  179. { pop %eax }
  180. emit_reg(A_POP,S_L,R_EAX);
  181. { ret ; jump to the address }
  182. emit_none(A_RET,S_L);
  183. end
  184. { case 4 }
  185. else if po_virtualmethod in procdef.procoptions then
  186. begin
  187. getselftoeax(0);
  188. loadvmttoeax;
  189. op_oneaxmethodaddr(A_JMP);
  190. end
  191. { case 0 }
  192. else
  193. begin
  194. lab:=newasmsymbol(procdef.mangledname);
  195. emit_sym(A_JMP,S_NO,lab);
  196. end;
  197. exprasmlist:=oldexprasmlist;
  198. end;
  199. initialization
  200. cclassheader:=ti386classheader;
  201. end.
  202. {
  203. $Log$
  204. Revision 1.9 2002-05-16 19:46:52 carl
  205. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  206. + try to fix temp allocation (still in ifdef)
  207. + generic constructor calls
  208. + start of tassembler / tmodulebase class cleanup
  209. Revision 1.7 2002/05/12 16:53:17 peter
  210. * moved entry and exitcode to ncgutil and cgobj
  211. * foreach gets extra argument for passing local data to the
  212. iterator function
  213. * -CR checks also class typecasts at runtime by changing them
  214. into as
  215. * fixed compiler to cycle with the -CR option
  216. * fixed stabs with elf writer, finally the global variables can
  217. be watched
  218. * removed a lot of routines from cga unit and replaced them by
  219. calls to cgobj
  220. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  221. u32bit then the other is typecasted also to u32bit without giving
  222. a rangecheck warning/error.
  223. * fixed pascal calling method with reversing also the high tree in
  224. the parast, detected by tcalcst3 test
  225. Revision 1.6 2002/04/02 17:11:36 peter
  226. * tlocation,treference update
  227. * LOC_CONSTANT added for better constant handling
  228. * secondadd splitted in multiple routines
  229. * location_force_reg added for loading a location to a register
  230. of a specified size
  231. * secondassignment parses now first the right and then the left node
  232. (this is compatible with Kylix). This saves a lot of push/pop especially
  233. with string operations
  234. * adapted some routines to use the new cg methods
  235. Revision 1.5 2002/03/31 20:26:39 jonas
  236. + a_loadfpu_* and a_loadmm_* methods in tcg
  237. * register allocation is now handled by a class and is mostly processor
  238. independent (+rgobj.pas and i386/rgcpu.pas)
  239. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  240. * some small improvements and fixes to the optimizer
  241. * some register allocation fixes
  242. * some fpuvaroffset fixes in the unary minus node
  243. * push/popusedregisters is now called rg.save/restoreusedregisters and
  244. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  245. also better optimizable)
  246. * fixed and optimized register saving/restoring for new/dispose nodes
  247. * LOC_FPU locations now also require their "register" field to be set to
  248. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  249. - list field removed of the tnode class because it's not used currently
  250. and can cause hard-to-find bugs
  251. Revision 1.4 2001/10/25 21:22:41 peter
  252. * calling convention rewrite
  253. Revision 1.3 2001/09/19 11:04:41 michael
  254. * Smartlinking with interfaces fixed
  255. * Better smartlinking for rtti and init tables
  256. Revision 1.2 2001/08/26 13:37:00 florian
  257. * some cg reorganisation
  258. * some PPC updates
  259. Revision 1.1 2001/04/21 13:37:17 peter
  260. * made tclassheader using class of to implement cpu dependent code
  261. Revision 1.5 2001/04/13 01:22:19 peter
  262. * symtable change to classes
  263. * range check generation and errors fixed, make cycle DEBUG=1 works
  264. * memory leaks fixed
  265. Revision 1.4 2000/12/25 00:07:33 peter
  266. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  267. tlinkedlist objects)
  268. Revision 1.3 2000/11/29 00:30:47 florian
  269. * unused units removed from uses clause
  270. * some changes for widestrings
  271. Revision 1.2 2000/11/12 23:24:15 florian
  272. * interfaces are basically running
  273. Revision 1.1 2000/11/04 14:25:23 florian
  274. + merged Attila's changes for interfaces, not tested yet
  275. Revision 1.1.2.2 2000/06/15 15:05:30 kaz
  276. * An minor bug fix
  277. Revision 1.1.2.1 2000/06/15 06:26:34 kaz
  278. * Initial version
  279. }