n386cal.pas 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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,verbose,globals,
  45. symconst,symbase,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 : tasmlabel;
  72. { temporary variables: }
  73. tempdeftype : tdeftype;
  74. r : preference;
  75. begin
  76. { set default para_alignment to target_info.stackalignment }
  77. if para_alignment=0 then
  78. para_alignment:=target_info.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.def) 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.def.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.def) 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 : tasmlabel;
  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 : tasmlabel;
  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_info.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. tprocdef(procdefinition).parast.symtablelevel:=aktprocsym.definition.localst.symtablelevel;
  277. if assigned(params) then
  278. inlinecode.para_offset:=gettempofsizepersistant(inlinecode.para_size);
  279. tprocdef(procdefinition).parast.address_fixup:=inlinecode.para_offset;
  280. {$ifdef extdebug}
  281. Comment(V_debug,
  282. 'inlined parasymtable is at offset '
  283. +tostr(tprocdef(procdefinition).parast.address_fixup));
  284. exprasmList.concat(Tai_asm_comment.Create(
  285. strpnew('inlined parasymtable is at offset '
  286. +tostr(tprocdef(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 := tprocdef(procdefinition).usedregisters;
  313. pushusedregisters(pushed,regs_to_push);
  314. { give used registers through }
  315. usedinproc:=usedinproc or tprocdef(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.def.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 (not is_void(resulttype.def)) and
  387. ret_in_param(resulttype.def) 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:=tprocdef(procdefinition).parast.address_fixup+
  415. tprocdef(procdefinition).parast.datasize
  416. else
  417. para_offset:=0;
  418. if not(inlined) and
  419. assigned(right) then
  420. tcallparanode(params).secondcallparan(TParaItem(tabstractprocdef(right.resulttype.def).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.def) 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. getexplicitregister32(R_EDI);
  443. emit_ref_reg(A_LEA,S_L,
  444. newreference(funcretref),R_EDI);
  445. r:=new_reference(procinfo^.framepointer,inlinecode.retoffset);
  446. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  447. ungetregister32(R_EDI);
  448. end
  449. else
  450. emitpushreferenceaddr(funcretref);
  451. end;
  452. { procedure variable ? }
  453. if inlined or
  454. (right=nil) then
  455. begin
  456. { overloaded operator have no symtable }
  457. { push self }
  458. if assigned(symtableproc) and
  459. (symtableproc.symtabletype=withsymtable) then
  460. begin
  461. { dirty trick to avoid the secondcall below }
  462. methodpointer:=ccallparanode.create(nil,nil);
  463. methodpointer.location.loc:=LOC_REGISTER;
  464. getexplicitregister32(R_ESI);
  465. methodpointer.location.register:=R_ESI;
  466. { ARGHHH this is wrong !!!
  467. if we can init from base class for a child
  468. class that the wrong VMT will be
  469. transfered to constructor !! }
  470. methodpointer.resulttype:=
  471. twithnode(twithsymtable(symtableproc).withnode).left.resulttype;
  472. { make a reference }
  473. new(r);
  474. reset_reference(r^);
  475. { if assigned(ptree(twithsymtable(symtable).withnode)^.pref) then
  476. begin
  477. r^:=ptree(twithsymtable(symtable).withnode)^.pref^;
  478. end
  479. else
  480. begin
  481. r^.offset:=symtable.datasize;
  482. r^.base:=procinfo^.framepointer;
  483. end; }
  484. r^:=twithnode(twithsymtable(symtableproc).withnode).withreference^;
  485. if ((not(nf_islocal in twithnode(twithsymtable(symtableproc).withnode).flags)) and
  486. (not twithsymtable(symtableproc).direct_with)) or
  487. is_class_or_interface(methodpointer.resulttype.def) then
  488. emit_ref_reg(A_MOV,S_L,r,R_ESI)
  489. else
  490. emit_ref_reg(A_LEA,S_L,r,R_ESI);
  491. end;
  492. { push self }
  493. if assigned(symtableproc) and
  494. ((symtableproc.symtabletype=objectsymtable) or
  495. (symtableproc.symtabletype=withsymtable)) then
  496. begin
  497. if assigned(methodpointer) then
  498. begin
  499. {
  500. if methodpointer^.resulttype.def=classrefdef then
  501. begin
  502. two possibilities:
  503. 1. constructor
  504. 2. class method
  505. end
  506. else }
  507. begin
  508. case methodpointer.nodetype of
  509. typen:
  510. begin
  511. { direct call to inherited method }
  512. if (po_abstractmethod in procdefinition.procoptions) then
  513. begin
  514. CGMessage(cg_e_cant_call_abstract_method);
  515. goto dont_call;
  516. end;
  517. { generate no virtual call }
  518. no_virtual_call:=true;
  519. if (sp_static in symtableprocentry.symoptions) then
  520. begin
  521. { well lets put the VMT address directly into ESI }
  522. { it is kind of dirty but that is the simplest }
  523. { way to accept virtual static functions (PM) }
  524. loadesi:=true;
  525. { if no VMT just use $0 bug0214 PM }
  526. getexplicitregister32(R_ESI);
  527. if not(oo_has_vmt in tobjectdef(methodpointer.resulttype.def).objectoptions) then
  528. emit_const_reg(A_MOV,S_L,0,R_ESI)
  529. else
  530. begin
  531. emit_sym_ofs_reg(A_MOV,S_L,
  532. newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),
  533. 0,R_ESI);
  534. end;
  535. { emit_reg(A_PUSH,S_L,R_ESI);
  536. this is done below !! }
  537. end
  538. else
  539. { this is a member call, so ESI isn't modfied }
  540. loadesi:=false;
  541. { a class destructor needs a flag }
  542. if is_class(tobjectdef(methodpointer.resulttype.def)) and
  543. {assigned(aktprocsym) and
  544. (aktprocsym.definition.proctypeoption=potype_destructor)}
  545. (procdefinition.proctypeoption=potype_destructor) then
  546. begin
  547. push_int(0);
  548. emit_reg(A_PUSH,S_L,R_ESI);
  549. end;
  550. if not(is_con_or_destructor and
  551. is_class(methodpointer.resulttype.def) and
  552. {assigned(aktprocsym) and
  553. (aktprocsym.definition.proctypeoption in [potype_constructor,potype_destructor])}
  554. (procdefinition.proctypeoption in [potype_constructor,potype_destructor])
  555. ) then
  556. emit_reg(A_PUSH,S_L,R_ESI);
  557. { if an inherited con- or destructor should be }
  558. { called in a con- or destructor then a warning }
  559. { will be made }
  560. { con- and destructors need a pointer to the vmt }
  561. if is_con_or_destructor and
  562. is_object(methodpointer.resulttype.def) and
  563. assigned(aktprocsym) then
  564. begin
  565. if not(aktprocsym.definition.proctypeoption in
  566. [potype_constructor,potype_destructor]) then
  567. CGMessage(cg_w_member_cd_call_from_method);
  568. end;
  569. { class destructors get there flag above }
  570. { constructor flags ? }
  571. if is_con_or_destructor and
  572. not(
  573. is_class(methodpointer.resulttype.def) and
  574. assigned(aktprocsym) and
  575. (aktprocsym.definition.proctypeoption=potype_destructor)) then
  576. begin
  577. { a constructor needs also a flag }
  578. if is_class(methodpointer.resulttype.def) then
  579. push_int(0);
  580. push_int(0);
  581. end;
  582. end;
  583. hnewn:
  584. begin
  585. { extended syntax of new }
  586. { ESI must be zero }
  587. getexplicitregister32(R_ESI);
  588. emit_reg_reg(A_XOR,S_L,R_ESI,R_ESI);
  589. emit_reg(A_PUSH,S_L,R_ESI);
  590. { insert the vmt }
  591. emit_sym(A_PUSH,S_L,
  592. newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname));
  593. extended_new:=true;
  594. end;
  595. hdisposen:
  596. begin
  597. secondpass(methodpointer);
  598. { destructor with extended syntax called from dispose }
  599. { hdisposen always deliver LOC_REFERENCE }
  600. getexplicitregister32(R_ESI);
  601. emit_ref_reg(A_LEA,S_L,
  602. newreference(methodpointer.location.reference),R_ESI);
  603. del_reference(methodpointer.location.reference);
  604. emit_reg(A_PUSH,S_L,R_ESI);
  605. emit_sym(A_PUSH,S_L,
  606. newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname));
  607. end;
  608. else
  609. begin
  610. { call to an instance member }
  611. if (symtableproc.symtabletype<>withsymtable) then
  612. begin
  613. secondpass(methodpointer);
  614. getexplicitregister32(R_ESI);
  615. case methodpointer.location.loc of
  616. LOC_CREGISTER,
  617. LOC_REGISTER:
  618. begin
  619. emit_reg_reg(A_MOV,S_L,methodpointer.location.register,R_ESI);
  620. ungetregister32(methodpointer.location.register);
  621. end;
  622. else
  623. begin
  624. if (methodpointer.resulttype.def.deftype=classrefdef) or
  625. is_class_or_interface(methodpointer.resulttype.def) then
  626. emit_ref_reg(A_MOV,S_L,
  627. newreference(methodpointer.location.reference),R_ESI)
  628. else
  629. emit_ref_reg(A_LEA,S_L,
  630. newreference(methodpointer.location.reference),R_ESI);
  631. del_reference(methodpointer.location.reference);
  632. end;
  633. end;
  634. end;
  635. { when calling a class method, we have to load ESI with the VMT !
  636. But, not for a class method via self }
  637. if not(po_containsself in procdefinition.procoptions) then
  638. begin
  639. if (po_classmethod in procdefinition.procoptions) and
  640. not(methodpointer.resulttype.def.deftype=classrefdef) then
  641. begin
  642. { class method needs current VMT }
  643. getexplicitregister32(R_ESI);
  644. new(r);
  645. reset_reference(r^);
  646. r^.base:=R_ESI;
  647. r^.offset:= tprocdef(procdefinition)._class.vmt_offset;
  648. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  649. end;
  650. { direct call to destructor: remove data }
  651. if (procdefinition.proctypeoption=potype_destructor) and
  652. is_class(methodpointer.resulttype.def) then
  653. emit_const(A_PUSH,S_L,1);
  654. { direct call to class constructor, don't allocate memory }
  655. if (procdefinition.proctypeoption=potype_constructor) and
  656. is_class(methodpointer.resulttype.def) then
  657. begin
  658. emit_const(A_PUSH,S_L,0);
  659. emit_const(A_PUSH,S_L,0);
  660. end
  661. else
  662. begin
  663. { constructor call via classreference => allocate memory }
  664. if (procdefinition.proctypeoption=potype_constructor) and
  665. (methodpointer.resulttype.def.deftype=classrefdef) and
  666. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  667. emit_const(A_PUSH,S_L,1);
  668. emit_reg(A_PUSH,S_L,R_ESI);
  669. end;
  670. end;
  671. if is_con_or_destructor then
  672. begin
  673. { classes don't get a VMT pointer pushed }
  674. if is_object(methodpointer.resulttype.def) then
  675. begin
  676. if (procdefinition.proctypeoption=potype_constructor) then
  677. begin
  678. { it's no bad idea, to insert the VMT }
  679. emit_sym(A_PUSH,S_L,newasmsymbol(
  680. tobjectdef(methodpointer.resulttype.def).vmt_mangledname));
  681. end
  682. { destructors haven't to dispose the instance, if this is }
  683. { a direct call }
  684. else
  685. push_int(0);
  686. end;
  687. end;
  688. end;
  689. end;
  690. end;
  691. end
  692. else
  693. begin
  694. if (po_classmethod in procdefinition.procoptions) and
  695. not(
  696. assigned(aktprocsym) and
  697. (po_classmethod in aktprocsym.definition.procoptions)
  698. ) then
  699. begin
  700. { class method needs current VMT }
  701. getexplicitregister32(R_ESI);
  702. new(r);
  703. reset_reference(r^);
  704. r^.base:=R_ESI;
  705. r^.offset:= tprocdef(procdefinition)._class.vmt_offset;
  706. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  707. end
  708. else
  709. begin
  710. { member call, ESI isn't modified }
  711. loadesi:=false;
  712. end;
  713. { direct call to destructor: don't remove data! }
  714. if is_class(procinfo^._class) then
  715. begin
  716. if (procdefinition.proctypeoption=potype_destructor) then
  717. begin
  718. emit_const(A_PUSH,S_L,0);
  719. emit_reg(A_PUSH,S_L,R_ESI);
  720. end
  721. else if (procdefinition.proctypeoption=potype_constructor) then
  722. begin
  723. emit_const(A_PUSH,S_L,0);
  724. emit_const(A_PUSH,S_L,0);
  725. end
  726. else
  727. emit_reg(A_PUSH,S_L,R_ESI);
  728. end
  729. else if is_object(procinfo^._class) then
  730. begin
  731. emit_reg(A_PUSH,S_L,R_ESI);
  732. if is_con_or_destructor then
  733. begin
  734. if (procdefinition.proctypeoption=potype_constructor) then
  735. begin
  736. { it's no bad idea, to insert the VMT }
  737. emit_sym(A_PUSH,S_L,newasmsymbol(
  738. procinfo^._class.vmt_mangledname));
  739. end
  740. { destructors haven't to dispose the instance, if this is }
  741. { a direct call }
  742. else
  743. push_int(0);
  744. end;
  745. end
  746. else
  747. Internalerror(200006165);
  748. end;
  749. end;
  750. { call to BeforeDestruction? }
  751. if (procdefinition.proctypeoption=potype_destructor) and
  752. assigned(methodpointer) and
  753. (methodpointer.nodetype<>typen) and
  754. is_class(tobjectdef(methodpointer.resulttype.def)) and
  755. (inlined or
  756. (right=nil)) then
  757. begin
  758. emit_reg(A_PUSH,S_L,R_ESI);
  759. new(r);
  760. reset_reference(r^);
  761. r^.base:=R_ESI;
  762. getexplicitregister32(R_EDI);
  763. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  764. new(r);
  765. reset_reference(r^);
  766. r^.offset:=72;
  767. r^.base:=R_EDI;
  768. emit_ref(A_CALL,S_NO,r);
  769. ungetregister32(R_EDI);
  770. end;
  771. { push base pointer ?}
  772. if (lexlevel>=normal_function_level) and assigned(tprocdef(procdefinition).parast) and
  773. ((tprocdef(procdefinition).parast.symtablelevel)>normal_function_level) then
  774. begin
  775. { if we call a nested function in a method, we must }
  776. { push also SELF! }
  777. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  778. { access }
  779. {
  780. begin
  781. loadesi:=false;
  782. emit_reg(A_PUSH,S_L,R_ESI);
  783. end;
  784. }
  785. if lexlevel=(tprocdef(procdefinition).parast.symtablelevel) then
  786. begin
  787. new(r);
  788. reset_reference(r^);
  789. r^.offset:=procinfo^.framepointer_offset;
  790. r^.base:=procinfo^.framepointer;
  791. emit_ref(A_PUSH,S_L,r)
  792. end
  793. { this is only true if the difference is one !!
  794. but it cannot be more !! }
  795. else if (lexlevel=tprocdef(procdefinition).parast.symtablelevel-1) then
  796. begin
  797. emit_reg(A_PUSH,S_L,procinfo^.framepointer)
  798. end
  799. else if (lexlevel>tprocdef(procdefinition).parast.symtablelevel) then
  800. begin
  801. hregister:=getregister32;
  802. new(r);
  803. reset_reference(r^);
  804. r^.offset:=procinfo^.framepointer_offset;
  805. r^.base:=procinfo^.framepointer;
  806. emit_ref_reg(A_MOV,S_L,r,hregister);
  807. for i:=(tprocdef(procdefinition).parast.symtablelevel) to lexlevel-1 do
  808. begin
  809. new(r);
  810. reset_reference(r^);
  811. {we should get the correct frame_pointer_offset at each level
  812. how can we do this !!! }
  813. r^.offset:=procinfo^.framepointer_offset;
  814. r^.base:=hregister;
  815. emit_ref_reg(A_MOV,S_L,r,hregister);
  816. end;
  817. emit_reg(A_PUSH,S_L,hregister);
  818. ungetregister32(hregister);
  819. end
  820. else
  821. internalerror(25000);
  822. end;
  823. saveregvars(regs_to_push);
  824. if (po_virtualmethod in procdefinition.procoptions) and
  825. not(no_virtual_call) then
  826. begin
  827. { static functions contain the vmt_address in ESI }
  828. { also class methods }
  829. { Here it is quite tricky because it also depends }
  830. { on the methodpointer PM }
  831. getexplicitregister32(R_ESI);
  832. if assigned(aktprocsym) then
  833. begin
  834. if (((sp_static in aktprocsym.symoptions) or
  835. (po_classmethod in aktprocsym.definition.procoptions)) and
  836. ((methodpointer=nil) or (methodpointer.nodetype=typen)))
  837. or
  838. (po_staticmethod in procdefinition.procoptions) or
  839. ((procdefinition.proctypeoption=potype_constructor) and
  840. { esi contains the vmt if we call a constructor via a class ref }
  841. assigned(methodpointer) and
  842. (methodpointer.resulttype.def.deftype=classrefdef)
  843. ) or
  844. { is_interface(tprocdef(procdefinition)._class) or }
  845. { ESI is loaded earlier }
  846. (po_classmethod in procdefinition.procoptions) then
  847. begin
  848. new(r);
  849. reset_reference(r^);
  850. r^.base:=R_ESI;
  851. end
  852. else
  853. begin
  854. new(r);
  855. reset_reference(r^);
  856. r^.base:=R_ESI;
  857. { this is one point where we need vmt_offset (PM) }
  858. r^.offset:= tprocdef(procdefinition)._class.vmt_offset;
  859. getexplicitregister32(R_EDI);
  860. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  861. new(r);
  862. reset_reference(r^);
  863. r^.base:=R_EDI;
  864. end;
  865. end
  866. else
  867. { aktprocsym should be assigned, also in main program }
  868. internalerror(12345);
  869. {
  870. begin
  871. new(r);
  872. reset_reference(r^);
  873. r^.base:=R_ESI;
  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. }
  880. if tprocdef(procdefinition).extnumber=-1 then
  881. internalerror(44584);
  882. r^.offset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  883. if not(is_interface(tprocdef(procdefinition)._class)) and
  884. not(is_cppclass(tprocdef(procdefinition)._class)) then
  885. begin
  886. if (cs_check_object_ext in aktlocalswitches) then
  887. begin
  888. emit_sym(A_PUSH,S_L,
  889. newasmsymbol(tprocdef(procdefinition)._class.vmt_mangledname));
  890. emit_reg(A_PUSH,S_L,r^.base);
  891. emitcall('FPC_CHECK_OBJECT_EXT');
  892. end
  893. else if (cs_check_range in aktlocalswitches) then
  894. begin
  895. emit_reg(A_PUSH,S_L,r^.base);
  896. emitcall('FPC_CHECK_OBJECT');
  897. end;
  898. end;
  899. emit_ref(A_CALL,S_NO,r);
  900. ungetregister32(R_EDI);
  901. end
  902. else if not inlined then
  903. begin
  904. { We can call interrupts from within the smae code
  905. by just pushing the flags and CS PM }
  906. if (po_interrupt in procdefinition.procoptions) then
  907. begin
  908. emit_none(A_PUSHF,S_L);
  909. emit_reg(A_PUSH,S_L,R_CS);
  910. end;
  911. emitcall(tprocdef(procdefinition).mangledname);
  912. end
  913. else { inlined proc }
  914. { inlined code is in inlinecode }
  915. begin
  916. { set poinline again }
  917. include(procdefinition.proccalloptions,pocall_inline);
  918. { process the inlinecode }
  919. secondpass(inlinecode);
  920. { free the args }
  921. if tprocdef(procdefinition).parast.datasize>0 then
  922. ungetpersistanttemp(tprocdef(procdefinition).parast.address_fixup);
  923. end;
  924. end
  925. else
  926. { now procedure variable case }
  927. begin
  928. secondpass(right);
  929. if (po_interrupt in procdefinition.procoptions) then
  930. begin
  931. emit_none(A_PUSHF,S_L);
  932. emit_reg(A_PUSH,S_L,R_CS);
  933. end;
  934. { procedure of object? }
  935. if (po_methodpointer in procdefinition.procoptions) then
  936. begin
  937. { method pointer can't be in a register }
  938. hregister:=R_NO;
  939. { do some hacking if we call a method pointer }
  940. { which is a class member }
  941. { else ESI is overwritten ! }
  942. if (right.location.reference.base=R_ESI) or
  943. (right.location.reference.index=R_ESI) then
  944. begin
  945. del_reference(right.location.reference);
  946. getexplicitregister32(R_EDI);
  947. emit_ref_reg(A_MOV,S_L,
  948. newreference(right.location.reference),R_EDI);
  949. hregister:=R_EDI;
  950. end;
  951. { load self, but not if it's already explicitly pushed }
  952. if not(po_containsself in procdefinition.procoptions) then
  953. begin
  954. { load ESI }
  955. inc(right.location.reference.offset,4);
  956. getexplicitregister32(R_ESI);
  957. emit_ref_reg(A_MOV,S_L,
  958. newreference(right.location.reference),R_ESI);
  959. dec(right.location.reference.offset,4);
  960. { push self pointer }
  961. emit_reg(A_PUSH,S_L,R_ESI);
  962. end;
  963. saveregvars($ff);
  964. if hregister=R_NO then
  965. emit_ref(A_CALL,S_NO,newreference(right.location.reference))
  966. else
  967. begin
  968. ungetregister32(hregister);
  969. emit_reg(A_CALL,S_NO,hregister);
  970. end;
  971. del_reference(right.location.reference);
  972. end
  973. else
  974. begin
  975. saveregvars($ff);
  976. case right.location.loc of
  977. LOC_REGISTER,LOC_CREGISTER:
  978. begin
  979. emit_reg(A_CALL,S_NO,right.location.register);
  980. ungetregister32(right.location.register);
  981. end
  982. else
  983. begin
  984. emit_ref(A_CALL,S_NO,newreference(right.location.reference));
  985. del_reference(right.location.reference);
  986. end;
  987. end;
  988. end;
  989. end;
  990. { this was only for normal functions
  991. displaced here so we also get
  992. it to work for procvars PM }
  993. if (not inlined) and (pocall_clearstack in procdefinition.proccalloptions) then
  994. begin
  995. { we also add the pop_size which is included in pushedparasize }
  996. pop_size:=0;
  997. { better than an add on all processors }
  998. if pushedparasize=4 then
  999. begin
  1000. getexplicitregister32(R_EDI);
  1001. emit_reg(A_POP,S_L,R_EDI);
  1002. ungetregister32(R_EDI);
  1003. end
  1004. { the pentium has two pipes and pop reg is pairable }
  1005. { but the registers must be different! }
  1006. else if (pushedparasize=8) and
  1007. not(cs_littlesize in aktglobalswitches) and
  1008. (aktoptprocessor=ClassP5) and
  1009. (procinfo^._class=nil) then
  1010. begin
  1011. getexplicitregister32(R_EDI);
  1012. emit_reg(A_POP,S_L,R_EDI);
  1013. ungetregister32(R_EDI);
  1014. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1015. emit_reg(A_POP,S_L,R_ESI);
  1016. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1017. end
  1018. else if pushedparasize<>0 then
  1019. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  1020. end;
  1021. if pop_esp then
  1022. emit_reg(A_POP,S_L,R_ESP);
  1023. dont_call:
  1024. pushedparasize:=oldpushedparasize;
  1025. unused:=unusedregisters;
  1026. usablereg32:=usablecount;
  1027. {$ifdef TEMPREGDEBUG}
  1028. testregisters32;
  1029. {$endif TEMPREGDEBUG}
  1030. { a constructor could be a function with boolean result }
  1031. { if calling constructor called fail we
  1032. must jump directly to quickexitlabel PM
  1033. but only if it is a call of an inherited constructor }
  1034. if (inlined or
  1035. (right=nil)) and
  1036. (procdefinition.proctypeoption=potype_constructor) and
  1037. assigned(methodpointer) and
  1038. (methodpointer.nodetype=typen) and
  1039. (aktprocsym.definition.proctypeoption=potype_constructor) then
  1040. begin
  1041. emitjmp(C_Z,faillabel);
  1042. end;
  1043. { call to AfterConstruction? }
  1044. if is_class(resulttype.def) and
  1045. (inlined or
  1046. (right=nil)) and
  1047. (procdefinition.proctypeoption=potype_constructor) and
  1048. assigned(methodpointer) and
  1049. (methodpointer.nodetype<>typen) then
  1050. begin
  1051. getlabel(constructorfailed);
  1052. emitjmp(C_Z,constructorfailed);
  1053. emit_reg(A_PUSH,S_L,R_ESI);
  1054. new(r);
  1055. reset_reference(r^);
  1056. r^.base:=R_ESI;
  1057. getexplicitregister32(R_EDI);
  1058. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  1059. new(r);
  1060. reset_reference(r^);
  1061. r^.offset:=68;
  1062. r^.base:=R_EDI;
  1063. emit_ref(A_CALL,S_NO,r);
  1064. ungetregister32(R_EDI);
  1065. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  1066. emitlab(constructorfailed);
  1067. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  1068. end;
  1069. { handle function results }
  1070. { structured results are easy to handle.... }
  1071. { needed also when result_no_used !! }
  1072. if (not is_void(resulttype.def)) and ret_in_param(resulttype.def) then
  1073. begin
  1074. location.loc:=LOC_MEM;
  1075. location.reference.symbol:=nil;
  1076. location.reference:=funcretref;
  1077. end;
  1078. { we have only to handle the result if it is used, but }
  1079. { ansi/widestrings must be registered, so we can dispose them }
  1080. if (not is_void(resulttype.def)) and ((nf_return_value_used in flags) or
  1081. is_ansistring(resulttype.def) or is_widestring(resulttype.def)) then
  1082. begin
  1083. { a contructor could be a function with boolean result }
  1084. if (inlined or
  1085. (right=nil)) and
  1086. (procdefinition.proctypeoption=potype_constructor) and
  1087. { quick'n'dirty check if it is a class or an object }
  1088. (resulttype.def.deftype=orddef) then
  1089. begin
  1090. { this fails if popsize > 0 PM }
  1091. location.loc:=LOC_FLAGS;
  1092. location.resflags:=F_NE;
  1093. if extended_new then
  1094. begin
  1095. {$ifdef test_dest_loc}
  1096. if dest_loc_known and (dest_loc_tree=p) then
  1097. mov_reg_to_dest(p,S_L,R_EAX)
  1098. else
  1099. {$endif test_dest_loc}
  1100. begin
  1101. hregister:=getexplicitregister32(R_EAX);
  1102. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1103. location.register:=hregister;
  1104. end;
  1105. end;
  1106. end
  1107. { structed results are easy to handle.... }
  1108. else if ret_in_param(resulttype.def) then
  1109. begin
  1110. {location.loc:=LOC_MEM;
  1111. stringdispose(location.reference.symbol);
  1112. location.reference:=funcretref;
  1113. already done above (PM) }
  1114. end
  1115. else
  1116. begin
  1117. if (resulttype.def.deftype in [orddef,enumdef]) then
  1118. begin
  1119. location.loc:=LOC_REGISTER;
  1120. case resulttype.def.size of
  1121. 4 :
  1122. begin
  1123. {$ifdef test_dest_loc}
  1124. if dest_loc_known and (dest_loc_tree=p) then
  1125. mov_reg_to_dest(p,S_L,R_EAX)
  1126. else
  1127. {$endif test_dest_loc}
  1128. begin
  1129. hregister:=getexplicitregister32(R_EAX);
  1130. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1131. location.register:=hregister;
  1132. end;
  1133. end;
  1134. 1 :
  1135. begin
  1136. {$ifdef test_dest_loc}
  1137. if dest_loc_known and (dest_loc_tree=p) then
  1138. mov_reg_to_dest(p,S_B,R_AL)
  1139. else
  1140. {$endif test_dest_loc}
  1141. begin
  1142. hregister:=getexplicitregister32(R_EAX);
  1143. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1144. location.register:=reg32toreg8(hregister);
  1145. end;
  1146. end;
  1147. 2 :
  1148. begin
  1149. {$ifdef test_dest_loc}
  1150. if dest_loc_known and (dest_loc_tree=p) then
  1151. mov_reg_to_dest(p,S_W,R_AX)
  1152. else
  1153. {$endif test_dest_loc}
  1154. begin
  1155. hregister:=getexplicitregister32(R_EAX);
  1156. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1157. location.register:=reg32toreg16(hregister);
  1158. end;
  1159. end;
  1160. 8 :
  1161. begin
  1162. {$ifdef test_dest_loc}
  1163. {$error Don't know what to do here}
  1164. {$endif test_dest_loc}
  1165. if R_EDX in unused then
  1166. begin
  1167. hregister2:=getexplicitregister32(R_EDX);
  1168. hregister:=getexplicitregister32(R_EAX);
  1169. end
  1170. else
  1171. begin
  1172. hregister:=getexplicitregister32(R_EAX);
  1173. hregister2:=getexplicitregister32(R_EDX);
  1174. end;
  1175. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1176. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  1177. location.registerlow:=hregister;
  1178. location.registerhigh:=hregister2;
  1179. end;
  1180. else internalerror(7);
  1181. end
  1182. end
  1183. else if (resulttype.def.deftype=floatdef) then
  1184. begin
  1185. location.loc:=LOC_FPU;
  1186. inc(fpuvaroffset);
  1187. end
  1188. else if is_ansistring(resulttype.def) or
  1189. is_widestring(resulttype.def) then
  1190. begin
  1191. hregister:=getexplicitregister32(R_EAX);
  1192. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1193. gettempansistringreference(hr);
  1194. decrstringref(resulttype.def,hr);
  1195. emit_reg_ref(A_MOV,S_L,hregister,
  1196. newreference(hr));
  1197. ungetregister32(hregister);
  1198. location.loc:=LOC_MEM;
  1199. location.reference:=hr;
  1200. end
  1201. else
  1202. begin
  1203. location.loc:=LOC_REGISTER;
  1204. {$ifdef test_dest_loc}
  1205. if dest_loc_known and (dest_loc_tree=p) then
  1206. mov_reg_to_dest(p,S_L,R_EAX)
  1207. else
  1208. {$endif test_dest_loc}
  1209. begin
  1210. hregister:=getexplicitregister32(R_EAX);
  1211. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1212. location.register:=hregister;
  1213. end;
  1214. end;
  1215. end;
  1216. end;
  1217. { perhaps i/o check ? }
  1218. if iolabel<>nil then
  1219. begin
  1220. emit_sym(A_PUSH,S_L,iolabel);
  1221. emitcall('FPC_IOCHECK');
  1222. end;
  1223. if pop_size>0 then
  1224. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1225. { restore registers }
  1226. popusedregisters(pushed);
  1227. { at last, restore instance pointer (SELF) }
  1228. if loadesi then
  1229. maybe_loadself;
  1230. pp:=tbinarynode(params);
  1231. while assigned(pp) do
  1232. begin
  1233. if assigned(pp.left) then
  1234. begin
  1235. if (pp.left.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1236. ungetiftemp(pp.left.location.reference);
  1237. { process also all nodes of an array of const }
  1238. if pp.left.nodetype=arrayconstructorn then
  1239. begin
  1240. if assigned(tarrayconstructornode(pp.left).left) then
  1241. begin
  1242. hp:=pp.left;
  1243. while assigned(hp) do
  1244. begin
  1245. if (tarrayconstructornode(tunarynode(hp).left).location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1246. ungetiftemp(tarrayconstructornode(hp).left.location.reference);
  1247. hp:=tbinarynode(hp).right;
  1248. end;
  1249. end;
  1250. end;
  1251. end;
  1252. pp:=tbinarynode(pp.right);
  1253. end;
  1254. if inlined then
  1255. ungetpersistanttemp(inlinecode.retoffset);
  1256. if assigned(params) then
  1257. params.free;
  1258. { from now on the result can be freed normally }
  1259. if inlined and ret_in_param(resulttype.def) then
  1260. persistanttemptonormal(funcretref.offset);
  1261. { if return value is not used }
  1262. if (not(nf_return_value_used in flags)) and (not is_void(resulttype.def)) then
  1263. begin
  1264. if location.loc in [LOC_MEM,LOC_REFERENCE] then
  1265. begin
  1266. { data which must be finalized ? }
  1267. if (resulttype.def.needs_inittable) then
  1268. finalize(resulttype.def,location.reference,false);
  1269. { release unused temp }
  1270. ungetiftemp(location.reference)
  1271. end
  1272. else if location.loc=LOC_FPU then
  1273. begin
  1274. { release FPU stack }
  1275. emit_reg(A_FSTP,S_NO,R_ST0);
  1276. {
  1277. dec(fpuvaroffset);
  1278. do NOT decrement as the increment before
  1279. is not called for unused results PM }
  1280. end;
  1281. end;
  1282. end;
  1283. {*****************************************************************************
  1284. TI386PROCINLINENODE
  1285. *****************************************************************************}
  1286. procedure ti386procinlinenode.pass_2;
  1287. var st : tsymtable;
  1288. oldprocsym : tprocsym;
  1289. ps, i : longint;
  1290. tmpreg: tregister;
  1291. oldprocinfo : pprocinfo;
  1292. oldinlining_procedure,
  1293. nostackframe,make_global : boolean;
  1294. inlineentrycode,inlineexitcode : TAAsmoutput;
  1295. oldexitlabel,oldexit2label,oldquickexitlabel:tasmlabel;
  1296. oldunused,oldusableregs : tregisterset;
  1297. oldc_usableregs : longint;
  1298. oldreg_pushes : regvar_longintarray;
  1299. oldregvar_loaded,
  1300. oldis_reg_var : regvar_booleanarray;
  1301. {$ifdef TEMPREGDEBUG}
  1302. oldreg_user : regvar_ptreearray;
  1303. oldreg_releaser : regvar_ptreearray;
  1304. {$endif TEMPREGDEBUG}
  1305. {$ifdef GDB}
  1306. startlabel,endlabel : tasmlabel;
  1307. pp : pchar;
  1308. mangled_length : longint;
  1309. {$endif GDB}
  1310. begin
  1311. { deallocate the registers used for the current procedure's regvars }
  1312. if assigned(aktprocsym.definition.regvarinfo) then
  1313. begin
  1314. with pregvarinfo(aktprocsym.definition.regvarinfo)^ do
  1315. for i := 1 to maxvarregs do
  1316. if assigned(regvars[i]) then
  1317. store_regvar(exprasmlist,regvars[i].reg);
  1318. oldunused := unused;
  1319. oldusableregs := usableregs;
  1320. oldc_usableregs := c_usableregs;
  1321. oldreg_pushes := reg_pushes;
  1322. oldis_reg_var := is_reg_var;
  1323. oldregvar_loaded := regvar_loaded;
  1324. {$ifdef TEMPREGDEBUG}
  1325. oldreg_user := reg_user;
  1326. oldreg_releaser := reg_releaser;
  1327. {$endif TEMPREGDEBUG}
  1328. { make sure the register allocator knows what the regvars in the }
  1329. { inlined code block are (JM) }
  1330. resetusableregisters;
  1331. clearregistercount;
  1332. cleartempgen;
  1333. if assigned(inlineprocsym.definition.regvarinfo) then
  1334. with pregvarinfo(inlineprocsym.definition.regvarinfo)^ do
  1335. for i := 1 to maxvarregs do
  1336. if assigned(regvars[i]) then
  1337. begin
  1338. case regsize(regvars[i].reg) of
  1339. S_B: tmpreg := reg8toreg32(regvars[i].reg);
  1340. S_W: tmpreg := reg16toreg32(regvars[i].reg);
  1341. S_L: tmpreg := regvars[i].reg;
  1342. end;
  1343. usableregs:=usableregs-[tmpreg];
  1344. is_reg_var[tmpreg]:=true;
  1345. dec(c_usableregs);
  1346. end;
  1347. end;
  1348. oldinlining_procedure:=inlining_procedure;
  1349. oldexitlabel:=aktexitlabel;
  1350. oldexit2label:=aktexit2label;
  1351. oldquickexitlabel:=quickexitlabel;
  1352. getlabel(aktexitlabel);
  1353. getlabel(aktexit2label);
  1354. oldprocsym:=aktprocsym;
  1355. { we're inlining a procedure }
  1356. inlining_procedure:=true;
  1357. { save old procinfo }
  1358. getmem(oldprocinfo,sizeof(tprocinfo));
  1359. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1360. { set the return value }
  1361. aktprocsym:=inlineprocsym;
  1362. procinfo^.returntype:=aktprocsym.definition.rettype;
  1363. procinfo^.return_offset:=retoffset;
  1364. procinfo^.para_offset:=para_offset;
  1365. { arg space has been filled by the parent secondcall }
  1366. st:=aktprocsym.definition.localst;
  1367. { set it to the same lexical level }
  1368. st.symtablelevel:=oldprocsym.definition.localst.symtablelevel;
  1369. if st.datasize>0 then
  1370. begin
  1371. st.address_fixup:=gettempofsizepersistant(st.datasize)+st.datasize;
  1372. {$ifdef extdebug}
  1373. Comment(V_debug,'local symtable is at offset '+tostr(st.address_fixup));
  1374. exprasmList.concat(Tai_asm_comment.Create(strpnew(
  1375. 'local symtable is at offset '+tostr(st.address_fixup))));
  1376. {$endif extdebug}
  1377. end;
  1378. exprasmList.concat(Tai_Marker.Create(InlineStart));
  1379. {$ifdef extdebug}
  1380. exprasmList.concat(Tai_asm_comment.Create(strpnew('Start of inlined proc')));
  1381. {$endif extdebug}
  1382. {$ifdef GDB}
  1383. if (cs_debuginfo in aktmoduleswitches) then
  1384. begin
  1385. getaddrlabel(startlabel);
  1386. getaddrlabel(endlabel);
  1387. emitlab(startlabel);
  1388. inlineprocsym.definition.localst.symtabletype:=inlinelocalsymtable;
  1389. inlineprocsym.definition.parast.symtabletype:=inlineparasymtable;
  1390. { Here we must include the para and local symtable info }
  1391. inlineprocsym.concatstabto(withdebuglist);
  1392. { set it back for safety }
  1393. inlineprocsym.definition.localst.symtabletype:=localsymtable;
  1394. inlineprocsym.definition.parast.symtabletype:=parasymtable;
  1395. mangled_length:=length(oldprocsym.definition.mangledname);
  1396. getmem(pp,mangled_length+50);
  1397. strpcopy(pp,'192,0,0,'+startlabel.name);
  1398. if (target_info.use_function_relative_addresses) then
  1399. begin
  1400. strpcopy(strend(pp),'-');
  1401. strpcopy(strend(pp),oldprocsym.definition.mangledname);
  1402. end;
  1403. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1404. end;
  1405. {$endif GDB}
  1406. { takes care of local data initialization }
  1407. inlineentrycode:=TAAsmoutput.Create;
  1408. inlineexitcode:=TAAsmoutput.Create;
  1409. ps:=para_size;
  1410. make_global:=false; { to avoid warning }
  1411. genentrycode(inlineentrycode,make_global,0,ps,nostackframe,true);
  1412. if po_assembler in aktprocsym.definition.procoptions then
  1413. inlineentrycode.insert(Tai_marker.Create(asmblockstart));
  1414. exprasmList.concatlist(inlineentrycode);
  1415. secondpass(inlinetree);
  1416. genexitcode(inlineexitcode,0,false,true);
  1417. if po_assembler in aktprocsym.definition.procoptions then
  1418. inlineexitcode.concat(Tai_marker.Create(asmblockend));
  1419. exprasmList.concatlist(inlineexitcode);
  1420. inlineentrycode.free;
  1421. inlineexitcode.free;
  1422. {$ifdef extdebug}
  1423. exprasmList.concat(Tai_asm_comment.Create(strpnew('End of inlined proc')));
  1424. {$endif extdebug}
  1425. exprasmList.concat(Tai_Marker.Create(InlineEnd));
  1426. {we can free the local data now, reset also the fixup address }
  1427. if st.datasize>0 then
  1428. begin
  1429. ungetpersistanttemp(st.address_fixup-st.datasize);
  1430. st.address_fixup:=0;
  1431. end;
  1432. { restore procinfo }
  1433. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1434. freemem(oldprocinfo,sizeof(tprocinfo));
  1435. {$ifdef GDB}
  1436. if (cs_debuginfo in aktmoduleswitches) then
  1437. begin
  1438. emitlab(endlabel);
  1439. strpcopy(pp,'224,0,0,'+endlabel.name);
  1440. if (target_info.use_function_relative_addresses) then
  1441. begin
  1442. strpcopy(strend(pp),'-');
  1443. strpcopy(strend(pp),oldprocsym.definition.mangledname);
  1444. end;
  1445. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1446. freemem(pp,mangled_length+50);
  1447. end;
  1448. {$endif GDB}
  1449. { restore }
  1450. aktprocsym:=oldprocsym;
  1451. aktexitlabel:=oldexitlabel;
  1452. aktexit2label:=oldexit2label;
  1453. quickexitlabel:=oldquickexitlabel;
  1454. inlining_procedure:=oldinlining_procedure;
  1455. { reallocate the registers used for the current procedure's regvars, }
  1456. { since they may have been used and then deallocated in the inlined }
  1457. { procedure (JM) }
  1458. if assigned(aktprocsym.definition.regvarinfo) then
  1459. begin
  1460. unused := oldunused;
  1461. usableregs := oldusableregs;
  1462. c_usableregs := oldc_usableregs;
  1463. reg_pushes := oldreg_pushes;
  1464. is_reg_var := oldis_reg_var;
  1465. regvar_loaded := oldregvar_loaded;
  1466. {$ifdef TEMPREGDEBUG}
  1467. reg_user := oldreg_user;
  1468. reg_releaser := oldreg_releaser;
  1469. {$endif TEMPREGDEBUG}
  1470. end;
  1471. end;
  1472. begin
  1473. ccallparanode:=ti386callparanode;
  1474. ccallnode:=ti386callnode;
  1475. cprocinlinenode:=ti386procinlinenode;
  1476. end.
  1477. {
  1478. $Log$
  1479. Revision 1.23 2001-05-16 15:11:42 jonas
  1480. * added missign begin..end pair (noticed by Carl)
  1481. Revision 1.22 2001/04/18 22:02:01 peter
  1482. * registration of targets and assemblers
  1483. Revision 1.21 2001/04/13 01:22:18 peter
  1484. * symtable change to classes
  1485. * range check generation and errors fixed, make cycle DEBUG=1 works
  1486. * memory leaks fixed
  1487. Revision 1.20 2001/04/02 21:20:36 peter
  1488. * resulttype rewrite
  1489. Revision 1.19 2001/03/11 22:58:51 peter
  1490. * getsym redesign, removed the globals srsym,srsymtable
  1491. Revision 1.18 2001/01/27 21:29:35 florian
  1492. * behavior -Oa optimized
  1493. Revision 1.17 2001/01/08 21:46:46 peter
  1494. * don't push high value for open array with cdecl;external;
  1495. Revision 1.16 2000/12/25 00:07:32 peter
  1496. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1497. tlinkedlist objects)
  1498. Revision 1.15 2000/12/09 10:45:40 florian
  1499. * AfterConstructor isn't called anymore when a constructor failed
  1500. Revision 1.14 2000/12/07 17:19:46 jonas
  1501. * new constant handling: from now on, hex constants >$7fffffff are
  1502. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1503. and became $ffffffff80000000), all constants in the longint range
  1504. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1505. are cardinals and the rest are int64's.
  1506. * added lots of longint typecast to prevent range check errors in the
  1507. compiler and rtl
  1508. * type casts of symbolic ordinal constants are now preserved
  1509. * fixed bug where the original resulttype.def wasn't restored correctly
  1510. after doing a 64bit rangecheck
  1511. Revision 1.13 2000/12/05 11:44:33 jonas
  1512. + new integer regvar handling, should be much more efficient
  1513. Revision 1.12 2000/12/03 22:26:54 florian
  1514. * fixed web buzg 1275: problem with int64 functions results
  1515. Revision 1.11 2000/11/29 00:30:46 florian
  1516. * unused units removed from uses clause
  1517. * some changes for widestrings
  1518. Revision 1.10 2000/11/23 13:26:34 jonas
  1519. * fix for webbug 1066/1126
  1520. Revision 1.9 2000/11/22 15:12:06 jonas
  1521. * fixed inline-related problems (partially "merges")
  1522. Revision 1.8 2000/11/17 09:54:58 florian
  1523. * INT_CHECK_OBJECT_* isn't applied to interfaces anymore
  1524. Revision 1.7 2000/11/12 23:24:14 florian
  1525. * interfaces are basically running
  1526. Revision 1.6 2000/11/07 23:40:49 florian
  1527. + AfterConstruction and BeforeDestruction impemented
  1528. Revision 1.5 2000/11/06 23:15:01 peter
  1529. * added copyvaluepara call again
  1530. Revision 1.4 2000/11/04 14:25:23 florian
  1531. + merged Attila's changes for interfaces, not tested yet
  1532. Revision 1.3 2000/11/04 13:12:14 jonas
  1533. * check for nil pointers before calling getcopy
  1534. Revision 1.2 2000/10/31 22:02:56 peter
  1535. * symtable splitted, no real code changes
  1536. Revision 1.1 2000/10/15 09:33:31 peter
  1537. * moved n386*.pas to i386/ cpu_target dir
  1538. Revision 1.2 2000/10/14 10:14:48 peter
  1539. * moehrendorf oct 2000 rewrite
  1540. Revision 1.1 2000/10/10 17:31:56 florian
  1541. * initial revision
  1542. }