n386cal.pas 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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. symdef,node,ncal,ncgcal;
  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(tcgcallnode)
  31. procedure pass_2;override;
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,
  36. cutils,verbose,globals,
  37. symconst,symbase,symsym,symtable,defbase,
  38. {$ifdef GDB}
  39. {$ifdef delphi}
  40. sysutils,
  41. {$else}
  42. strings,
  43. {$endif}
  44. gdb,
  45. {$endif GDB}
  46. cginfo,cgbase,pass_2,
  47. cpubase,paramgr,
  48. aasmbase,aasmtai,aasmcpu,
  49. nmem,nld,ncnv,
  50. ncgutil,cga,cgobj,tgobj,regvars,rgobj,rgcpu,cg64f32,cgcpu,cpuinfo;
  51. {*****************************************************************************
  52. TI386CALLPARANODE
  53. *****************************************************************************}
  54. procedure ti386callparanode.secondcallparan(defcoll : TParaItem;
  55. push_from_left_to_right,inlined,is_cdecl : boolean;para_alignment,para_offset : longint);
  56. procedure maybe_push_high;
  57. begin
  58. { open array ? }
  59. { defcoll.data can be nil for read/write }
  60. if assigned(defcoll.paratype.def) and
  61. assigned(hightree) then
  62. begin
  63. secondpass(hightree);
  64. { this is a longint anyway ! }
  65. push_value_para(hightree,inlined,false,para_offset,4,paralocdummy);
  66. end;
  67. end;
  68. var
  69. otlabel,oflabel : tasmlabel;
  70. { temporary variables: }
  71. tempdeftype : tdeftype;
  72. tmpreg : tregister;
  73. href : treference;
  74. begin
  75. { set default para_alignment to target_info.stackalignment }
  76. if para_alignment=0 then
  77. para_alignment:=aktalignment.paraalign;
  78. { push from left to right if specified }
  79. if push_from_left_to_right and assigned(right) then
  80. begin
  81. if (nf_varargs_para in flags) then
  82. tcallparanode(right).secondcallparan(defcoll,push_from_left_to_right,
  83. inlined,is_cdecl,para_alignment,para_offset)
  84. else
  85. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  86. inlined,is_cdecl,para_alignment,para_offset);
  87. end;
  88. otlabel:=truelabel;
  89. oflabel:=falselabel;
  90. objectlibrary.getlabel(truelabel);
  91. objectlibrary.getlabel(falselabel);
  92. secondpass(left);
  93. { handle varargs first, because defcoll is not valid }
  94. if (nf_varargs_para in flags) then
  95. begin
  96. if paramanager.push_addr_param(left.resulttype.def,is_cdecl) then
  97. begin
  98. inc(pushedparasize,4);
  99. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paralocdummy);
  100. location_release(exprasmlist,left.location);
  101. end
  102. else
  103. push_value_para(left,inlined,is_cdecl,para_offset,para_alignment,paralocdummy);
  104. end
  105. { filter array constructor with c styled args }
  106. else if is_array_constructor(left.resulttype.def) and (nf_cargs in left.flags) then
  107. begin
  108. { nothing, everything is already pushed }
  109. end
  110. { in codegen.handleread.. defcoll.data is set to nil }
  111. else if assigned(defcoll.paratype.def) and
  112. (defcoll.paratype.def.deftype=formaldef) then
  113. begin
  114. { allow passing of a constant to a const formaldef }
  115. if (defcoll.paratyp=vs_const) and
  116. (left.location.loc=LOC_CONSTANT) then
  117. location_force_mem(exprasmlist,left.location);
  118. { allow @var }
  119. inc(pushedparasize,4);
  120. if (left.nodetype=addrn) and
  121. (not(nf_procvarload in left.flags)) then
  122. begin
  123. if inlined then
  124. begin
  125. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  126. cg.a_load_loc_ref(exprasmlist,left.location,href);
  127. end
  128. else
  129. cg.a_param_loc(exprasmlist,left.location,paralocdummy);
  130. location_release(exprasmlist,left.location);
  131. end
  132. else
  133. begin
  134. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  135. CGMessage(type_e_mismatch)
  136. else
  137. begin
  138. if inlined then
  139. begin
  140. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  141. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  142. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  143. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  144. cg.free_scratch_reg(exprasmlist,tmpreg);
  145. end
  146. else
  147. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paralocdummy);
  148. location_release(exprasmlist,left.location);
  149. end;
  150. end;
  151. end
  152. { handle call by reference parameter }
  153. else if (defcoll.paratyp in [vs_var,vs_out]) then
  154. begin
  155. if (left.location.loc<>LOC_REFERENCE) then
  156. begin
  157. { passing self to a var parameter is allowed in
  158. TP and delphi }
  159. if not((left.location.loc=LOC_CREFERENCE) and
  160. (left.nodetype=selfn)) then
  161. internalerror(200106041);
  162. end;
  163. if not push_from_left_to_right then
  164. maybe_push_high;
  165. if (defcoll.paratyp=vs_out) and
  166. assigned(defcoll.paratype.def) and
  167. not is_class(defcoll.paratype.def) and
  168. defcoll.paratype.def.needs_inittable then
  169. cg.g_finalize(exprasmlist,defcoll.paratype.def,left.location.reference,false);
  170. inc(pushedparasize,4);
  171. if inlined then
  172. begin
  173. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  174. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  175. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  176. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  177. cg.free_scratch_reg(exprasmlist,tmpreg);
  178. end
  179. else
  180. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paralocdummy);
  181. location_release(exprasmlist,left.location);
  182. if push_from_left_to_right then
  183. maybe_push_high;
  184. end
  185. else
  186. begin
  187. tempdeftype:=resulttype.def.deftype;
  188. if tempdeftype=filedef then
  189. CGMessage(cg_e_file_must_call_by_reference);
  190. { open array must always push the address, this is needed to
  191. also push addr of small open arrays and with cdecl functions (PFV) }
  192. if (
  193. assigned(defcoll.paratype.def) and
  194. (is_open_array(defcoll.paratype.def) or
  195. is_array_of_const(defcoll.paratype.def))
  196. ) or
  197. (
  198. paramanager.push_addr_param(resulttype.def,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.GetTemp(exprasmlist,tcgsize2size[left.location.size],tt_normal,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. if not push_from_left_to_right then
  217. maybe_push_high;
  218. inc(pushedparasize,4);
  219. if inlined then
  220. begin
  221. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  222. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  223. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  224. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  225. cg.free_scratch_reg(exprasmlist,tmpreg);
  226. end
  227. else
  228. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paralocdummy);
  229. location_release(exprasmlist,left.location);
  230. if push_from_left_to_right then
  231. maybe_push_high;
  232. end
  233. else
  234. begin
  235. push_value_para(left,inlined,is_cdecl,
  236. para_offset,para_alignment,paralocdummy);
  237. end;
  238. end;
  239. truelabel:=otlabel;
  240. falselabel:=oflabel;
  241. { push from right to left }
  242. if not push_from_left_to_right and assigned(right) then
  243. begin
  244. if (nf_varargs_para in flags) then
  245. tcallparanode(right).secondcallparan(defcoll,push_from_left_to_right,
  246. inlined,is_cdecl,para_alignment,para_offset)
  247. else
  248. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  249. inlined,is_cdecl,para_alignment,para_offset);
  250. end;
  251. end;
  252. {*****************************************************************************
  253. TI386CALLNODE
  254. *****************************************************************************}
  255. procedure ti386callnode.pass_2;
  256. var
  257. regs_to_push : tregisterset;
  258. unusedstate: pointer;
  259. pushed : tpushedsaved;
  260. tmpreg : tregister;
  261. hregister : tregister;
  262. oldpushedparasize : longint;
  263. { true if ESI must be loaded again after the subroutine }
  264. loadesi : boolean;
  265. { true if a virtual method must be called directly }
  266. no_virtual_call : boolean;
  267. { true if we produce a con- or destrutor in a call }
  268. is_con_or_destructor : boolean;
  269. { true if a constructor is called again }
  270. extended_new : boolean;
  271. { adress returned from an I/O-error }
  272. iolabel : tasmlabel;
  273. { lexlevel count }
  274. i : longint;
  275. { help reference pointer }
  276. href : treference;
  277. hrefvmt : treference;
  278. hp : tnode;
  279. pp : tbinarynode;
  280. params : tnode;
  281. inlined : boolean;
  282. inlinecode : tprocinlinenode;
  283. store_parast_fixup,
  284. para_alignment,
  285. para_offset : longint;
  286. cgsize : tcgsize;
  287. { instruction for alignement correction }
  288. { corr : paicpu;}
  289. { we must pop this size also after !! }
  290. { must_pop : boolean; }
  291. pop_size : longint;
  292. {$ifdef OPTALIGN}
  293. pop_esp : boolean;
  294. push_size : longint;
  295. {$endif OPTALIGN}
  296. pop_allowed : boolean;
  297. release_tmpreg : boolean;
  298. constructorfailed : tasmlabel;
  299. returnref,
  300. pararef : treference;
  301. label
  302. dont_call;
  303. begin
  304. extended_new:=false;
  305. iolabel:=nil;
  306. inlinecode:=nil;
  307. inlined:=false;
  308. loadesi:=true;
  309. no_virtual_call:=false;
  310. rg.saveunusedstate(unusedstate);
  311. { if we allocate the temp. location for ansi- or widestrings }
  312. { already here, we avoid later a push/pop }
  313. if is_widestring(resulttype.def) then
  314. begin
  315. tg.GetTemp(exprasmlist,pointer_size,tt_widestring,refcountedtemp);
  316. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp);
  317. end
  318. else if is_ansistring(resulttype.def) then
  319. begin
  320. tg.GetTemp(exprasmlist,pointer_size,tt_ansistring,refcountedtemp);
  321. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp);
  322. end;
  323. if (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_stdcall]) then
  324. para_alignment:=4
  325. else
  326. para_alignment:=aktalignment.paraalign;
  327. if not assigned(procdefinition) then
  328. exit;
  329. { Deciding whether we may still need the parameters happens next (JM) }
  330. if assigned(left) then
  331. params:=left.getcopy
  332. else params := nil;
  333. if (procdefinition.proccalloption=pocall_inline) then
  334. begin
  335. inlined:=true;
  336. inlinecode:=tprocinlinenode(right);
  337. right:=nil;
  338. { set it to the same lexical level as the local symtable, becuase
  339. the para's are stored there }
  340. tprocdef(procdefinition).parast.symtablelevel:=aktprocdef.localst.symtablelevel;
  341. if assigned(params) then
  342. begin
  343. inlinecode.para_size:=tprocdef(procdefinition).para_size(para_alignment);
  344. tg.GetTemp(exprasmlist,inlinecode.para_size,tt_persistant,pararef);
  345. inlinecode.para_offset:=pararef.offset;
  346. end;
  347. store_parast_fixup:=tprocdef(procdefinition).parast.address_fixup;
  348. tprocdef(procdefinition).parast.address_fixup:=inlinecode.para_offset;
  349. {$ifdef extdebug}
  350. Comment(V_debug,
  351. 'inlined parasymtable is at offset '
  352. +tostr(tprocdef(procdefinition).parast.address_fixup));
  353. exprasmList.concat(tai_comment.Create(
  354. strpnew('inlined parasymtable is at offset '
  355. +tostr(tprocdef(procdefinition).parast.address_fixup))));
  356. {$endif extdebug}
  357. end;
  358. { only if no proc var }
  359. if inlined or
  360. not(assigned(right)) then
  361. is_con_or_destructor:=(procdefinition.proctypeoption in [potype_constructor,potype_destructor]);
  362. { proc variables destroy all registers }
  363. if (inlined or
  364. (right=nil)) and
  365. { virtual methods too }
  366. not(po_virtualmethod in procdefinition.procoptions) then
  367. begin
  368. if (cs_check_io in aktlocalswitches) and
  369. (po_iocheck in procdefinition.procoptions) and
  370. not(po_iocheck in aktprocdef.procoptions) then
  371. begin
  372. objectlibrary.getaddrlabel(iolabel);
  373. cg.a_label(exprasmlist,iolabel);
  374. end
  375. else
  376. iolabel:=nil;
  377. { save all used registers and possible registers
  378. used for the return value }
  379. regs_to_push := tprocdef(procdefinition).usedregisters;
  380. if (not is_void(resulttype.def)) and
  381. (not paramanager.ret_in_param(resulttype.def)) then
  382. begin
  383. include(regs_to_push,accumulator);
  384. if resulttype.def.size>sizeof(aword) then
  385. include(regs_to_push,accumulatorhigh);
  386. end;
  387. rg.saveusedregisters(exprasmlist,pushed,regs_to_push);
  388. { give used registers through }
  389. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedregisters;
  390. end
  391. else
  392. begin
  393. regs_to_push := all_registers;
  394. rg.saveusedregisters(exprasmlist,pushed,regs_to_push);
  395. rg.usedinproc:=all_registers;
  396. { no IO check for methods and procedure variables }
  397. iolabel:=nil;
  398. end;
  399. { generate the code for the parameter and push them }
  400. oldpushedparasize:=pushedparasize;
  401. pushedparasize:=0;
  402. pop_size:=0;
  403. { no inc esp for inlined procedure
  404. and for objects constructors PM }
  405. if inlined or
  406. ((procdefinition.proctypeoption=potype_constructor) and
  407. { quick'n'dirty check if it is a class or an object }
  408. (resulttype.def.deftype=orddef)) then
  409. pop_allowed:=false
  410. else
  411. pop_allowed:=true;
  412. if pop_allowed then
  413. begin
  414. { Old pushedsize aligned on 4 ? }
  415. i:=oldpushedparasize and 3;
  416. if i>0 then
  417. inc(pop_size,4-i);
  418. { This parasize aligned on 4 ? }
  419. i:=procdefinition.para_size(para_alignment) and 3;
  420. if i>0 then
  421. inc(pop_size,4-i);
  422. { insert the opcode and update pushedparasize }
  423. { never push 4 or more !! }
  424. pop_size:=pop_size mod 4;
  425. if pop_size>0 then
  426. begin
  427. inc(pushedparasize,pop_size);
  428. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  429. {$ifdef GDB}
  430. if (cs_debuginfo in aktmoduleswitches) and
  431. (exprasmList.first=exprasmList.last) then
  432. exprasmList.concat(Tai_force_line.Create);
  433. {$endif GDB}
  434. end;
  435. end;
  436. {$ifdef OPTALIGN}
  437. if pop_allowed and (cs_align in aktglobalswitches) then
  438. begin
  439. pop_esp:=true;
  440. push_size:=procdefinition.para_size(para_alignment);
  441. { !!!! here we have to take care of return type, self
  442. and nested procedures
  443. }
  444. inc(push_size,12);
  445. emit_reg_reg(A_MOV,S_L,R_ESP,R_EDI);
  446. if (push_size mod 8)=0 then
  447. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP)
  448. else
  449. begin
  450. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  451. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP);
  452. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  453. end;
  454. emit_reg(A_PUSH,S_L,R_EDI);
  455. end
  456. else
  457. pop_esp:=false;
  458. {$endif OPTALIGN}
  459. { Push parameters }
  460. if assigned(params) then
  461. begin
  462. { be found elsewhere }
  463. if inlined then
  464. para_offset:=tprocdef(procdefinition).parast.address_fixup+
  465. tprocdef(procdefinition).parast.datasize
  466. else
  467. para_offset:=0;
  468. if not(inlined) and
  469. assigned(right) then
  470. tcallparanode(params).secondcallparan(TParaItem(tabstractprocdef(right.resulttype.def).Para.first),
  471. (po_leftright in procdefinition.procoptions),inlined,
  472. (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl]),
  473. para_alignment,para_offset)
  474. else
  475. tcallparanode(params).secondcallparan(TParaItem(procdefinition.Para.first),
  476. (po_leftright in procdefinition.procoptions),inlined,
  477. (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl]),
  478. para_alignment,para_offset);
  479. end;
  480. { Allocate return value for inlined routines }
  481. if inlined and
  482. (resulttype.def.size>0) then
  483. begin
  484. tg.GetTemp(exprasmlist,Align(resulttype.def.size,aktalignment.paraalign),tt_persistant,returnref);
  485. inlinecode.retoffset:=returnref.offset;
  486. end;
  487. { Allocate return value when returned in argument }
  488. if paramanager.ret_in_param(resulttype.def) then
  489. begin
  490. if assigned(funcretrefnode) then
  491. begin
  492. secondpass(funcretrefnode);
  493. if codegenerror then
  494. exit;
  495. if (funcretrefnode.location.loc<>LOC_REFERENCE) then
  496. internalerror(200204246);
  497. funcretref:=funcretrefnode.location.reference;
  498. end
  499. else
  500. begin
  501. if inlined then
  502. begin
  503. tg.GetTemp(exprasmlist,resulttype.def.size,tt_persistant,funcretref);
  504. {$ifdef extdebug}
  505. Comment(V_debug,'function return value is at offset '
  506. +tostr(funcretref.offset));
  507. exprasmlist.concat(tai_comment.create(
  508. strpnew('function return value is at offset '
  509. +tostr(funcretref.offset))));
  510. {$endif extdebug}
  511. end
  512. else
  513. tg.GetTemp(exprasmlist,resulttype.def.size,tt_normal,funcretref);
  514. end;
  515. { This must not be counted for C code
  516. complex return address is removed from stack
  517. by function itself ! }
  518. {$ifdef OLD_C_STACK}
  519. inc(pushedparasize,4); { lets try without it PM }
  520. {$endif not OLD_C_STACK}
  521. if inlined then
  522. begin
  523. hregister:=cg.get_scratch_reg_address(exprasmlist);
  524. cg.a_loadaddr_ref_reg(exprasmlist,funcretref,hregister);
  525. reference_reset_base(href,procinfo.framepointer,inlinecode.retoffset);
  526. cg.a_load_reg_ref(exprasmlist,OS_ADDR,hregister,href);
  527. cg.free_scratch_reg(exprasmlist,hregister);
  528. end
  529. else
  530. cg.a_paramaddr_ref(exprasmlist,funcretref,paralocdummy);
  531. end;
  532. { procedure variable or normal function call ? }
  533. if inlined or
  534. (right=nil) then
  535. begin
  536. { Normal function call }
  537. { overloaded operator has no symtable }
  538. { push self }
  539. if assigned(symtableproc) and
  540. (symtableproc.symtabletype=withsymtable) then
  541. begin
  542. { dirty trick to avoid the secondcall below }
  543. methodpointer:=ccallparanode.create(nil,nil);
  544. location_reset(methodpointer.location,LOC_REGISTER,OS_ADDR);
  545. rg.getexplicitregisterint(exprasmlist,R_ESI);
  546. methodpointer.location.register:=R_ESI;
  547. { ARGHHH this is wrong !!!
  548. if we can init from base class for a child
  549. class that the wrong VMT will be
  550. transfered to constructor !! }
  551. methodpointer.resulttype:=
  552. twithnode(twithsymtable(symtableproc).withnode).left.resulttype;
  553. { make a reference }
  554. href:=twithnode(twithsymtable(symtableproc).withnode).withreference;
  555. if ((not(nf_islocal in twithnode(twithsymtable(symtableproc).withnode).flags)) and
  556. (not twithsymtable(symtableproc).direct_with)) or
  557. is_class_or_interface(methodpointer.resulttype.def) then
  558. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg)
  559. else
  560. cg.a_loadaddr_ref_reg(exprasmlist,href,self_pointer_reg);
  561. end;
  562. { push self }
  563. if assigned(symtableproc) and
  564. ((symtableproc.symtabletype=objectsymtable) or
  565. (symtableproc.symtabletype=withsymtable)) then
  566. begin
  567. if assigned(methodpointer) then
  568. begin
  569. {
  570. if methodpointer^.resulttype.def=classrefdef then
  571. begin
  572. two possibilities:
  573. 1. constructor
  574. 2. class method
  575. end
  576. else }
  577. begin
  578. case methodpointer.nodetype of
  579. typen:
  580. begin
  581. { direct call to inherited method }
  582. if (po_abstractmethod in procdefinition.procoptions) then
  583. begin
  584. CGMessage(cg_e_cant_call_abstract_method);
  585. goto dont_call;
  586. end;
  587. { generate no virtual call }
  588. no_virtual_call:=true;
  589. if (sp_static in symtableprocentry.symoptions) then
  590. begin
  591. { well lets put the VMT address directly into ESI }
  592. { it is kind of dirty but that is the simplest }
  593. { way to accept virtual static functions (PM) }
  594. loadesi:=true;
  595. { if no VMT just use $0 bug0214 PM }
  596. rg.getexplicitregisterint(exprasmlist,R_ESI);
  597. if not(oo_has_vmt in tobjectdef(methodpointer.resulttype.def).objectoptions) then
  598. cg.a_load_const_reg(exprasmlist,OS_ADDR,0,self_pointer_reg)
  599. else
  600. begin
  601. reference_reset_symbol(href,objectlibrary.newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  602. cg.a_loadaddr_ref_reg(exprasmlist,href,self_pointer_reg);
  603. end;
  604. { emit_reg(A_PUSH,S_L,R_ESI);
  605. this is done below !! }
  606. end
  607. else
  608. { this is a member call, so ESI isn't modfied }
  609. loadesi:=false;
  610. { a class destructor needs a flag }
  611. if is_class(tobjectdef(methodpointer.resulttype.def)) and
  612. (procdefinition.proctypeoption=potype_destructor) then
  613. begin
  614. cg.a_param_const(exprasmlist,OS_ADDR,0,paramanager.getintparaloc(2));
  615. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paramanager.getintparaloc(1));
  616. end;
  617. if not(is_con_or_destructor and
  618. is_class(methodpointer.resulttype.def) and
  619. (procdefinition.proctypeoption in [potype_constructor,potype_destructor])
  620. ) then
  621. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paramanager.getintparaloc(1));
  622. { if an inherited con- or destructor should be }
  623. { called in a con- or destructor then a warning }
  624. { will be made }
  625. { con- and destructors need a pointer to the vmt }
  626. if is_con_or_destructor and
  627. is_object(methodpointer.resulttype.def) and
  628. assigned(aktprocdef) then
  629. begin
  630. if not(aktprocdef.proctypeoption in
  631. [potype_constructor,potype_destructor]) then
  632. CGMessage(cg_w_member_cd_call_from_method);
  633. end;
  634. { class destructors get there flag above }
  635. { constructor flags ? }
  636. if is_con_or_destructor and
  637. not(
  638. is_class(methodpointer.resulttype.def) and
  639. assigned(aktprocdef) and
  640. (aktprocdef.proctypeoption=potype_destructor)) then
  641. begin
  642. { a constructor needs also a flag }
  643. if is_class(methodpointer.resulttype.def) then
  644. cg.a_param_const(exprasmlist,OS_ADDR,0,paramanager.getintparaloc(2));
  645. cg.a_param_const(exprasmlist,OS_ADDR,0,paramanager.getintparaloc(1));
  646. end;
  647. end;
  648. hnewn:
  649. begin
  650. { extended syntax of new }
  651. { ESI must be zero }
  652. rg.getexplicitregisterint(exprasmlist,R_ESI);
  653. cg.a_load_const_reg(exprasmlist,OS_ADDR,0,self_pointer_reg);
  654. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paramanager.getintparaloc(2));
  655. { insert the vmt }
  656. reference_reset_symbol(href,objectlibrary.newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  657. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  658. extended_new:=true;
  659. end;
  660. hdisposen:
  661. begin
  662. secondpass(methodpointer);
  663. { destructor with extended syntax called from dispose }
  664. { hdisposen always deliver LOC_REFERENCE }
  665. rg.getexplicitregisterint(exprasmlist,R_ESI);
  666. emit_ref_reg(A_LEA,S_L,methodpointer.location.reference,R_ESI);
  667. reference_release(exprasmlist,methodpointer.location.reference);
  668. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paramanager.getintparaloc(2));
  669. reference_reset_symbol(href,objectlibrary.newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  670. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  671. end;
  672. else
  673. begin
  674. { call to an instance member }
  675. if (symtableproc.symtabletype<>withsymtable) then
  676. begin
  677. secondpass(methodpointer);
  678. rg.getexplicitregisterint(exprasmlist,R_ESI);
  679. case methodpointer.location.loc of
  680. LOC_CREGISTER,
  681. LOC_REGISTER:
  682. begin
  683. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,methodpointer.location.register,R_ESI);
  684. rg.ungetregisterint(exprasmlist,methodpointer.location.register);
  685. end;
  686. else
  687. begin
  688. if (methodpointer.resulttype.def.deftype=classrefdef) or
  689. is_class_or_interface(methodpointer.resulttype.def) then
  690. cg.a_load_ref_reg(exprasmlist,OS_ADDR,methodpointer.location.reference,R_ESI)
  691. else
  692. cg.a_loadaddr_ref_reg(exprasmlist,methodpointer.location.reference,R_ESI);
  693. reference_release(exprasmlist,methodpointer.location.reference);
  694. end;
  695. end;
  696. end;
  697. { when calling a class method, we have to load ESI with the VMT !
  698. But, not for a class method via self }
  699. if not(po_containsself in procdefinition.procoptions) then
  700. begin
  701. if (po_classmethod in procdefinition.procoptions) and
  702. not(methodpointer.resulttype.def.deftype=classrefdef) then
  703. begin
  704. { class method needs current VMT }
  705. rg.getexplicitregisterint(exprasmlist,R_ESI);
  706. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  707. cg.g_maybe_testself(exprasmlist);
  708. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg);
  709. end;
  710. { direct call to destructor: remove data }
  711. if (procdefinition.proctypeoption=potype_destructor) and
  712. is_class(methodpointer.resulttype.def) then
  713. cg.a_param_const(exprasmlist,OS_INT,1,paramanager.getintparaloc(1));
  714. { direct call to class constructor, don't allocate memory }
  715. if (procdefinition.proctypeoption=potype_constructor) and
  716. is_class(methodpointer.resulttype.def) then
  717. begin
  718. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(2));
  719. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(1));
  720. end
  721. else
  722. begin
  723. { constructor call via classreference => allocate memory }
  724. if (procdefinition.proctypeoption=potype_constructor) and
  725. (methodpointer.resulttype.def.deftype=classrefdef) and
  726. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  727. cg.a_param_const(exprasmlist,OS_INT,1,paramanager.getintparaloc(1));
  728. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paramanager.getintparaloc(1));
  729. end;
  730. end;
  731. if is_con_or_destructor then
  732. begin
  733. { classes don't get a VMT pointer pushed }
  734. if is_object(methodpointer.resulttype.def) then
  735. begin
  736. if (procdefinition.proctypeoption=potype_constructor) then
  737. begin
  738. { it's no bad idea, to insert the VMT }
  739. reference_reset_symbol(href,objectlibrary.newasmsymbol(
  740. tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  741. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  742. end
  743. { destructors haven't to dispose the instance, if this is }
  744. { a direct call }
  745. else
  746. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(1));
  747. end;
  748. end;
  749. end;
  750. end;
  751. end;
  752. end
  753. else
  754. begin
  755. if (po_classmethod in procdefinition.procoptions) and
  756. not(
  757. assigned(aktprocdef) and
  758. (po_classmethod in aktprocdef.procoptions)
  759. ) then
  760. begin
  761. { class method needs current VMT }
  762. rg.getexplicitregisterint(exprasmlist,R_ESI);
  763. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  764. cg.g_maybe_testself(exprasmlist);
  765. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,R_ESI);
  766. end
  767. else
  768. begin
  769. { member call, ESI isn't modified }
  770. loadesi:=false;
  771. end;
  772. { direct call to destructor: don't remove data! }
  773. if is_class(procinfo._class) then
  774. begin
  775. if (procdefinition.proctypeoption=potype_destructor) then
  776. begin
  777. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(2));
  778. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,paramanager.getintparaloc(1));
  779. end
  780. else if (procdefinition.proctypeoption=potype_constructor) then
  781. begin
  782. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(2));
  783. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(1));
  784. end
  785. else
  786. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,paramanager.getintparaloc(1));
  787. end
  788. else if is_object(procinfo._class) then
  789. begin
  790. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,paramanager.getintparaloc(1));
  791. if is_con_or_destructor then
  792. begin
  793. (*
  794. The constructor/destructor is called from the class
  795. itself, no need to push the VMT to create a new object
  796. if (procdefinition.proctypeoption=potype_constructor) then
  797. begin
  798. { it's no bad idea, to insert the VMT }
  799. reference_reset_symbol(href,objectlibrary.newasmsymbol(procinfo._class.vmt_mangledname),0);
  800. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  801. end
  802. { destructors haven't to dispose the instance, if this is }
  803. { a direct call }
  804. else
  805. *)
  806. cg.a_param_const(exprasmlist,OS_INT,0,paramanager.getintparaloc(1));
  807. end;
  808. end
  809. else
  810. Internalerror(200006165);
  811. end;
  812. end;
  813. { call to BeforeDestruction? }
  814. if (procdefinition.proctypeoption=potype_destructor) and
  815. assigned(methodpointer) and
  816. (methodpointer.nodetype<>typen) and
  817. is_class(tobjectdef(methodpointer.resulttype.def)) and
  818. (inlined or
  819. (right=nil)) then
  820. begin
  821. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paramanager.getintparaloc(1));
  822. reference_reset_base(href,self_pointer_reg,0);
  823. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  824. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  825. reference_reset_base(href,tmpreg,72);
  826. cg.a_call_ref(exprasmlist,href);
  827. cg.free_scratch_reg(exprasmlist,tmpreg);
  828. end;
  829. { push base pointer ?}
  830. { never when inlining, since if necessary, the base pointer }
  831. { can/will be gottten from the current procedure's symtable }
  832. { (JM) }
  833. if not inlined then
  834. if (lexlevel>=normal_function_level) and assigned(tprocdef(procdefinition).parast) and
  835. ((tprocdef(procdefinition).parast.symtablelevel)>normal_function_level) then
  836. begin
  837. { if we call a nested function in a method, we must }
  838. { push also SELF! }
  839. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  840. { access }
  841. {
  842. begin
  843. loadesi:=false;
  844. emit_reg(A_PUSH,S_L,R_ESI);
  845. end;
  846. }
  847. if lexlevel=(tprocdef(procdefinition).parast.symtablelevel) then
  848. begin
  849. reference_reset_base(href,procinfo.framepointer,procinfo.framepointer_offset);
  850. cg.a_param_ref(exprasmlist,OS_ADDR,href,paralocdummy);
  851. end
  852. { this is only true if the difference is one !!
  853. but it cannot be more !! }
  854. else if (lexlevel=(tprocdef(procdefinition).parast.symtablelevel)-1) then
  855. begin
  856. cg.a_param_reg(exprasmlist,OS_ADDR,procinfo.framepointer,paralocdummy);
  857. end
  858. else if (lexlevel>(tprocdef(procdefinition).parast.symtablelevel)) then
  859. begin
  860. hregister:=rg.getregisterint(exprasmlist);
  861. reference_reset_base(href,procinfo.framepointer,procinfo.framepointer_offset);
  862. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  863. for i:=(tprocdef(procdefinition).parast.symtablelevel) to lexlevel-1 do
  864. begin
  865. {we should get the correct frame_pointer_offset at each level
  866. how can we do this !!! }
  867. reference_reset_base(href,hregister,procinfo.framepointer_offset);
  868. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  869. end;
  870. cg.a_param_reg(exprasmlist,OS_ADDR,hregister,paralocdummy);
  871. rg.ungetregisterint(exprasmlist,hregister);
  872. end
  873. else
  874. internalerror(25000);
  875. end;
  876. rg.saveregvars(exprasmlist,regs_to_push);
  877. if (po_virtualmethod in procdefinition.procoptions) and
  878. not(no_virtual_call) then
  879. begin
  880. { static functions contain the vmt_address in ESI }
  881. { also class methods }
  882. { Here it is quite tricky because it also depends }
  883. { on the methodpointer PM }
  884. release_tmpreg:=false;
  885. rg.getexplicitregisterint(exprasmlist,R_ESI);
  886. if assigned(aktprocdef) then
  887. begin
  888. if (((sp_static in aktprocdef.procsym.symoptions) or
  889. (po_classmethod in aktprocdef.procoptions)) and
  890. ((methodpointer=nil) or (methodpointer.nodetype=typen)))
  891. or
  892. (po_staticmethod in procdefinition.procoptions) or
  893. ((procdefinition.proctypeoption=potype_constructor) and
  894. { esi contains the vmt if we call a constructor via a class ref }
  895. assigned(methodpointer) and
  896. (methodpointer.resulttype.def.deftype=classrefdef)
  897. ) or
  898. { is_interface(tprocdef(procdefinition)._class) or }
  899. { ESI is loaded earlier }
  900. (po_classmethod in procdefinition.procoptions) then
  901. begin
  902. reference_reset_base(href,R_ESI,0);
  903. end
  904. else
  905. begin
  906. { this is one point where we need vmt_offset (PM) }
  907. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  908. cg.g_maybe_testself(exprasmlist);
  909. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  910. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  911. reference_reset_base(href,tmpreg,0);
  912. release_tmpreg:=true;
  913. end;
  914. end
  915. else
  916. { aktprocdef should be assigned, also in main program }
  917. internalerror(12345);
  918. if tprocdef(procdefinition).extnumber=-1 then
  919. internalerror(44584);
  920. href.offset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  921. if not(is_interface(tprocdef(procdefinition)._class)) and
  922. not(is_cppclass(tprocdef(procdefinition)._class)) then
  923. begin
  924. if (cs_check_object in aktlocalswitches) then
  925. begin
  926. reference_reset_symbol(hrefvmt,objectlibrary.newasmsymbol(tprocdef(procdefinition)._class.vmt_mangledname),0);
  927. cg.a_paramaddr_ref(exprasmlist,hrefvmt,paramanager.getintparaloc(2));
  928. cg.a_param_reg(exprasmlist,OS_ADDR,href.base,paramanager.getintparaloc(1));
  929. cg.a_call_name(exprasmlist,'FPC_CHECK_OBJECT_EXT');
  930. end
  931. else if (cs_check_range in aktlocalswitches) then
  932. begin
  933. cg.a_param_reg(exprasmlist,OS_ADDR,href.base,paramanager.getintparaloc(1));
  934. cg.a_call_name(exprasmlist,'FPC_CHECK_OBJECT');
  935. end;
  936. end;
  937. cg.a_call_ref(exprasmlist,href);
  938. if release_tmpreg then
  939. cg.free_scratch_reg(exprasmlist,tmpreg);
  940. end
  941. else if not inlined then
  942. begin
  943. { We can call interrupts from within the smae code
  944. by just pushing the flags and CS PM }
  945. if (po_interrupt in procdefinition.procoptions) then
  946. begin
  947. emit_none(A_PUSHF,S_L);
  948. emit_reg(A_PUSH,S_L,R_CS);
  949. end;
  950. cg.a_call_name(exprasmlist,tprocdef(procdefinition).mangledname);
  951. end
  952. else { inlined proc }
  953. { inlined code is in inlinecode }
  954. begin
  955. { process the inlinecode }
  956. secondpass(tnode(inlinecode));
  957. { free the args }
  958. if tprocdef(procdefinition).parast.datasize>0 then
  959. tg.UnGetTemp(exprasmlist,pararef);
  960. end;
  961. end
  962. else
  963. { now procedure variable case }
  964. begin
  965. secondpass(right);
  966. if (po_interrupt in procdefinition.procoptions) then
  967. begin
  968. emit_none(A_PUSHF,S_L);
  969. emit_reg(A_PUSH,S_L,R_CS);
  970. end;
  971. { procedure of object? }
  972. if (po_methodpointer in procdefinition.procoptions) then
  973. begin
  974. { method pointer can't be in a register }
  975. hregister:=R_NO;
  976. { do some hacking if we call a method pointer }
  977. { which is a class member }
  978. { else ESI is overwritten ! }
  979. if (right.location.reference.base=R_ESI) or
  980. (right.location.reference.index=R_ESI) then
  981. begin
  982. reference_release(exprasmlist,right.location.reference);
  983. hregister:=cg.get_scratch_reg_address(exprasmlist);
  984. cg.a_load_ref_reg(exprasmlist,OS_ADDR,right.location.reference,hregister);
  985. end;
  986. { load self, but not if it's already explicitly pushed }
  987. if not(po_containsself in procdefinition.procoptions) then
  988. begin
  989. { load ESI }
  990. href:=right.location.reference;
  991. inc(href.offset,4);
  992. rg.getexplicitregisterint(exprasmlist,R_ESI);
  993. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg);
  994. { push self pointer }
  995. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paralocdummy);
  996. end;
  997. rg.saveregvars(exprasmlist,ALL_REGISTERS);
  998. if hregister<>R_NO then
  999. cg.a_call_reg(exprasmlist,hregister)
  1000. else
  1001. cg.a_call_ref(exprasmlist,right.location.reference);
  1002. if hregister<>R_NO then
  1003. cg.free_scratch_reg(exprasmlist,hregister);
  1004. reference_release(exprasmlist,right.location.reference);
  1005. tg.Ungetiftemp(exprasmlist,right.location.reference);
  1006. end
  1007. else
  1008. begin
  1009. rg.saveregvars(exprasmlist,ALL_REGISTERS);
  1010. cg.a_call_loc(exprasmlist,right.location);
  1011. location_release(exprasmlist,right.location);
  1012. location_freetemp(exprasmlist,right.location);
  1013. end;
  1014. end;
  1015. { this was only for normal functions
  1016. displaced here so we also get
  1017. it to work for procvars PM }
  1018. if (not inlined) and (po_clearstack in procdefinition.procoptions) then
  1019. begin
  1020. { we also add the pop_size which is included in pushedparasize }
  1021. pop_size:=0;
  1022. { better than an add on all processors }
  1023. if pushedparasize=4 then
  1024. begin
  1025. rg.getexplicitregisterint(exprasmlist,R_EDI);
  1026. emit_reg(A_POP,S_L,R_EDI);
  1027. rg.ungetregisterint(exprasmlist,R_EDI);
  1028. end
  1029. { the pentium has two pipes and pop reg is pairable }
  1030. { but the registers must be different! }
  1031. else if (pushedparasize=8) and
  1032. not(cs_littlesize in aktglobalswitches) and
  1033. (aktoptprocessor=ClassP5) and
  1034. (procinfo._class=nil) then
  1035. begin
  1036. rg.getexplicitregisterint(exprasmlist,R_EDI);
  1037. emit_reg(A_POP,S_L,R_EDI);
  1038. rg.ungetregisterint(exprasmlist,R_EDI);
  1039. exprasmList.concat(tai_regalloc.Alloc(R_ESI));
  1040. emit_reg(A_POP,S_L,R_ESI);
  1041. exprasmList.concat(tai_regalloc.DeAlloc(R_ESI));
  1042. end
  1043. else if pushedparasize<>0 then
  1044. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  1045. end;
  1046. {$ifdef OPTALIGN}
  1047. if pop_esp then
  1048. emit_reg(A_POP,S_L,R_ESP);
  1049. {$endif OPTALIGN}
  1050. dont_call:
  1051. pushedparasize:=oldpushedparasize;
  1052. rg.restoreunusedstate(unusedstate);
  1053. {$ifdef TEMPREGDEBUG}
  1054. testregisters32;
  1055. {$endif TEMPREGDEBUG}
  1056. { a constructor could be a function with boolean result }
  1057. { if calling constructor called fail we
  1058. must jump directly to quickexitlabel PM
  1059. but only if it is a call of an inherited constructor }
  1060. if (inlined or
  1061. (right=nil)) and
  1062. (procdefinition.proctypeoption=potype_constructor) and
  1063. assigned(methodpointer) and
  1064. (methodpointer.nodetype=typen) and
  1065. (aktprocdef.proctypeoption=potype_constructor) then
  1066. begin
  1067. emitjmp(C_Z,faillabel);
  1068. {$ifdef TEST_GENERIC}
  1069. { should be moved to generic version! }
  1070. reference_reset_base(href, procinfo.framepointer,procinfo.selfpointer_offset);
  1071. cg.a_load_ref_reg(exprasmlist, OS_ADDR, href, SELF_POINTER_REG);
  1072. {$endif}
  1073. end;
  1074. { call to AfterConstruction? }
  1075. if is_class(resulttype.def) and
  1076. (inlined or
  1077. (right=nil)) and
  1078. (procdefinition.proctypeoption=potype_constructor) and
  1079. assigned(methodpointer) and
  1080. (methodpointer.nodetype<>typen) then
  1081. begin
  1082. objectlibrary.getlabel(constructorfailed);
  1083. emitjmp(C_Z,constructorfailed);
  1084. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,paramanager.getintparaloc(1));
  1085. reference_reset_base(href,self_pointer_reg,0);
  1086. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  1087. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  1088. reference_reset_base(href,tmpreg,68);
  1089. cg.a_call_ref(exprasmlist,href);
  1090. cg.free_scratch_reg(exprasmlist,tmpreg);
  1091. exprasmList.concat(tai_regalloc.Alloc(accumulator));
  1092. cg.a_label(exprasmlist,constructorfailed);
  1093. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,self_pointer_reg,accumulator);
  1094. end;
  1095. { handle function results }
  1096. if (not is_void(resulttype.def)) then
  1097. handle_return_value(inlined,extended_new);
  1098. { perhaps i/o check ? }
  1099. if iolabel<>nil then
  1100. begin
  1101. reference_reset_symbol(href,iolabel,0);
  1102. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  1103. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  1104. end;
  1105. if pop_size>0 then
  1106. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1107. { restore registers }
  1108. rg.restoreusedregisters(exprasmlist,pushed);
  1109. { at last, restore instance pointer (SELF) }
  1110. if loadesi then
  1111. cg.g_maybe_loadself(exprasmlist);
  1112. pp:=tbinarynode(params);
  1113. while assigned(pp) do
  1114. begin
  1115. if assigned(pp.left) then
  1116. begin
  1117. location_freetemp(exprasmlist,pp.left.location);
  1118. { process also all nodes of an array of const }
  1119. if pp.left.nodetype=arrayconstructorn then
  1120. begin
  1121. if assigned(tarrayconstructornode(pp.left).left) then
  1122. begin
  1123. hp:=pp.left;
  1124. while assigned(hp) do
  1125. begin
  1126. location_freetemp(exprasmlist,tarrayconstructornode(hp).left.location);
  1127. hp:=tarrayconstructornode(hp).right;
  1128. end;
  1129. end;
  1130. end;
  1131. end;
  1132. pp:=tbinarynode(pp.right);
  1133. end;
  1134. if inlined then
  1135. begin
  1136. if (resulttype.def.size>0) then
  1137. tg.UnGetTemp(exprasmlist,returnref);
  1138. tprocdef(procdefinition).parast.address_fixup:=store_parast_fixup;
  1139. right:=inlinecode;
  1140. end;
  1141. if assigned(params) then
  1142. params.free;
  1143. { from now on the result can be freed normally }
  1144. if inlined and paramanager.ret_in_param(resulttype.def) then
  1145. tg.ChangeTempType(exprasmlist,funcretref,tt_normal);
  1146. { if return value is not used }
  1147. if (not(nf_return_value_used in flags)) and (not is_void(resulttype.def)) then
  1148. begin
  1149. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  1150. begin
  1151. { data which must be finalized ? }
  1152. if (resulttype.def.needs_inittable) then
  1153. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  1154. { release unused temp }
  1155. tg.ungetiftemp(exprasmlist,location.reference)
  1156. end
  1157. else if location.loc=LOC_FPUREGISTER then
  1158. begin
  1159. { release FPU stack }
  1160. emit_reg(A_FSTP,S_NO,R_ST);
  1161. {
  1162. dec(trgcpu(rg).fpuvaroffset);
  1163. do NOT decrement as the increment before
  1164. is not called for unused results PM }
  1165. end;
  1166. end;
  1167. end;
  1168. begin
  1169. ccallparanode:=ti386callparanode;
  1170. ccallnode:=ti386callnode;
  1171. end.
  1172. {
  1173. $Log$
  1174. Revision 1.74 2002-11-15 01:58:57 peter
  1175. * merged changes from 1.0.7 up to 04-11
  1176. - -V option for generating bug report tracing
  1177. - more tracing for option parsing
  1178. - errors for cdecl and high()
  1179. - win32 import stabs
  1180. - win32 records<=8 are returned in eax:edx (turned off by default)
  1181. - heaptrc update
  1182. - more info for temp management in .s file with EXTDEBUG
  1183. Revision 1.73 2002/10/05 12:43:29 carl
  1184. * fixes for Delphi 6 compilation
  1185. (warning : Some features do not work under Delphi)
  1186. Revision 1.72 2002/09/17 18:54:03 jonas
  1187. * a_load_reg_reg() now has two size parameters: source and dest. This
  1188. allows some optimizations on architectures that don't encode the
  1189. register size in the register name.
  1190. Revision 1.71 2002/09/16 19:07:37 peter
  1191. * push 0 instead of VMT when calling a constructor from a member
  1192. Revision 1.70 2002/09/07 15:25:10 peter
  1193. * old logs removed and tabs fixed
  1194. Revision 1.69 2002/09/01 18:43:27 peter
  1195. * include accumulator in regs_to_push list
  1196. Revision 1.68 2002/09/01 12:13:00 peter
  1197. * use a_call_reg
  1198. * ungetiftemp for procvar of object temp
  1199. Revision 1.67 2002/08/25 19:25:21 peter
  1200. * sym.insert_in_data removed
  1201. * symtable.insertvardata/insertconstdata added
  1202. * removed insert_in_data call from symtable.insert, it needs to be
  1203. called separatly. This allows to deref the address calculation
  1204. * procedures now calculate the parast addresses after the procedure
  1205. directives are parsed. This fixes the cdecl parast problem
  1206. * push_addr_param has an extra argument that specifies if cdecl is used
  1207. or not
  1208. Revision 1.66 2002/08/23 16:14:49 peter
  1209. * tempgen cleanup
  1210. * tt_noreuse temp type added that will be used in genentrycode
  1211. Revision 1.65 2002/08/18 20:06:30 peter
  1212. * inlining is now also allowed in interface
  1213. * renamed write/load to ppuwrite/ppuload
  1214. * tnode storing in ppu
  1215. * nld,ncon,nbas are already updated for storing in ppu
  1216. Revision 1.64 2002/08/17 09:23:45 florian
  1217. * first part of procinfo rewrite
  1218. Revision 1.63 2002/08/12 15:08:42 carl
  1219. + stab register indexes for powerpc (moved from gdb to cpubase)
  1220. + tprocessor enumeration moved to cpuinfo
  1221. + linker in target_info is now a class
  1222. * many many updates for m68k (will soon start to compile)
  1223. - removed some ifdef or correct them for correct cpu
  1224. Revision 1.62 2002/08/11 14:32:30 peter
  1225. * renamed current_library to objectlibrary
  1226. Revision 1.61 2002/08/11 13:24:16 peter
  1227. * saving of asmsymbols in ppu supported
  1228. * asmsymbollist global is removed and moved into a new class
  1229. tasmlibrarydata that will hold the info of a .a file which
  1230. corresponds with a single module. Added librarydata to tmodule
  1231. to keep the library info stored for the module. In the future the
  1232. objectfiles will also be stored to the tasmlibrarydata class
  1233. * all getlabel/newasmsymbol and friends are moved to the new class
  1234. Revision 1.60 2002/07/20 11:58:01 florian
  1235. * types.pas renamed to defbase.pas because D6 contains a types
  1236. unit so this would conflicts if D6 programms are compiled
  1237. + Willamette/SSE2 instructions to assembler added
  1238. Revision 1.59 2002/07/11 14:41:33 florian
  1239. * start of the new generic parameter handling
  1240. Revision 1.58 2002/07/07 09:52:34 florian
  1241. * powerpc target fixed, very simple units can be compiled
  1242. * some basic stuff for better callparanode handling, far from being finished
  1243. Revision 1.57 2002/07/06 20:27:26 carl
  1244. + generic set handling
  1245. Revision 1.56 2002/07/01 18:46:31 peter
  1246. * internal linker
  1247. * reorganized aasm layer
  1248. Revision 1.55 2002/07/01 16:23:56 peter
  1249. * cg64 patch
  1250. * basics for currency
  1251. * asnode updates for class and interface (not finished)
  1252. Revision 1.54 2002/05/20 13:30:40 carl
  1253. * bugfix of hdisponen (base must be set, not index)
  1254. * more portability fixes
  1255. Revision 1.53 2002/05/18 13:34:23 peter
  1256. * readded missing revisions
  1257. Revision 1.52 2002/05/16 19:46:51 carl
  1258. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1259. + try to fix temp allocation (still in ifdef)
  1260. + generic constructor calls
  1261. + start of tassembler / tmodulebase class cleanup
  1262. Revision 1.50 2002/05/13 19:54:38 peter
  1263. * removed n386ld and n386util units
  1264. * maybe_save/maybe_restore added instead of the old maybe_push
  1265. Revision 1.49 2002/05/12 16:53:17 peter
  1266. * moved entry and exitcode to ncgutil and cgobj
  1267. * foreach gets extra argument for passing local data to the
  1268. iterator function
  1269. * -CR checks also class typecasts at runtime by changing them
  1270. into as
  1271. * fixed compiler to cycle with the -CR option
  1272. * fixed stabs with elf writer, finally the global variables can
  1273. be watched
  1274. * removed a lot of routines from cga unit and replaced them by
  1275. calls to cgobj
  1276. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1277. u32bit then the other is typecasted also to u32bit without giving
  1278. a rangecheck warning/error.
  1279. * fixed pascal calling method with reversing also the high tree in
  1280. the parast, detected by tcalcst3 test
  1281. }