2
0

cg386cal.pas 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for in call nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386cal;
  19. interface
  20. {$define EXtdebug}
  21. uses
  22. symtable,tree;
  23. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  24. push_from_left_to_right,inlined : boolean;para_offset : longint);
  25. procedure secondcalln(var p : ptree);
  26. procedure secondprocinline(var p : ptree);
  27. implementation
  28. uses
  29. globtype,systems,
  30. cobjects,verbose,globals,
  31. aasm,types,
  32. {$ifdef GDB}
  33. gdb,
  34. {$endif GDB}
  35. hcodegen,temp_gen,pass_2,
  36. {$ifdef ag386bin}
  37. i386base,i386asm,
  38. {$else}
  39. i386,
  40. {$endif}
  41. cgai386,tgeni386,cg386ld;
  42. {*****************************************************************************
  43. SecondCallParaN
  44. *****************************************************************************}
  45. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  46. push_from_left_to_right,inlined : boolean;para_offset : longint);
  47. procedure maybe_push_high;
  48. begin
  49. { open array ? }
  50. { defcoll^.data can be nil for read/write }
  51. if assigned(defcoll^.data) and
  52. push_high_param(defcoll^.data) then
  53. begin
  54. if assigned(p^.hightree) then
  55. begin
  56. secondpass(p^.hightree);
  57. push_value_para(p^.hightree,inlined,para_offset);
  58. end
  59. else
  60. internalerror(432645);
  61. end;
  62. end;
  63. var
  64. otlabel,oflabel : plabel;
  65. { temporary variables: }
  66. tempdeftype : tdeftype;
  67. r : preference;
  68. begin
  69. { push from left to right if specified }
  70. if push_from_left_to_right and assigned(p^.right) then
  71. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,inlined,para_offset);
  72. otlabel:=truelabel;
  73. oflabel:=falselabel;
  74. getlabel(truelabel);
  75. getlabel(falselabel);
  76. secondpass(p^.left);
  77. { filter array constructor with c styled args }
  78. if is_array_constructor(p^.left^.resulttype) and p^.left^.cargs then
  79. begin
  80. { nothing, everything is already pushed }
  81. end
  82. { in codegen.handleread.. defcoll^.data is set to nil }
  83. else if assigned(defcoll^.data) and
  84. (defcoll^.data^.deftype=formaldef) then
  85. begin
  86. { allow @var }
  87. inc(pushedparasize,4);
  88. if p^.left^.treetype=addrn then
  89. begin
  90. { always a register }
  91. if inlined then
  92. begin
  93. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  94. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  95. p^.left^.location.register,r)));
  96. end
  97. else
  98. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
  99. ungetregister32(p^.left^.location.register);
  100. end
  101. else
  102. begin
  103. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  104. CGMessage(type_e_mismatch)
  105. else
  106. begin
  107. if inlined then
  108. begin
  109. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  110. newreference(p^.left^.location.reference),R_EDI)));
  111. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  112. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  113. end
  114. else
  115. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  116. del_reference(p^.left^.location.reference);
  117. end;
  118. end;
  119. end
  120. { handle call by reference parameter }
  121. else if (defcoll^.paratyp=vs_var) then
  122. begin
  123. if (p^.left^.location.loc<>LOC_REFERENCE) then
  124. CGMessage(cg_e_var_must_be_reference);
  125. maybe_push_high;
  126. inc(pushedparasize,4);
  127. if inlined then
  128. begin
  129. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  130. newreference(p^.left^.location.reference),R_EDI)));
  131. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  132. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  133. end
  134. else
  135. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  136. del_reference(p^.left^.location.reference);
  137. end
  138. else
  139. begin
  140. tempdeftype:=p^.resulttype^.deftype;
  141. if tempdeftype=filedef then
  142. CGMessage(cg_e_file_must_call_by_reference);
  143. if push_addr_param(p^.resulttype) then
  144. begin
  145. maybe_push_high;
  146. inc(pushedparasize,4);
  147. if inlined then
  148. begin
  149. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  150. newreference(p^.left^.location.reference),R_EDI)));
  151. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  152. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  153. R_EDI,r)));
  154. end
  155. else
  156. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  157. del_reference(p^.left^.location.reference);
  158. end
  159. else
  160. push_value_para(p^.left,inlined,para_offset);
  161. end;
  162. freelabel(truelabel);
  163. freelabel(falselabel);
  164. truelabel:=otlabel;
  165. falselabel:=oflabel;
  166. { push from right to left }
  167. if not push_from_left_to_right and assigned(p^.right) then
  168. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,inlined,para_offset);
  169. end;
  170. {*****************************************************************************
  171. SecondCallN
  172. *****************************************************************************}
  173. procedure secondcalln(var p : ptree);
  174. var
  175. unusedregisters : tregisterset;
  176. pushed,pushedregs : tpushed;
  177. hr,funcretref : treference;
  178. hregister,hregister2 : tregister;
  179. oldpushedparasize : longint;
  180. { true if ESI must be loaded again after the subroutine }
  181. loadesi : boolean;
  182. { true if a virtual method must be called directly }
  183. no_virtual_call : boolean;
  184. { true if we produce a con- or destrutor in a call }
  185. is_con_or_destructor : boolean;
  186. { true if a constructor is called again }
  187. extended_new : boolean;
  188. { adress returned from an I/O-error }
  189. iolabel : plabel;
  190. { lexlevel count }
  191. i : longint;
  192. { help reference pointer }
  193. r : preference;
  194. hp,
  195. pp,params : ptree;
  196. inlined : boolean;
  197. inlinecode : ptree;
  198. para_offset : longint;
  199. { instruction for alignement correction }
  200. { corr : pai386;}
  201. { we must pop this size also after !! }
  202. { must_pop : boolean; }
  203. pop_size : longint;
  204. oldrl : plinkedlist;
  205. label
  206. dont_call;
  207. begin
  208. reset_reference(p^.location.reference);
  209. extended_new:=false;
  210. iolabel:=nil;
  211. inlinecode:=nil;
  212. inlined:=false;
  213. loadesi:=true;
  214. no_virtual_call:=false;
  215. unusedregisters:=unused;
  216. { save old ansi string release list }
  217. oldrl:=temptoremove;
  218. temptoremove:=new(plinkedlist,init);
  219. if not assigned(p^.procdefinition) then
  220. exit;
  221. if (p^.procdefinition^.options and poinline)<>0 then
  222. begin
  223. inlined:=true;
  224. inlinecode:=p^.right;
  225. { set it to the same lexical level as the local symtable, becuase
  226. the para's are stored there }
  227. p^.procdefinition^.parast^.symtablelevel:=aktprocsym^.definition^.localst^.symtablelevel;
  228. if assigned(p^.left) then
  229. inlinecode^.para_offset:=gettempofsizepersistant(inlinecode^.para_size);
  230. p^.procdefinition^.parast^.address_fixup:=inlinecode^.para_offset;
  231. {$ifdef extdebug}
  232. Comment(V_debug,
  233. 'inlined parasymtable is at offset '
  234. +tostr(p^.procdefinition^.parast^.address_fixup));
  235. exprasmlist^.concat(new(pai_asm_comment,init(
  236. strpnew('inlined parasymtable is at offset '
  237. +tostr(p^.procdefinition^.parast^.address_fixup)))));
  238. {$endif extdebug}
  239. p^.right:=nil;
  240. { disable further inlining of the same proc
  241. in the args }
  242. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  243. end;
  244. { only if no proc var }
  245. if not(assigned(p^.right)) then
  246. is_con_or_destructor:=((p^.procdefinition^.options and poconstructor)<>0)
  247. or ((p^.procdefinition^.options and podestructor)<>0);
  248. { proc variables destroy all registers }
  249. if (p^.right=nil) and
  250. { virtual methods too }
  251. ((p^.procdefinition^.options and povirtualmethod)=0) then
  252. begin
  253. if ((p^.procdefinition^.options and poiocheck)<>0) and
  254. ((aktprocsym^.definition^.options and poiocheck)=0) and
  255. (cs_check_io in aktlocalswitches) then
  256. begin
  257. getlabel(iolabel);
  258. emitlab(iolabel);
  259. end
  260. else
  261. iolabel:=nil;
  262. { save all used registers }
  263. pushusedregisters(pushed,p^.procdefinition^.usedregisters);
  264. { give used registers through }
  265. usedinproc:=usedinproc or p^.procdefinition^.usedregisters;
  266. end
  267. else
  268. begin
  269. pushusedregisters(pushed,$ff);
  270. usedinproc:=$ff;
  271. { no IO check for methods and procedure variables }
  272. iolabel:=nil;
  273. end;
  274. { generate the code for the parameter and push them }
  275. oldpushedparasize:=pushedparasize;
  276. pushedparasize:=0;
  277. pop_size:=0;
  278. if (not inlined) then
  279. begin
  280. { Old pushedsize aligned on 4 ? }
  281. i:=oldpushedparasize and 3;
  282. if i>0 then
  283. inc(pop_size,4-i);
  284. { This parasize aligned on 4 ? }
  285. i:=p^.procdefinition^.para_size and 3;
  286. if i>0 then
  287. inc(pop_size,4-i);
  288. { insert the opcode and update pushedparasize }
  289. if pop_size>0 then
  290. begin
  291. inc(pushedparasize,pop_size);
  292. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,pop_size,R_ESP)));
  293. {$ifdef GDB}
  294. if (cs_debuginfo in aktmoduleswitches) and
  295. (exprasmlist^.first=exprasmlist^.last) then
  296. exprasmlist^.concat(new(pai_force_line,init));
  297. {$endif GDB}
  298. end;
  299. end;
  300. if (p^.resulttype<>pdef(voiddef)) and
  301. ret_in_param(p^.resulttype) then
  302. begin
  303. funcretref.symbol:=nil;
  304. {$ifdef test_dest_loc}
  305. if dest_loc_known and (dest_loc_tree=p) and
  306. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  307. begin
  308. funcretref:=dest_loc.reference;
  309. if assigned(dest_loc.reference.symbol) then
  310. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  311. in_dest_loc:=true;
  312. end
  313. else
  314. {$endif test_dest_loc}
  315. if inlined then
  316. begin
  317. reset_reference(funcretref);
  318. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.retdef^.size);
  319. funcretref.base:=procinfo.framepointer;
  320. end
  321. else
  322. gettempofsizereference(p^.procdefinition^.retdef^.size,funcretref);
  323. end;
  324. if assigned(p^.left) then
  325. begin
  326. { be found elsewhere }
  327. if inlined then
  328. para_offset:=p^.procdefinition^.parast^.address_fixup+
  329. p^.procdefinition^.parast^.datasize
  330. else
  331. para_offset:=0;
  332. if assigned(p^.right) then
  333. secondcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1,
  334. (p^.procdefinition^.options and poleftright)<>0,inlined,para_offset)
  335. else
  336. secondcallparan(p^.left,p^.procdefinition^.para1,
  337. (p^.procdefinition^.options and poleftright)<>0,inlined,para_offset);
  338. end;
  339. params:=p^.left;
  340. p^.left:=nil;
  341. if inlined then
  342. inlinecode^.retoffset:=gettempofsizepersistant(4);
  343. if ret_in_param(p^.resulttype) then
  344. begin
  345. inc(pushedparasize,4);
  346. if inlined then
  347. begin
  348. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  349. newreference(funcretref),R_EDI)));
  350. r:=new_reference(procinfo.framepointer,inlinecode^.retoffset);
  351. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  352. R_EDI,r)));
  353. end
  354. else
  355. emitpushreferenceaddr(exprasmlist,funcretref);
  356. end;
  357. { procedure variable ? }
  358. if (p^.right=nil) then
  359. begin
  360. { overloaded operator have no symtable }
  361. { push self }
  362. if assigned(p^.symtable) and
  363. (p^.symtable^.symtabletype=withsymtable) then
  364. begin
  365. { dirty trick to avoid the secondcall below }
  366. p^.methodpointer:=genzeronode(callparan);
  367. p^.methodpointer^.location.loc:=LOC_REGISTER;
  368. p^.methodpointer^.location.register:=R_ESI;
  369. { ARGHHH this is wrong !!!
  370. if we can init from base class for a child
  371. class that the wrong VMT will be
  372. transfered to constructor !! }
  373. {$ifdef NODIRECTWITH}
  374. p^.methodpointer^.resulttype:=p^.symtable^.defowner;
  375. {$else NODIRECTWITH}
  376. p^.methodpointer^.resulttype:=
  377. ptree(pwithsymtable(p^.symtable)^.withnode)^.left^.resulttype;
  378. {$endif def NODIRECTWITH}
  379. { change dispose type !! }
  380. p^.disposetyp:=dt_mbleft_and_method;
  381. { make a reference }
  382. new(r);
  383. reset_reference(r^);
  384. {$ifndef NODIRECTWITH}
  385. if assigned(ptree(pwithsymtable(p^.symtable)^.withnode)^.pref) then
  386. begin
  387. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.pref^;
  388. {if assigned(r^.symbol) then
  389. r^.symbol:=stringdup(r^.symbol^);}
  390. end
  391. else
  392. {$endif def NODIRECTWITH}
  393. begin
  394. r^.offset:=p^.symtable^.datasize;
  395. r^.base:=procinfo.framepointer;
  396. end;
  397. {$ifndef NODIRECTWITH}
  398. if (not pwithsymtable(p^.symtable)^.direct_with) or
  399. pobjectdef(p^.methodpointer^.resulttype)^.isclass then
  400. {$endif def NODIRECTWITH}
  401. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)))
  402. {$ifndef NODIRECTWITH}
  403. else
  404. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,r,R_ESI)))
  405. {$endif def NODIRECTWITH}
  406. ;
  407. end;
  408. { push self }
  409. if assigned(p^.symtable) and
  410. ((p^.symtable^.symtabletype=objectsymtable) or
  411. (p^.symtable^.symtabletype=withsymtable)) then
  412. begin
  413. if assigned(p^.methodpointer) then
  414. begin
  415. {
  416. if p^.methodpointer^.resulttype=classrefdef then
  417. begin
  418. two possibilities:
  419. 1. constructor
  420. 2. class method
  421. end
  422. else }
  423. begin
  424. case p^.methodpointer^.treetype of
  425. typen:
  426. begin
  427. { direct call to inherited method }
  428. if (p^.procdefinition^.options and poabstractmethod)<>0 then
  429. begin
  430. CGMessage(cg_e_cant_call_abstract_method);
  431. goto dont_call;
  432. end;
  433. { generate no virtual call }
  434. no_virtual_call:=true;
  435. if (p^.symtableprocentry^.properties and sp_static)<>0 then
  436. begin
  437. { well lets put the VMT address directly into ESI }
  438. { it is kind of dirty but that is the simplest }
  439. { way to accept virtual static functions (PM) }
  440. loadesi:=true;
  441. { if no VMT just use $0 bug0214 PM }
  442. if (pobjectdef(p^.methodpointer^.resulttype)^.options and oo_hasvmt)=0 then
  443. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,0,R_ESI)))
  444. else
  445. begin
  446. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,S_L,
  447. newasmsymbol(pobjectdef(
  448. p^.methodpointer^.resulttype)^.vmt_mangledname),0,R_ESI)));
  449. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  450. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  451. end;
  452. { exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  453. this is done below !! }
  454. end
  455. else
  456. { this is a member call, so ESI isn't modfied }
  457. loadesi:=false;
  458. { a class destructor needs a flag }
  459. if pobjectdef(p^.methodpointer^.resulttype)^.isclass and
  460. assigned(aktprocsym) and
  461. ((aktprocsym^.definition^.options and
  462. (podestructor))<>0) then
  463. begin
  464. push_int(0);
  465. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  466. end;
  467. if not(is_con_or_destructor and
  468. pobjectdef(p^.methodpointer^.resulttype)^.isclass and
  469. assigned(aktprocsym) and
  470. ((aktprocsym^.definition^.options and
  471. (poconstructor or podestructor))<>0)) then
  472. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  473. { if an inherited con- or destructor should be }
  474. { called in a con- or destructor then a warning }
  475. { will be made }
  476. { con- and destructors need a pointer to the vmt }
  477. if is_con_or_destructor and
  478. not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) and
  479. assigned(aktprocsym) then
  480. begin
  481. if not ((aktprocsym^.definition^.options
  482. and (poconstructor or podestructor))<>0) then
  483. CGMessage(cg_w_member_cd_call_from_method);
  484. end;
  485. { class destructors get there flag below }
  486. if is_con_or_destructor and
  487. not(pobjectdef(p^.methodpointer^.resulttype)^.isclass and
  488. assigned(aktprocsym) and
  489. ((aktprocsym^.definition^.options and
  490. (podestructor))<>0)) then
  491. push_int(0);
  492. end;
  493. hnewn:
  494. begin
  495. { extended syntax of new }
  496. { ESI must be zero }
  497. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_ESI,R_ESI)));
  498. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  499. { insert the vmt }
  500. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,
  501. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname))));
  502. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  503. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  504. extended_new:=true;
  505. end;
  506. hdisposen:
  507. begin
  508. secondpass(p^.methodpointer);
  509. { destructor with extended syntax called from dispose }
  510. { hdisposen always deliver LOC_REFERENCE }
  511. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  512. newreference(p^.methodpointer^.location.reference),R_ESI)));
  513. del_reference(p^.methodpointer^.location.reference);
  514. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  515. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,
  516. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname))));
  517. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  518. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  519. end;
  520. else
  521. begin
  522. { call to an instance member }
  523. if (p^.symtable^.symtabletype<>withsymtable) then
  524. begin
  525. secondpass(p^.methodpointer);
  526. case p^.methodpointer^.location.loc of
  527. LOC_CREGISTER,
  528. LOC_REGISTER:
  529. begin
  530. ungetregister32(p^.methodpointer^.location.register);
  531. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  532. end;
  533. else
  534. begin
  535. if (p^.methodpointer^.resulttype^.deftype=classrefdef) or
  536. ((p^.methodpointer^.resulttype^.deftype=objectdef) and
  537. pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  538. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  539. newreference(p^.methodpointer^.location.reference),R_ESI)))
  540. else
  541. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  542. newreference(p^.methodpointer^.location.reference),R_ESI)));
  543. del_reference(p^.methodpointer^.location.reference);
  544. end;
  545. end;
  546. end;
  547. { when calling a class method, we have
  548. to load ESI with the VMT !
  549. But that's wrong, if we call a class method via self
  550. }
  551. if ((p^.procdefinition^.options and poclassmethod)<>0)
  552. and not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
  553. begin
  554. { class method needs current VMT }
  555. new(r);
  556. reset_reference(r^);
  557. r^.base:=R_ESI;
  558. r^.offset:= p^.procdefinition^._class^.vmt_offset;
  559. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  560. end;
  561. { direct call to destructor: don't remove data! }
  562. if ((p^.procdefinition^.options and podestructor)<>0) and
  563. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  564. (pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  565. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,1)));
  566. { direct call to class constructor, don't allocate memory }
  567. if ((p^.procdefinition^.options and poconstructor)<>0) and
  568. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  569. (pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  570. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,0)))
  571. else
  572. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  573. if is_con_or_destructor then
  574. begin
  575. { classes don't get a VMT pointer pushed }
  576. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  577. not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  578. begin
  579. if ((p^.procdefinition^.options and poconstructor)<>0) then
  580. begin
  581. { it's no bad idea, to insert the VMT }
  582. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,newasmsymbol(
  583. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname))));
  584. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  585. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  586. end
  587. { destructors haven't to dispose the instance, if this is }
  588. { a direct call }
  589. else
  590. push_int(0);
  591. end;
  592. end;
  593. end;
  594. end;
  595. end;
  596. end
  597. else
  598. begin
  599. if ((p^.procdefinition^.options and poclassmethod)<>0) and
  600. not(
  601. assigned(aktprocsym) and
  602. ((aktprocsym^.definition^.options and poclassmethod)<>0)
  603. ) then
  604. begin
  605. { class method needs current VMT }
  606. new(r);
  607. reset_reference(r^);
  608. r^.base:=R_ESI;
  609. r^.offset:= p^.procdefinition^._class^.vmt_offset;
  610. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  611. end
  612. else
  613. begin
  614. { member call, ESI isn't modified }
  615. loadesi:=false;
  616. end;
  617. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  618. { but a con- or destructor here would probably almost }
  619. { always be placed wrong }
  620. if is_con_or_destructor then
  621. begin
  622. CGMessage(cg_w_member_cd_call_from_method);
  623. push_int(0);
  624. end;
  625. end;
  626. end;
  627. { push base pointer ?}
  628. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  629. ((p^.procdefinition^.parast^.symtablelevel)>normal_function_level) then
  630. begin
  631. { if we call a nested function in a method, we must }
  632. { push also SELF! }
  633. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  634. { access }
  635. {
  636. begin
  637. loadesi:=false;
  638. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  639. end;
  640. }
  641. if lexlevel=(p^.procdefinition^.parast^.symtablelevel) then
  642. begin
  643. new(r);
  644. reset_reference(r^);
  645. r^.offset:=procinfo.framepointer_offset;
  646. r^.base:=procinfo.framepointer;
  647. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)))
  648. end
  649. { this is only true if the difference is one !!
  650. but it cannot be more !! }
  651. else if (lexlevel=p^.procdefinition^.parast^.symtablelevel-1) then
  652. begin
  653. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,procinfo.framepointer)))
  654. end
  655. else if (lexlevel>p^.procdefinition^.parast^.symtablelevel) then
  656. begin
  657. hregister:=getregister32;
  658. new(r);
  659. reset_reference(r^);
  660. r^.offset:=procinfo.framepointer_offset;
  661. r^.base:=procinfo.framepointer;
  662. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  663. for i:=(p^.procdefinition^.parast^.symtablelevel) to lexlevel-1 do
  664. begin
  665. new(r);
  666. reset_reference(r^);
  667. {we should get the correct frame_pointer_offset at each level
  668. how can we do this !!! }
  669. r^.offset:=procinfo.framepointer_offset;
  670. r^.base:=hregister;
  671. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  672. end;
  673. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hregister)));
  674. ungetregister32(hregister);
  675. end
  676. else
  677. internalerror(25000);
  678. end;
  679. if ((p^.procdefinition^.options and povirtualmethod)<>0) and
  680. not(no_virtual_call) then
  681. begin
  682. { static functions contain the vmt_address in ESI }
  683. { also class methods }
  684. { Here it is quite tricky because it also depends }
  685. { on the methodpointer PM }
  686. if assigned(aktprocsym) then
  687. begin
  688. if ((((aktprocsym^.properties and sp_static)<>0) or
  689. ((aktprocsym^.definition^.options and poclassmethod)<>0)) and
  690. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  691. or
  692. ((p^.procdefinition^.options and postaticmethod)<>0) or
  693. ((p^.procdefinition^.options and poconstructor)<>0) or
  694. { ESI is loaded earlier }
  695. ((p^.procdefinition^.options and poclassmethod)<>0)then
  696. begin
  697. new(r);
  698. reset_reference(r^);
  699. r^.base:=R_ESI;
  700. end
  701. else
  702. begin
  703. new(r);
  704. reset_reference(r^);
  705. r^.base:=R_ESI;
  706. { this is one point where we need vmt_offset (PM) }
  707. r^.offset:= p^.procdefinition^._class^.vmt_offset;
  708. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  709. new(r);
  710. reset_reference(r^);
  711. r^.base:=R_EDI;
  712. end;
  713. end
  714. else
  715. { aktprocsym should be assigned, also in main program }
  716. internalerror(12345);
  717. {
  718. begin
  719. new(r);
  720. reset_reference(r^);
  721. r^.base:=R_ESI;
  722. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  723. new(r);
  724. reset_reference(r^);
  725. r^.base:=R_EDI;
  726. end;
  727. }
  728. if p^.procdefinition^.extnumber=-1 then
  729. internalerror($Da);
  730. r^.offset:=p^.procdefinition^.extnumber*4+12;
  731. {$ifndef TESTOBJEXT}
  732. if (cs_check_range in aktlocalswitches) then
  733. begin
  734. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r^.base)));
  735. emitcall('FPC_CHECK_OBJECT',true);
  736. end;
  737. {$else TESTOBJEXT}
  738. if (cs_check_range in aktlocalswitches) then
  739. begin
  740. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,
  741. newasmsymbol(p^.procdefinition^._class^.vmt_mangledname,0))));
  742. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r^.base)));
  743. emitcall('FPC_CHECK_OBJECT_EXT',true);
  744. end;
  745. {$endif TESTOBJEXT}
  746. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,r)));
  747. end
  748. else if not inlined then
  749. emitcall(p^.procdefinition^.mangledname,
  750. (p^.symtableproc^.symtabletype=unitsymtable) or
  751. ((p^.symtableproc^.symtabletype=objectsymtable) and
  752. (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable))or
  753. ((p^.symtableproc^.symtabletype=withsymtable) and
  754. (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable)))
  755. else { inlined proc }
  756. { inlined code is in inlinecode }
  757. begin
  758. { set poinline again }
  759. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  760. { process the inlinecode }
  761. secondpass(inlinecode);
  762. { free the args }
  763. ungetpersistanttemp(p^.procdefinition^.parast^.address_fixup,
  764. p^.procdefinition^.parast^.datasize);
  765. end;
  766. end
  767. else
  768. { now procedure variable case }
  769. begin
  770. secondpass(p^.right);
  771. { method pointer ? }
  772. if (p^.procdefinition^.options and pomethodpointer)<>0 then
  773. begin
  774. { method pointer can't be in a register }
  775. hregister:=R_NO;
  776. { do some hacking if we call a method pointer }
  777. { which is a class member }
  778. { else ESI is overwritten ! }
  779. if (p^.right^.location.reference.base=R_ESI) or
  780. (p^.right^.location.reference.index=R_ESI) then
  781. begin
  782. del_reference(p^.right^.location.reference);
  783. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  784. newreference(p^.right^.location.reference),R_EDI)));
  785. hregister:=R_EDI;
  786. end;
  787. inc(p^.right^.location.reference.offset,4);
  788. { load ESI }
  789. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  790. newreference(p^.right^.location.reference),R_ESI)));
  791. { push self pointer }
  792. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  793. dec(p^.right^.location.reference.offset,4);
  794. if hregister=R_NO then
  795. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))))
  796. else
  797. exprasmlist^.concat(new(pai386,op_reg(A_CALL,S_NO,hregister)));
  798. del_reference(p^.right^.location.reference);
  799. end
  800. else
  801. begin
  802. case p^.right^.location.loc of
  803. LOC_REGISTER,LOC_CREGISTER:
  804. begin
  805. exprasmlist^.concat(new(pai386,op_reg(A_CALL,S_NO,p^.right^.location.register)));
  806. ungetregister32(p^.right^.location.register);
  807. end
  808. else
  809. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))));
  810. del_reference(p^.right^.location.reference);
  811. end;
  812. end;
  813. end;
  814. { this was only for normal functions
  815. displaced here so we also get
  816. it to work for procvars PM }
  817. if (not inlined) and ((p^.procdefinition^.options and poclearstack)<>0) then
  818. begin
  819. { consider the alignment with the rest (PM) }
  820. inc(pushedparasize,pop_size);
  821. pop_size:=0;
  822. { better than an add on all processors }
  823. if pushedparasize=4 then
  824. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)))
  825. { the pentium has two pipes and pop reg is pairable }
  826. { but the registers must be different! }
  827. else if (pushedparasize=8) and
  828. not(cs_littlesize in aktglobalswitches) and
  829. (aktoptprocessor=ClassP5) and
  830. (procinfo._class=nil) then
  831. begin
  832. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  833. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  834. end
  835. else exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pushedparasize,R_ESP)));
  836. end;
  837. dont_call:
  838. pushedparasize:=oldpushedparasize;
  839. unused:=unusedregisters;
  840. { handle function results }
  841. { structured results are easy to handle.... }
  842. { needed also when result_no_used !! }
  843. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  844. begin
  845. p^.location.loc:=LOC_MEM;
  846. p^.location.reference.symbol:=nil;
  847. p^.location.reference:=funcretref;
  848. end;
  849. { we have only to handle the result if it is used, but }
  850. { ansi/widestrings must be registered, so we can dispose them }
  851. if (p^.resulttype<>pdef(voiddef)) and (p^.return_value_used or
  852. is_ansistring(p^.resulttype) or is_widestring(p^.resulttype)) then
  853. begin
  854. { a contructor could be a function with boolean result }
  855. if (p^.right=nil) and
  856. ((p^.procdefinition^.options and poconstructor)<>0) and
  857. { quick'n'dirty check if it is a class or an object }
  858. (p^.resulttype^.deftype=orddef) then
  859. begin
  860. p^.location.loc:=LOC_FLAGS;
  861. p^.location.resflags:=F_NE;
  862. if extended_new then
  863. begin
  864. {$ifdef test_dest_loc}
  865. if dest_loc_known and (dest_loc_tree=p) then
  866. mov_reg_to_dest(p,S_L,R_EAX)
  867. else
  868. {$endif test_dest_loc}
  869. begin
  870. hregister:=getexplicitregister32(R_EAX);
  871. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  872. p^.location.register:=hregister;
  873. end;
  874. end;
  875. end
  876. { structed results are easy to handle.... }
  877. else if ret_in_param(p^.resulttype) then
  878. begin
  879. {p^.location.loc:=LOC_MEM;
  880. stringdispose(p^.location.reference.symbol);
  881. p^.location.reference:=funcretref;
  882. already done above (PM) }
  883. end
  884. else
  885. begin
  886. if (p^.resulttype^.deftype=orddef) then
  887. begin
  888. p^.location.loc:=LOC_REGISTER;
  889. case porddef(p^.resulttype)^.typ of
  890. s32bit,u32bit,bool32bit :
  891. begin
  892. {$ifdef test_dest_loc}
  893. if dest_loc_known and (dest_loc_tree=p) then
  894. mov_reg_to_dest(p,S_L,R_EAX)
  895. else
  896. {$endif test_dest_loc}
  897. begin
  898. hregister:=getexplicitregister32(R_EAX);
  899. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  900. p^.location.register:=hregister;
  901. end;
  902. end;
  903. uchar,u8bit,bool8bit,s8bit:
  904. begin
  905. {$ifdef test_dest_loc}
  906. if dest_loc_known and (dest_loc_tree=p) then
  907. mov_reg_to_dest(p,S_B,R_AL)
  908. else
  909. {$endif test_dest_loc}
  910. begin
  911. hregister:=getexplicitregister32(R_EAX);
  912. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  913. p^.location.register:=reg32toreg8(hregister);
  914. end;
  915. end;
  916. s16bit,u16bit,bool16bit :
  917. begin
  918. {$ifdef test_dest_loc}
  919. if dest_loc_known and (dest_loc_tree=p) then
  920. mov_reg_to_dest(p,S_W,R_AX)
  921. else
  922. {$endif test_dest_loc}
  923. begin
  924. hregister:=getexplicitregister32(R_EAX);
  925. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  926. p^.location.register:=reg32toreg16(hregister);
  927. end;
  928. end;
  929. s64bitint,u64bit:
  930. begin
  931. {$ifdef test_dest_loc}
  932. {$error Don't know what to do here}
  933. {$endif test_dest_loc}
  934. hregister:=getexplicitregister32(R_EAX);
  935. hregister2:=getexplicitregister32(R_EDX);
  936. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  937. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  938. p^.location.registerlow:=hregister;
  939. p^.location.registerhigh:=hregister2;
  940. end;
  941. else internalerror(7);
  942. end
  943. end
  944. else if (p^.resulttype^.deftype=floatdef) then
  945. case pfloatdef(p^.resulttype)^.typ of
  946. f32bit:
  947. begin
  948. p^.location.loc:=LOC_REGISTER;
  949. {$ifdef test_dest_loc}
  950. if dest_loc_known and (dest_loc_tree=p) then
  951. mov_reg_to_dest(p,S_L,R_EAX)
  952. else
  953. {$endif test_dest_loc}
  954. begin
  955. hregister:=getexplicitregister32(R_EAX);
  956. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  957. p^.location.register:=hregister;
  958. end;
  959. end;
  960. else
  961. p^.location.loc:=LOC_FPU;
  962. end
  963. else
  964. begin
  965. p^.location.loc:=LOC_REGISTER;
  966. {$ifdef test_dest_loc}
  967. if dest_loc_known and (dest_loc_tree=p) then
  968. mov_reg_to_dest(p,S_L,R_EAX)
  969. else
  970. {$endif test_dest_loc}
  971. begin
  972. hregister:=getexplicitregister32(R_EAX);
  973. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  974. p^.location.register:=hregister;
  975. if is_ansistring(p^.resulttype) or
  976. is_widestring(p^.resulttype) then
  977. begin
  978. gettempansistringreference(hr);
  979. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,p^.location.register,
  980. newreference(hr))));
  981. { unnessary ansi/wide strings are imm. disposed }
  982. if not(p^.return_value_used) then
  983. begin
  984. pushusedregisters(pushedregs,$ff);
  985. emitpushreferenceaddr(exprasmlist,hr);
  986. if is_ansistring(p^.resulttype) then
  987. begin
  988. exprasmlist^.concat(new(pai386,
  989. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ANSISTR_DECR_REF'))));
  990. if not (cs_compilesystem in aktmoduleswitches) then
  991. concat_external('FPC_ANSISTR_DECR_REF',EXT_NEAR);
  992. end
  993. else
  994. begin
  995. exprasmlist^.concat(new(pai386,
  996. op_sym(A_CALL,S_NO,newasmsymbol('FPC_WIDESTR_DECR_REF'))));
  997. if not (cs_compilesystem in aktmoduleswitches) then
  998. concat_external('FPC_WIDESTR_DECR_REF',EXT_NEAR);
  999. end;
  1000. ungetiftemp(hr);
  1001. popusedregisters(pushedregs);
  1002. end
  1003. else
  1004. oldrl^.concat(new(ptemptodestroy,init(hr,p^.resulttype)));
  1005. end;
  1006. end;
  1007. end;
  1008. end;
  1009. end;
  1010. { perhaps i/o check ? }
  1011. if iolabel<>nil then
  1012. begin
  1013. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,newasmsymbol(lab2str(iolabel)))));
  1014. emitcall('FPC_IOCHECK',true);
  1015. end;
  1016. if pop_size>0 then
  1017. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pop_size,R_ESP)));
  1018. { release temp. ansi strings }
  1019. removetemps(exprasmlist,temptoremove);
  1020. dispose(temptoremove,done);
  1021. temptoremove:=oldrl;
  1022. { restore registers }
  1023. popusedregisters(pushed);
  1024. { at last, restore instance pointer (SELF) }
  1025. if loadesi then
  1026. maybe_loadesi;
  1027. pp:=params;
  1028. while assigned(pp) do
  1029. begin
  1030. if assigned(pp^.left) then
  1031. begin
  1032. if pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM] then
  1033. ungetiftemp(pp^.left^.location.reference);
  1034. { process also all nodes of an array of const }
  1035. if pp^.left^.treetype=arrayconstructn then
  1036. begin
  1037. if assigned(pp^.left^.left) then
  1038. begin
  1039. hp:=pp^.left;
  1040. while assigned(hp) do
  1041. begin
  1042. if hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM] then
  1043. ungetiftemp(hp^.left^.location.reference);
  1044. hp:=hp^.right;
  1045. end;
  1046. end;
  1047. end;
  1048. end;
  1049. pp:=pp^.right;
  1050. end;
  1051. if inlined then
  1052. ungetpersistanttemp(inlinecode^.retoffset,4);
  1053. disposetree(params);
  1054. { from now on the result can be freed normally }
  1055. if inlined and ret_in_param(p^.resulttype) then
  1056. persistanttemptonormal(funcretref.offset);
  1057. { if return value is not used }
  1058. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1059. begin
  1060. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1061. { release unused temp }
  1062. ungetiftemp(p^.location.reference)
  1063. else if p^.location.loc=LOC_FPU then
  1064. { release FPU stack }
  1065. exprasmlist^.concat(new(pai386,op_none(A_FDECSTP,S_NO)));
  1066. end;
  1067. end;
  1068. {*****************************************************************************
  1069. SecondProcInlineN
  1070. *****************************************************************************}
  1071. procedure secondprocinline(var p : ptree);
  1072. var st : psymtable;
  1073. oldprocsym : pprocsym;
  1074. para_size : longint;
  1075. oldprocinfo : tprocinfo;
  1076. { just dummies for genentrycode }
  1077. nostackframe,make_global : boolean;
  1078. proc_names : tstringcontainer;
  1079. inlineentrycode,inlineexitcode : paasmoutput;
  1080. oldexitlabel,oldexit2label,oldquickexitlabel:Plabel;
  1081. begin
  1082. oldexitlabel:=aktexitlabel;
  1083. oldexit2label:=aktexit2label;
  1084. oldquickexitlabel:=quickexitlabel;
  1085. getlabel(aktexitlabel);
  1086. getlabel(aktexit2label);
  1087. oldprocsym:=aktprocsym;
  1088. oldprocinfo:=procinfo;
  1089. { set the return value }
  1090. aktprocsym:=p^.inlineprocsym;
  1091. procinfo.retdef:=aktprocsym^.definition^.retdef;
  1092. procinfo.retoffset:=p^.retoffset;
  1093. { arg space has been filled by the parent secondcall }
  1094. st:=aktprocsym^.definition^.localst;
  1095. { set it to the same lexical level }
  1096. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1097. if st^.datasize>0 then
  1098. begin
  1099. st^.address_fixup:=gettempofsizepersistant(st^.datasize);
  1100. {$ifdef extdebug}
  1101. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1102. exprasmlist^.concat(new(pai_asm_comment,init(strpnew(
  1103. 'local symtable is at offset '+tostr(st^.address_fixup)))));
  1104. {$endif extdebug}
  1105. end;
  1106. {$ifdef extdebug}
  1107. exprasmlist^.concat(new(pai_asm_comment,init('Start of inlined proc')));
  1108. {$endif extdebug}
  1109. { takes care of local data initialization }
  1110. inlineentrycode:=new(paasmoutput,init);
  1111. inlineexitcode:=new(paasmoutput,init);
  1112. proc_names.init;
  1113. para_size:=p^.para_size;
  1114. make_global:=false; { to avoid warning }
  1115. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  1116. exprasmlist^.concatlist(inlineentrycode);
  1117. secondpass(p^.inlinetree);
  1118. genexitcode(inlineexitcode,0,false,true);
  1119. exprasmlist^.concatlist(inlineexitcode);
  1120. {$ifdef extdebug}
  1121. exprasmlist^.concat(new(pai_asm_comment,init('End of inlined proc')));
  1122. {$endif extdebug}
  1123. {we can free the local data now, reset also the fixup address }
  1124. if st^.datasize>0 then
  1125. begin
  1126. ungetpersistanttemp(st^.address_fixup,st^.datasize);
  1127. st^.address_fixup:=0;
  1128. end;
  1129. aktprocsym:=oldprocsym;
  1130. freelabel(aktexitlabel);
  1131. freelabel(aktexit2label);
  1132. aktexitlabel:=oldexitlabel;
  1133. aktexit2label:=oldexit2label;
  1134. quickexitlabel:=oldquickexitlabel;
  1135. procinfo:=oldprocinfo;
  1136. end;
  1137. end.
  1138. {
  1139. $Log$
  1140. Revision 1.71 1999-03-31 13:55:04 peter
  1141. * assembler inlining working for ag386bin
  1142. Revision 1.70 1999/03/24 23:16:46 peter
  1143. * fixed bugs 212,222,225,227,229,231,233
  1144. Revision 1.69 1999/02/25 21:02:21 peter
  1145. * ag386bin updates
  1146. + coff writer
  1147. Revision 1.68 1999/02/22 02:15:04 peter
  1148. * updates for ag386bin
  1149. Revision 1.67 1999/02/11 09:46:21 pierre
  1150. * fix for normal method calls inside static methods :
  1151. WARNING there were both parser and codegen errors !!
  1152. added static_call boolean to calln tree
  1153. Revision 1.66 1999/02/09 15:45:46 florian
  1154. + complex results for assembler functions, fixes bug0155
  1155. Revision 1.65 1999/02/08 11:29:04 pierre
  1156. * fix for bug0214
  1157. several problems where combined
  1158. search_class_member did not set srsymtable
  1159. => in do_member_read the call node got a wrong symtable
  1160. in cg386cal the vmt was pushed twice without chacking if it exists
  1161. now %esi is set to zero and pushed if not vmt
  1162. (not very efficient but should work !)
  1163. Revision 1.64 1999/02/04 10:49:39 florian
  1164. + range checking for ansi- and widestrings
  1165. * made it compilable with TP
  1166. Revision 1.63 1999/02/03 10:18:14 pierre
  1167. * conditionnal code for extended check of virtual methods
  1168. Revision 1.62 1999/02/02 23:52:32 florian
  1169. * problem with calls to method pointers in methods fixed
  1170. - double ansistrings temp management removed
  1171. Revision 1.61 1999/02/02 11:04:36 florian
  1172. * class destructors fixed, class instances weren't disposed correctly
  1173. Revision 1.60 1999/01/28 23:56:44 florian
  1174. * the reference in the result location of a function call wasn't resetted =>
  1175. problem with unallowed far pointer, is solved now
  1176. Revision 1.59 1999/01/27 00:13:52 florian
  1177. * "procedure of object"-stuff fixed
  1178. Revision 1.58 1999/01/21 22:10:35 peter
  1179. * fixed array of const
  1180. * generic platform independent high() support
  1181. Revision 1.57 1999/01/21 16:40:51 pierre
  1182. * fix for constructor inside with statements
  1183. Revision 1.56 1998/12/30 13:41:05 peter
  1184. * released valuepara
  1185. Revision 1.55 1998/12/22 13:10:58 florian
  1186. * memory leaks for ansistring type casts fixed
  1187. Revision 1.54 1998/12/19 00:23:41 florian
  1188. * ansistring memory leaks fixed
  1189. Revision 1.53 1998/12/11 00:02:47 peter
  1190. + globtype,tokens,version unit splitted from globals
  1191. Revision 1.52 1998/12/10 14:39:29 florian
  1192. * bug with p(const a : ansistring) fixed
  1193. * duplicate constant ansistrings were handled wrong, fixed
  1194. Revision 1.51 1998/12/10 09:47:15 florian
  1195. + basic operations with int64/qord (compiler with -dint64)
  1196. + rtti of enumerations extended: names are now written
  1197. Revision 1.50 1998/12/06 13:12:44 florian
  1198. * better code generation for classes which are passed as parameters to
  1199. subroutines
  1200. Revision 1.49 1998/11/30 09:43:00 pierre
  1201. * some range check bugs fixed (still not working !)
  1202. + added DLL writing support for win32 (also accepts variables)
  1203. + TempAnsi for code that could be used for Temporary ansi strings
  1204. handling
  1205. Revision 1.48 1998/11/27 14:50:30 peter
  1206. + open strings, $P switch support
  1207. Revision 1.47 1998/11/26 21:30:03 peter
  1208. * fix for valuepara
  1209. Revision 1.46 1998/11/26 14:39:10 peter
  1210. * ansistring -> pchar fixed
  1211. * ansistring constants fixed
  1212. * ansistring constants are now written once
  1213. Revision 1.45 1998/11/18 15:44:07 peter
  1214. * VALUEPARA for tp7 compatible value parameters
  1215. Revision 1.44 1998/11/16 15:35:36 peter
  1216. * rename laod/copystring -> load/copyshortstring
  1217. * fixed int-bool cnv bug
  1218. + char-ansistring conversion
  1219. Revision 1.43 1998/11/15 16:32:33 florian
  1220. * some stuff of Pavel implement (win32 dll creation)
  1221. * bug with ansistring function results fixed
  1222. Revision 1.42 1998/11/13 15:40:13 pierre
  1223. + added -Se in Makefile cvstest target
  1224. + lexlevel cleanup
  1225. normal_function_level main_program_level and unit_init_level defined
  1226. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  1227. (test added in code !)
  1228. * -Un option was wrong
  1229. * _FAIL and _SELF only keyword inside
  1230. constructors and methods respectively
  1231. Revision 1.41 1998/11/12 11:19:40 pierre
  1232. * fix for first line of function break
  1233. Revision 1.40 1998/11/10 10:09:08 peter
  1234. * va_list -> array of const
  1235. Revision 1.39 1998/11/09 11:44:33 peter
  1236. + va_list for printf support
  1237. Revision 1.38 1998/10/21 15:12:49 pierre
  1238. * bug fix for IOCHECK inside a procedure with iocheck modifier
  1239. * removed the GPF for unexistant overloading
  1240. (firstcall was called with procedinition=nil !)
  1241. * changed typen to what Florian proposed
  1242. gentypenode(p : pdef) sets the typenodetype field
  1243. and resulttype is only set if inside bt_type block !
  1244. Revision 1.37 1998/10/21 08:39:57 florian
  1245. + ansistring operator +
  1246. + $h and string[n] for n>255 added
  1247. * small problem with TP fixed
  1248. Revision 1.36 1998/10/20 08:06:39 pierre
  1249. * several memory corruptions due to double freemem solved
  1250. => never use p^.loc.location:=p^.left^.loc.location;
  1251. + finally I added now by default
  1252. that ra386dir translates global and unit symbols
  1253. + added a first field in tsymtable and
  1254. a nextsym field in tsym
  1255. (this allows to obtain ordered type info for
  1256. records and objects in gdb !)
  1257. Revision 1.35 1998/10/16 08:51:45 peter
  1258. + target_os.stackalignment
  1259. + stack can be aligned at 2 or 4 byte boundaries
  1260. Revision 1.34 1998/10/09 08:56:22 pierre
  1261. * several memory leaks fixed
  1262. Revision 1.33 1998/10/06 17:16:39 pierre
  1263. * some memory leaks fixed (thanks to Peter for heaptrc !)
  1264. Revision 1.32 1998/10/01 09:22:52 peter
  1265. * fixed value openarray
  1266. * ungettemp of arrayconstruct
  1267. Revision 1.31 1998/09/28 16:57:15 pierre
  1268. * changed all length(p^.value_str^) into str_length(p)
  1269. to get it work with and without ansistrings
  1270. * changed sourcefiles field of tmodule to a pointer
  1271. Revision 1.30 1998/09/26 15:03:02 florian
  1272. * small problems with DOM and excpetions fixed (code generation
  1273. of raise was wrong and self was sometimes destroyed :()
  1274. Revision 1.29 1998/09/25 00:04:00 florian
  1275. * problems when calling class methods fixed
  1276. Revision 1.28 1998/09/24 14:27:37 peter
  1277. * some better support for openarray
  1278. Revision 1.27 1998/09/24 09:02:13 peter
  1279. * rewritten isconvertable to use case
  1280. * array of .. and single variable are compatible
  1281. Revision 1.26 1998/09/21 08:45:06 pierre
  1282. + added vmt_offset in tobjectdef.write for fututre use
  1283. (first steps to have objects without vmt if no virtual !!)
  1284. + added fpu_used field for tabstractprocdef :
  1285. sets this level to 2 if the functions return with value in FPU
  1286. (is then set to correct value at parsing of implementation)
  1287. THIS MIGHT refuse some code with FPU expression too complex
  1288. that were accepted before and even in some cases
  1289. that don't overflow in fact
  1290. ( like if f : float; is a forward that finally in implementation
  1291. only uses one fpu register !!)
  1292. Nevertheless I think that it will improve security on
  1293. FPU operations !!
  1294. * most other changes only for UseBrowser code
  1295. (added symtable references for record and objects)
  1296. local switch for refs to args and local of each function
  1297. (static symtable still missing)
  1298. UseBrowser still not stable and probably broken by
  1299. the definition hash array !!
  1300. Revision 1.25 1998/09/20 12:26:35 peter
  1301. * merged fixes
  1302. Revision 1.24 1998/09/17 09:42:10 peter
  1303. + pass_2 for cg386
  1304. * Message() -> CGMessage() for pass_1/pass_2
  1305. Revision 1.23 1998/09/14 10:43:45 peter
  1306. * all internal RTL functions start with FPC_
  1307. Revision 1.22.2.1 1998/09/20 12:20:06 peter
  1308. * Fixed stack not on 4 byte boundary when doing a call
  1309. Revision 1.22 1998/09/04 08:41:37 peter
  1310. * updated some error CGMessages
  1311. Revision 1.21 1998/09/01 12:47:57 peter
  1312. * use pdef^.size instead of orddef^.typ
  1313. Revision 1.20 1998/08/31 12:22:15 peter
  1314. * secondinline moved to cg386inl
  1315. Revision 1.19 1998/08/31 08:52:03 peter
  1316. * fixed error 10 with succ() and pref()
  1317. Revision 1.18 1998/08/20 21:36:38 peter
  1318. * fixed 'with object do' bug
  1319. Revision 1.17 1998/08/19 16:07:36 jonas
  1320. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1321. Revision 1.16 1998/08/18 09:24:36 pierre
  1322. * small warning position bug fixed
  1323. * support_mmx switches splitting was missing
  1324. * rhide error and warning output corrected
  1325. Revision 1.15 1998/08/13 11:00:09 peter
  1326. * fixed procedure<>procedure construct
  1327. Revision 1.14 1998/08/11 14:05:33 peter
  1328. * fixed sizeof(array of char)
  1329. Revision 1.13 1998/08/10 14:49:45 peter
  1330. + localswitches, moduleswitches, globalswitches splitting
  1331. Revision 1.12 1998/07/30 13:30:31 florian
  1332. * final implemenation of exception support, maybe it needs
  1333. some fixes :)
  1334. Revision 1.11 1998/07/24 22:16:52 florian
  1335. * internal error 10 together with array access fixed. I hope
  1336. that's the final fix.
  1337. Revision 1.10 1998/07/18 22:54:23 florian
  1338. * some ansi/wide/longstring support fixed:
  1339. o parameter passing
  1340. o returning as result from functions
  1341. Revision 1.9 1998/07/07 17:40:37 peter
  1342. * packrecords 4 works
  1343. * word aligning of parameters
  1344. Revision 1.8 1998/07/06 15:51:15 michael
  1345. Added length checking for string reading
  1346. Revision 1.7 1998/07/06 14:19:51 michael
  1347. + Added calls for reading/writing ansistrings
  1348. Revision 1.6 1998/07/01 15:28:48 peter
  1349. + better writeln/readln handling, now 100% like tp7
  1350. Revision 1.5 1998/06/25 14:04:17 peter
  1351. + internal inc/dec
  1352. Revision 1.4 1998/06/25 08:48:06 florian
  1353. * first version of rtti support
  1354. Revision 1.3 1998/06/09 16:01:33 pierre
  1355. + added procedure directive parsing for procvars
  1356. (accepted are popstack cdecl and pascal)
  1357. + added C vars with the following syntax
  1358. var C calias 'true_c_name';(can be followed by external)
  1359. reason is that you must add the Cprefix
  1360. which is target dependent
  1361. Revision 1.2 1998/06/08 13:13:29 pierre
  1362. + temporary variables now in temp_gen.pas unit
  1363. because it is processor independent
  1364. * mppc68k.bat modified to undefine i386 and support_mmx
  1365. (which are defaults for i386)
  1366. Revision 1.1 1998/06/05 17:44:10 peter
  1367. * splitted cgi386
  1368. }