cg386cal.pas 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$ifdef FPC}
  19. {$goto on}
  20. {$endif FPC}
  21. unit cg386cal;
  22. interface
  23. { $define AnsiStrRef}
  24. uses
  25. symtable,tree;
  26. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  27. push_from_left_to_right,inlined,is_cdecl : boolean;para_alignment,para_offset : longint);
  28. procedure secondcalln(var p : ptree);
  29. procedure secondprocinline(var p : ptree);
  30. implementation
  31. uses
  32. globtype,systems,
  33. cutils,cobjects,verbose,globals,
  34. symconst,aasm,types,
  35. {$ifdef GDB}
  36. strings,gdb,
  37. {$endif GDB}
  38. hcodegen,temp_gen,pass_2,
  39. cpubase,cpuasm,
  40. cgai386,tgeni386,cg386ld;
  41. {*****************************************************************************
  42. SecondCallParaN
  43. *****************************************************************************}
  44. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  45. push_from_left_to_right,inlined,is_cdecl : boolean;para_alignment,para_offset : longint);
  46. procedure maybe_push_high;
  47. begin
  48. { open array ? }
  49. { defcoll^.data can be nil for read/write }
  50. if assigned(defcoll^.paratype.def) and
  51. push_high_param(defcoll^.paratype.def) then
  52. begin
  53. if assigned(p^.hightree) then
  54. begin
  55. secondpass(p^.hightree);
  56. { this is a longint anyway ! }
  57. push_value_para(p^.hightree,inlined,false,para_offset,4);
  58. end
  59. else
  60. internalerror(432645);
  61. end;
  62. end;
  63. procedure prepareout(const r : treference);
  64. var
  65. hr : treference;
  66. pushed : tpushed;
  67. begin
  68. { out parameters needs to be finalized }
  69. if (defcoll^.paratype.def^.needs_inittable) then
  70. begin
  71. reset_reference(hr);
  72. hr.symbol:=defcoll^.paratype.def^.get_inittable_label;
  73. emitpushreferenceaddr(hr);
  74. emitpushreferenceaddr(r);
  75. emitcall('FPC_FINALIZE');
  76. end
  77. else
  78. { or at least it zeroed out }
  79. begin
  80. case defcoll^.paratype.def^.size of
  81. 1:
  82. emit_const_ref(A_MOV,S_B,0,newreference(r));
  83. 2:
  84. emit_const_ref(A_MOV,S_W,0,newreference(r));
  85. 4:
  86. emit_const_ref(A_MOV,S_L,0,newreference(r));
  87. else
  88. begin
  89. pushusedregisters(pushed,$ff);
  90. emit_const(A_PUSH,S_W,0);
  91. push_int(defcoll^.paratype.def^.size);
  92. emitpushreferenceaddr(r);
  93. emitcall('FPC_FILLCHAR');
  94. popusedregisters(pushed);
  95. end
  96. end;
  97. end;
  98. end;
  99. var
  100. otlabel,oflabel : pasmlabel;
  101. { temporary variables: }
  102. tempdeftype : tdeftype;
  103. r : preference;
  104. begin
  105. { set default para_alignment to target_os.stackalignment }
  106. if para_alignment=0 then
  107. para_alignment:=target_os.stackalignment;
  108. { push from left to right if specified }
  109. if push_from_left_to_right and assigned(p^.right) then
  110. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  111. inlined,is_cdecl,para_alignment,para_offset);
  112. otlabel:=truelabel;
  113. oflabel:=falselabel;
  114. getlabel(truelabel);
  115. getlabel(falselabel);
  116. secondpass(p^.left);
  117. { filter array constructor with c styled args }
  118. if is_array_constructor(p^.left^.resulttype) and p^.left^.cargs then
  119. begin
  120. { nothing, everything is already pushed }
  121. end
  122. { in codegen.handleread.. defcoll^.data is set to nil }
  123. else if assigned(defcoll^.paratype.def) and
  124. (defcoll^.paratype.def^.deftype=formaldef) then
  125. begin
  126. { allow @var }
  127. inc(pushedparasize,4);
  128. if (p^.left^.treetype=addrn) and
  129. (not p^.left^.procvarload) then
  130. begin
  131. { always a register }
  132. if inlined then
  133. begin
  134. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  135. emit_reg_ref(A_MOV,S_L,
  136. p^.left^.location.register,r);
  137. end
  138. else
  139. emit_reg(A_PUSH,S_L,p^.left^.location.register);
  140. ungetregister32(p^.left^.location.register);
  141. end
  142. else
  143. begin
  144. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  145. CGMessage(type_e_mismatch)
  146. else
  147. begin
  148. if inlined then
  149. begin
  150. {$ifndef noAllocEdi}
  151. getexplicitregister32(R_EDI);
  152. {$endif noAllocEdi}
  153. emit_ref_reg(A_LEA,S_L,
  154. newreference(p^.left^.location.reference),R_EDI);
  155. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  156. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  157. {$ifndef noAllocEdi}
  158. ungetregister32(R_EDI);
  159. {$endif noAllocEdi}
  160. end
  161. else
  162. emitpushreferenceaddr(p^.left^.location.reference);
  163. del_reference(p^.left^.location.reference);
  164. end;
  165. end;
  166. end
  167. { handle call by reference parameter }
  168. else if (defcoll^.paratyp in [vs_var,vs_out]) then
  169. begin
  170. if (p^.left^.location.loc<>LOC_REFERENCE) then
  171. CGMessage(cg_e_var_must_be_reference);
  172. maybe_push_high;
  173. inc(pushedparasize,4);
  174. if inlined then
  175. begin
  176. {$ifndef noAllocEdi}
  177. getexplicitregister32(R_EDI);
  178. {$endif noAllocEdi}
  179. emit_ref_reg(A_LEA,S_L,
  180. newreference(p^.left^.location.reference),R_EDI);
  181. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  182. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  183. {$ifndef noAllocEdi}
  184. ungetregister32(R_EDI);
  185. {$endif noAllocEdi}
  186. end
  187. else
  188. emitpushreferenceaddr(p^.left^.location.reference);
  189. if defcoll^.paratyp=vs_out then
  190. prepareout(p^.left^.location.reference);
  191. del_reference(p^.left^.location.reference);
  192. end
  193. else
  194. begin
  195. tempdeftype:=p^.resulttype^.deftype;
  196. if tempdeftype=filedef then
  197. CGMessage(cg_e_file_must_call_by_reference);
  198. { open array must always push the address, this is needed to
  199. also push addr of small open arrays and with cdecl functions (PFV) }
  200. if (
  201. assigned(defcoll^.paratype.def) and
  202. (is_open_array(defcoll^.paratype.def) or
  203. is_array_of_const(defcoll^.paratype.def))
  204. ) or
  205. (
  206. push_addr_param(p^.resulttype) and
  207. not is_cdecl
  208. ) then
  209. begin
  210. maybe_push_high;
  211. inc(pushedparasize,4);
  212. if inlined then
  213. begin
  214. {$ifndef noAllocEdi}
  215. getexplicitregister32(R_EDI);
  216. {$endif noAllocEdi}
  217. emit_ref_reg(A_LEA,S_L,
  218. newreference(p^.left^.location.reference),R_EDI);
  219. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  220. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  221. {$ifndef noAllocEdi}
  222. ungetregister32(R_EDI);
  223. {$endif noAllocEdi}
  224. end
  225. else
  226. emitpushreferenceaddr(p^.left^.location.reference);
  227. del_reference(p^.left^.location.reference);
  228. end
  229. else
  230. begin
  231. push_value_para(p^.left,inlined,is_cdecl,
  232. para_offset,para_alignment);
  233. end;
  234. end;
  235. truelabel:=otlabel;
  236. falselabel:=oflabel;
  237. { push from right to left }
  238. if not push_from_left_to_right and assigned(p^.right) then
  239. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  240. inlined,is_cdecl,para_alignment,para_offset);
  241. end;
  242. {*****************************************************************************
  243. SecondCallN
  244. *****************************************************************************}
  245. procedure secondcalln(var p : ptree);
  246. var
  247. unusedregisters : tregisterset;
  248. usablecount : byte;
  249. pushed : tpushed;
  250. hr,funcretref : treference;
  251. hregister,hregister2 : tregister;
  252. oldpushedparasize : longint;
  253. { true if ESI must be loaded again after the subroutine }
  254. loadesi : boolean;
  255. { true if a virtual method must be called directly }
  256. no_virtual_call : boolean;
  257. { true if we produce a con- or destrutor in a call }
  258. is_con_or_destructor : boolean;
  259. { true if a constructor is called again }
  260. extended_new : boolean;
  261. { adress returned from an I/O-error }
  262. iolabel : pasmlabel;
  263. { lexlevel count }
  264. i : longint;
  265. { help reference pointer }
  266. r : preference;
  267. hp,
  268. pp,params : ptree;
  269. inlined : boolean;
  270. inlinecode : ptree;
  271. para_alignment,
  272. para_offset : longint;
  273. { instruction for alignement correction }
  274. { corr : paicpu;}
  275. { we must pop this size also after !! }
  276. { must_pop : boolean; }
  277. pop_size : longint;
  278. pop_allowed : boolean;
  279. pop_esp : boolean;
  280. push_size : longint;
  281. label
  282. dont_call;
  283. begin
  284. reset_reference(p^.location.reference);
  285. extended_new:=false;
  286. iolabel:=nil;
  287. inlinecode:=nil;
  288. inlined:=false;
  289. loadesi:=true;
  290. no_virtual_call:=false;
  291. unusedregisters:=unused;
  292. usablecount:=usablereg32;
  293. if (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  294. (pocall_stdcall in p^.procdefinition^.proccalloptions) then
  295. para_alignment:=4
  296. else
  297. para_alignment:=target_os.stackalignment;
  298. if not assigned(p^.procdefinition) then
  299. exit;
  300. { Deciding whether we may still need the parameters happens next (JM) }
  301. params:=p^.left;
  302. if (pocall_inline in p^.procdefinition^.proccalloptions) then
  303. begin
  304. { make a copy for the next time the procedure is inlined (JM) }
  305. p^.left:=getcopy(p^.left);
  306. inlined:=true;
  307. inlinecode:=p^.right;
  308. { set it to the same lexical level as the local symtable, becuase
  309. the para's are stored there }
  310. pprocdef(p^.procdefinition)^.parast^.symtablelevel:=aktprocsym^.definition^.localst^.symtablelevel;
  311. if assigned(params) then
  312. inlinecode^.para_offset:=gettempofsizepersistant(inlinecode^.para_size);
  313. pprocdef(p^.procdefinition)^.parast^.address_fixup:=inlinecode^.para_offset;
  314. {$ifdef extdebug}
  315. Comment(V_debug,
  316. 'inlined parasymtable is at offset '
  317. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup));
  318. exprasmlist^.concat(new(pai_asm_comment,init(
  319. strpnew('inlined parasymtable is at offset '
  320. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup)))));
  321. {$endif extdebug}
  322. { copy for the next time the procedure is inlined (JM) }
  323. p^.right:=getcopy(p^.right);
  324. { disable further inlining of the same proc
  325. in the args }
  326. exclude(p^.procdefinition^.proccalloptions,pocall_inline);
  327. end
  328. else
  329. { parameters not necessary anymore (JM) }
  330. p^.left := nil;
  331. { only if no proc var }
  332. if inlined or
  333. not(assigned(p^.right)) then
  334. is_con_or_destructor:=(p^.procdefinition^.proctypeoption in [potype_constructor,potype_destructor]);
  335. { proc variables destroy all registers }
  336. if (inlined or
  337. (p^.right=nil)) and
  338. { virtual methods too }
  339. not(po_virtualmethod in p^.procdefinition^.procoptions) then
  340. begin
  341. if (cs_check_io in aktlocalswitches) and
  342. (po_iocheck in p^.procdefinition^.procoptions) and
  343. not(po_iocheck in aktprocsym^.definition^.procoptions) then
  344. begin
  345. getaddrlabel(iolabel);
  346. emitlab(iolabel);
  347. end
  348. else
  349. iolabel:=nil;
  350. { save all used registers }
  351. pushusedregisters(pushed,pprocdef(p^.procdefinition)^.usedregisters);
  352. { give used registers through }
  353. usedinproc:=usedinproc or pprocdef(p^.procdefinition)^.usedregisters;
  354. end
  355. else
  356. begin
  357. pushusedregisters(pushed,$ff);
  358. usedinproc:=$ff;
  359. { no IO check for methods and procedure variables }
  360. iolabel:=nil;
  361. end;
  362. { generate the code for the parameter and push them }
  363. oldpushedparasize:=pushedparasize;
  364. pushedparasize:=0;
  365. pop_size:=0;
  366. { no inc esp for inlined procedure
  367. and for objects constructors PM }
  368. if (inlined or
  369. (p^.right=nil)) and
  370. (p^.procdefinition^.proctypeoption=potype_constructor) and
  371. { quick'n'dirty check if it is a class or an object }
  372. (p^.resulttype^.deftype=orddef) then
  373. pop_allowed:=false
  374. else
  375. pop_allowed:=true;
  376. if pop_allowed then
  377. begin
  378. { Old pushedsize aligned on 4 ? }
  379. i:=oldpushedparasize and 3;
  380. if i>0 then
  381. inc(pop_size,4-i);
  382. { This parasize aligned on 4 ? }
  383. i:=p^.procdefinition^.para_size(para_alignment) and 3;
  384. if i>0 then
  385. inc(pop_size,4-i);
  386. { insert the opcode and update pushedparasize }
  387. { never push 4 or more !! }
  388. pop_size:=pop_size mod 4;
  389. if pop_size>0 then
  390. begin
  391. inc(pushedparasize,pop_size);
  392. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  393. {$ifdef GDB}
  394. if (cs_debuginfo in aktmoduleswitches) and
  395. (exprasmlist^.first=exprasmlist^.last) then
  396. exprasmlist^.concat(new(pai_force_line,init));
  397. {$endif GDB}
  398. end;
  399. end;
  400. if pop_allowed and (cs_align in aktglobalswitches) then
  401. begin
  402. pop_esp:=true;
  403. push_size:=p^.procdefinition^.para_size(para_alignment);
  404. { !!!! here we have to take care of return type, self
  405. and nested procedures
  406. }
  407. inc(push_size,12);
  408. emit_reg_reg(A_MOV,S_L,R_ESP,R_EDI);
  409. if (push_size mod 8)=0 then
  410. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP)
  411. else
  412. begin
  413. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  414. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP);
  415. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  416. end;
  417. emit_reg(A_PUSH,S_L,R_EDI);
  418. end
  419. else
  420. pop_esp:=false;
  421. if (p^.resulttype<>pdef(voiddef)) and
  422. ret_in_param(p^.resulttype) then
  423. begin
  424. funcretref.symbol:=nil;
  425. {$ifdef test_dest_loc}
  426. if dest_loc_known and (dest_loc_tree=p) and
  427. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  428. begin
  429. funcretref:=dest_loc.reference;
  430. if assigned(dest_loc.reference.symbol) then
  431. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  432. in_dest_loc:=true;
  433. end
  434. else
  435. {$endif test_dest_loc}
  436. if inlined then
  437. begin
  438. reset_reference(funcretref);
  439. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.rettype.def^.size);
  440. funcretref.base:=procinfo^.framepointer;
  441. end
  442. else
  443. gettempofsizereference(p^.procdefinition^.rettype.def^.size,funcretref);
  444. end;
  445. if assigned(params) then
  446. begin
  447. { be found elsewhere }
  448. if inlined then
  449. para_offset:=pprocdef(p^.procdefinition)^.parast^.address_fixup+
  450. pprocdef(p^.procdefinition)^.parast^.datasize
  451. else
  452. para_offset:=0;
  453. if not(inlined) and
  454. assigned(p^.right) then
  455. secondcallparan(params,pparaitem(pabstractprocdef(p^.right^.resulttype)^.para^.first),
  456. (pocall_leftright in p^.procdefinition^.proccalloptions),inlined,
  457. (pocall_cdecl in p^.procdefinition^.proccalloptions),
  458. para_alignment,para_offset)
  459. else
  460. secondcallparan(params,pparaitem(p^.procdefinition^.para^.first),
  461. (pocall_leftright in p^.procdefinition^.proccalloptions),inlined,
  462. (pocall_cdecl in p^.procdefinition^.proccalloptions),
  463. para_alignment,para_offset);
  464. end;
  465. if inlined then
  466. inlinecode^.retoffset:=gettempofsizepersistant(4);
  467. if ret_in_param(p^.resulttype) then
  468. begin
  469. { This must not be counted for C code
  470. complex return address is removed from stack
  471. by function itself ! }
  472. {$ifdef OLD_C_STACK}
  473. inc(pushedparasize,4); { lets try without it PM }
  474. {$endif not OLD_C_STACK}
  475. if inlined then
  476. begin
  477. {$ifndef noAllocEdi}
  478. getexplicitregister32(R_EDI);
  479. {$endif noAllocEdi}
  480. emit_ref_reg(A_LEA,S_L,
  481. newreference(funcretref),R_EDI);
  482. r:=new_reference(procinfo^.framepointer,inlinecode^.retoffset);
  483. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  484. {$ifndef noAllocEdi}
  485. ungetregister32(R_EDI);
  486. {$endif noAllocEdi}
  487. end
  488. else
  489. emitpushreferenceaddr(funcretref);
  490. end;
  491. { procedure variable ? }
  492. if inlined or
  493. (p^.right=nil) then
  494. begin
  495. { overloaded operator have no symtable }
  496. { push self }
  497. if assigned(p^.symtable) and
  498. (p^.symtable^.symtabletype=withsymtable) then
  499. begin
  500. { dirty trick to avoid the secondcall below }
  501. p^.methodpointer:=genzeronode(callparan);
  502. p^.methodpointer^.location.loc:=LOC_REGISTER;
  503. {$ifndef noAllocEDI}
  504. getexplicitregister32(R_ESI);
  505. {$endif noAllocEDI}
  506. p^.methodpointer^.location.register:=R_ESI;
  507. { ARGHHH this is wrong !!!
  508. if we can init from base class for a child
  509. class that the wrong VMT will be
  510. transfered to constructor !! }
  511. p^.methodpointer^.resulttype:=
  512. ptree(pwithsymtable(p^.symtable)^.withnode)^.left^.resulttype;
  513. { change dispose type !! }
  514. p^.disposetyp:=dt_mbleft_and_method;
  515. { make a reference }
  516. new(r);
  517. reset_reference(r^);
  518. { if assigned(ptree(pwithsymtable(p^.symtable)^.withnode)^.pref) then
  519. begin
  520. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.pref^;
  521. end
  522. else
  523. begin
  524. r^.offset:=p^.symtable^.datasize;
  525. r^.base:=procinfo^.framepointer;
  526. end; }
  527. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^;
  528. if ((not ptree(pwithsymtable(p^.symtable)^.withnode)^.islocal) and
  529. (not pwithsymtable(p^.symtable)^.direct_with)) or
  530. pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  531. emit_ref_reg(A_MOV,S_L,r,R_ESI)
  532. else
  533. emit_ref_reg(A_LEA,S_L,r,R_ESI);
  534. end;
  535. { push self }
  536. if assigned(p^.symtable) and
  537. ((p^.symtable^.symtabletype=objectsymtable) or
  538. (p^.symtable^.symtabletype=withsymtable)) then
  539. begin
  540. if assigned(p^.methodpointer) then
  541. begin
  542. {
  543. if p^.methodpointer^.resulttype=classrefdef then
  544. begin
  545. two possibilities:
  546. 1. constructor
  547. 2. class method
  548. end
  549. else }
  550. begin
  551. case p^.methodpointer^.treetype of
  552. typen:
  553. begin
  554. { direct call to inherited method }
  555. if (po_abstractmethod in p^.procdefinition^.procoptions) then
  556. begin
  557. CGMessage(cg_e_cant_call_abstract_method);
  558. goto dont_call;
  559. end;
  560. { generate no virtual call }
  561. no_virtual_call:=true;
  562. if (sp_static in p^.symtableprocentry^.symoptions) then
  563. begin
  564. { well lets put the VMT address directly into ESI }
  565. { it is kind of dirty but that is the simplest }
  566. { way to accept virtual static functions (PM) }
  567. loadesi:=true;
  568. { if no VMT just use $0 bug0214 PM }
  569. {$ifndef noAllocEDI}
  570. getexplicitregister32(R_ESI);
  571. {$endif noAllocEDI}
  572. if not(oo_has_vmt in pobjectdef(p^.methodpointer^.resulttype)^.objectoptions) then
  573. emit_const_reg(A_MOV,S_L,0,R_ESI)
  574. else
  575. begin
  576. emit_sym_ofs_reg(A_MOV,S_L,
  577. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname),
  578. 0,R_ESI);
  579. end;
  580. { emit_reg(A_PUSH,S_L,R_ESI);
  581. this is done below !! }
  582. end
  583. else
  584. { this is a member call, so ESI isn't modfied }
  585. loadesi:=false;
  586. { a class destructor needs a flag }
  587. if pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  588. {assigned(aktprocsym) and
  589. (aktprocsym^.definition^.proctypeoption=potype_destructor)}
  590. (p^.procdefinition^.proctypeoption=potype_destructor) then
  591. begin
  592. push_int(0);
  593. emit_reg(A_PUSH,S_L,R_ESI);
  594. end;
  595. if not(is_con_or_destructor and
  596. pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  597. {assigned(aktprocsym) and
  598. (aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor])}
  599. (p^.procdefinition^.proctypeoption in [potype_constructor,potype_destructor])
  600. ) then
  601. emit_reg(A_PUSH,S_L,R_ESI);
  602. { if an inherited con- or destructor should be }
  603. { called in a con- or destructor then a warning }
  604. { will be made }
  605. { con- and destructors need a pointer to the vmt }
  606. if is_con_or_destructor and
  607. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) and
  608. assigned(aktprocsym) then
  609. begin
  610. if not(aktprocsym^.definition^.proctypeoption in
  611. [potype_constructor,potype_destructor]) then
  612. CGMessage(cg_w_member_cd_call_from_method);
  613. end;
  614. { class destructors get there flag above }
  615. { constructor flags ? }
  616. if is_con_or_destructor and
  617. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  618. assigned(aktprocsym) and
  619. (aktprocsym^.definition^.proctypeoption=potype_destructor)) then
  620. begin
  621. { a constructor needs also a flag }
  622. if pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  623. push_int(0);
  624. push_int(0);
  625. end;
  626. end;
  627. hnewn:
  628. begin
  629. { extended syntax of new }
  630. { ESI must be zero }
  631. {$ifndef noAllocEDI}
  632. getexplicitregister32(R_ESI);
  633. {$endif noAllocEDI}
  634. emit_reg_reg(A_XOR,S_L,R_ESI,R_ESI);
  635. emit_reg(A_PUSH,S_L,R_ESI);
  636. { insert the vmt }
  637. emit_sym(A_PUSH,S_L,
  638. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  639. extended_new:=true;
  640. end;
  641. hdisposen:
  642. begin
  643. secondpass(p^.methodpointer);
  644. { destructor with extended syntax called from dispose }
  645. { hdisposen always deliver LOC_REFERENCE }
  646. {$ifndef noAllocEDI}
  647. getexplicitregister32(R_ESI);
  648. {$endif noAllocEDI}
  649. emit_ref_reg(A_LEA,S_L,
  650. newreference(p^.methodpointer^.location.reference),R_ESI);
  651. del_reference(p^.methodpointer^.location.reference);
  652. emit_reg(A_PUSH,S_L,R_ESI);
  653. emit_sym(A_PUSH,S_L,
  654. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  655. end;
  656. else
  657. begin
  658. { call to an instance member }
  659. if (p^.symtable^.symtabletype<>withsymtable) then
  660. begin
  661. secondpass(p^.methodpointer);
  662. {$ifndef noAllocEDI}
  663. getexplicitregister32(R_ESI);
  664. {$endif noAllocEDI}
  665. case p^.methodpointer^.location.loc of
  666. LOC_CREGISTER,
  667. LOC_REGISTER:
  668. begin
  669. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  670. ungetregister32(p^.methodpointer^.location.register);
  671. end;
  672. else
  673. begin
  674. if (p^.methodpointer^.resulttype^.deftype=classrefdef) or
  675. ((p^.methodpointer^.resulttype^.deftype=objectdef) and
  676. pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  677. emit_ref_reg(A_MOV,S_L,
  678. newreference(p^.methodpointer^.location.reference),R_ESI)
  679. else
  680. emit_ref_reg(A_LEA,S_L,
  681. newreference(p^.methodpointer^.location.reference),R_ESI);
  682. del_reference(p^.methodpointer^.location.reference);
  683. end;
  684. end;
  685. end;
  686. { when calling a class method, we have to load ESI with the VMT !
  687. But, not for a class method via self }
  688. if not(po_containsself in p^.procdefinition^.procoptions) then
  689. begin
  690. if (po_classmethod in p^.procdefinition^.procoptions) and
  691. not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
  692. begin
  693. { class method needs current VMT }
  694. getexplicitregister32(R_ESI);
  695. new(r);
  696. reset_reference(r^);
  697. r^.base:=R_ESI;
  698. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  699. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  700. end;
  701. { direct call to destructor: remove data }
  702. if (p^.procdefinition^.proctypeoption=potype_destructor) and
  703. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  704. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  705. emit_const(A_PUSH,S_L,1);
  706. { direct call to class constructor, don't allocate memory }
  707. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  708. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  709. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  710. begin
  711. emit_const(A_PUSH,S_L,0);
  712. emit_const(A_PUSH,S_L,0);
  713. end
  714. else
  715. begin
  716. { constructor call via classreference => allocate memory }
  717. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  718. (p^.methodpointer^.resulttype^.deftype=classrefdef) and
  719. (pobjectdef(pclassrefdef(p^.methodpointer^.resulttype)^.
  720. pointertype.def)^.is_class) then
  721. emit_const(A_PUSH,S_L,1);
  722. emit_reg(A_PUSH,S_L,R_ESI);
  723. end;
  724. end;
  725. if is_con_or_destructor then
  726. begin
  727. { classes don't get a VMT pointer pushed }
  728. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  729. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  730. begin
  731. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  732. begin
  733. { it's no bad idea, to insert the VMT }
  734. emit_sym(A_PUSH,S_L,newasmsymbol(
  735. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  736. end
  737. { destructors haven't to dispose the instance, if this is }
  738. { a direct call }
  739. else
  740. push_int(0);
  741. end;
  742. end;
  743. end;
  744. end;
  745. end;
  746. end
  747. else
  748. begin
  749. if (po_classmethod in p^.procdefinition^.procoptions) and
  750. not(
  751. assigned(aktprocsym) and
  752. (po_classmethod in aktprocsym^.definition^.procoptions)
  753. ) then
  754. begin
  755. { class method needs current VMT }
  756. getexplicitregister32(R_ESI);
  757. new(r);
  758. reset_reference(r^);
  759. r^.base:=R_ESI;
  760. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  761. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  762. end
  763. else
  764. begin
  765. { member call, ESI isn't modified }
  766. loadesi:=false;
  767. end;
  768. { direct call to destructor: don't remove data! }
  769. if procinfo^._class^.is_class then
  770. begin
  771. if (p^.procdefinition^.proctypeoption=potype_destructor) then
  772. begin
  773. emit_const(A_PUSH,S_L,0);
  774. emit_reg(A_PUSH,S_L,R_ESI);
  775. end
  776. else if (p^.procdefinition^.proctypeoption=potype_constructor) then
  777. begin
  778. emit_const(A_PUSH,S_L,0);
  779. emit_const(A_PUSH,S_L,0);
  780. end
  781. else
  782. emit_reg(A_PUSH,S_L,R_ESI);
  783. end
  784. else
  785. begin
  786. emit_reg(A_PUSH,S_L,R_ESI);
  787. if is_con_or_destructor then
  788. begin
  789. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  790. begin
  791. { it's no bad idea, to insert the VMT }
  792. emit_sym(A_PUSH,S_L,newasmsymbol(
  793. procinfo^._class^.vmt_mangledname));
  794. end
  795. { destructors haven't to dispose the instance, if this is }
  796. { a direct call }
  797. else
  798. push_int(0);
  799. end;
  800. end;
  801. end;
  802. end;
  803. { push base pointer ?}
  804. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  805. ((pprocdef(p^.procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  806. begin
  807. { if we call a nested function in a method, we must }
  808. { push also SELF! }
  809. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  810. { access }
  811. {
  812. begin
  813. loadesi:=false;
  814. emit_reg(A_PUSH,S_L,R_ESI);
  815. end;
  816. }
  817. if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  818. begin
  819. new(r);
  820. reset_reference(r^);
  821. r^.offset:=procinfo^.framepointer_offset;
  822. r^.base:=procinfo^.framepointer;
  823. emit_ref(A_PUSH,S_L,r)
  824. end
  825. { this is only true if the difference is one !!
  826. but it cannot be more !! }
  827. else if (lexlevel=pprocdef(p^.procdefinition)^.parast^.symtablelevel-1) then
  828. begin
  829. emit_reg(A_PUSH,S_L,procinfo^.framepointer)
  830. end
  831. else if (lexlevel>pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  832. begin
  833. hregister:=getregister32;
  834. new(r);
  835. reset_reference(r^);
  836. r^.offset:=procinfo^.framepointer_offset;
  837. r^.base:=procinfo^.framepointer;
  838. emit_ref_reg(A_MOV,S_L,r,hregister);
  839. for i:=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  840. begin
  841. new(r);
  842. reset_reference(r^);
  843. {we should get the correct frame_pointer_offset at each level
  844. how can we do this !!! }
  845. r^.offset:=procinfo^.framepointer_offset;
  846. r^.base:=hregister;
  847. emit_ref_reg(A_MOV,S_L,r,hregister);
  848. end;
  849. emit_reg(A_PUSH,S_L,hregister);
  850. ungetregister32(hregister);
  851. end
  852. else
  853. internalerror(25000);
  854. end;
  855. if (po_virtualmethod in p^.procdefinition^.procoptions) and
  856. not(no_virtual_call) then
  857. begin
  858. { static functions contain the vmt_address in ESI }
  859. { also class methods }
  860. { Here it is quite tricky because it also depends }
  861. { on the methodpointer PM }
  862. getexplicitregister32(R_ESI);
  863. if assigned(aktprocsym) then
  864. begin
  865. if (((sp_static in aktprocsym^.symoptions) or
  866. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  867. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  868. or
  869. (po_staticmethod in p^.procdefinition^.procoptions) or
  870. ((p^.procdefinition^.proctypeoption=potype_constructor) and
  871. { esi contains the vmt if we call a constructor via a class ref }
  872. assigned(p^.methodpointer) and
  873. (p^.methodpointer^.resulttype^.deftype=classrefdef)
  874. ) or
  875. { ESI is loaded earlier }
  876. (po_classmethod in p^.procdefinition^.procoptions) then
  877. begin
  878. new(r);
  879. reset_reference(r^);
  880. r^.base:=R_ESI;
  881. end
  882. else
  883. begin
  884. new(r);
  885. reset_reference(r^);
  886. r^.base:=R_ESI;
  887. { this is one point where we need vmt_offset (PM) }
  888. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  889. {$ifndef noAllocEdi}
  890. getexplicitregister32(R_EDI);
  891. {$endif noAllocEdi}
  892. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  893. new(r);
  894. reset_reference(r^);
  895. r^.base:=R_EDI;
  896. end;
  897. end
  898. else
  899. { aktprocsym should be assigned, also in main program }
  900. internalerror(12345);
  901. {
  902. begin
  903. new(r);
  904. reset_reference(r^);
  905. r^.base:=R_ESI;
  906. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  907. new(r);
  908. reset_reference(r^);
  909. r^.base:=R_EDI;
  910. end;
  911. }
  912. if pprocdef(p^.procdefinition)^.extnumber=-1 then
  913. internalerror(44584);
  914. r^.offset:=pprocdef(p^.procdefinition)^._class^.vmtmethodoffset(pprocdef(p^.procdefinition)^.extnumber);
  915. if (cs_check_object_ext in aktlocalswitches) then
  916. begin
  917. emit_sym(A_PUSH,S_L,
  918. newasmsymbol(pprocdef(p^.procdefinition)^._class^.vmt_mangledname));
  919. emit_reg(A_PUSH,S_L,r^.base);
  920. emitcall('FPC_CHECK_OBJECT_EXT');
  921. end
  922. else if (cs_check_range in aktlocalswitches) then
  923. begin
  924. emit_reg(A_PUSH,S_L,r^.base);
  925. emitcall('FPC_CHECK_OBJECT');
  926. end;
  927. emit_ref(A_CALL,S_NO,r);
  928. {$ifndef noAllocEdi}
  929. ungetregister32(R_EDI);
  930. {$endif noAllocEdi}
  931. end
  932. else if not inlined then
  933. begin
  934. { We can call interrupts from within the smae code
  935. by just pushing the flags and CS PM }
  936. if (po_interrupt in p^.procdefinition^.procoptions) then
  937. begin
  938. emit_none(A_PUSHF,S_L);
  939. emit_reg(A_PUSH,S_L,R_CS);
  940. end;
  941. emitcall(pprocdef(p^.procdefinition)^.mangledname);
  942. end
  943. else { inlined proc }
  944. { inlined code is in inlinecode }
  945. begin
  946. { set poinline again }
  947. include(p^.procdefinition^.proccalloptions,pocall_inline);
  948. { process the inlinecode }
  949. secondpass(inlinecode);
  950. { free the args }
  951. if pprocdef(p^.procdefinition)^.parast^.datasize>0 then
  952. ungetpersistanttemp(pprocdef(p^.procdefinition)^.parast^.address_fixup);
  953. end;
  954. end
  955. else
  956. { now procedure variable case }
  957. begin
  958. secondpass(p^.right);
  959. if (po_interrupt in p^.procdefinition^.procoptions) then
  960. begin
  961. emit_none(A_PUSHF,S_L);
  962. emit_reg(A_PUSH,S_L,R_CS);
  963. end;
  964. { procedure of object? }
  965. if (po_methodpointer in p^.procdefinition^.procoptions) then
  966. begin
  967. { method pointer can't be in a register }
  968. hregister:=R_NO;
  969. { do some hacking if we call a method pointer }
  970. { which is a class member }
  971. { else ESI is overwritten ! }
  972. if (p^.right^.location.reference.base=R_ESI) or
  973. (p^.right^.location.reference.index=R_ESI) then
  974. begin
  975. del_reference(p^.right^.location.reference);
  976. {$ifndef noAllocEdi}
  977. getexplicitregister32(R_EDI);
  978. {$endif noAllocEdi}
  979. emit_ref_reg(A_MOV,S_L,
  980. newreference(p^.right^.location.reference),R_EDI);
  981. hregister:=R_EDI;
  982. end;
  983. { load self, but not if it's already explicitly pushed }
  984. if not(po_containsself in p^.procdefinition^.procoptions) then
  985. begin
  986. { load ESI }
  987. inc(p^.right^.location.reference.offset,4);
  988. getexplicitregister32(R_ESI);
  989. emit_ref_reg(A_MOV,S_L,
  990. newreference(p^.right^.location.reference),R_ESI);
  991. dec(p^.right^.location.reference.offset,4);
  992. { push self pointer }
  993. emit_reg(A_PUSH,S_L,R_ESI);
  994. end;
  995. if hregister=R_NO then
  996. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))
  997. else
  998. begin
  999. {$ifndef noAllocEdi}
  1000. ungetregister32(hregister);
  1001. {$else noAllocEdi}
  1002. { the same code, the previous line is just to }
  1003. { indicate EDI actually is deallocated if allocated }
  1004. { above (JM) }
  1005. ungetregister32(hregister);
  1006. {$endif noAllocEdi}
  1007. emit_reg(A_CALL,S_NO,hregister);
  1008. end;
  1009. del_reference(p^.right^.location.reference);
  1010. end
  1011. else
  1012. begin
  1013. case p^.right^.location.loc of
  1014. LOC_REGISTER,LOC_CREGISTER:
  1015. begin
  1016. emit_reg(A_CALL,S_NO,p^.right^.location.register);
  1017. ungetregister32(p^.right^.location.register);
  1018. end
  1019. else
  1020. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference));
  1021. del_reference(p^.right^.location.reference);
  1022. end;
  1023. end;
  1024. end;
  1025. { this was only for normal functions
  1026. displaced here so we also get
  1027. it to work for procvars PM }
  1028. if (not inlined) and (pocall_clearstack in p^.procdefinition^.proccalloptions) then
  1029. begin
  1030. { we also add the pop_size which is included in pushedparasize }
  1031. pop_size:=0;
  1032. { better than an add on all processors }
  1033. if pushedparasize=4 then
  1034. begin
  1035. {$ifndef noAllocEdi}
  1036. getexplicitregister32(R_EDI);
  1037. {$endif noAllocEdi}
  1038. emit_reg(A_POP,S_L,R_EDI);
  1039. {$ifndef noAllocEdi}
  1040. ungetregister32(R_EDI);
  1041. {$endif noAllocEdi}
  1042. end
  1043. { the pentium has two pipes and pop reg is pairable }
  1044. { but the registers must be different! }
  1045. else if (pushedparasize=8) and
  1046. not(cs_littlesize in aktglobalswitches) and
  1047. (aktoptprocessor=ClassP5) and
  1048. (procinfo^._class=nil) then
  1049. begin
  1050. {$ifndef noAllocEdi}
  1051. getexplicitregister32(R_EDI);
  1052. {$endif noAllocEdi}
  1053. emit_reg(A_POP,S_L,R_EDI);
  1054. {$ifndef noAllocEdi}
  1055. ungetregister32(R_EDI);
  1056. {$endif noAllocEdi}
  1057. {$ifndef noAllocEdi}
  1058. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  1059. {$endif noAllocEdi}
  1060. emit_reg(A_POP,S_L,R_ESI);
  1061. {$ifndef noAllocEdi}
  1062. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  1063. {$endif noAllocEdi}
  1064. end
  1065. else if pushedparasize<>0 then
  1066. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  1067. end;
  1068. if pop_esp then
  1069. emit_reg(A_POP,S_L,R_ESP);
  1070. dont_call:
  1071. pushedparasize:=oldpushedparasize;
  1072. unused:=unusedregisters;
  1073. usablereg32:=usablecount;
  1074. {$ifdef TEMPREGDEBUG}
  1075. testregisters32;
  1076. {$endif TEMPREGDEBUG}
  1077. { a constructor could be a function with boolean result }
  1078. { if calling constructor called fail we
  1079. must jump directly to quickexitlabel PM
  1080. but only if it is a call of an inherited constructor }
  1081. if (inlined or
  1082. (p^.right=nil)) and
  1083. (p^.procdefinition^.proctypeoption=potype_constructor) and
  1084. assigned(p^.methodpointer) and
  1085. (p^.methodpointer^.treetype=typen) and
  1086. (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1087. begin
  1088. emitjmp(C_Z,faillabel);
  1089. end;
  1090. { handle function results }
  1091. { structured results are easy to handle.... }
  1092. { needed also when result_no_used !! }
  1093. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  1094. begin
  1095. p^.location.loc:=LOC_MEM;
  1096. p^.location.reference.symbol:=nil;
  1097. p^.location.reference:=funcretref;
  1098. end;
  1099. { we have only to handle the result if it is used, but }
  1100. { ansi/widestrings must be registered, so we can dispose them }
  1101. if (p^.resulttype<>pdef(voiddef)) and (p^.return_value_used or
  1102. is_ansistring(p^.resulttype) or is_widestring(p^.resulttype)) then
  1103. begin
  1104. { a contructor could be a function with boolean result }
  1105. if (inlined or
  1106. (p^.right=nil)) and
  1107. (p^.procdefinition^.proctypeoption=potype_constructor) and
  1108. { quick'n'dirty check if it is a class or an object }
  1109. (p^.resulttype^.deftype=orddef) then
  1110. begin
  1111. { this fails if popsize > 0 PM }
  1112. p^.location.loc:=LOC_FLAGS;
  1113. p^.location.resflags:=F_NE;
  1114. if extended_new then
  1115. begin
  1116. {$ifdef test_dest_loc}
  1117. if dest_loc_known and (dest_loc_tree=p) then
  1118. mov_reg_to_dest(p,S_L,R_EAX)
  1119. else
  1120. {$endif test_dest_loc}
  1121. begin
  1122. hregister:=getexplicitregister32(R_EAX);
  1123. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1124. p^.location.register:=hregister;
  1125. end;
  1126. end;
  1127. end
  1128. { structed results are easy to handle.... }
  1129. else if ret_in_param(p^.resulttype) then
  1130. begin
  1131. {p^.location.loc:=LOC_MEM;
  1132. stringdispose(p^.location.reference.symbol);
  1133. p^.location.reference:=funcretref;
  1134. already done above (PM) }
  1135. end
  1136. else
  1137. begin
  1138. if (p^.resulttype^.deftype in [orddef,enumdef]) then
  1139. begin
  1140. p^.location.loc:=LOC_REGISTER;
  1141. case p^.resulttype^.size of
  1142. 4 :
  1143. begin
  1144. {$ifdef test_dest_loc}
  1145. if dest_loc_known and (dest_loc_tree=p) then
  1146. mov_reg_to_dest(p,S_L,R_EAX)
  1147. else
  1148. {$endif test_dest_loc}
  1149. begin
  1150. hregister:=getexplicitregister32(R_EAX);
  1151. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1152. p^.location.register:=hregister;
  1153. end;
  1154. end;
  1155. 1 :
  1156. begin
  1157. {$ifdef test_dest_loc}
  1158. if dest_loc_known and (dest_loc_tree=p) then
  1159. mov_reg_to_dest(p,S_B,R_AL)
  1160. else
  1161. {$endif test_dest_loc}
  1162. begin
  1163. hregister:=getexplicitregister32(R_EAX);
  1164. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1165. p^.location.register:=reg32toreg8(hregister);
  1166. end;
  1167. end;
  1168. 2 :
  1169. begin
  1170. {$ifdef test_dest_loc}
  1171. if dest_loc_known and (dest_loc_tree=p) then
  1172. mov_reg_to_dest(p,S_W,R_AX)
  1173. else
  1174. {$endif test_dest_loc}
  1175. begin
  1176. hregister:=getexplicitregister32(R_EAX);
  1177. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1178. p^.location.register:=reg32toreg16(hregister);
  1179. end;
  1180. end;
  1181. 8 :
  1182. begin
  1183. {$ifdef test_dest_loc}
  1184. {$error Don't know what to do here}
  1185. {$endif test_dest_loc}
  1186. hregister:=getexplicitregister32(R_EAX);
  1187. hregister2:=getexplicitregister32(R_EDX);
  1188. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1189. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  1190. p^.location.registerlow:=hregister;
  1191. p^.location.registerhigh:=hregister2;
  1192. end;
  1193. else internalerror(7);
  1194. end
  1195. end
  1196. else if (p^.resulttype^.deftype=floatdef) then
  1197. case pfloatdef(p^.resulttype)^.typ of
  1198. f32bit:
  1199. begin
  1200. p^.location.loc:=LOC_REGISTER;
  1201. {$ifdef test_dest_loc}
  1202. if dest_loc_known and (dest_loc_tree=p) then
  1203. mov_reg_to_dest(p,S_L,R_EAX)
  1204. else
  1205. {$endif test_dest_loc}
  1206. begin
  1207. hregister:=getexplicitregister32(R_EAX);
  1208. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1209. p^.location.register:=hregister;
  1210. end;
  1211. end;
  1212. else
  1213. begin
  1214. p^.location.loc:=LOC_FPU;
  1215. inc(fpuvaroffset);
  1216. end;
  1217. end
  1218. else if is_ansistring(p^.resulttype) or
  1219. is_widestring(p^.resulttype) then
  1220. begin
  1221. hregister:=getexplicitregister32(R_EAX);
  1222. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1223. gettempansistringreference(hr);
  1224. decrstringref(p^.resulttype,hr);
  1225. emit_reg_ref(A_MOV,S_L,hregister,
  1226. newreference(hr));
  1227. ungetregister32(hregister);
  1228. p^.location.loc:=LOC_MEM;
  1229. p^.location.reference:=hr;
  1230. end
  1231. else
  1232. begin
  1233. p^.location.loc:=LOC_REGISTER;
  1234. {$ifdef test_dest_loc}
  1235. if dest_loc_known and (dest_loc_tree=p) then
  1236. mov_reg_to_dest(p,S_L,R_EAX)
  1237. else
  1238. {$endif test_dest_loc}
  1239. begin
  1240. hregister:=getexplicitregister32(R_EAX);
  1241. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1242. p^.location.register:=hregister;
  1243. end;
  1244. end;
  1245. end;
  1246. end;
  1247. { perhaps i/o check ? }
  1248. if iolabel<>nil then
  1249. begin
  1250. emit_sym(A_PUSH,S_L,iolabel);
  1251. emitcall('FPC_IOCHECK');
  1252. end;
  1253. if pop_size>0 then
  1254. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1255. { restore registers }
  1256. popusedregisters(pushed);
  1257. { at last, restore instance pointer (SELF) }
  1258. if loadesi then
  1259. maybe_loadesi;
  1260. pp:=params;
  1261. while assigned(pp) do
  1262. begin
  1263. if assigned(pp^.left) then
  1264. begin
  1265. if (pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1266. ungetiftemp(pp^.left^.location.reference);
  1267. { process also all nodes of an array of const }
  1268. if pp^.left^.treetype=arrayconstructn then
  1269. begin
  1270. if assigned(pp^.left^.left) then
  1271. begin
  1272. hp:=pp^.left;
  1273. while assigned(hp) do
  1274. begin
  1275. if (hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1276. ungetiftemp(hp^.left^.location.reference);
  1277. hp:=hp^.right;
  1278. end;
  1279. end;
  1280. end;
  1281. end;
  1282. pp:=pp^.right;
  1283. end;
  1284. if inlined then
  1285. ungetpersistanttemp(inlinecode^.retoffset);
  1286. if assigned(inlinecode) then
  1287. disposetree(inlinecode);
  1288. disposetree(params);
  1289. { from now on the result can be freed normally }
  1290. if inlined and ret_in_param(p^.resulttype) then
  1291. persistanttemptonormal(funcretref.offset);
  1292. { if return value is not used }
  1293. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1294. begin
  1295. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1296. begin
  1297. { data which must be finalized ? }
  1298. if (p^.resulttype^.needs_inittable) and
  1299. ( (p^.resulttype^.deftype<>objectdef) or
  1300. not(pobjectdef(p^.resulttype)^.is_class)) then
  1301. finalize(p^.resulttype,p^.location.reference,false);
  1302. { release unused temp }
  1303. ungetiftemp(p^.location.reference)
  1304. end
  1305. else if p^.location.loc=LOC_FPU then
  1306. begin
  1307. { release FPU stack }
  1308. emit_reg(A_FSTP,S_NO,R_ST0);
  1309. {
  1310. dec(fpuvaroffset);
  1311. do NOT decrement as the increment before
  1312. is not called for unused results PM }
  1313. end;
  1314. end;
  1315. end;
  1316. {*****************************************************************************
  1317. SecondProcInlineN
  1318. *****************************************************************************}
  1319. procedure secondprocinline(var p : ptree);
  1320. var st : psymtable;
  1321. oldprocsym : pprocsym;
  1322. para_size, i : longint;
  1323. tmpreg: tregister;
  1324. oldprocinfo : pprocinfo;
  1325. oldinlining_procedure,
  1326. nostackframe,make_global : boolean;
  1327. proc_names : tstringcontainer;
  1328. inlineentrycode,inlineexitcode : paasmoutput;
  1329. oldexitlabel,oldexit2label,oldquickexitlabel:Pasmlabel;
  1330. oldunused,oldusableregs : tregisterset;
  1331. oldc_usableregs : longint;
  1332. oldreg_pushes : regvar_longintarray;
  1333. oldis_reg_var : regvar_booleanarray;
  1334. {$ifdef TEMPREGDEBUG}
  1335. oldreg_user : regvar_ptreearray;
  1336. oldreg_releaser : regvar_ptreearray;
  1337. {$endif TEMPREGDEBUG}
  1338. {$ifdef GDB}
  1339. startlabel,endlabel : pasmlabel;
  1340. pp : pchar;
  1341. mangled_length : longint;
  1342. {$endif GDB}
  1343. begin
  1344. { deallocate the registers used for the current procedure's regvars }
  1345. if assigned(aktprocsym^.definition^.regvarinfo) then
  1346. begin
  1347. with pregvarinfo(aktprocsym^.definition^.regvarinfo)^ do
  1348. for i := 1 to maxvarregs do
  1349. if assigned(regvars[i]) then
  1350. begin
  1351. case regsize(regvars[i]^.reg) of
  1352. S_B: tmpreg := reg8toreg32(regvars[i]^.reg);
  1353. S_W: tmpreg := reg16toreg32(regvars[i]^.reg);
  1354. S_L: tmpreg := regvars[i]^.reg;
  1355. end;
  1356. exprasmlist^.concat(new(pairegalloc,dealloc(tmpreg)));
  1357. end;
  1358. oldunused := unused;
  1359. oldusableregs := usableregs;
  1360. oldc_usableregs := c_usableregs;
  1361. oldreg_pushes := reg_pushes;
  1362. oldis_reg_var := is_reg_var;
  1363. {$ifdef TEMPREGDEBUG}
  1364. oldreg_user := reg_user;
  1365. oldreg_releaser := reg_releaser;
  1366. {$endif TEMPREGDEBUG}
  1367. { make sure the register allocator knows what the regvars in the }
  1368. { inlined code block are (JM) }
  1369. resetusableregisters;
  1370. clearregistercount;
  1371. cleartempgen;
  1372. if assigned(p^.inlineprocsym^.definition^.regvarinfo) then
  1373. with pregvarinfo(p^.inlineprocsym^.definition^.regvarinfo)^ do
  1374. for i := 1 to maxvarregs do
  1375. if assigned(regvars[i]) then
  1376. begin
  1377. case regsize(regvars[i]^.reg) of
  1378. S_B: tmpreg := reg8toreg32(regvars[i]^.reg);
  1379. S_W: tmpreg := reg16toreg32(regvars[i]^.reg);
  1380. S_L: tmpreg := regvars[i]^.reg;
  1381. end;
  1382. usableregs:=usableregs-[tmpreg];
  1383. is_reg_var[tmpreg]:=true;
  1384. dec(c_usableregs);
  1385. end;
  1386. end;
  1387. oldinlining_procedure:=inlining_procedure;
  1388. oldexitlabel:=aktexitlabel;
  1389. oldexit2label:=aktexit2label;
  1390. oldquickexitlabel:=quickexitlabel;
  1391. getlabel(aktexitlabel);
  1392. getlabel(aktexit2label);
  1393. oldprocsym:=aktprocsym;
  1394. { we're inlining a procedure }
  1395. inlining_procedure:=true;
  1396. { save old procinfo }
  1397. getmem(oldprocinfo,sizeof(tprocinfo));
  1398. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1399. { set the return value }
  1400. aktprocsym:=p^.inlineprocsym;
  1401. procinfo^.returntype:=aktprocsym^.definition^.rettype;
  1402. procinfo^.return_offset:=p^.retoffset;
  1403. procinfo^.para_offset:=p^.para_offset;
  1404. { arg space has been filled by the parent secondcall }
  1405. st:=aktprocsym^.definition^.localst;
  1406. { set it to the same lexical level }
  1407. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1408. if st^.datasize>0 then
  1409. begin
  1410. st^.address_fixup:=gettempofsizepersistant(st^.datasize)+st^.datasize;
  1411. {$ifdef extdebug}
  1412. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1413. exprasmlist^.concat(new(pai_asm_comment,init(strpnew(
  1414. 'local symtable is at offset '+tostr(st^.address_fixup)))));
  1415. {$endif extdebug}
  1416. end;
  1417. exprasmlist^.concat(new(Pai_Marker, Init(InlineStart)));
  1418. {$ifdef extdebug}
  1419. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('Start of inlined proc'))));
  1420. {$endif extdebug}
  1421. {$ifdef GDB}
  1422. if (cs_debuginfo in aktmoduleswitches) then
  1423. begin
  1424. getaddrlabel(startlabel);
  1425. getaddrlabel(endlabel);
  1426. emitlab(startlabel);
  1427. p^.inlineprocsym^.definition^.localst^.symtabletype:=inlinelocalsymtable;
  1428. p^.inlineprocsym^.definition^.parast^.symtabletype:=inlineparasymtable;
  1429. { Here we must include the para and local symtable info }
  1430. p^.inlineprocsym^.concatstabto(withdebuglist);
  1431. { set it back for savety }
  1432. p^.inlineprocsym^.definition^.localst^.symtabletype:=localsymtable;
  1433. p^.inlineprocsym^.definition^.parast^.symtabletype:=parasymtable;
  1434. mangled_length:=length(oldprocsym^.definition^.mangledname);
  1435. getmem(pp,mangled_length+50);
  1436. strpcopy(pp,'192,0,0,'+startlabel^.name);
  1437. if (target_os.use_function_relative_addresses) then
  1438. begin
  1439. strpcopy(strend(pp),'-');
  1440. strpcopy(strend(pp),oldprocsym^.definition^.mangledname);
  1441. end;
  1442. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  1443. end;
  1444. {$endif GDB}
  1445. { takes care of local data initialization }
  1446. inlineentrycode:=new(paasmoutput,init);
  1447. inlineexitcode:=new(paasmoutput,init);
  1448. proc_names.init;
  1449. para_size:=p^.para_size;
  1450. make_global:=false; { to avoid warning }
  1451. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  1452. exprasmlist^.concatlist(inlineentrycode);
  1453. secondpass(p^.inlinetree);
  1454. genexitcode(inlineexitcode,0,false,true);
  1455. exprasmlist^.concatlist(inlineexitcode);
  1456. dispose(inlineentrycode,done);
  1457. dispose(inlineexitcode,done);
  1458. {$ifdef extdebug}
  1459. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('End of inlined proc'))));
  1460. {$endif extdebug}
  1461. exprasmlist^.concat(new(Pai_Marker, Init(InlineEnd)));
  1462. {we can free the local data now, reset also the fixup address }
  1463. if st^.datasize>0 then
  1464. begin
  1465. ungetpersistanttemp(st^.address_fixup-st^.datasize);
  1466. st^.address_fixup:=0;
  1467. end;
  1468. { restore procinfo }
  1469. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1470. freemem(oldprocinfo,sizeof(tprocinfo));
  1471. {$ifdef GDB}
  1472. if (cs_debuginfo in aktmoduleswitches) then
  1473. begin
  1474. emitlab(endlabel);
  1475. strpcopy(pp,'224,0,0,'+endlabel^.name);
  1476. if (target_os.use_function_relative_addresses) then
  1477. begin
  1478. strpcopy(strend(pp),'-');
  1479. strpcopy(strend(pp),oldprocsym^.definition^.mangledname);
  1480. end;
  1481. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  1482. freemem(pp,mangled_length+50);
  1483. end;
  1484. {$endif GDB}
  1485. { restore }
  1486. aktprocsym:=oldprocsym;
  1487. aktexitlabel:=oldexitlabel;
  1488. aktexit2label:=oldexit2label;
  1489. quickexitlabel:=oldquickexitlabel;
  1490. inlining_procedure:=oldinlining_procedure;
  1491. { reallocate the registers used for the current procedure's regvars, }
  1492. { since they may have been used and then deallocated in the inlined }
  1493. { procedure (JM) }
  1494. if assigned(aktprocsym^.definition^.regvarinfo) then
  1495. begin
  1496. with pregvarinfo(aktprocsym^.definition^.regvarinfo)^ do
  1497. for i := 1 to maxvarregs do
  1498. if assigned(regvars[i]) then
  1499. begin
  1500. case regsize(regvars[i]^.reg) of
  1501. S_B: tmpreg := reg8toreg32(regvars[i]^.reg);
  1502. S_W: tmpreg := reg16toreg32(regvars[i]^.reg);
  1503. S_L: tmpreg := regvars[i]^.reg;
  1504. end;
  1505. exprasmlist^.concat(new(pairegalloc,alloc(tmpreg)));
  1506. end;
  1507. oldunused := oldunused;
  1508. oldusableregs := oldusableregs;
  1509. oldc_usableregs := oldc_usableregs;
  1510. oldreg_pushes := oldreg_pushes;
  1511. oldis_reg_var := oldis_reg_var;
  1512. {$ifdef TEMPREGDEBUG}
  1513. oldreg_user := oldreg_user;
  1514. oldreg_releaser := oldreg_releaser;
  1515. {$endif TEMPREGDEBUG}
  1516. end;
  1517. end;
  1518. end.
  1519. {
  1520. $Log$
  1521. Revision 1.10 2000-09-19 23:09:07 pierre
  1522. * problems wih extdebug cond. solved
  1523. Revision 1.9 2000/09/16 12:21:56 peter
  1524. * fixed for with and local object loading
  1525. Revision 1.8 2000/09/10 20:18:06 peter
  1526. * fixed open array with cdecl
  1527. * fixed finalize call with unused function return
  1528. Revision 1.7 2000/08/03 14:27:04 jonas
  1529. * save/reset/restore regvar info around inlined code
  1530. Revision 1.5 2000/07/27 13:03:35 jonas
  1531. * release alignopts
  1532. Revision 1.4 2000/07/21 15:14:01 jonas
  1533. + added is_addr field for labels, if they are only used for getting the address
  1534. (e.g. for io checks) and corresponding getaddrlabel() procedure
  1535. Revision 1.3 2000/07/13 12:08:24 michael
  1536. + patched to 1.1.0 with former 1.09patch from peter
  1537. Revision 1.2 2000/07/13 11:32:32 michael
  1538. + removed logs
  1539. }