cg386cal.pas 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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 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 AnsiStrRef}
  21. uses
  22. symtable,tree;
  23. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  24. push_from_left_to_right,inlined,dword_align : 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. symconst,aasm,types,
  32. {$ifdef GDB}
  33. gdb,
  34. {$endif GDB}
  35. hcodegen,temp_gen,pass_2,
  36. cpubase,cpuasm,
  37. cgai386,tgeni386,cg386ld;
  38. {*****************************************************************************
  39. SecondCallParaN
  40. *****************************************************************************}
  41. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  42. push_from_left_to_right,inlined,dword_align : boolean;para_offset : longint);
  43. procedure maybe_push_high;
  44. begin
  45. { open array ? }
  46. { defcoll^.data can be nil for read/write }
  47. if assigned(defcoll^.paratype.def) and
  48. push_high_param(defcoll^.paratype.def) then
  49. begin
  50. if assigned(p^.hightree) then
  51. begin
  52. secondpass(p^.hightree);
  53. { this is a longint anyway ! }
  54. push_value_para(p^.hightree,inlined,para_offset,4);
  55. end
  56. else
  57. internalerror(432645);
  58. end;
  59. end;
  60. var
  61. otlabel,oflabel : pasmlabel;
  62. align : longint;
  63. { temporary variables: }
  64. tempdeftype : tdeftype;
  65. r : preference;
  66. begin
  67. { push from left to right if specified }
  68. if push_from_left_to_right and assigned(p^.right) then
  69. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  70. inlined,dword_align,para_offset);
  71. otlabel:=truelabel;
  72. oflabel:=falselabel;
  73. getlabel(truelabel);
  74. getlabel(falselabel);
  75. secondpass(p^.left);
  76. { filter array constructor with c styled args }
  77. if is_array_constructor(p^.left^.resulttype) and p^.left^.cargs then
  78. begin
  79. { nothing, everything is already pushed }
  80. end
  81. { in codegen.handleread.. defcoll^.data is set to nil }
  82. else if assigned(defcoll^.paratype.def) and
  83. (defcoll^.paratype.def^.deftype=formaldef) then
  84. begin
  85. { allow @var }
  86. inc(pushedparasize,4);
  87. if p^.left^.treetype=addrn then
  88. begin
  89. { always a register }
  90. if inlined then
  91. begin
  92. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  93. emit_reg_ref(A_MOV,S_L,
  94. p^.left^.location.register,r);
  95. end
  96. else
  97. emit_reg(A_PUSH,S_L,p^.left^.location.register);
  98. ungetregister32(p^.left^.location.register);
  99. end
  100. else
  101. begin
  102. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  103. CGMessage(type_e_mismatch)
  104. else
  105. begin
  106. if inlined then
  107. begin
  108. {$ifndef noAllocEdi}
  109. getexplicitregister32(R_EDI);
  110. {$endif noAllocEdi}
  111. emit_ref_reg(A_LEA,S_L,
  112. newreference(p^.left^.location.reference),R_EDI);
  113. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  114. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  115. {$ifndef noAllocEdi}
  116. ungetregister32(R_EDI);
  117. {$endif noAllocEdi}
  118. end
  119. else
  120. emitpushreferenceaddr(p^.left^.location.reference);
  121. del_reference(p^.left^.location.reference);
  122. end;
  123. end;
  124. end
  125. { handle call by reference parameter }
  126. else if (defcoll^.paratyp=vs_var) then
  127. begin
  128. if (p^.left^.location.loc<>LOC_REFERENCE) then
  129. CGMessage(cg_e_var_must_be_reference);
  130. maybe_push_high;
  131. inc(pushedparasize,4);
  132. if inlined then
  133. begin
  134. {$ifndef noAllocEdi}
  135. getexplicitregister32(R_EDI);
  136. {$endif noAllocEdi}
  137. emit_ref_reg(A_LEA,S_L,
  138. newreference(p^.left^.location.reference),R_EDI);
  139. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  140. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  141. {$ifndef noAllocEdi}
  142. ungetregister32(R_EDI);
  143. {$endif noAllocEdi}
  144. end
  145. else
  146. emitpushreferenceaddr(p^.left^.location.reference);
  147. del_reference(p^.left^.location.reference);
  148. end
  149. else
  150. begin
  151. tempdeftype:=p^.resulttype^.deftype;
  152. if tempdeftype=filedef then
  153. CGMessage(cg_e_file_must_call_by_reference);
  154. { open array must always push the address, this is needed to
  155. also push addr of small arrays (PFV) }
  156. if (assigned(defcoll^.paratype.def) and
  157. is_open_array(defcoll^.paratype.def)) or
  158. push_addr_param(p^.resulttype) then
  159. begin
  160. maybe_push_high;
  161. inc(pushedparasize,4);
  162. if inlined then
  163. begin
  164. {$ifndef noAllocEdi}
  165. getexplicitregister32(R_EDI);
  166. {$endif noAllocEdi}
  167. emit_ref_reg(A_LEA,S_L,
  168. newreference(p^.left^.location.reference),R_EDI);
  169. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  170. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  171. {$ifndef noAllocEdi}
  172. ungetregister32(R_EDI);
  173. {$endif noAllocEdi}
  174. end
  175. else
  176. emitpushreferenceaddr(p^.left^.location.reference);
  177. del_reference(p^.left^.location.reference);
  178. end
  179. else
  180. begin
  181. align:=target_os.stackalignment;
  182. if dword_align then
  183. align:=4;
  184. push_value_para(p^.left,inlined,para_offset,align);
  185. end;
  186. end;
  187. truelabel:=otlabel;
  188. falselabel:=oflabel;
  189. { push from right to left }
  190. if not push_from_left_to_right and assigned(p^.right) then
  191. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  192. inlined,dword_align,para_offset);
  193. end;
  194. {*****************************************************************************
  195. SecondCallN
  196. *****************************************************************************}
  197. procedure secondcalln(var p : ptree);
  198. var
  199. unusedregisters : tregisterset;
  200. usablecount : byte;
  201. pushed : tpushed;
  202. hr,funcretref : treference;
  203. hregister,hregister2 : tregister;
  204. oldpushedparasize : longint;
  205. { true if ESI must be loaded again after the subroutine }
  206. loadesi : boolean;
  207. { true if a virtual method must be called directly }
  208. no_virtual_call : boolean;
  209. { true if we produce a con- or destrutor in a call }
  210. is_con_or_destructor : boolean;
  211. { true if a constructor is called again }
  212. extended_new : boolean;
  213. { adress returned from an I/O-error }
  214. iolabel : pasmlabel;
  215. { lexlevel count }
  216. i : longint;
  217. { help reference pointer }
  218. r : preference;
  219. hp,
  220. pp,params : ptree;
  221. inlined : boolean;
  222. inlinecode : ptree;
  223. para_offset : longint;
  224. { instruction for alignement correction }
  225. { corr : paicpu;}
  226. { we must pop this size also after !! }
  227. { must_pop : boolean; }
  228. pop_size : longint;
  229. pop_allowed : boolean;
  230. label
  231. dont_call;
  232. begin
  233. reset_reference(p^.location.reference);
  234. extended_new:=false;
  235. iolabel:=nil;
  236. inlinecode:=nil;
  237. inlined:=false;
  238. loadesi:=true;
  239. no_virtual_call:=false;
  240. unusedregisters:=unused;
  241. usablecount:=usablereg32;
  242. if not assigned(p^.procdefinition) then
  243. exit;
  244. if (pocall_inline in p^.procdefinition^.proccalloptions) then
  245. begin
  246. inlined:=true;
  247. inlinecode:=p^.right;
  248. { set it to the same lexical level as the local symtable, becuase
  249. the para's are stored there }
  250. pprocdef(p^.procdefinition)^.parast^.symtablelevel:=aktprocsym^.definition^.localst^.symtablelevel;
  251. if assigned(p^.left) then
  252. inlinecode^.para_offset:=gettempofsizepersistant(inlinecode^.para_size);
  253. pprocdef(p^.procdefinition)^.parast^.address_fixup:=inlinecode^.para_offset;
  254. {$ifdef extdebug}
  255. Comment(V_debug,
  256. 'inlined parasymtable is at offset '
  257. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup));
  258. exprasmlist^.concat(new(pai_asm_comment,init(
  259. strpnew('inlined parasymtable is at offset '
  260. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup)))));
  261. {$endif extdebug}
  262. p^.right:=nil;
  263. { disable further inlining of the same proc
  264. in the args }
  265. {$ifdef INCLUDEOK}
  266. exclude(p^.procdefinition^.proccalloptions,pocall_inline);
  267. {$else}
  268. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions-[pocall_inline];
  269. {$endif}
  270. end;
  271. { only if no proc var }
  272. if not(assigned(p^.right)) then
  273. is_con_or_destructor:=(p^.procdefinition^.proctypeoption in [potype_constructor,potype_destructor]);
  274. { proc variables destroy all registers }
  275. if (p^.right=nil) and
  276. { virtual methods too }
  277. not(po_virtualmethod in p^.procdefinition^.procoptions) then
  278. begin
  279. if (cs_check_io in aktlocalswitches) and
  280. (po_iocheck in p^.procdefinition^.procoptions) and
  281. not(po_iocheck in aktprocsym^.definition^.procoptions) then
  282. begin
  283. getlabel(iolabel);
  284. emitlab(iolabel);
  285. end
  286. else
  287. iolabel:=nil;
  288. { save all used registers }
  289. pushusedregisters(pushed,pprocdef(p^.procdefinition)^.usedregisters);
  290. { give used registers through }
  291. usedinproc:=usedinproc or pprocdef(p^.procdefinition)^.usedregisters;
  292. end
  293. else
  294. begin
  295. pushusedregisters(pushed,$ff);
  296. usedinproc:=$ff;
  297. { no IO check for methods and procedure variables }
  298. iolabel:=nil;
  299. end;
  300. { generate the code for the parameter and push them }
  301. oldpushedparasize:=pushedparasize;
  302. pushedparasize:=0;
  303. pop_size:=0;
  304. { no inc esp for inlined procedure
  305. and for objects constructors PM }
  306. if inlined or
  307. ((p^.right=nil) and
  308. (p^.procdefinition^.proctypeoption=potype_constructor) and
  309. { quick'n'dirty check if it is a class or an object }
  310. (p^.resulttype^.deftype=orddef)) then
  311. pop_allowed:=false
  312. else
  313. pop_allowed:=true;
  314. if pop_allowed then
  315. begin
  316. { Old pushedsize aligned on 4 ? }
  317. i:=oldpushedparasize and 3;
  318. if i>0 then
  319. inc(pop_size,4-i);
  320. { This parasize aligned on 4 ? }
  321. i:=p^.procdefinition^.para_size and 3;
  322. if i>0 then
  323. inc(pop_size,4-i);
  324. { insert the opcode and update pushedparasize }
  325. { never push 4 or more !! }
  326. pop_size:=pop_size mod 4;
  327. if pop_size>0 then
  328. begin
  329. inc(pushedparasize,pop_size);
  330. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  331. {$ifdef GDB}
  332. if (cs_debuginfo in aktmoduleswitches) and
  333. (exprasmlist^.first=exprasmlist^.last) then
  334. exprasmlist^.concat(new(pai_force_line,init));
  335. {$endif GDB}
  336. end;
  337. end;
  338. if (p^.resulttype<>pdef(voiddef)) and
  339. ret_in_param(p^.resulttype) then
  340. begin
  341. funcretref.symbol:=nil;
  342. {$ifdef test_dest_loc}
  343. if dest_loc_known and (dest_loc_tree=p) and
  344. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  345. begin
  346. funcretref:=dest_loc.reference;
  347. if assigned(dest_loc.reference.symbol) then
  348. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  349. in_dest_loc:=true;
  350. end
  351. else
  352. {$endif test_dest_loc}
  353. if inlined then
  354. begin
  355. reset_reference(funcretref);
  356. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.rettype.def^.size);
  357. funcretref.base:=procinfo^.framepointer;
  358. end
  359. else
  360. gettempofsizereference(p^.procdefinition^.rettype.def^.size,funcretref);
  361. end;
  362. if assigned(p^.left) then
  363. begin
  364. { be found elsewhere }
  365. if inlined then
  366. para_offset:=pprocdef(p^.procdefinition)^.parast^.address_fixup+
  367. pprocdef(p^.procdefinition)^.parast^.datasize
  368. else
  369. para_offset:=0;
  370. if assigned(p^.right) then
  371. secondcallparan(p^.left,pparaitem(pabstractprocdef(p^.right^.resulttype)^.para^.first),
  372. (pocall_leftright in p^.procdefinition^.proccalloptions),
  373. inlined,
  374. (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  375. (pocall_stdcall in p^.procdefinition^.proccalloptions),
  376. para_offset)
  377. else
  378. secondcallparan(p^.left,pparaitem(p^.procdefinition^.para^.first),
  379. (pocall_leftright in p^.procdefinition^.proccalloptions),
  380. inlined,
  381. (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  382. (pocall_stdcall in p^.procdefinition^.proccalloptions),
  383. para_offset);
  384. end;
  385. params:=p^.left;
  386. p^.left:=nil;
  387. if inlined then
  388. inlinecode^.retoffset:=gettempofsizepersistant(4);
  389. if ret_in_param(p^.resulttype) then
  390. begin
  391. { This must not be counted for C code
  392. complex return address is removed from stack
  393. by function itself ! }
  394. {$ifdef OLD_C_STACK}
  395. inc(pushedparasize,4); { lets try without it PM }
  396. {$endif not OLD_C_STACK}
  397. if inlined then
  398. begin
  399. {$ifndef noAllocEdi}
  400. getexplicitregister32(R_EDI);
  401. {$endif noAllocEdi}
  402. emit_ref_reg(A_LEA,S_L,
  403. newreference(funcretref),R_EDI);
  404. r:=new_reference(procinfo^.framepointer,inlinecode^.retoffset);
  405. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  406. {$ifndef noAllocEdi}
  407. ungetregister32(R_EDI);
  408. {$endif noAllocEdi}
  409. end
  410. else
  411. emitpushreferenceaddr(funcretref);
  412. end;
  413. { procedure variable ? }
  414. if (p^.right=nil) then
  415. begin
  416. { overloaded operator have no symtable }
  417. { push self }
  418. if assigned(p^.symtable) and
  419. (p^.symtable^.symtabletype=withsymtable) then
  420. begin
  421. { dirty trick to avoid the secondcall below }
  422. p^.methodpointer:=genzeronode(callparan);
  423. p^.methodpointer^.location.loc:=LOC_REGISTER;
  424. p^.methodpointer^.location.register:=R_ESI;
  425. { ARGHHH this is wrong !!!
  426. if we can init from base class for a child
  427. class that the wrong VMT will be
  428. transfered to constructor !! }
  429. p^.methodpointer^.resulttype:=
  430. ptree(pwithsymtable(p^.symtable)^.withnode)^.left^.resulttype;
  431. { change dispose type !! }
  432. p^.disposetyp:=dt_mbleft_and_method;
  433. { make a reference }
  434. new(r);
  435. reset_reference(r^);
  436. { if assigned(ptree(pwithsymtable(p^.symtable)^.withnode)^.pref) then
  437. begin
  438. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.pref^;
  439. end
  440. else
  441. begin
  442. r^.offset:=p^.symtable^.datasize;
  443. r^.base:=procinfo^.framepointer;
  444. end; }
  445. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^;
  446. if (not pwithsymtable(p^.symtable)^.direct_with) or
  447. pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  448. emit_ref_reg(A_MOV,S_L,r,R_ESI)
  449. else
  450. emit_ref_reg(A_LEA,S_L,r,R_ESI);
  451. end;
  452. { push self }
  453. if assigned(p^.symtable) and
  454. ((p^.symtable^.symtabletype=objectsymtable) or
  455. (p^.symtable^.symtabletype=withsymtable)) then
  456. begin
  457. if assigned(p^.methodpointer) then
  458. begin
  459. {
  460. if p^.methodpointer^.resulttype=classrefdef then
  461. begin
  462. two possibilities:
  463. 1. constructor
  464. 2. class method
  465. end
  466. else }
  467. begin
  468. case p^.methodpointer^.treetype of
  469. typen:
  470. begin
  471. { direct call to inherited method }
  472. if (po_abstractmethod in p^.procdefinition^.procoptions) then
  473. begin
  474. CGMessage(cg_e_cant_call_abstract_method);
  475. goto dont_call;
  476. end;
  477. { generate no virtual call }
  478. no_virtual_call:=true;
  479. if (sp_static in p^.symtableprocentry^.symoptions) then
  480. begin
  481. { well lets put the VMT address directly into ESI }
  482. { it is kind of dirty but that is the simplest }
  483. { way to accept virtual static functions (PM) }
  484. loadesi:=true;
  485. { if no VMT just use $0 bug0214 PM }
  486. if not(oo_has_vmt in pobjectdef(p^.methodpointer^.resulttype)^.objectoptions) then
  487. emit_const_reg(A_MOV,S_L,0,R_ESI)
  488. else
  489. begin
  490. emit_sym_ofs_reg(A_MOV,S_L,
  491. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname),
  492. 0,R_ESI);
  493. end;
  494. { emit_reg(A_PUSH,S_L,R_ESI);
  495. this is done below !! }
  496. end
  497. else
  498. { this is a member call, so ESI isn't modfied }
  499. loadesi:=false;
  500. { a class destructor needs a flag }
  501. if pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  502. assigned(aktprocsym) and
  503. (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  504. begin
  505. push_int(0);
  506. emit_reg(A_PUSH,S_L,R_ESI);
  507. end;
  508. if not(is_con_or_destructor and
  509. pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  510. assigned(aktprocsym) and
  511. (aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor])
  512. ) then
  513. emit_reg(A_PUSH,S_L,R_ESI);
  514. { if an inherited con- or destructor should be }
  515. { called in a con- or destructor then a warning }
  516. { will be made }
  517. { con- and destructors need a pointer to the vmt }
  518. if is_con_or_destructor and
  519. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) and
  520. assigned(aktprocsym) then
  521. begin
  522. if not(aktprocsym^.definition^.proctypeoption in
  523. [potype_constructor,potype_destructor]) then
  524. CGMessage(cg_w_member_cd_call_from_method);
  525. end;
  526. { class destructors get there flag below }
  527. if is_con_or_destructor and
  528. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  529. assigned(aktprocsym) and
  530. (aktprocsym^.definition^.proctypeoption=potype_destructor)) then
  531. push_int(0);
  532. end;
  533. hnewn:
  534. begin
  535. { extended syntax of new }
  536. { ESI must be zero }
  537. emit_reg_reg(A_XOR,S_L,R_ESI,R_ESI);
  538. emit_reg(A_PUSH,S_L,R_ESI);
  539. { insert the vmt }
  540. emit_sym(A_PUSH,S_L,
  541. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  542. extended_new:=true;
  543. end;
  544. hdisposen:
  545. begin
  546. secondpass(p^.methodpointer);
  547. { destructor with extended syntax called from dispose }
  548. { hdisposen always deliver LOC_REFERENCE }
  549. emit_ref_reg(A_LEA,S_L,
  550. newreference(p^.methodpointer^.location.reference),R_ESI);
  551. del_reference(p^.methodpointer^.location.reference);
  552. emit_reg(A_PUSH,S_L,R_ESI);
  553. emit_sym(A_PUSH,S_L,
  554. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  555. end;
  556. else
  557. begin
  558. { call to an instance member }
  559. if (p^.symtable^.symtabletype<>withsymtable) then
  560. begin
  561. secondpass(p^.methodpointer);
  562. case p^.methodpointer^.location.loc of
  563. LOC_CREGISTER,
  564. LOC_REGISTER:
  565. begin
  566. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  567. ungetregister32(p^.methodpointer^.location.register);
  568. end;
  569. else
  570. begin
  571. if (p^.methodpointer^.resulttype^.deftype=classrefdef) or
  572. ((p^.methodpointer^.resulttype^.deftype=objectdef) and
  573. pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  574. emit_ref_reg(A_MOV,S_L,
  575. newreference(p^.methodpointer^.location.reference),R_ESI)
  576. else
  577. emit_ref_reg(A_LEA,S_L,
  578. newreference(p^.methodpointer^.location.reference),R_ESI);
  579. del_reference(p^.methodpointer^.location.reference);
  580. end;
  581. end;
  582. end;
  583. { when calling a class method, we have to load ESI with the VMT !
  584. But, not for a class method via self }
  585. if not(po_containsself in p^.procdefinition^.procoptions) then
  586. begin
  587. if (po_classmethod in p^.procdefinition^.procoptions) and
  588. not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
  589. begin
  590. { class method needs current VMT }
  591. new(r);
  592. reset_reference(r^);
  593. r^.base:=R_ESI;
  594. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  595. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  596. end;
  597. { direct call to destructor: don't remove data! }
  598. if (p^.procdefinition^.proctypeoption=potype_destructor) and
  599. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  600. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  601. emit_const(A_PUSH,S_L,1);
  602. { direct call to class constructor, don't allocate memory }
  603. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  604. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  605. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  606. emit_const(A_PUSH,S_L,0)
  607. else
  608. emit_reg(A_PUSH,S_L,R_ESI);
  609. end;
  610. if is_con_or_destructor then
  611. begin
  612. { classes don't get a VMT pointer pushed }
  613. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  614. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  615. begin
  616. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  617. begin
  618. { it's no bad idea, to insert the VMT }
  619. emit_sym(A_PUSH,S_L,newasmsymbol(
  620. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  621. end
  622. { destructors haven't to dispose the instance, if this is }
  623. { a direct call }
  624. else
  625. push_int(0);
  626. end;
  627. end;
  628. end;
  629. end;
  630. end;
  631. end
  632. else
  633. begin
  634. if (po_classmethod in p^.procdefinition^.procoptions) and
  635. not(
  636. assigned(aktprocsym) and
  637. (po_classmethod in aktprocsym^.definition^.procoptions)
  638. ) then
  639. begin
  640. { class method needs current VMT }
  641. new(r);
  642. reset_reference(r^);
  643. r^.base:=R_ESI;
  644. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  645. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  646. end
  647. else
  648. begin
  649. { member call, ESI isn't modified }
  650. loadesi:=false;
  651. end;
  652. emit_reg(A_PUSH,S_L,R_ESI);
  653. { but a con- or destructor here would probably almost }
  654. { always be placed wrong }
  655. if is_con_or_destructor then
  656. begin
  657. CGMessage(cg_w_member_cd_call_from_method);
  658. push_int(0);
  659. end;
  660. end;
  661. end;
  662. { push base pointer ?}
  663. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  664. ((pprocdef(p^.procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  665. begin
  666. { if we call a nested function in a method, we must }
  667. { push also SELF! }
  668. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  669. { access }
  670. {
  671. begin
  672. loadesi:=false;
  673. emit_reg(A_PUSH,S_L,R_ESI);
  674. end;
  675. }
  676. if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  677. begin
  678. new(r);
  679. reset_reference(r^);
  680. r^.offset:=procinfo^.framepointer_offset;
  681. r^.base:=procinfo^.framepointer;
  682. emit_ref(A_PUSH,S_L,r)
  683. end
  684. { this is only true if the difference is one !!
  685. but it cannot be more !! }
  686. else if (lexlevel=pprocdef(p^.procdefinition)^.parast^.symtablelevel-1) then
  687. begin
  688. emit_reg(A_PUSH,S_L,procinfo^.framepointer)
  689. end
  690. else if (lexlevel>pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  691. begin
  692. hregister:=getregister32;
  693. new(r);
  694. reset_reference(r^);
  695. r^.offset:=procinfo^.framepointer_offset;
  696. r^.base:=procinfo^.framepointer;
  697. emit_ref_reg(A_MOV,S_L,r,hregister);
  698. for i:=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  699. begin
  700. new(r);
  701. reset_reference(r^);
  702. {we should get the correct frame_pointer_offset at each level
  703. how can we do this !!! }
  704. r^.offset:=procinfo^.framepointer_offset;
  705. r^.base:=hregister;
  706. emit_ref_reg(A_MOV,S_L,r,hregister);
  707. end;
  708. emit_reg(A_PUSH,S_L,hregister);
  709. ungetregister32(hregister);
  710. end
  711. else
  712. internalerror(25000);
  713. end;
  714. if (po_virtualmethod in p^.procdefinition^.procoptions) and
  715. not(no_virtual_call) then
  716. begin
  717. { static functions contain the vmt_address in ESI }
  718. { also class methods }
  719. { Here it is quite tricky because it also depends }
  720. { on the methodpointer PM }
  721. if assigned(aktprocsym) then
  722. begin
  723. if (((sp_static in aktprocsym^.symoptions) or
  724. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  725. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  726. or
  727. (po_staticmethod in p^.procdefinition^.procoptions) or
  728. (p^.procdefinition^.proctypeoption=potype_constructor) or
  729. { ESI is loaded earlier }
  730. (po_classmethod in p^.procdefinition^.procoptions) then
  731. begin
  732. new(r);
  733. reset_reference(r^);
  734. r^.base:=R_ESI;
  735. end
  736. else
  737. begin
  738. new(r);
  739. reset_reference(r^);
  740. r^.base:=R_ESI;
  741. { this is one point where we need vmt_offset (PM) }
  742. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  743. {$ifndef noAllocEdi}
  744. getexplicitregister32(R_EDI);
  745. {$endif noAllocEdi}
  746. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  747. new(r);
  748. reset_reference(r^);
  749. r^.base:=R_EDI;
  750. end;
  751. end
  752. else
  753. { aktprocsym should be assigned, also in main program }
  754. internalerror(12345);
  755. {
  756. begin
  757. new(r);
  758. reset_reference(r^);
  759. r^.base:=R_ESI;
  760. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  761. new(r);
  762. reset_reference(r^);
  763. r^.base:=R_EDI;
  764. end;
  765. }
  766. if pprocdef(p^.procdefinition)^.extnumber=-1 then
  767. internalerror(44584);
  768. r^.offset:=pprocdef(p^.procdefinition)^._class^.vmtmethodoffset(pprocdef(p^.procdefinition)^.extnumber);
  769. {$ifndef TESTOBJEXT}
  770. if (cs_check_range in aktlocalswitches) then
  771. begin
  772. emit_reg(A_PUSH,S_L,r^.base);
  773. emitcall('FPC_CHECK_OBJECT');
  774. end;
  775. {$else TESTOBJEXT}
  776. if (cs_check_range in aktlocalswitches) then
  777. begin
  778. emit_sym(A_PUSH,S_L,
  779. newasmsymbol(pprocdef(p^.procdefinition)^._class^.vmt_mangledname));
  780. emit_reg(A_PUSH,S_L,r^.base);
  781. emitcall('FPC_CHECK_OBJECT_EXT');
  782. end;
  783. {$endif TESTOBJEXT}
  784. emit_ref(A_CALL,S_NO,r);
  785. {$ifndef noAllocEdi}
  786. ungetregister32(R_EDI);
  787. {$endif noAllocEdi}
  788. end
  789. else if not inlined then
  790. emitcall(pprocdef(p^.procdefinition)^.mangledname)
  791. else { inlined proc }
  792. { inlined code is in inlinecode }
  793. begin
  794. { set poinline again }
  795. {$ifdef INCLUDEOK}
  796. include(p^.procdefinition^.proccalloptions,pocall_inline);
  797. {$else}
  798. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions+[pocall_inline];
  799. {$endif}
  800. { process the inlinecode }
  801. secondpass(inlinecode);
  802. { free the args }
  803. ungetpersistanttemp(pprocdef(p^.procdefinition)^.parast^.address_fixup);
  804. end;
  805. end
  806. else
  807. { now procedure variable case }
  808. begin
  809. secondpass(p^.right);
  810. { procedure of object? }
  811. if (po_methodpointer in p^.procdefinition^.procoptions) then
  812. begin
  813. { method pointer can't be in a register }
  814. hregister:=R_NO;
  815. { do some hacking if we call a method pointer }
  816. { which is a class member }
  817. { else ESI is overwritten ! }
  818. if (p^.right^.location.reference.base=R_ESI) or
  819. (p^.right^.location.reference.index=R_ESI) then
  820. begin
  821. del_reference(p^.right^.location.reference);
  822. {$ifndef noAllocEdi}
  823. getexplicitregister32(R_EDI);
  824. {$endif noAllocEdi}
  825. emit_ref_reg(A_MOV,S_L,
  826. newreference(p^.right^.location.reference),R_EDI);
  827. hregister:=R_EDI;
  828. end;
  829. { load self, but not if it's already explicitly pushed }
  830. if not(po_containsself in p^.procdefinition^.procoptions) then
  831. begin
  832. { load ESI }
  833. inc(p^.right^.location.reference.offset,4);
  834. emit_ref_reg(A_MOV,S_L,
  835. newreference(p^.right^.location.reference),R_ESI);
  836. dec(p^.right^.location.reference.offset,4);
  837. { push self pointer }
  838. emit_reg(A_PUSH,S_L,R_ESI);
  839. end;
  840. if hregister=R_NO then
  841. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))
  842. else
  843. begin
  844. {$ifndef noAllocEdi}
  845. ungetregister32(hregister);
  846. {$else noAllocEdi}
  847. { the same code, the previous line is just to }
  848. { indicate EDI actually is deallocated if allocated }
  849. { above (JM) }
  850. ungetregister32(hregister);
  851. {$endif noAllocEdi}
  852. emit_reg(A_CALL,S_NO,hregister);
  853. end;
  854. del_reference(p^.right^.location.reference);
  855. end
  856. else
  857. begin
  858. case p^.right^.location.loc of
  859. LOC_REGISTER,LOC_CREGISTER:
  860. begin
  861. emit_reg(A_CALL,S_NO,p^.right^.location.register);
  862. ungetregister32(p^.right^.location.register);
  863. end
  864. else
  865. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference));
  866. del_reference(p^.right^.location.reference);
  867. end;
  868. end;
  869. end;
  870. { this was only for normal functions
  871. displaced here so we also get
  872. it to work for procvars PM }
  873. if (not inlined) and (pocall_clearstack in p^.procdefinition^.proccalloptions) then
  874. begin
  875. { consider the alignment with the rest (PM) }
  876. inc(pushedparasize,pop_size);
  877. pop_size:=0;
  878. { better than an add on all processors }
  879. if pushedparasize=4 then
  880. begin
  881. {$ifndef noAllocEdi}
  882. getexplicitregister32(R_EDI);
  883. {$endif noAllocEdi}
  884. emit_reg(A_POP,S_L,R_EDI);
  885. {$ifndef noAllocEdi}
  886. ungetregister32(R_EDI);
  887. {$endif noAllocEdi}
  888. end
  889. { the pentium has two pipes and pop reg is pairable }
  890. { but the registers must be different! }
  891. else if (pushedparasize=8) and
  892. not(cs_littlesize in aktglobalswitches) and
  893. (aktoptprocessor=ClassP5) and
  894. (procinfo^._class=nil) then
  895. begin
  896. {$ifndef noAllocEdi}
  897. getexplicitregister32(R_EDI);
  898. {$endif noAllocEdi}
  899. emit_reg(A_POP,S_L,R_EDI);
  900. {$ifndef noAllocEdi}
  901. ungetregister32(R_EDI);
  902. {$endif noAllocEdi}
  903. {$ifndef noAllocEdi}
  904. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  905. {$endif noAllocEdi}
  906. emit_reg(A_POP,S_L,R_ESI);
  907. {$ifndef noAllocEdi}
  908. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  909. {$endif noAllocEdi}
  910. end
  911. else if pushedparasize<>0 then
  912. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  913. end;
  914. dont_call:
  915. pushedparasize:=oldpushedparasize;
  916. unused:=unusedregisters;
  917. usablereg32:=usablecount;
  918. {$ifdef TEMPREGDEBUG}
  919. testregisters32;
  920. {$endif TEMPREGDEBUG}
  921. { a constructor could be a function with boolean result }
  922. { if calling constructor called fail we
  923. must jump directly to quickexitlabel PM
  924. but only if it is a call of an inherited constructor }
  925. if (p^.right=nil) and
  926. (p^.procdefinition^.proctypeoption=potype_constructor) and
  927. assigned(p^.methodpointer) and
  928. (p^.methodpointer^.treetype=typen) and
  929. (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  930. begin
  931. emitjmp(C_Z,faillabel);
  932. end;
  933. { handle function results }
  934. { structured results are easy to handle.... }
  935. { needed also when result_no_used !! }
  936. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  937. begin
  938. p^.location.loc:=LOC_MEM;
  939. p^.location.reference.symbol:=nil;
  940. p^.location.reference:=funcretref;
  941. end;
  942. { we have only to handle the result if it is used, but }
  943. { ansi/widestrings must be registered, so we can dispose them }
  944. if (p^.resulttype<>pdef(voiddef)) and (p^.return_value_used or
  945. is_ansistring(p^.resulttype) or is_widestring(p^.resulttype)) then
  946. begin
  947. { a contructor could be a function with boolean result }
  948. if (p^.right=nil) and
  949. (p^.procdefinition^.proctypeoption=potype_constructor) and
  950. { quick'n'dirty check if it is a class or an object }
  951. (p^.resulttype^.deftype=orddef) then
  952. begin
  953. { this fails if popsize > 0 PM }
  954. p^.location.loc:=LOC_FLAGS;
  955. p^.location.resflags:=F_NE;
  956. if extended_new then
  957. begin
  958. {$ifdef test_dest_loc}
  959. if dest_loc_known and (dest_loc_tree=p) then
  960. mov_reg_to_dest(p,S_L,R_EAX)
  961. else
  962. {$endif test_dest_loc}
  963. begin
  964. hregister:=getexplicitregister32(R_EAX);
  965. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  966. p^.location.register:=hregister;
  967. end;
  968. end;
  969. end
  970. { structed results are easy to handle.... }
  971. else if ret_in_param(p^.resulttype) then
  972. begin
  973. {p^.location.loc:=LOC_MEM;
  974. stringdispose(p^.location.reference.symbol);
  975. p^.location.reference:=funcretref;
  976. already done above (PM) }
  977. end
  978. else
  979. begin
  980. if (p^.resulttype^.deftype in [orddef,enumdef]) then
  981. begin
  982. p^.location.loc:=LOC_REGISTER;
  983. case p^.resulttype^.size of
  984. 4 :
  985. begin
  986. {$ifdef test_dest_loc}
  987. if dest_loc_known and (dest_loc_tree=p) then
  988. mov_reg_to_dest(p,S_L,R_EAX)
  989. else
  990. {$endif test_dest_loc}
  991. begin
  992. hregister:=getexplicitregister32(R_EAX);
  993. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  994. p^.location.register:=hregister;
  995. end;
  996. end;
  997. 1 :
  998. begin
  999. {$ifdef test_dest_loc}
  1000. if dest_loc_known and (dest_loc_tree=p) then
  1001. mov_reg_to_dest(p,S_B,R_AL)
  1002. else
  1003. {$endif test_dest_loc}
  1004. begin
  1005. hregister:=getexplicitregister32(R_EAX);
  1006. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1007. p^.location.register:=reg32toreg8(hregister);
  1008. end;
  1009. end;
  1010. 2 :
  1011. begin
  1012. {$ifdef test_dest_loc}
  1013. if dest_loc_known and (dest_loc_tree=p) then
  1014. mov_reg_to_dest(p,S_W,R_AX)
  1015. else
  1016. {$endif test_dest_loc}
  1017. begin
  1018. hregister:=getexplicitregister32(R_EAX);
  1019. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1020. p^.location.register:=reg32toreg16(hregister);
  1021. end;
  1022. end;
  1023. 8 :
  1024. begin
  1025. {$ifdef test_dest_loc}
  1026. {$error Don't know what to do here}
  1027. {$endif test_dest_loc}
  1028. hregister:=getexplicitregister32(R_EAX);
  1029. hregister2:=getexplicitregister32(R_EDX);
  1030. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1031. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  1032. p^.location.registerlow:=hregister;
  1033. p^.location.registerhigh:=hregister2;
  1034. end;
  1035. else internalerror(7);
  1036. end
  1037. end
  1038. else if (p^.resulttype^.deftype=floatdef) then
  1039. case pfloatdef(p^.resulttype)^.typ of
  1040. f32bit:
  1041. begin
  1042. p^.location.loc:=LOC_REGISTER;
  1043. {$ifdef test_dest_loc}
  1044. if dest_loc_known and (dest_loc_tree=p) then
  1045. mov_reg_to_dest(p,S_L,R_EAX)
  1046. else
  1047. {$endif test_dest_loc}
  1048. begin
  1049. hregister:=getexplicitregister32(R_EAX);
  1050. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1051. p^.location.register:=hregister;
  1052. end;
  1053. end;
  1054. else
  1055. begin
  1056. p^.location.loc:=LOC_FPU;
  1057. inc(fpuvaroffset);
  1058. end;
  1059. end
  1060. else if is_ansistring(p^.resulttype) or
  1061. is_widestring(p^.resulttype) then
  1062. begin
  1063. hregister:=getexplicitregister32(R_EAX);
  1064. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1065. gettempansistringreference(hr);
  1066. decrstringref(p^.resulttype,hr);
  1067. emit_reg_ref(A_MOV,S_L,hregister,
  1068. newreference(hr));
  1069. ungetregister32(hregister);
  1070. p^.location.loc:=LOC_MEM;
  1071. p^.location.reference:=hr;
  1072. end
  1073. else
  1074. begin
  1075. p^.location.loc:=LOC_REGISTER;
  1076. {$ifdef test_dest_loc}
  1077. if dest_loc_known and (dest_loc_tree=p) then
  1078. mov_reg_to_dest(p,S_L,R_EAX)
  1079. else
  1080. {$endif test_dest_loc}
  1081. begin
  1082. hregister:=getexplicitregister32(R_EAX);
  1083. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1084. p^.location.register:=hregister;
  1085. end;
  1086. end;
  1087. end;
  1088. end;
  1089. { perhaps i/o check ? }
  1090. if iolabel<>nil then
  1091. begin
  1092. emit_sym(A_PUSH,S_L,iolabel);
  1093. emitcall('FPC_IOCHECK');
  1094. end;
  1095. if pop_size>0 then
  1096. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1097. { restore registers }
  1098. popusedregisters(pushed);
  1099. { at last, restore instance pointer (SELF) }
  1100. if loadesi then
  1101. maybe_loadesi;
  1102. pp:=params;
  1103. while assigned(pp) do
  1104. begin
  1105. if assigned(pp^.left) then
  1106. begin
  1107. if (pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1108. ungetiftemp(pp^.left^.location.reference);
  1109. { process also all nodes of an array of const }
  1110. if pp^.left^.treetype=arrayconstructn then
  1111. begin
  1112. if assigned(pp^.left^.left) then
  1113. begin
  1114. hp:=pp^.left;
  1115. while assigned(hp) do
  1116. begin
  1117. if (hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1118. ungetiftemp(hp^.left^.location.reference);
  1119. hp:=hp^.right;
  1120. end;
  1121. end;
  1122. end;
  1123. end;
  1124. pp:=pp^.right;
  1125. end;
  1126. if inlined then
  1127. ungetpersistanttemp(inlinecode^.retoffset);
  1128. disposetree(params);
  1129. { from now on the result can be freed normally }
  1130. if inlined and ret_in_param(p^.resulttype) then
  1131. persistanttemptonormal(funcretref.offset);
  1132. { if return value is not used }
  1133. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1134. begin
  1135. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1136. begin
  1137. { data which must be finalized ? }
  1138. if (p^.resulttype^.needs_inittable) and
  1139. ( (p^.resulttype^.deftype<>objectdef) or
  1140. not(pobjectdef(p^.resulttype)^.is_class)) then
  1141. finalize(p^.resulttype,p^.location.reference,ret_in_param(p^.resulttype));
  1142. { release unused temp }
  1143. ungetiftemp(p^.location.reference)
  1144. end
  1145. else if p^.location.loc=LOC_FPU then
  1146. begin
  1147. { release FPU stack }
  1148. emit_reg(A_FSTP,S_NO,R_ST0);
  1149. {
  1150. dec(fpuvaroffset);
  1151. do NOT decrement as the increment before
  1152. is not called for unused results PM }
  1153. end;
  1154. end;
  1155. end;
  1156. {*****************************************************************************
  1157. SecondProcInlineN
  1158. *****************************************************************************}
  1159. procedure secondprocinline(var p : ptree);
  1160. var st : psymtable;
  1161. oldprocsym : pprocsym;
  1162. para_size : longint;
  1163. oldprocinfo : pprocinfo;
  1164. oldinlining_procedure,
  1165. nostackframe,make_global : boolean;
  1166. proc_names : tstringcontainer;
  1167. inlineentrycode,inlineexitcode : paasmoutput;
  1168. oldexitlabel,oldexit2label,oldquickexitlabel:Pasmlabel;
  1169. begin
  1170. oldinlining_procedure:=inlining_procedure;
  1171. oldexitlabel:=aktexitlabel;
  1172. oldexit2label:=aktexit2label;
  1173. oldquickexitlabel:=quickexitlabel;
  1174. getlabel(aktexitlabel);
  1175. getlabel(aktexit2label);
  1176. oldprocsym:=aktprocsym;
  1177. { save old procinfo }
  1178. getmem(oldprocinfo,sizeof(tprocinfo));
  1179. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1180. { we're inlining a procedure }
  1181. inlining_procedure:=true;
  1182. { set the return value }
  1183. aktprocsym:=p^.inlineprocsym;
  1184. procinfo^.returntype:=aktprocsym^.definition^.rettype;
  1185. procinfo^.return_offset:=p^.retoffset;
  1186. { arg space has been filled by the parent secondcall }
  1187. st:=aktprocsym^.definition^.localst;
  1188. { set it to the same lexical level }
  1189. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1190. if st^.datasize>0 then
  1191. begin
  1192. st^.address_fixup:=gettempofsizepersistant(st^.datasize);
  1193. {$ifdef extdebug}
  1194. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1195. exprasmlist^.concat(new(pai_asm_comment,init(strpnew(
  1196. 'local symtable is at offset '+tostr(st^.address_fixup)))));
  1197. {$endif extdebug}
  1198. end;
  1199. {$ifdef extdebug}
  1200. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('Start of inlined proc'))));
  1201. {$endif extdebug}
  1202. { takes care of local data initialization }
  1203. inlineentrycode:=new(paasmoutput,init);
  1204. inlineexitcode:=new(paasmoutput,init);
  1205. proc_names.init;
  1206. para_size:=p^.para_size;
  1207. make_global:=false; { to avoid warning }
  1208. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  1209. exprasmlist^.concatlist(inlineentrycode);
  1210. secondpass(p^.inlinetree);
  1211. genexitcode(inlineexitcode,0,false,true);
  1212. exprasmlist^.concatlist(inlineexitcode);
  1213. {$ifdef extdebug}
  1214. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('End of inlined proc'))));
  1215. {$endif extdebug}
  1216. {we can free the local data now, reset also the fixup address }
  1217. if st^.datasize>0 then
  1218. begin
  1219. ungetpersistanttemp(st^.address_fixup);
  1220. st^.address_fixup:=0;
  1221. end;
  1222. { restore procinfo }
  1223. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1224. freemem(oldprocinfo,sizeof(tprocinfo));
  1225. { restore }
  1226. aktprocsym:=oldprocsym;
  1227. aktexitlabel:=oldexitlabel;
  1228. aktexit2label:=oldexit2label;
  1229. quickexitlabel:=oldquickexitlabel;
  1230. inlining_procedure:=oldinlining_procedure;
  1231. end;
  1232. end.
  1233. {
  1234. $Log$
  1235. Revision 1.116 2000-01-09 12:35:00 jonas
  1236. * changed edi allocation to use getexplicitregister32/ungetregister
  1237. (adapted tgeni386 a bit for this) and enabled it by default
  1238. * fixed very big and stupid bug of mine in cg386mat that broke the
  1239. include() code (and make cycle :( ) if you compiled without
  1240. -dnewoptimizations
  1241. Revision 1.115 2000/01/09 01:44:19 jonas
  1242. + (de)allocation info for EDI to fix reported bug on mailinglist.
  1243. Also some (de)allocation info for ESI added. Between -dallocEDI
  1244. because at this time of the night bugs could easily slip in ;)
  1245. Revision 1.114 2000/01/07 01:14:20 peter
  1246. * updated copyright to 2000
  1247. Revision 1.113 1999/12/22 01:01:46 peter
  1248. - removed freelabel()
  1249. * added undefined label detection in internal assembler, this prevents
  1250. a lot of ld crashes and wrong .o files
  1251. * .o files aren't written anymore if errors have occured
  1252. * inlining of assembler labels is now correct
  1253. Revision 1.112 1999/12/13 21:49:54 pierre
  1254. * bug in extdebugg code for inlined procedures
  1255. Revision 1.111 1999/11/30 10:40:42 peter
  1256. + ttype, tsymlist
  1257. Revision 1.110 1999/11/06 14:34:17 peter
  1258. * truncated log to 20 revs
  1259. Revision 1.109 1999/11/04 00:23:58 pierre
  1260. * fix for fpuvaroffset for unused return value
  1261. Revision 1.108 1999/10/26 12:30:40 peter
  1262. * const parameter is now checked
  1263. * better and generic check if a node can be used for assigning
  1264. * export fixes
  1265. * procvar equal works now (it never had worked at least from 0.99.8)
  1266. * defcoll changed to linkedlist with pparaitem so it can easily be
  1267. walked both directions
  1268. Revision 1.107 1999/10/08 15:40:47 pierre
  1269. * use and remember that C functions with complex data results use ret $4
  1270. Revision 1.106 1999/09/27 23:44:46 peter
  1271. * procinfo is now a pointer
  1272. * support for result setting in sub procedure
  1273. Revision 1.105 1999/09/26 13:26:02 florian
  1274. * exception patch of Romio nevertheless the excpetion handling
  1275. needs some corections regarding register saving
  1276. * gettempansistring is again a procedure
  1277. Revision 1.104 1999/09/16 11:34:46 pierre
  1278. * typo correction
  1279. Revision 1.103 1999/09/07 07:54:23 peter
  1280. * small array push to open array fixed, open array always needs addr
  1281. pushing
  1282. Revision 1.102 1999/08/25 11:59:39 jonas
  1283. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1284. Revision 1.101 1999/08/23 23:38:18 pierre
  1285. + TEMPREGDEBUG code added
  1286. Revision 1.100 1999/08/19 13:08:45 pierre
  1287. * emit_??? used
  1288. Revision 1.99 1999/08/09 22:19:47 peter
  1289. * classes vmt changed to only positive addresses
  1290. * sharedlib creation is working
  1291. Revision 1.98 1999/08/09 10:37:55 peter
  1292. * fixed pushing of self with methodpointer
  1293. Revision 1.97 1999/08/04 13:45:18 florian
  1294. + floating point register variables !!
  1295. * pairegalloc is now generated for register variables
  1296. Revision 1.96 1999/08/04 00:22:41 florian
  1297. * renamed i386asm and i386base to cpuasm and cpubase
  1298. Revision 1.95 1999/08/03 22:02:34 peter
  1299. * moved bitmask constants to sets
  1300. * some other type/const renamings
  1301. Revision 1.94 1999/07/06 21:48:09 florian
  1302. * a lot bug fixes:
  1303. - po_external isn't any longer necessary for procedure compatibility
  1304. - m_tp_procvar is in -Sd now available
  1305. - error messages of procedure variables improved
  1306. - return values with init./finalization fixed
  1307. - data types with init./finalization aren't any longer allowed in variant
  1308. record
  1309. Revision 1.93 1999/06/22 13:31:24 peter
  1310. * merged
  1311. Revision 1.92 1999/06/16 09:32:45 peter
  1312. * merged
  1313. Revision 1.91 1999/06/14 17:47:47 peter
  1314. * merged
  1315. Revision 1.90.2.3 1999/06/22 13:30:08 peter
  1316. * fixed return with packenum
  1317. }