2
0

n386cal.pas 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  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 bymethodpointer
  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. symdef,node,ncal;
  24. type
  25. ti386callparanode = class(tcallparanode)
  26. procedure secondcallparan(defcoll : TParaItem;
  27. push_from_left_to_right,inlined,is_cdecl : boolean;
  28. para_alignment,para_offset : longint);override;
  29. end;
  30. ti386callnode = class(tcallnode)
  31. procedure pass_2;override;
  32. end;
  33. ti386procinlinenode = class(tprocinlinenode)
  34. procedure pass_2;override;
  35. end;
  36. implementation
  37. uses
  38. globtype,systems,
  39. cutils,verbose,globals,
  40. symconst,symbase,symsym,symtable,types,
  41. {$ifdef GDB}
  42. {$ifdef delphi}
  43. sysutils,
  44. {$else}
  45. strings,
  46. {$endif}
  47. gdb,
  48. {$endif GDB}
  49. cginfo,cgbase,pass_2,
  50. cpubase,aasmbase,aasmtai,aasmcpu,
  51. nmem,nld,ncnv,
  52. ncgutil,cga,cgobj,tgobj,regvars,rgobj,rgcpu,cg64f32,cgcpu;
  53. {*****************************************************************************
  54. TI386CALLPARANODE
  55. *****************************************************************************}
  56. procedure ti386callparanode.secondcallparan(defcoll : TParaItem;
  57. push_from_left_to_right,inlined,is_cdecl : boolean;para_alignment,para_offset : longint);
  58. procedure maybe_push_high;
  59. begin
  60. { open array ? }
  61. { defcoll.data can be nil for read/write }
  62. if assigned(defcoll.paratype.def) and
  63. assigned(hightree) then
  64. begin
  65. secondpass(hightree);
  66. { this is a longint anyway ! }
  67. push_value_para(hightree,inlined,false,para_offset,4);
  68. end;
  69. end;
  70. var
  71. otlabel,oflabel : tasmlabel;
  72. { temporary variables: }
  73. tempdeftype : tdeftype;
  74. tmpreg : tregister;
  75. href : treference;
  76. begin
  77. { set default para_alignment to target_info.stackalignment }
  78. if para_alignment=0 then
  79. para_alignment:=aktalignment.paraalign;
  80. { push from left to right if specified }
  81. if push_from_left_to_right and assigned(right) then
  82. begin
  83. if (nf_varargs_para in flags) then
  84. tcallparanode(right).secondcallparan(defcoll,push_from_left_to_right,
  85. inlined,is_cdecl,para_alignment,para_offset)
  86. else
  87. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  88. inlined,is_cdecl,para_alignment,para_offset);
  89. end;
  90. otlabel:=truelabel;
  91. oflabel:=falselabel;
  92. getlabel(truelabel);
  93. getlabel(falselabel);
  94. secondpass(left);
  95. { handle varargs first, because defcoll is not valid }
  96. if (nf_varargs_para in flags) then
  97. begin
  98. if push_addr_param(left.resulttype.def) then
  99. begin
  100. inc(pushedparasize,4);
  101. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  102. location_release(exprasmlist,left.location);
  103. end
  104. else
  105. push_value_para(left,inlined,is_cdecl,para_offset,para_alignment);
  106. end
  107. { filter array constructor with c styled args }
  108. else if is_array_constructor(left.resulttype.def) and (nf_cargs in left.flags) then
  109. begin
  110. { nothing, everything is already pushed }
  111. end
  112. { in codegen.handleread.. defcoll.data is set to nil }
  113. else if assigned(defcoll.paratype.def) and
  114. (defcoll.paratype.def.deftype=formaldef) then
  115. begin
  116. { allow passing of a constant to a const formaldef }
  117. if (defcoll.paratyp=vs_const) and
  118. (left.location.loc=LOC_CONSTANT) then
  119. location_force_mem(exprasmlist,left.location);
  120. { allow @var }
  121. inc(pushedparasize,4);
  122. if (left.nodetype=addrn) and
  123. (not(nf_procvarload in left.flags)) then
  124. begin
  125. if inlined then
  126. begin
  127. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  128. cg.a_load_loc_ref(exprasmlist,left.location,href);
  129. end
  130. else
  131. cg.a_param_loc(exprasmlist,left.location,-1);
  132. location_release(exprasmlist,left.location);
  133. end
  134. else
  135. begin
  136. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  137. CGMessage(type_e_mismatch)
  138. else
  139. begin
  140. if inlined then
  141. begin
  142. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  143. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  144. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  145. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  146. cg.free_scratch_reg(exprasmlist,tmpreg);
  147. end
  148. else
  149. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  150. location_release(exprasmlist,left.location);
  151. end;
  152. end;
  153. end
  154. { handle call by reference parameter }
  155. else if (defcoll.paratyp in [vs_var,vs_out]) then
  156. begin
  157. if (left.location.loc<>LOC_REFERENCE) then
  158. begin
  159. { passing self to a var parameter is allowed in
  160. TP and delphi }
  161. if not((left.location.loc=LOC_CREFERENCE) and
  162. (left.nodetype=selfn)) then
  163. internalerror(200106041);
  164. end;
  165. maybe_push_high;
  166. if (defcoll.paratyp=vs_out) and
  167. assigned(defcoll.paratype.def) and
  168. not is_class(defcoll.paratype.def) and
  169. defcoll.paratype.def.needs_inittable then
  170. cg.g_finalize(exprasmlist,defcoll.paratype.def,left.location.reference,false);
  171. inc(pushedparasize,4);
  172. if inlined then
  173. begin
  174. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  175. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  176. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  177. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  178. cg.free_scratch_reg(exprasmlist,tmpreg);
  179. end
  180. else
  181. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  182. location_release(exprasmlist,left.location);
  183. end
  184. else
  185. begin
  186. tempdeftype:=resulttype.def.deftype;
  187. if tempdeftype=filedef then
  188. CGMessage(cg_e_file_must_call_by_reference);
  189. { open array must always push the address, this is needed to
  190. also push addr of small open arrays and with cdecl functions (PFV) }
  191. if (
  192. assigned(defcoll.paratype.def) and
  193. (is_open_array(defcoll.paratype.def) or
  194. is_array_of_const(defcoll.paratype.def))
  195. ) or
  196. (
  197. push_addr_param(resulttype.def) and
  198. not is_cdecl
  199. ) then
  200. begin
  201. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  202. begin
  203. { allow passing nil to a procvardef (methodpointer) }
  204. if (left.nodetype=typeconvn) and
  205. (left.resulttype.def.deftype=procvardef) and
  206. (ttypeconvnode(left).left.nodetype=niln) then
  207. begin
  208. tg.gettempofsizereference(exprasmlist,tcgsize2size[left.location.size],href);
  209. cg.a_load_loc_ref(exprasmlist,left.location,href);
  210. location_reset(left.location,LOC_REFERENCE,left.location.size);
  211. left.location.reference:=href;
  212. end
  213. else
  214. internalerror(200204011);
  215. end;
  216. maybe_push_high;
  217. inc(pushedparasize,4);
  218. if inlined then
  219. begin
  220. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  221. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  222. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  223. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  224. cg.free_scratch_reg(exprasmlist,tmpreg);
  225. end
  226. else
  227. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  228. location_release(exprasmlist,left.location);
  229. end
  230. else
  231. begin
  232. push_value_para(left,inlined,is_cdecl,
  233. para_offset,para_alignment);
  234. end;
  235. end;
  236. truelabel:=otlabel;
  237. falselabel:=oflabel;
  238. { push from right to left }
  239. if not push_from_left_to_right and assigned(right) then
  240. begin
  241. if (nf_varargs_para in flags) then
  242. tcallparanode(right).secondcallparan(defcoll,push_from_left_to_right,
  243. inlined,is_cdecl,para_alignment,para_offset)
  244. else
  245. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  246. inlined,is_cdecl,para_alignment,para_offset);
  247. end;
  248. end;
  249. {*****************************************************************************
  250. TI386CALLNODE
  251. *****************************************************************************}
  252. procedure ti386callnode.pass_2;
  253. var
  254. regs_to_push : tregisterset;
  255. unusedstate: pointer;
  256. pushed : tpushedsaved;
  257. funcretref,refcountedtemp : treference;
  258. tmpreg : tregister;
  259. hregister : tregister;
  260. oldpushedparasize : longint;
  261. { true if ESI must be loaded again after the subroutine }
  262. loadesi : boolean;
  263. { true if a virtual method must be called directly }
  264. no_virtual_call : boolean;
  265. { true if we produce a con- or destrutor in a call }
  266. is_con_or_destructor : boolean;
  267. { true if a constructor is called again }
  268. extended_new : boolean;
  269. { adress returned from an I/O-error }
  270. iolabel : tasmlabel;
  271. { lexlevel count }
  272. i : longint;
  273. { help reference pointer }
  274. href : treference;
  275. hrefvmt : treference;
  276. hp : tnode;
  277. pp : tbinarynode;
  278. params : tnode;
  279. inlined : boolean;
  280. inlinecode : tprocinlinenode;
  281. store_parast_fixup,
  282. para_alignment,
  283. para_offset : longint;
  284. cgsize : tcgsize;
  285. { instruction for alignement correction }
  286. { corr : paicpu;}
  287. { we must pop this size also after !! }
  288. { must_pop : boolean; }
  289. pop_size : longint;
  290. {$ifdef OPTALIGN}
  291. pop_esp : boolean;
  292. push_size : longint;
  293. {$endif OPTALIGN}
  294. pop_allowed : boolean;
  295. release_tmpreg : boolean;
  296. constructorfailed : tasmlabel;
  297. label
  298. dont_call;
  299. begin
  300. extended_new:=false;
  301. iolabel:=nil;
  302. inlinecode:=nil;
  303. inlined:=false;
  304. loadesi:=true;
  305. no_virtual_call:=false;
  306. rg.saveunusedstate(unusedstate);
  307. { if we allocate the temp. location for ansi- or widestrings }
  308. { already here, we avoid later a push/pop }
  309. if is_widestring(resulttype.def) then
  310. begin
  311. tg.gettempwidestringreference(exprasmlist,refcountedtemp);
  312. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp);
  313. end
  314. else if is_ansistring(resulttype.def) then
  315. begin
  316. tg.gettempansistringreference(exprasmlist,refcountedtemp);
  317. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp);
  318. end;
  319. if (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_stdcall]) then
  320. para_alignment:=4
  321. else
  322. para_alignment:=aktalignment.paraalign;
  323. if not assigned(procdefinition) then
  324. exit;
  325. { Deciding whether we may still need the parameters happens next (JM) }
  326. if assigned(left) then
  327. params:=left.getcopy
  328. else params := nil;
  329. if (procdefinition.proccalloption=pocall_inline) then
  330. begin
  331. inlined:=true;
  332. inlinecode:=tprocinlinenode(right);
  333. right:=nil;
  334. { set it to the same lexical level as the local symtable, becuase
  335. the para's are stored there }
  336. tprocdef(procdefinition).parast.symtablelevel:=aktprocdef.localst.symtablelevel;
  337. if assigned(params) then
  338. inlinecode.para_offset:=tg.gettempofsizepersistant(exprasmlist,inlinecode.para_size);
  339. store_parast_fixup:=tprocdef(procdefinition).parast.address_fixup;
  340. tprocdef(procdefinition).parast.address_fixup:=inlinecode.para_offset;
  341. {$ifdef extdebug}
  342. Comment(V_debug,
  343. 'inlined parasymtable is at offset '
  344. +tostr(tprocdef(procdefinition).parast.address_fixup));
  345. exprasmList.concat(Tai_asm_comment.Create(
  346. strpnew('inlined parasymtable is at offset '
  347. +tostr(tprocdef(procdefinition).parast.address_fixup))));
  348. {$endif extdebug}
  349. end;
  350. { only if no proc var }
  351. if inlined or
  352. not(assigned(right)) then
  353. is_con_or_destructor:=(procdefinition.proctypeoption in [potype_constructor,potype_destructor]);
  354. { proc variables destroy all registers }
  355. if (inlined or
  356. (right=nil)) and
  357. { virtual methods too }
  358. not(po_virtualmethod in procdefinition.procoptions) then
  359. begin
  360. if (cs_check_io in aktlocalswitches) and
  361. (po_iocheck in procdefinition.procoptions) and
  362. not(po_iocheck in aktprocdef.procoptions) then
  363. begin
  364. getaddrlabel(iolabel);
  365. cg.a_label(exprasmlist,iolabel);
  366. end
  367. else
  368. iolabel:=nil;
  369. { save all used registers }
  370. regs_to_push := tprocdef(procdefinition).usedregisters;
  371. rg.saveusedregisters(exprasmlist,pushed,regs_to_push);
  372. { give used registers through }
  373. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedregisters;
  374. end
  375. else
  376. begin
  377. regs_to_push := all_registers;
  378. rg.saveusedregisters(exprasmlist,pushed,regs_to_push);
  379. rg.usedinproc:=all_registers;
  380. { no IO check for methods and procedure variables }
  381. iolabel:=nil;
  382. end;
  383. { generate the code for the parameter and push them }
  384. oldpushedparasize:=pushedparasize;
  385. pushedparasize:=0;
  386. pop_size:=0;
  387. { no inc esp for inlined procedure
  388. and for objects constructors PM }
  389. if inlined or
  390. ((procdefinition.proctypeoption=potype_constructor) and
  391. { quick'n'dirty check if it is a class or an object }
  392. (resulttype.def.deftype=orddef)) then
  393. pop_allowed:=false
  394. else
  395. pop_allowed:=true;
  396. if pop_allowed then
  397. begin
  398. { Old pushedsize aligned on 4 ? }
  399. i:=oldpushedparasize and 3;
  400. if i>0 then
  401. inc(pop_size,4-i);
  402. { This parasize aligned on 4 ? }
  403. i:=procdefinition.para_size(para_alignment) and 3;
  404. if i>0 then
  405. inc(pop_size,4-i);
  406. { insert the opcode and update pushedparasize }
  407. { never push 4 or more !! }
  408. pop_size:=pop_size mod 4;
  409. if pop_size>0 then
  410. begin
  411. inc(pushedparasize,pop_size);
  412. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  413. {$ifdef GDB}
  414. if (cs_debuginfo in aktmoduleswitches) and
  415. (exprasmList.first=exprasmList.last) then
  416. exprasmList.concat(Tai_force_line.Create);
  417. {$endif GDB}
  418. end;
  419. end;
  420. {$ifdef OPTALIGN}
  421. if pop_allowed and (cs_align in aktglobalswitches) then
  422. begin
  423. pop_esp:=true;
  424. push_size:=procdefinition.para_size(para_alignment);
  425. { !!!! here we have to take care of return type, self
  426. and nested procedures
  427. }
  428. inc(push_size,12);
  429. emit_reg_reg(A_MOV,S_L,R_ESP,R_EDI);
  430. if (push_size mod 8)=0 then
  431. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP)
  432. else
  433. begin
  434. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  435. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP);
  436. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  437. end;
  438. emit_reg(A_PUSH,S_L,R_EDI);
  439. end
  440. else
  441. pop_esp:=false;
  442. {$endif OPTALIGN}
  443. { Push parameters }
  444. if assigned(params) then
  445. begin
  446. { be found elsewhere }
  447. if inlined then
  448. para_offset:=tprocdef(procdefinition).parast.address_fixup+
  449. tprocdef(procdefinition).parast.datasize
  450. else
  451. para_offset:=0;
  452. if not(inlined) and
  453. assigned(right) then
  454. tcallparanode(params).secondcallparan(TParaItem(tabstractprocdef(right.resulttype.def).Para.first),
  455. (po_leftright in procdefinition.procoptions),inlined,
  456. (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl]),
  457. para_alignment,para_offset)
  458. else
  459. tcallparanode(params).secondcallparan(TParaItem(procdefinition.Para.first),
  460. (po_leftright in procdefinition.procoptions),inlined,
  461. (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl]),
  462. para_alignment,para_offset);
  463. end;
  464. { Allocate return value for inlined routines }
  465. if inlined then
  466. inlinecode.retoffset:=tg.gettempofsizepersistant(exprasmlist,Align(resulttype.def.size,aktalignment.paraalign));
  467. { Allocate return value when returned in argument }
  468. if ret_in_param(resulttype.def) then
  469. begin
  470. if assigned(funcretrefnode) then
  471. begin
  472. secondpass(funcretrefnode);
  473. if codegenerror then
  474. exit;
  475. if (funcretrefnode.location.loc<>LOC_REFERENCE) then
  476. internalerror(200204246);
  477. funcretref:=funcretrefnode.location.reference;
  478. end
  479. else
  480. begin
  481. if inlined then
  482. begin
  483. reference_reset(funcretref);
  484. funcretref.offset:=tg.gettempofsizepersistant(exprasmlist,resulttype.def.size);
  485. funcretref.base:=procinfo^.framepointer;
  486. {$ifdef extdebug}
  487. Comment(V_debug,'function return value is at offset '
  488. +tostr(funcretref.offset));
  489. exprasmlist.concat(tai_asm_comment.create(
  490. strpnew('function return value is at offset '
  491. +tostr(funcretref.offset))));
  492. {$endif extdebug}
  493. end
  494. else
  495. tg.gettempofsizereference(exprasmlist,resulttype.def.size,funcretref);
  496. end;
  497. { This must not be counted for C code
  498. complex return address is removed from stack
  499. by function itself ! }
  500. {$ifdef OLD_C_STACK}
  501. inc(pushedparasize,4); { lets try without it PM }
  502. {$endif not OLD_C_STACK}
  503. if inlined then
  504. begin
  505. hregister:=cg.get_scratch_reg_address(exprasmlist);
  506. cg.a_loadaddr_ref_reg(exprasmlist,funcretref,hregister);
  507. reference_reset_base(href,procinfo^.framepointer,inlinecode.retoffset);
  508. cg.a_load_reg_ref(exprasmlist,OS_ADDR,hregister,href);
  509. cg.free_scratch_reg(exprasmlist,hregister);
  510. end
  511. else
  512. cg.a_paramaddr_ref(exprasmlist,funcretref,-1);
  513. end;
  514. { procedure variable or normal function call ? }
  515. if inlined or
  516. (right=nil) then
  517. begin
  518. { Normal function call }
  519. { overloaded operator has no symtable }
  520. { push self }
  521. if assigned(symtableproc) and
  522. (symtableproc.symtabletype=withsymtable) then
  523. begin
  524. { dirty trick to avoid the secondcall below }
  525. methodpointer:=ccallparanode.create(nil,nil);
  526. location_reset(methodpointer.location,LOC_REGISTER,OS_ADDR);
  527. rg.getexplicitregisterint(exprasmlist,R_ESI);
  528. methodpointer.location.register:=R_ESI;
  529. { ARGHHH this is wrong !!!
  530. if we can init from base class for a child
  531. class that the wrong VMT will be
  532. transfered to constructor !! }
  533. methodpointer.resulttype:=
  534. twithnode(twithsymtable(symtableproc).withnode).left.resulttype;
  535. { make a reference }
  536. href:=twithnode(twithsymtable(symtableproc).withnode).withreference;
  537. if ((not(nf_islocal in twithnode(twithsymtable(symtableproc).withnode).flags)) and
  538. (not twithsymtable(symtableproc).direct_with)) or
  539. is_class_or_interface(methodpointer.resulttype.def) then
  540. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg)
  541. else
  542. cg.a_loadaddr_ref_reg(exprasmlist,href,self_pointer_reg);
  543. end;
  544. { push self }
  545. if assigned(symtableproc) and
  546. ((symtableproc.symtabletype=objectsymtable) or
  547. (symtableproc.symtabletype=withsymtable)) then
  548. begin
  549. if assigned(methodpointer) then
  550. begin
  551. {
  552. if methodpointer^.resulttype.def=classrefdef then
  553. begin
  554. two possibilities:
  555. 1. constructor
  556. 2. class method
  557. end
  558. else }
  559. begin
  560. case methodpointer.nodetype of
  561. typen:
  562. begin
  563. { direct call to inherited method }
  564. if (po_abstractmethod in procdefinition.procoptions) then
  565. begin
  566. CGMessage(cg_e_cant_call_abstract_method);
  567. goto dont_call;
  568. end;
  569. { generate no virtual call }
  570. no_virtual_call:=true;
  571. if (sp_static in symtableprocentry.symoptions) then
  572. begin
  573. { well lets put the VMT address directly into ESI }
  574. { it is kind of dirty but that is the simplest }
  575. { way to accept virtual static functions (PM) }
  576. loadesi:=true;
  577. { if no VMT just use $0 bug0214 PM }
  578. rg.getexplicitregisterint(exprasmlist,R_ESI);
  579. if not(oo_has_vmt in tobjectdef(methodpointer.resulttype.def).objectoptions) then
  580. cg.a_load_const_reg(exprasmlist,OS_ADDR,0,self_pointer_reg)
  581. else
  582. begin
  583. reference_reset_symbol(href,newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  584. cg.a_loadaddr_ref_reg(exprasmlist,href,self_pointer_reg);
  585. end;
  586. { emit_reg(A_PUSH,S_L,R_ESI);
  587. this is done below !! }
  588. end
  589. else
  590. { this is a member call, so ESI isn't modfied }
  591. loadesi:=false;
  592. { a class destructor needs a flag }
  593. if is_class(tobjectdef(methodpointer.resulttype.def)) and
  594. (procdefinition.proctypeoption=potype_destructor) then
  595. begin
  596. cg.a_param_const(exprasmlist,OS_ADDR,0,2);
  597. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  598. end;
  599. if not(is_con_or_destructor and
  600. is_class(methodpointer.resulttype.def) and
  601. (procdefinition.proctypeoption in [potype_constructor,potype_destructor])
  602. ) then
  603. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  604. { if an inherited con- or destructor should be }
  605. { called in a con- or destructor then a warning }
  606. { will be made }
  607. { con- and destructors need a pointer to the vmt }
  608. if is_con_or_destructor and
  609. is_object(methodpointer.resulttype.def) and
  610. assigned(aktprocdef) then
  611. begin
  612. if not(aktprocdef.proctypeoption in
  613. [potype_constructor,potype_destructor]) then
  614. CGMessage(cg_w_member_cd_call_from_method);
  615. end;
  616. { class destructors get there flag above }
  617. { constructor flags ? }
  618. if is_con_or_destructor and
  619. not(
  620. is_class(methodpointer.resulttype.def) and
  621. assigned(aktprocdef) and
  622. (aktprocdef.proctypeoption=potype_destructor)) then
  623. begin
  624. { a constructor needs also a flag }
  625. if is_class(methodpointer.resulttype.def) then
  626. cg.a_param_const(exprasmlist,OS_ADDR,0,2);
  627. cg.a_param_const(exprasmlist,OS_ADDR,0,1);
  628. end;
  629. end;
  630. hnewn:
  631. begin
  632. { extended syntax of new }
  633. { ESI must be zero }
  634. rg.getexplicitregisterint(exprasmlist,R_ESI);
  635. cg.a_load_const_reg(exprasmlist,OS_ADDR,0,self_pointer_reg);
  636. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,2);
  637. { insert the vmt }
  638. reference_reset_symbol(href,newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  639. cg.a_paramaddr_ref(exprasmlist,href,1);
  640. extended_new:=true;
  641. end;
  642. hdisposen:
  643. begin
  644. secondpass(methodpointer);
  645. { destructor with extended syntax called from dispose }
  646. { hdisposen always deliver LOC_REFERENCE }
  647. rg.getexplicitregisterint(exprasmlist,R_ESI);
  648. emit_ref_reg(A_LEA,S_L,methodpointer.location.reference,R_ESI);
  649. reference_release(exprasmlist,methodpointer.location.reference);
  650. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,2);
  651. reference_reset_symbol(href,newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  652. cg.a_paramaddr_ref(exprasmlist,href,1);
  653. end;
  654. else
  655. begin
  656. { call to an instance member }
  657. if (symtableproc.symtabletype<>withsymtable) then
  658. begin
  659. secondpass(methodpointer);
  660. rg.getexplicitregisterint(exprasmlist,R_ESI);
  661. case methodpointer.location.loc of
  662. LOC_CREGISTER,
  663. LOC_REGISTER:
  664. begin
  665. cg.a_load_reg_reg(exprasmlist,OS_ADDR,methodpointer.location.register,R_ESI);
  666. rg.ungetregisterint(exprasmlist,methodpointer.location.register);
  667. end;
  668. else
  669. begin
  670. if (methodpointer.resulttype.def.deftype=classrefdef) or
  671. is_class_or_interface(methodpointer.resulttype.def) then
  672. cg.a_load_ref_reg(exprasmlist,OS_ADDR,methodpointer.location.reference,R_ESI)
  673. else
  674. cg.a_loadaddr_ref_reg(exprasmlist,methodpointer.location.reference,R_ESI);
  675. reference_release(exprasmlist,methodpointer.location.reference);
  676. end;
  677. end;
  678. end;
  679. { when calling a class method, we have to load ESI with the VMT !
  680. But, not for a class method via self }
  681. if not(po_containsself in procdefinition.procoptions) then
  682. begin
  683. if (po_classmethod in procdefinition.procoptions) and
  684. not(methodpointer.resulttype.def.deftype=classrefdef) then
  685. begin
  686. { class method needs current VMT }
  687. rg.getexplicitregisterint(exprasmlist,R_ESI);
  688. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  689. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg);
  690. end;
  691. { direct call to destructor: remove data }
  692. if (procdefinition.proctypeoption=potype_destructor) and
  693. is_class(methodpointer.resulttype.def) then
  694. cg.a_param_const(exprasmlist,OS_INT,1,1);
  695. { direct call to class constructor, don't allocate memory }
  696. if (procdefinition.proctypeoption=potype_constructor) and
  697. is_class(methodpointer.resulttype.def) then
  698. begin
  699. cg.a_param_const(exprasmlist,OS_INT,0,2);
  700. cg.a_param_const(exprasmlist,OS_INT,0,1);
  701. end
  702. else
  703. begin
  704. { constructor call via classreference => allocate memory }
  705. if (procdefinition.proctypeoption=potype_constructor) and
  706. (methodpointer.resulttype.def.deftype=classrefdef) and
  707. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  708. cg.a_param_const(exprasmlist,OS_INT,1,1);
  709. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  710. end;
  711. end;
  712. if is_con_or_destructor then
  713. begin
  714. { classes don't get a VMT pointer pushed }
  715. if is_object(methodpointer.resulttype.def) then
  716. begin
  717. if (procdefinition.proctypeoption=potype_constructor) then
  718. begin
  719. { it's no bad idea, to insert the VMT }
  720. reference_reset_symbol(href,newasmsymbol(
  721. tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  722. cg.a_paramaddr_ref(exprasmlist,href,1);
  723. end
  724. { destructors haven't to dispose the instance, if this is }
  725. { a direct call }
  726. else
  727. cg.a_param_const(exprasmlist,OS_INT,0,1);
  728. end;
  729. end;
  730. end;
  731. end;
  732. end;
  733. end
  734. else
  735. begin
  736. if (po_classmethod in procdefinition.procoptions) and
  737. not(
  738. assigned(aktprocdef) and
  739. (po_classmethod in aktprocdef.procoptions)
  740. ) then
  741. begin
  742. { class method needs current VMT }
  743. rg.getexplicitregisterint(exprasmlist,R_ESI);
  744. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  745. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,R_ESI);
  746. end
  747. else
  748. begin
  749. { member call, ESI isn't modified }
  750. loadesi:=false;
  751. end;
  752. { direct call to destructor: don't remove data! }
  753. if is_class(procinfo^._class) then
  754. begin
  755. if (procdefinition.proctypeoption=potype_destructor) then
  756. begin
  757. cg.a_param_const(exprasmlist,OS_INT,0,2);
  758. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,1);
  759. end
  760. else if (procdefinition.proctypeoption=potype_constructor) then
  761. begin
  762. cg.a_param_const(exprasmlist,OS_INT,0,2);
  763. cg.a_param_const(exprasmlist,OS_INT,0,1);
  764. end
  765. else
  766. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,1);
  767. end
  768. else if is_object(procinfo^._class) then
  769. begin
  770. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,1);
  771. if is_con_or_destructor then
  772. begin
  773. if (procdefinition.proctypeoption=potype_constructor) then
  774. begin
  775. { it's no bad idea, to insert the VMT }
  776. reference_reset_symbol(href,newasmsymbol(procinfo^._class.vmt_mangledname),0);
  777. cg.a_paramaddr_ref(exprasmlist,href,1);
  778. end
  779. { destructors haven't to dispose the instance, if this is }
  780. { a direct call }
  781. else
  782. cg.a_param_const(exprasmlist,OS_INT,0,1);
  783. end;
  784. end
  785. else
  786. Internalerror(200006165);
  787. end;
  788. end;
  789. { call to BeforeDestruction? }
  790. if (procdefinition.proctypeoption=potype_destructor) and
  791. assigned(methodpointer) and
  792. (methodpointer.nodetype<>typen) and
  793. is_class(tobjectdef(methodpointer.resulttype.def)) and
  794. (inlined or
  795. (right=nil)) then
  796. begin
  797. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  798. reference_reset_base(href,self_pointer_reg,0);
  799. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  800. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  801. reference_reset_base(href,tmpreg,72);
  802. cg.a_call_ref(exprasmlist,href);
  803. cg.free_scratch_reg(exprasmlist,tmpreg);
  804. end;
  805. { push base pointer ?}
  806. { never when inlining, since if necessary, the base pointer }
  807. { can/will be gottten from the current procedure's symtable }
  808. { (JM) }
  809. if not inlined then
  810. if (lexlevel>=normal_function_level) and assigned(tprocdef(procdefinition).parast) and
  811. ((tprocdef(procdefinition).parast.symtablelevel)>normal_function_level) then
  812. begin
  813. { if we call a nested function in a method, we must }
  814. { push also SELF! }
  815. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  816. { access }
  817. {
  818. begin
  819. loadesi:=false;
  820. emit_reg(A_PUSH,S_L,R_ESI);
  821. end;
  822. }
  823. if lexlevel=(tprocdef(procdefinition).parast.symtablelevel) then
  824. begin
  825. reference_reset_base(href,procinfo^.framepointer,procinfo^.framepointer_offset);
  826. cg.a_param_ref(exprasmlist,OS_ADDR,href,-1);
  827. end
  828. { this is only true if the difference is one !!
  829. but it cannot be more !! }
  830. else if (lexlevel=(tprocdef(procdefinition).parast.symtablelevel)-1) then
  831. begin
  832. cg.a_param_reg(exprasmlist,OS_ADDR,procinfo^.framepointer,-1);
  833. end
  834. else if (lexlevel>(tprocdef(procdefinition).parast.symtablelevel)) then
  835. begin
  836. hregister:=rg.getregisterint(exprasmlist);
  837. reference_reset_base(href,procinfo^.framepointer,procinfo^.framepointer_offset);
  838. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  839. for i:=(tprocdef(procdefinition).parast.symtablelevel) to lexlevel-1 do
  840. begin
  841. {we should get the correct frame_pointer_offset at each level
  842. how can we do this !!! }
  843. reference_reset_base(href,hregister,procinfo^.framepointer_offset);
  844. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  845. end;
  846. cg.a_param_reg(exprasmlist,OS_ADDR,hregister,-1);
  847. rg.ungetregisterint(exprasmlist,hregister);
  848. end
  849. else
  850. internalerror(25000);
  851. end;
  852. rg.saveregvars(exprasmlist,regs_to_push);
  853. if (po_virtualmethod in procdefinition.procoptions) and
  854. not(no_virtual_call) then
  855. begin
  856. { static functions contain the vmt_address in ESI }
  857. { also class methods }
  858. { Here it is quite tricky because it also depends }
  859. { on the methodpointer PM }
  860. release_tmpreg:=false;
  861. rg.getexplicitregisterint(exprasmlist,R_ESI);
  862. if assigned(aktprocdef) then
  863. begin
  864. if (((sp_static in aktprocdef.procsym.symoptions) or
  865. (po_classmethod in aktprocdef.procoptions)) and
  866. ((methodpointer=nil) or (methodpointer.nodetype=typen)))
  867. or
  868. (po_staticmethod in procdefinition.procoptions) or
  869. ((procdefinition.proctypeoption=potype_constructor) and
  870. { esi contains the vmt if we call a constructor via a class ref }
  871. assigned(methodpointer) and
  872. (methodpointer.resulttype.def.deftype=classrefdef)
  873. ) or
  874. { is_interface(tprocdef(procdefinition)._class) or }
  875. { ESI is loaded earlier }
  876. (po_classmethod in procdefinition.procoptions) then
  877. begin
  878. reference_reset_base(href,R_ESI,0);
  879. end
  880. else
  881. begin
  882. { this is one point where we need vmt_offset (PM) }
  883. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  884. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  885. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  886. reference_reset_base(href,tmpreg,0);
  887. release_tmpreg:=true;
  888. end;
  889. end
  890. else
  891. { aktprocdef should be assigned, also in main program }
  892. internalerror(12345);
  893. if tprocdef(procdefinition).extnumber=-1 then
  894. internalerror(44584);
  895. href.offset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  896. if not(is_interface(tprocdef(procdefinition)._class)) and
  897. not(is_cppclass(tprocdef(procdefinition)._class)) then
  898. begin
  899. if (cs_check_object in aktlocalswitches) then
  900. begin
  901. reference_reset_symbol(hrefvmt,newasmsymbol(tprocdef(procdefinition)._class.vmt_mangledname),0);
  902. cg.a_paramaddr_ref(exprasmlist,hrefvmt,2);
  903. cg.a_param_reg(exprasmlist,OS_ADDR,href.base,1);
  904. cg.a_call_name(exprasmlist,'FPC_CHECK_OBJECT_EXT');
  905. end
  906. else if (cs_check_range in aktlocalswitches) then
  907. begin
  908. cg.a_param_reg(exprasmlist,OS_ADDR,href.base,1);
  909. cg.a_call_name(exprasmlist,'FPC_CHECK_OBJECT');
  910. end;
  911. end;
  912. cg.a_call_ref(exprasmlist,href);
  913. if release_tmpreg then
  914. cg.free_scratch_reg(exprasmlist,tmpreg);
  915. end
  916. else if not inlined then
  917. begin
  918. { We can call interrupts from within the smae code
  919. by just pushing the flags and CS PM }
  920. if (po_interrupt in procdefinition.procoptions) then
  921. begin
  922. emit_none(A_PUSHF,S_L);
  923. emit_reg(A_PUSH,S_L,R_CS);
  924. end;
  925. cg.a_call_name(exprasmlist,tprocdef(procdefinition).mangledname);
  926. end
  927. else { inlined proc }
  928. { inlined code is in inlinecode }
  929. begin
  930. { process the inlinecode }
  931. secondpass(inlinecode);
  932. { free the args }
  933. if tprocdef(procdefinition).parast.datasize>0 then
  934. tg.ungetpersistanttemp(exprasmlist,tprocdef(procdefinition).parast.address_fixup);
  935. end;
  936. end
  937. else
  938. { now procedure variable case }
  939. begin
  940. secondpass(right);
  941. if (po_interrupt in procdefinition.procoptions) then
  942. begin
  943. emit_none(A_PUSHF,S_L);
  944. emit_reg(A_PUSH,S_L,R_CS);
  945. end;
  946. { procedure of object? }
  947. if (po_methodpointer in procdefinition.procoptions) then
  948. begin
  949. { method pointer can't be in a register }
  950. hregister:=R_NO;
  951. { do some hacking if we call a method pointer }
  952. { which is a class member }
  953. { else ESI is overwritten ! }
  954. if (right.location.reference.base=R_ESI) or
  955. (right.location.reference.index=R_ESI) then
  956. begin
  957. reference_release(exprasmlist,right.location.reference);
  958. hregister:=cg.get_scratch_reg_address(exprasmlist);
  959. cg.a_load_ref_reg(exprasmlist,OS_ADDR,right.location.reference,hregister);
  960. end;
  961. { load self, but not if it's already explicitly pushed }
  962. if not(po_containsself in procdefinition.procoptions) then
  963. begin
  964. { load ESI }
  965. href:=right.location.reference;
  966. inc(href.offset,4);
  967. rg.getexplicitregisterint(exprasmlist,R_ESI);
  968. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg);
  969. { push self pointer }
  970. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,-1);
  971. end;
  972. rg.saveregvars(exprasmlist,ALL_REGISTERS);
  973. if hregister<>R_NO then
  974. reference_reset_base(href,hregister,0)
  975. else
  976. href:=right.location.reference;
  977. cg.a_call_ref(exprasmlist,href);
  978. if hregister<>R_NO then
  979. cg.free_scratch_reg(exprasmlist,hregister);
  980. reference_release(exprasmlist,right.location.reference);
  981. end
  982. else
  983. begin
  984. rg.saveregvars(exprasmlist,ALL_REGISTERS);
  985. case right.location.loc of
  986. LOC_REGISTER,LOC_CREGISTER:
  987. reference_reset_base(href,right.location.register,0);
  988. LOC_REFERENCE,LOC_CREFERENCE :
  989. href:=right.location.reference;
  990. else
  991. internalerror(200203311);
  992. end;
  993. cg.a_call_ref(exprasmlist,href);
  994. location_release(exprasmlist,right.location);
  995. end;
  996. end;
  997. { this was only for normal functions
  998. displaced here so we also get
  999. it to work for procvars PM }
  1000. if (not inlined) and (po_clearstack in procdefinition.procoptions) then
  1001. begin
  1002. { we also add the pop_size which is included in pushedparasize }
  1003. pop_size:=0;
  1004. { better than an add on all processors }
  1005. if pushedparasize=4 then
  1006. begin
  1007. rg.getexplicitregisterint(exprasmlist,R_EDI);
  1008. emit_reg(A_POP,S_L,R_EDI);
  1009. rg.ungetregisterint(exprasmlist,R_EDI);
  1010. end
  1011. { the pentium has two pipes and pop reg is pairable }
  1012. { but the registers must be different! }
  1013. else if (pushedparasize=8) and
  1014. not(cs_littlesize in aktglobalswitches) and
  1015. (aktoptprocessor=ClassP5) and
  1016. (procinfo^._class=nil) then
  1017. begin
  1018. rg.getexplicitregisterint(exprasmlist,R_EDI);
  1019. emit_reg(A_POP,S_L,R_EDI);
  1020. rg.ungetregisterint(exprasmlist,R_EDI);
  1021. exprasmList.concat(tai_regalloc.Alloc(R_ESI));
  1022. emit_reg(A_POP,S_L,R_ESI);
  1023. exprasmList.concat(tai_regalloc.DeAlloc(R_ESI));
  1024. end
  1025. else if pushedparasize<>0 then
  1026. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  1027. end;
  1028. {$ifdef OPTALIGN}
  1029. if pop_esp then
  1030. emit_reg(A_POP,S_L,R_ESP);
  1031. {$endif OPTALIGN}
  1032. dont_call:
  1033. pushedparasize:=oldpushedparasize;
  1034. rg.restoreunusedstate(unusedstate);
  1035. {$ifdef TEMPREGDEBUG}
  1036. testregisters32;
  1037. {$endif TEMPREGDEBUG}
  1038. { a constructor could be a function with boolean result }
  1039. { if calling constructor called fail we
  1040. must jump directly to quickexitlabel PM
  1041. but only if it is a call of an inherited constructor }
  1042. if (inlined or
  1043. (right=nil)) and
  1044. (procdefinition.proctypeoption=potype_constructor) and
  1045. assigned(methodpointer) and
  1046. (methodpointer.nodetype=typen) and
  1047. (aktprocdef.proctypeoption=potype_constructor) then
  1048. begin
  1049. emitjmp(C_Z,faillabel);
  1050. {$ifdef TEST_GENERIC}
  1051. { should be moved to generic version! }
  1052. reference_reset_base(href, procinfo^.framepointer,procinfo^.selfpointer_offset);
  1053. cg.a_load_ref_reg(exprasmlist, OS_ADDR, href, SELF_POINTER_REG);
  1054. {$endif}
  1055. end;
  1056. { call to AfterConstruction? }
  1057. if is_class(resulttype.def) and
  1058. (inlined or
  1059. (right=nil)) and
  1060. (procdefinition.proctypeoption=potype_constructor) and
  1061. assigned(methodpointer) and
  1062. (methodpointer.nodetype<>typen) then
  1063. begin
  1064. getlabel(constructorfailed);
  1065. emitjmp(C_Z,constructorfailed);
  1066. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  1067. reference_reset_base(href,self_pointer_reg,0);
  1068. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  1069. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  1070. reference_reset_base(href,tmpreg,68);
  1071. cg.a_call_ref(exprasmlist,href);
  1072. cg.free_scratch_reg(exprasmlist,tmpreg);
  1073. exprasmList.concat(tai_regalloc.Alloc(accumulator));
  1074. cg.a_label(exprasmlist,constructorfailed);
  1075. cg.a_load_reg_reg(exprasmlist,OS_ADDR,self_pointer_reg,accumulator);
  1076. end;
  1077. { handle function results }
  1078. if (not is_void(resulttype.def)) then
  1079. begin
  1080. { structured results are easy to handle.... }
  1081. { needed also when result_no_used !! }
  1082. if ret_in_param(resulttype.def) then
  1083. begin
  1084. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  1085. location.reference.symbol:=nil;
  1086. location.reference:=funcretref;
  1087. end
  1088. else
  1089. { ansi/widestrings must be registered, so we can dispose them }
  1090. if is_ansistring(resulttype.def) or
  1091. is_widestring(resulttype.def) then
  1092. begin
  1093. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  1094. location.reference:=refcountedtemp;
  1095. cg.a_reg_alloc(exprasmlist,accumulator);
  1096. cg.a_load_reg_ref(exprasmlist,OS_ADDR,accumulator,location.reference);
  1097. cg.a_reg_dealloc(exprasmlist,accumulator);
  1098. end
  1099. else
  1100. { we have only to handle the result if it is used }
  1101. if (nf_return_value_used in flags) then
  1102. begin
  1103. case resulttype.def.deftype of
  1104. enumdef,
  1105. orddef :
  1106. begin
  1107. cgsize:=def_cgsize(resulttype.def);
  1108. { an object constructor is a function with boolean result }
  1109. if (inlined or (right=nil)) and
  1110. (procdefinition.proctypeoption=potype_constructor) then
  1111. begin
  1112. if extended_new then
  1113. cgsize:=OS_INT
  1114. else
  1115. begin
  1116. cgsize:=OS_NO;
  1117. { this fails if popsize > 0 PM }
  1118. location_reset(location,LOC_FLAGS,OS_NO);
  1119. location.resflags:=F_NE;
  1120. end;
  1121. end;
  1122. if cgsize<>OS_NO then
  1123. begin
  1124. location_reset(location,LOC_REGISTER,cgsize);
  1125. cg.a_reg_alloc(exprasmlist,accumulator);
  1126. if cgsize in [OS_64,OS_S64] then
  1127. begin
  1128. cg.a_reg_alloc(exprasmlist,accumulatorhigh);
  1129. if accumulatorhigh in rg.unusedregsint then
  1130. begin
  1131. location.registerhigh:=rg.getexplicitregisterint(exprasmlist,accumulatorhigh);
  1132. location.registerlow:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1133. end
  1134. else
  1135. begin
  1136. location.registerhigh:=rg.getexplicitregisterint(exprasmlist,accumulatorhigh);
  1137. location.registerlow:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1138. end;
  1139. cg64.a_load64_reg_reg(exprasmlist,joinreg64(accumulator,accumulatorhigh),
  1140. location.register64);
  1141. end
  1142. else
  1143. begin
  1144. location.register:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1145. hregister:=rg.makeregsize(accumulator,cgsize);
  1146. location.register:=rg.makeregsize(location.register,cgsize);
  1147. cg.a_load_reg_reg(exprasmlist,cgsize,hregister,location.register);
  1148. end;
  1149. end;
  1150. end;
  1151. floatdef :
  1152. begin
  1153. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  1154. location.register:=R_ST;
  1155. inc(trgcpu(rg).fpuvaroffset);
  1156. end;
  1157. else
  1158. begin
  1159. location_reset(location,LOC_REGISTER,OS_INT);
  1160. location.register:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1161. cg.a_load_reg_reg(exprasmlist,OS_INT,accumulator,location.register);
  1162. end;
  1163. end;
  1164. end;
  1165. end;
  1166. { perhaps i/o check ? }
  1167. if iolabel<>nil then
  1168. begin
  1169. reference_reset_symbol(href,iolabel,0);
  1170. cg.a_paramaddr_ref(exprasmlist,href,1);
  1171. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  1172. end;
  1173. if pop_size>0 then
  1174. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1175. { restore registers }
  1176. rg.restoreusedregisters(exprasmlist,pushed);
  1177. { at last, restore instance pointer (SELF) }
  1178. if loadesi then
  1179. cg.g_maybe_loadself(exprasmlist);
  1180. pp:=tbinarynode(params);
  1181. while assigned(pp) do
  1182. begin
  1183. if assigned(pp.left) then
  1184. begin
  1185. location_freetemp(exprasmlist,pp.left.location);
  1186. { process also all nodes of an array of const }
  1187. if pp.left.nodetype=arrayconstructorn then
  1188. begin
  1189. if assigned(tarrayconstructornode(pp.left).left) then
  1190. begin
  1191. hp:=pp.left;
  1192. while assigned(hp) do
  1193. begin
  1194. location_freetemp(exprasmlist,tarrayconstructornode(hp).left.location);
  1195. hp:=tarrayconstructornode(hp).right;
  1196. end;
  1197. end;
  1198. end;
  1199. end;
  1200. pp:=tbinarynode(pp.right);
  1201. end;
  1202. if inlined then
  1203. begin
  1204. tg.ungetpersistanttemp(exprasmlist,inlinecode.retoffset);
  1205. tprocdef(procdefinition).parast.address_fixup:=store_parast_fixup;
  1206. right:=inlinecode;
  1207. end;
  1208. if assigned(params) then
  1209. params.free;
  1210. { from now on the result can be freed normally }
  1211. if inlined and ret_in_param(resulttype.def) then
  1212. tg.persistanttemptonormal(funcretref.offset);
  1213. { if return value is not used }
  1214. if (not(nf_return_value_used in flags)) and (not is_void(resulttype.def)) then
  1215. begin
  1216. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  1217. begin
  1218. { data which must be finalized ? }
  1219. if (resulttype.def.needs_inittable) then
  1220. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  1221. { release unused temp }
  1222. tg.ungetiftemp(exprasmlist,location.reference)
  1223. end
  1224. else if location.loc=LOC_FPUREGISTER then
  1225. begin
  1226. { release FPU stack }
  1227. emit_reg(A_FSTP,S_NO,R_ST);
  1228. {
  1229. dec(trgcpu(rg).fpuvaroffset);
  1230. do NOT decrement as the increment before
  1231. is not called for unused results PM }
  1232. end;
  1233. end;
  1234. end;
  1235. {*****************************************************************************
  1236. TI386PROCINLINENODE
  1237. *****************************************************************************}
  1238. procedure ti386procinlinenode.pass_2;
  1239. var st : tsymtable;
  1240. oldprocdef : tprocdef;
  1241. ps, i : longint;
  1242. tmpreg: tregister;
  1243. oldprocinfo : pprocinfo;
  1244. oldinlining_procedure,
  1245. nostackframe,make_global : boolean;
  1246. inlineentrycode,inlineexitcode : TAAsmoutput;
  1247. oldexitlabel,oldexit2label,oldquickexitlabel:tasmlabel;
  1248. oldregstate: pointer;
  1249. {$ifdef GDB}
  1250. startlabel,endlabel : tasmlabel;
  1251. pp : pchar;
  1252. mangled_length : longint;
  1253. {$endif GDB}
  1254. begin
  1255. { deallocate the registers used for the current procedure's regvars }
  1256. if assigned(aktprocdef.regvarinfo) then
  1257. begin
  1258. with pregvarinfo(aktprocdef.regvarinfo)^ do
  1259. for i := 1 to maxvarregs do
  1260. if assigned(regvars[i]) then
  1261. store_regvar(exprasmlist,regvars[i].reg);
  1262. rg.saveStateForInline(oldregstate);
  1263. { make sure the register allocator knows what the regvars in the }
  1264. { inlined code block are (JM) }
  1265. rg.resetusableregisters;
  1266. rg.clearregistercount;
  1267. rg.cleartempgen;
  1268. if assigned(inlineprocdef.regvarinfo) then
  1269. with pregvarinfo(inlineprocdef.regvarinfo)^ do
  1270. for i := 1 to maxvarregs do
  1271. if assigned(regvars[i]) then
  1272. begin
  1273. tmpreg:=rg.makeregsize(regvars[i].reg,OS_INT);
  1274. rg.makeregvar(tmpreg);
  1275. end;
  1276. end;
  1277. oldinlining_procedure:=inlining_procedure;
  1278. oldexitlabel:=aktexitlabel;
  1279. oldexit2label:=aktexit2label;
  1280. oldquickexitlabel:=quickexitlabel;
  1281. getlabel(aktexitlabel);
  1282. getlabel(aktexit2label);
  1283. { we're inlining a procedure }
  1284. inlining_procedure:=true;
  1285. { save old procinfo }
  1286. oldprocdef:=aktprocdef;
  1287. getmem(oldprocinfo,sizeof(tprocinfo));
  1288. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1289. { set new procinfo }
  1290. aktprocdef:=inlineprocdef;
  1291. procinfo^.return_offset:=retoffset;
  1292. procinfo^.para_offset:=para_offset;
  1293. procinfo^.no_fast_exit:=false;
  1294. { arg space has been filled by the parent secondcall }
  1295. st:=aktprocdef.localst;
  1296. { set it to the same lexical level }
  1297. st.symtablelevel:=oldprocdef.localst.symtablelevel;
  1298. if st.datasize>0 then
  1299. begin
  1300. st.address_fixup:=tg.gettempofsizepersistant(exprasmlist,st.datasize)+st.datasize;
  1301. {$ifdef extdebug}
  1302. Comment(V_debug,'local symtable is at offset '+tostr(st.address_fixup));
  1303. exprasmList.concat(Tai_asm_comment.Create(strpnew(
  1304. 'local symtable is at offset '+tostr(st.address_fixup))));
  1305. {$endif extdebug}
  1306. end;
  1307. exprasmList.concat(Tai_Marker.Create(InlineStart));
  1308. {$ifdef extdebug}
  1309. exprasmList.concat(Tai_asm_comment.Create(strpnew('Start of inlined proc')));
  1310. {$endif extdebug}
  1311. {$ifdef GDB}
  1312. if (cs_debuginfo in aktmoduleswitches) then
  1313. begin
  1314. getaddrlabel(startlabel);
  1315. getaddrlabel(endlabel);
  1316. cg.a_label(exprasmlist,startlabel);
  1317. inlineprocdef.localst.symtabletype:=inlinelocalsymtable;
  1318. inlineprocdef.parast.symtabletype:=inlineparasymtable;
  1319. { Here we must include the para and local symtable info }
  1320. inlineprocdef.concatstabto(withdebuglist);
  1321. { set it back for safety }
  1322. inlineprocdef.localst.symtabletype:=localsymtable;
  1323. inlineprocdef.parast.symtabletype:=parasymtable;
  1324. mangled_length:=length(oldprocdef.mangledname);
  1325. getmem(pp,mangled_length+50);
  1326. strpcopy(pp,'192,0,0,'+startlabel.name);
  1327. if (target_info.use_function_relative_addresses) then
  1328. begin
  1329. strpcopy(strend(pp),'-');
  1330. strpcopy(strend(pp),oldprocdef.mangledname);
  1331. end;
  1332. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1333. end;
  1334. {$endif GDB}
  1335. { takes care of local data initialization }
  1336. inlineentrycode:=TAAsmoutput.Create;
  1337. inlineexitcode:=TAAsmoutput.Create;
  1338. ps:=para_size;
  1339. make_global:=false; { to avoid warning }
  1340. genentrycode(inlineentrycode,make_global,0,ps,nostackframe,true);
  1341. if po_assembler in aktprocdef.procoptions then
  1342. inlineentrycode.insert(Tai_marker.Create(asmblockstart));
  1343. exprasmList.concatlist(inlineentrycode);
  1344. secondpass(inlinetree);
  1345. genexitcode(inlineexitcode,0,false,true);
  1346. if po_assembler in aktprocdef.procoptions then
  1347. inlineexitcode.concat(Tai_marker.Create(asmblockend));
  1348. exprasmList.concatlist(inlineexitcode);
  1349. inlineentrycode.free;
  1350. inlineexitcode.free;
  1351. {$ifdef extdebug}
  1352. exprasmList.concat(Tai_asm_comment.Create(strpnew('End of inlined proc')));
  1353. {$endif extdebug}
  1354. exprasmList.concat(Tai_Marker.Create(InlineEnd));
  1355. {we can free the local data now, reset also the fixup address }
  1356. if st.datasize>0 then
  1357. begin
  1358. tg.ungetpersistanttemp(exprasmlist,st.address_fixup-st.datasize);
  1359. st.address_fixup:=0;
  1360. end;
  1361. { restore procinfo }
  1362. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1363. freemem(oldprocinfo,sizeof(tprocinfo));
  1364. {$ifdef GDB}
  1365. if (cs_debuginfo in aktmoduleswitches) then
  1366. begin
  1367. cg.a_label(exprasmlist,endlabel);
  1368. strpcopy(pp,'224,0,0,'+endlabel.name);
  1369. if (target_info.use_function_relative_addresses) then
  1370. begin
  1371. strpcopy(strend(pp),'-');
  1372. strpcopy(strend(pp),oldprocdef.mangledname);
  1373. end;
  1374. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1375. freemem(pp,mangled_length+50);
  1376. end;
  1377. {$endif GDB}
  1378. { restore }
  1379. aktprocdef:=oldprocdef;
  1380. aktexitlabel:=oldexitlabel;
  1381. aktexit2label:=oldexit2label;
  1382. quickexitlabel:=oldquickexitlabel;
  1383. inlining_procedure:=oldinlining_procedure;
  1384. { reallocate the registers used for the current procedure's regvars, }
  1385. { since they may have been used and then deallocated in the inlined }
  1386. { procedure (JM) }
  1387. if assigned(aktprocdef.regvarinfo) then
  1388. begin
  1389. rg.restoreStateAfterInline(oldregstate);
  1390. end;
  1391. end;
  1392. begin
  1393. ccallparanode:=ti386callparanode;
  1394. ccallnode:=ti386callnode;
  1395. cprocinlinenode:=ti386procinlinenode;
  1396. end.
  1397. {
  1398. $Log$
  1399. Revision 1.57 2002-07-06 20:27:26 carl
  1400. + generic set handling
  1401. Revision 1.56 2002/07/01 18:46:31 peter
  1402. * internal linker
  1403. * reorganized aasm layer
  1404. Revision 1.55 2002/07/01 16:23:56 peter
  1405. * cg64 patch
  1406. * basics for currency
  1407. * asnode updates for class and interface (not finished)
  1408. Revision 1.54 2002/05/20 13:30:40 carl
  1409. * bugfix of hdisponen (base must be set, not index)
  1410. * more portability fixes
  1411. Revision 1.53 2002/05/18 13:34:23 peter
  1412. * readded missing revisions
  1413. Revision 1.52 2002/05/16 19:46:51 carl
  1414. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1415. + try to fix temp allocation (still in ifdef)
  1416. + generic constructor calls
  1417. + start of tassembler / tmodulebase class cleanup
  1418. Revision 1.50 2002/05/13 19:54:38 peter
  1419. * removed n386ld and n386util units
  1420. * maybe_save/maybe_restore added instead of the old maybe_push
  1421. Revision 1.49 2002/05/12 16:53:17 peter
  1422. * moved entry and exitcode to ncgutil and cgobj
  1423. * foreach gets extra argument for passing local data to the
  1424. iterator function
  1425. * -CR checks also class typecasts at runtime by changing them
  1426. into as
  1427. * fixed compiler to cycle with the -CR option
  1428. * fixed stabs with elf writer, finally the global variables can
  1429. be watched
  1430. * removed a lot of routines from cga unit and replaced them by
  1431. calls to cgobj
  1432. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1433. u32bit then the other is typecasted also to u32bit without giving
  1434. a rangecheck warning/error.
  1435. * fixed pascal calling method with reversing also the high tree in
  1436. the parast, detected by tcalcst3 test
  1437. Revision 1.48 2002/04/25 20:16:40 peter
  1438. * moved more routines from cga/n386util
  1439. Revision 1.47 2002/04/21 19:02:07 peter
  1440. * removed newn and disposen nodes, the code is now directly
  1441. inlined from pexpr
  1442. * -an option that will write the secondpass nodes to the .s file, this
  1443. requires EXTDEBUG define to actually write the info
  1444. * fixed various internal errors and crashes due recent code changes
  1445. Revision 1.46 2002/04/21 15:34:25 carl
  1446. * changeregsize -> rg.makeregsize
  1447. Revision 1.45 2002/04/15 19:44:21 peter
  1448. * fixed stackcheck that would be called recursively when a stack
  1449. error was found
  1450. * generic changeregsize(reg,size) for i386 register resizing
  1451. * removed some more routines from cga unit
  1452. * fixed returnvalue handling
  1453. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1454. Revision 1.44 2002/04/04 19:06:10 peter
  1455. * removed unused units
  1456. * use tlocation.size in cg.a_*loc*() routines
  1457. Revision 1.43 2002/04/02 17:11:35 peter
  1458. * tlocation,treference update
  1459. * LOC_CONSTANT added for better constant handling
  1460. * secondadd splitted in multiple routines
  1461. * location_force_reg added for loading a location to a register
  1462. of a specified size
  1463. * secondassignment parses now first the right and then the left node
  1464. (this is compatible with Kylix). This saves a lot of push/pop especially
  1465. with string operations
  1466. * adapted some routines to use the new cg methods
  1467. Revision 1.42 2002/03/31 20:26:38 jonas
  1468. + a_loadfpu_* and a_loadmm_* methods in tcg
  1469. * register allocation is now handled by a class and is mostly processor
  1470. independent (+rgobj.pas and i386/rgcpu.pas)
  1471. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1472. * some small improvements and fixes to the optimizer
  1473. * some register allocation fixes
  1474. * some fpuvaroffset fixes in the unary minus node
  1475. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1476. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1477. also better optimizable)
  1478. * fixed and optimized register saving/restoring for new/dispose nodes
  1479. * LOC_FPU locations now also require their "register" field to be set to
  1480. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1481. - list field removed of the tnode class because it's not used currently
  1482. and can cause hard-to-find bugs
  1483. Revision 1.41 2002/03/04 19:10:13 peter
  1484. * removed compiler warnings
  1485. }