n386cal.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate i386 assembler for in call nodes
  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 n386cal;
  19. {$i fpcdefs.inc}
  20. interface
  21. { $define AnsiStrRef}
  22. uses
  23. ncgcal;
  24. type
  25. ti386callnode = class(tcgcallnode)
  26. protected
  27. function align_parasize:longint;override;
  28. procedure pop_parasize(pop_size:longint);override;
  29. procedure extra_interrupt_code;override;
  30. end;
  31. implementation
  32. uses
  33. globtype,systems,
  34. cutils,verbose,globals,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. cgbase,
  39. cpubase,paramgr,
  40. aasmtai,aasmcpu,
  41. ncal,nbas,nmem,nld,ncnv,
  42. cga,cgobj,cpuinfo;
  43. {*****************************************************************************
  44. TI386CALLNODE
  45. *****************************************************************************}
  46. procedure ti386callnode.extra_interrupt_code;
  47. begin
  48. emit_none(A_PUSHF,S_L);
  49. emit_reg(A_PUSH,S_L,NR_CS);
  50. end;
  51. function ti386callnode.align_parasize:longint;
  52. var
  53. pop_size : longint;
  54. {$ifdef OPTALIGN}
  55. pop_esp : boolean;
  56. push_size : longint;
  57. {$endif OPTALIGN}
  58. i : integer;
  59. begin
  60. pop_size:=0;
  61. { This parasize aligned on 4 ? }
  62. i:=pushedparasize and 3;
  63. if i>0 then
  64. inc(pop_size,4-i);
  65. { insert the opcode and update pushedparasize }
  66. { never push 4 or more !! }
  67. pop_size:=pop_size mod 4;
  68. if pop_size>0 then
  69. begin
  70. inc(pushedparasize,pop_size);
  71. exprasmlist.concat(taicpu.op_const_reg(A_SUB,S_L,pop_size,NR_ESP));
  72. {$ifdef GDB}
  73. if (cs_debuginfo in aktmoduleswitches) and
  74. (exprasmList.first=exprasmList.last) then
  75. exprasmList.concat(Tai_force_line.Create);
  76. {$endif GDB}
  77. end;
  78. {$ifdef OPTALIGN}
  79. if pop_allowed and (cs_align in aktglobalswitches) then
  80. begin
  81. pop_esp:=true;
  82. push_size:=pushedparasize;
  83. { !!!! here we have to take care of return type, self
  84. and nested procedures
  85. }
  86. inc(push_size,12);
  87. emit_reg_reg(A_MOV,S_L,rsp,R_EDI);
  88. if (push_size mod 8)=0 then
  89. emit_const_reg(A_AND,S_L,$fffffff8,rsp)
  90. else
  91. begin
  92. emit_const_reg(A_SUB,S_L,push_size,rsp);
  93. emit_const_reg(A_AND,S_L,$fffffff8,rsp);
  94. emit_const_reg(A_SUB,S_L,push_size,rsp);
  95. end;
  96. r.enum:=R_INTREGISTER;
  97. r.number:=R_EDI;
  98. emit_reg(A_PUSH,S_L,r);
  99. end
  100. else
  101. pop_esp:=false;
  102. {$endif OPTALIGN}
  103. align_parasize:=pop_size;
  104. end;
  105. procedure ti386callnode.pop_parasize(pop_size:longint);
  106. var
  107. hreg : tregister;
  108. begin
  109. { better than an add on all processors }
  110. if pop_size=4 then
  111. begin
  112. hreg:=cg.getintregister(exprasmlist,OS_INT);
  113. exprasmlist.concat(taicpu.op_reg(A_POP,S_L,hreg));
  114. cg.ungetregister(exprasmlist,hreg);
  115. end
  116. { the pentium has two pipes and pop reg is pairable }
  117. { but the registers must be different! }
  118. else
  119. if (pop_size=8) and
  120. not(cs_littlesize in aktglobalswitches) and
  121. (aktoptprocessor=ClassP5) then
  122. begin
  123. hreg:=cg.getintregister(exprasmlist,OS_INT);
  124. exprasmlist.concat(taicpu.op_reg(A_POP,S_L,hreg));
  125. cg.ungetregister(exprasmlist,hreg);
  126. hreg:=cg.getintregister(exprasmlist,OS_INT);
  127. exprasmlist.concat(taicpu.op_reg(A_POP,S_L,hreg));
  128. cg.ungetregister(exprasmlist,hreg);
  129. end
  130. else
  131. if pop_size<>0 then
  132. exprasmlist.concat(taicpu.op_const_reg(A_ADD,S_L,pop_size,NR_ESP));
  133. {$ifdef OPTALIGN}
  134. if pop_esp then
  135. emit_reg(A_POP,S_L,NR_ESP);
  136. {$endif OPTALIGN}
  137. end;
  138. begin
  139. ccallnode:=ti386callnode;
  140. end.
  141. {
  142. $Log$
  143. Revision 1.98 2003-10-10 17:48:14 peter
  144. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  145. * tregisteralloctor renamed to trgobj
  146. * removed rgobj from a lot of units
  147. * moved location_* and reference_* to cgobj
  148. * first things for mmx register allocation
  149. Revision 1.97 2003/10/09 21:31:37 daniel
  150. * Register allocator splitted, ans abstract now
  151. Revision 1.96 2003/10/01 20:34:49 peter
  152. * procinfo unit contains tprocinfo
  153. * cginfo renamed to cgbase
  154. * moved cgmessage to verbose
  155. * fixed ppc and sparc compiles
  156. Revision 1.95 2003/09/23 17:56:06 peter
  157. * locals and paras are allocated in the code generation
  158. * tvarsym.localloc contains the location of para/local when
  159. generating code for the current procedure
  160. Revision 1.94 2003/09/03 15:55:01 peter
  161. * NEWRA branch merged
  162. Revision 1.93.2.1 2003/08/29 17:29:00 peter
  163. * next batch of updates
  164. Revision 1.93 2003/05/30 23:57:08 peter
  165. * more sparc cleanup
  166. * accumulator removed, splitted in function_return_reg (called) and
  167. function_result_reg (caller)
  168. Revision 1.92 2003/05/26 21:17:18 peter
  169. * procinlinenode removed
  170. * aktexit2label removed, fast exit removed
  171. + tcallnode.inlined_pass_2 added
  172. Revision 1.91 2003/05/22 21:32:29 peter
  173. * removed some unit dependencies
  174. Revision 1.90 2003/04/23 14:42:08 daniel
  175. * Further register allocator work. Compiler now smaller with new
  176. allocator than without.
  177. * Somebody forgot to adjust ppu version number
  178. Revision 1.89 2003/04/22 14:33:38 peter
  179. * removed some notes/hints
  180. Revision 1.88 2003/04/22 10:09:35 daniel
  181. + Implemented the actual register allocator
  182. + Scratch registers unavailable when new register allocator used
  183. + maybe_save/maybe_restore unavailable when new register allocator used
  184. Revision 1.87 2003/04/04 15:38:56 peter
  185. * moved generic code from n386cal to ncgcal, i386 now also
  186. uses the generic ncgcal
  187. Revision 1.86 2003/03/30 20:59:07 peter
  188. * fix classmethod from classmethod call
  189. * move BeforeDestruction/AfterConstruction calls to
  190. genentrycode/genexitcode instead of generating them on the fly
  191. after a call to a constructor
  192. Revision 1.85 2003/03/28 19:16:57 peter
  193. * generic constructor working for i386
  194. * remove fixed self register
  195. * esi added as address register for i386
  196. Revision 1.84 2003/03/13 19:52:23 jonas
  197. * and more new register allocator fixes (in the i386 code generator this
  198. time). At least now the ppc cross compiler can compile the linux
  199. system unit again, but I haven't tested it.
  200. Revision 1.83 2003/03/06 11:35:50 daniel
  201. * Fixed internalerror 7843 issue
  202. Revision 1.82 2003/02/19 22:00:15 daniel
  203. * Code generator converted to new register notation
  204. - Horribily outdated todo.txt removed
  205. Revision 1.81 2003/01/30 21:46:57 peter
  206. * self fixes for static methods (merged)
  207. Revision 1.80 2003/01/13 18:37:44 daniel
  208. * Work on register conversion
  209. Revision 1.79 2003/01/08 18:43:57 daniel
  210. * Tregister changed into a record
  211. Revision 1.78 2002/12/15 21:30:12 florian
  212. * tcallnode.paraitem introduced, all references to defcoll removed
  213. Revision 1.77 2002/11/27 20:05:06 peter
  214. * cdecl array of const fixes
  215. Revision 1.76 2002/11/25 17:43:26 peter
  216. * splitted defbase in defutil,symutil,defcmp
  217. * merged isconvertable and is_equal into compare_defs(_ext)
  218. * made operator search faster by walking the list only once
  219. Revision 1.75 2002/11/18 17:32:00 peter
  220. * pass proccalloption to ret_in_xxx and push_xxx functions
  221. Revision 1.74 2002/11/15 01:58:57 peter
  222. * merged changes from 1.0.7 up to 04-11
  223. - -V option for generating bug report tracing
  224. - more tracing for option parsing
  225. - errors for cdecl and high()
  226. - win32 import stabs
  227. - win32 records<=8 are returned in eax:edx (turned off by default)
  228. - heaptrc update
  229. - more info for temp management in .s file with EXTDEBUG
  230. Revision 1.73 2002/10/05 12:43:29 carl
  231. * fixes for Delphi 6 compilation
  232. (warning : Some features do not work under Delphi)
  233. Revision 1.72 2002/09/17 18:54:03 jonas
  234. * a_load_reg_reg() now has two size parameters: source and dest. This
  235. allows some optimizations on architectures that don't encode the
  236. register size in the register name.
  237. Revision 1.71 2002/09/16 19:07:37 peter
  238. * push 0 instead of VMT when calling a constructor from a member
  239. Revision 1.70 2002/09/07 15:25:10 peter
  240. * old logs removed and tabs fixed
  241. Revision 1.69 2002/09/01 18:43:27 peter
  242. * include FUNCTION_RETURN_REG in regs_to_push list
  243. Revision 1.68 2002/09/01 12:13:00 peter
  244. * use a_call_reg
  245. * ungetiftemp for procvar of object temp
  246. Revision 1.67 2002/08/25 19:25:21 peter
  247. * sym.insert_in_data removed
  248. * symtable.insertvardata/insertconstdata added
  249. * removed insert_in_data call from symtable.insert, it needs to be
  250. called separatly. This allows to deref the address calculation
  251. * procedures now calculate the parast addresses after the procedure
  252. directives are parsed. This fixes the cdecl parast problem
  253. * push_addr_param has an extra argument that specifies if cdecl is used
  254. or not
  255. Revision 1.66 2002/08/23 16:14:49 peter
  256. * tempgen cleanup
  257. * tt_noreuse temp type added that will be used in genentrycode
  258. Revision 1.65 2002/08/18 20:06:30 peter
  259. * inlining is now also allowed in interface
  260. * renamed write/load to ppuwrite/ppuload
  261. * tnode storing in ppu
  262. * nld,ncon,nbas are already updated for storing in ppu
  263. Revision 1.64 2002/08/17 09:23:45 florian
  264. * first part of procinfo rewrite
  265. Revision 1.63 2002/08/12 15:08:42 carl
  266. + stab register indexes for powerpc (moved from gdb to cpubase)
  267. + tprocessor enumeration moved to cpuinfo
  268. + linker in target_info is now a class
  269. * many many updates for m68k (will soon start to compile)
  270. - removed some ifdef or correct them for correct cpu
  271. Revision 1.62 2002/08/11 14:32:30 peter
  272. * renamed current_library to objectlibrary
  273. Revision 1.61 2002/08/11 13:24:16 peter
  274. * saving of asmsymbols in ppu supported
  275. * asmsymbollist global is removed and moved into a new class
  276. tasmlibrarydata that will hold the info of a .a file which
  277. corresponds with a single module. Added librarydata to tmodule
  278. to keep the library info stored for the module. In the future the
  279. objectfiles will also be stored to the tasmlibrarydata class
  280. * all getlabel/newasmsymbol and friends are moved to the new class
  281. Revision 1.60 2002/07/20 11:58:01 florian
  282. * types.pas renamed to defbase.pas because D6 contains a types
  283. unit so this would conflicts if D6 programms are compiled
  284. + Willamette/SSE2 instructions to assembler added
  285. Revision 1.59 2002/07/11 14:41:33 florian
  286. * start of the new generic parameter handling
  287. Revision 1.58 2002/07/07 09:52:34 florian
  288. * powerpc target fixed, very simple units can be compiled
  289. * some basic stuff for better callparanode handling, far from being finished
  290. Revision 1.57 2002/07/06 20:27:26 carl
  291. + generic set handling
  292. Revision 1.56 2002/07/01 18:46:31 peter
  293. * internal linker
  294. * reorganized aasm layer
  295. Revision 1.55 2002/07/01 16:23:56 peter
  296. * cg64 patch
  297. * basics for currency
  298. * asnode updates for class and interface (not finished)
  299. Revision 1.54 2002/05/20 13:30:40 carl
  300. * bugfix of hdisponen (base must be set, not index)
  301. * more portability fixes
  302. Revision 1.53 2002/05/18 13:34:23 peter
  303. * readded missing revisions
  304. Revision 1.52 2002/05/16 19:46:51 carl
  305. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  306. + try to fix temp allocation (still in ifdef)
  307. + generic constructor calls
  308. + start of tassembler / tmodulebase class cleanup
  309. Revision 1.50 2002/05/13 19:54:38 peter
  310. * removed n386ld and n386util units
  311. * maybe_save/maybe_restore added instead of the old maybe_push
  312. Revision 1.49 2002/05/12 16:53:17 peter
  313. * moved entry and exitcode to ncgutil and cgobj
  314. * foreach gets extra argument for passing local data to the
  315. iterator function
  316. * -CR checks also class typecasts at runtime by changing them
  317. into as
  318. * fixed compiler to cycle with the -CR option
  319. * fixed stabs with elf writer, finally the global variables can
  320. be watched
  321. * removed a lot of routines from cga unit and replaced them by
  322. calls to cgobj
  323. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  324. u32bit then the other is typecasted also to u32bit without giving
  325. a rangecheck warning/error.
  326. * fixed pascal calling method with reversing also the high tree in
  327. the parast, detected by tcalcst3 test
  328. }