n386obj.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 n386obj;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. systems,
  24. verbose,globals,globtype,
  25. aasmbase,aasmtai,
  26. symconst,symdef,
  27. fmodule,
  28. nobj,
  29. cpuinfo,cpubase,
  30. cga,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 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. function getselfoffsetfromsp(procdef: tprocdef): longint;
  71. begin
  72. { framepointer is pushed for nested procs }
  73. if procdef.parast.symtablelevel>normal_function_level then
  74. getselfoffsetfromsp:=2*POINTER_SIZE
  75. else
  76. getselfoffsetfromsp:=POINTER_SIZE;
  77. end;
  78. procedure ti386classheader.cgintfwrapper(asmlist: TAAsmoutput; procdef: tprocdef; const labelname: string; ioffset: longint);
  79. procedure getselftoeax(offs: longint);
  80. var
  81. href : treference;
  82. begin
  83. { mov offset(%esp),%eax }
  84. if (procdef.proccalloption<>pocall_register) then
  85. begin
  86. reference_reset_base(href,NR_ESP,getselfoffsetfromsp(procdef)+offs);
  87. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,NR_EAX);
  88. end;
  89. end;
  90. procedure loadvmttoeax;
  91. var
  92. href : treference;
  93. begin
  94. { mov 0(%eax),%eax ; load vmt}
  95. reference_reset_base(href,NR_EAX,0);
  96. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,NR_EAX);
  97. end;
  98. procedure op_oneaxmethodaddr(op: TAsmOp);
  99. var
  100. href : treference;
  101. begin
  102. if (procdef.extnumber=-1) then
  103. Internalerror(200006139);
  104. { call/jmp vmtoffs(%eax) ; method offs }
  105. reference_reset_base(href,NR_EAX,procdef._class.vmtmethodoffset(procdef.extnumber));
  106. emit_ref(op,S_L,href);
  107. end;
  108. procedure loadmethodoffstoeax;
  109. var
  110. href : treference;
  111. begin
  112. if (procdef.extnumber=-1) then
  113. Internalerror(200006139);
  114. { mov vmtoffs(%eax),%eax ; method offs }
  115. reference_reset_base(href,NR_EAX,procdef._class.vmtmethodoffset(procdef.extnumber));
  116. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,NR_EAX);
  117. end;
  118. var
  119. oldexprasmlist: TAAsmoutput;
  120. lab : tasmsymbol;
  121. make_global : boolean;
  122. href : treference;
  123. begin
  124. if procdef.proctypeoption<>potype_none then
  125. Internalerror(200006137);
  126. if not assigned(procdef._class) or
  127. (procdef.procoptions*[po_classmethod, po_staticmethod,
  128. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  129. Internalerror(200006138);
  130. if procdef.owner.symtabletype<>objectsymtable then
  131. Internalerror(200109191);
  132. oldexprasmlist:=exprasmlist;
  133. exprasmlist:=asmlist;
  134. make_global:=false;
  135. if (not current_module.is_unit) or
  136. (cs_create_smart in aktmoduleswitches) or
  137. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  138. make_global:=true;
  139. if make_global then
  140. exprasmList.concat(Tai_symbol.Createname_global(labelname,0))
  141. else
  142. exprasmList.concat(Tai_symbol.Createname(labelname,0));
  143. { set param1 interface to self }
  144. adjustselfvalue(procdef,ioffset);
  145. { case 1 or 2 }
  146. if (procdef.proccalloption in clearstack_pocalls) then
  147. begin
  148. if po_virtualmethod in procdef.procoptions then
  149. begin { case 2 }
  150. getselftoeax(0);
  151. loadvmttoeax;
  152. op_oneaxmethodaddr(A_CALL);
  153. end
  154. else { case 1 }
  155. cg.a_call_name(exprasmlist,procdef.mangledname);
  156. { restore param1 value self to interface }
  157. adjustselfvalue(procdef,-ioffset);
  158. end
  159. { case 3 }
  160. else if (procdef.proccalloption=pocall_register) or
  161. ([po_virtualmethod,po_saveregisters]*procdef.procoptions=[po_virtualmethod,po_saveregisters]) then
  162. begin
  163. emit_reg(A_PUSH,S_L,NR_EBX); { allocate space for address}
  164. emit_reg(A_PUSH,S_L,NR_EAX);
  165. getselftoeax(8);
  166. loadvmttoeax;
  167. loadmethodoffstoeax;
  168. { mov %eax,4(%esp) }
  169. reference_reset_base(href,NR_ESP,4);
  170. emit_reg_ref(A_MOV,S_L,NR_EAX,href);
  171. { pop %eax }
  172. emit_reg(A_POP,S_L,NR_EAX);
  173. { ret ; jump to the address }
  174. emit_none(A_RET,S_L);
  175. end
  176. { case 4 }
  177. else if po_virtualmethod in procdef.procoptions then
  178. begin
  179. getselftoeax(0);
  180. loadvmttoeax;
  181. op_oneaxmethodaddr(A_JMP);
  182. end
  183. { case 0 }
  184. else
  185. begin
  186. lab:=objectlibrary.newasmsymbol(procdef.mangledname);
  187. emit_sym(A_JMP,S_NO,lab);
  188. end;
  189. exprasmlist:=oldexprasmlist;
  190. end;
  191. initialization
  192. cclassheader:=ti386classheader;
  193. end.
  194. {
  195. $Log$
  196. Revision 1.28 2003-12-17 21:59:59 peter
  197. * register call fix
  198. Revision 1.27 2003/10/10 17:48:14 peter
  199. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  200. * tregisteralloctor renamed to trgobj
  201. * removed rgobj from a lot of units
  202. * moved location_* and reference_* to cgobj
  203. * first things for mmx register allocation
  204. Revision 1.26 2003/10/01 20:34:49 peter
  205. * procinfo unit contains tprocinfo
  206. * cginfo renamed to cgbase
  207. * moved cgmessage to verbose
  208. * fixed ppc and sparc compiles
  209. Revision 1.25 2003/09/25 21:30:11 peter
  210. * parameter fixes
  211. Revision 1.24 2003/09/25 14:59:06 peter
  212. * fix intf wrapper code
  213. Revision 1.23 2003/09/23 17:56:06 peter
  214. * locals and paras are allocated in the code generation
  215. * tvarsym.localloc contains the location of para/local when
  216. generating code for the current procedure
  217. Revision 1.22 2003/09/07 22:09:35 peter
  218. * preparations for different default calling conventions
  219. * various RA fixes
  220. Revision 1.21 2003/09/03 15:55:01 peter
  221. * NEWRA branch merged
  222. Revision 1.20.2.1 2003/08/29 17:29:00 peter
  223. * next batch of updates
  224. Revision 1.20 2003/06/03 21:11:09 peter
  225. * cg.a_load_* get a from and to size specifier
  226. * makeregsize only accepts newregister
  227. * i386 uses generic tcgnotnode,tcgunaryminus
  228. Revision 1.19 2003/05/15 18:58:54 peter
  229. * removed selfpointer_offset, vmtpointer_offset
  230. * tvarsym.adjusted_address
  231. * address in localsymtable is now in the real direction
  232. * removed some obsolete globals
  233. Revision 1.18 2003/04/22 14:33:38 peter
  234. * removed some notes/hints
  235. Revision 1.17 2003/01/13 14:54:34 daniel
  236. * Further work to convert codegenerator register convention;
  237. internalerror bug fixed.
  238. Revision 1.16 2003/01/08 18:43:57 daniel
  239. * Tregister changed into a record
  240. Revision 1.15 2002/08/11 14:32:30 peter
  241. * renamed current_library to objectlibrary
  242. Revision 1.14 2002/08/11 13:24:17 peter
  243. * saving of asmsymbols in ppu supported
  244. * asmsymbollist global is removed and moved into a new class
  245. tasmlibrarydata that will hold the info of a .a file which
  246. corresponds with a single module. Added librarydata to tmodule
  247. to keep the library info stored for the module. In the future the
  248. objectfiles will also be stored to the tasmlibrarydata class
  249. * all getlabel/newasmsymbol and friends are moved to the new class
  250. Revision 1.13 2002/08/09 07:33:04 florian
  251. * a couple of interface related fixes
  252. Revision 1.12 2002/07/16 15:34:21 florian
  253. * exit is now a syssym instead of a keyword
  254. Revision 1.11 2002/07/01 18:46:33 peter
  255. * internal linker
  256. * reorganized aasm layer
  257. Revision 1.10 2002/05/18 13:34:25 peter
  258. * readded missing revisions
  259. Revision 1.9 2002/05/16 19:46:52 carl
  260. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  261. + try to fix temp allocation (still in ifdef)
  262. + generic constructor calls
  263. + start of tassembler / tmodulebase class cleanup
  264. Revision 1.7 2002/05/12 16:53:17 peter
  265. * moved entry and exitcode to ncgutil and cgobj
  266. * foreach gets extra argument for passing local data to the
  267. iterator function
  268. * -CR checks also class typecasts at runtime by changing them
  269. into as
  270. * fixed compiler to cycle with the -CR option
  271. * fixed stabs with elf writer, finally the global variables can
  272. be watched
  273. * removed a lot of routines from cga unit and replaced them by
  274. calls to cgobj
  275. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  276. u32bit then the other is typecasted also to u32bit without giving
  277. a rangecheck warning/error.
  278. * fixed pascal calling method with reversing also the high tree in
  279. the parast, detected by tcalcst3 test
  280. Revision 1.6 2002/04/02 17:11:36 peter
  281. * tlocation,treference update
  282. * LOC_CONSTANT added for better constant handling
  283. * secondadd splitted in multiple routines
  284. * location_force_reg added for loading a location to a register
  285. of a specified size
  286. * secondassignment parses now first the right and then the left node
  287. (this is compatible with Kylix). This saves a lot of push/pop especially
  288. with string operations
  289. * adapted some routines to use the new cg methods
  290. Revision 1.5 2002/03/31 20:26:39 jonas
  291. + a_loadfpu_* and a_loadmm_* methods in tcg
  292. * register allocation is now handled by a class and is mostly processor
  293. independent (+rgobj.pas and i386/rgcpu.pas)
  294. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  295. * some small improvements and fixes to the optimizer
  296. * some register allocation fixes
  297. * some fpuvaroffset fixes in the unary minus node
  298. * push/popusedregisters is now called rg.save/restoreusedregisters and
  299. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  300. also better optimizable)
  301. * fixed and optimized register saving/restoring for new/dispose nodes
  302. * LOC_FPU locations now also require their "register" field to be set to
  303. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  304. - list field removed of the tnode class because it's not used currently
  305. and can cause hard-to-find bugs
  306. }