n386cal.pas 72 KB

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