cg386cal.pas 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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. { direct call to destructor: don't remove data! }
  653. if procinfo^._class^.is_class then
  654. begin
  655. if (p^.procdefinition^.proctypeoption=potype_destructor) then
  656. begin
  657. emit_const(A_PUSH,S_L,0);
  658. emit_reg(A_PUSH,S_L,R_ESI);
  659. end
  660. else if (p^.procdefinition^.proctypeoption=potype_constructor) then
  661. emit_const(A_PUSH,S_L,0)
  662. else
  663. emit_reg(A_PUSH,S_L,R_ESI);
  664. end
  665. else
  666. begin
  667. if is_con_or_destructor then
  668. begin
  669. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  670. begin
  671. { it's no bad idea, to insert the VMT }
  672. emit_sym(A_PUSH,S_L,newasmsymbol(
  673. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  674. end
  675. { destructors haven't to dispose the instance, if this is }
  676. { a direct call }
  677. else
  678. push_int(0);
  679. end
  680. else
  681. emit_reg(A_PUSH,S_L,R_ESI);
  682. end;
  683. end;
  684. end;
  685. { push base pointer ?}
  686. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  687. ((pprocdef(p^.procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  688. begin
  689. { if we call a nested function in a method, we must }
  690. { push also SELF! }
  691. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  692. { access }
  693. {
  694. begin
  695. loadesi:=false;
  696. emit_reg(A_PUSH,S_L,R_ESI);
  697. end;
  698. }
  699. if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  700. begin
  701. new(r);
  702. reset_reference(r^);
  703. r^.offset:=procinfo^.framepointer_offset;
  704. r^.base:=procinfo^.framepointer;
  705. emit_ref(A_PUSH,S_L,r)
  706. end
  707. { this is only true if the difference is one !!
  708. but it cannot be more !! }
  709. else if (lexlevel=pprocdef(p^.procdefinition)^.parast^.symtablelevel-1) then
  710. begin
  711. emit_reg(A_PUSH,S_L,procinfo^.framepointer)
  712. end
  713. else if (lexlevel>pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  714. begin
  715. hregister:=getregister32;
  716. new(r);
  717. reset_reference(r^);
  718. r^.offset:=procinfo^.framepointer_offset;
  719. r^.base:=procinfo^.framepointer;
  720. emit_ref_reg(A_MOV,S_L,r,hregister);
  721. for i:=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  722. begin
  723. new(r);
  724. reset_reference(r^);
  725. {we should get the correct frame_pointer_offset at each level
  726. how can we do this !!! }
  727. r^.offset:=procinfo^.framepointer_offset;
  728. r^.base:=hregister;
  729. emit_ref_reg(A_MOV,S_L,r,hregister);
  730. end;
  731. emit_reg(A_PUSH,S_L,hregister);
  732. ungetregister32(hregister);
  733. end
  734. else
  735. internalerror(25000);
  736. end;
  737. if (po_virtualmethod in p^.procdefinition^.procoptions) and
  738. not(no_virtual_call) then
  739. begin
  740. { static functions contain the vmt_address in ESI }
  741. { also class methods }
  742. { Here it is quite tricky because it also depends }
  743. { on the methodpointer PM }
  744. if assigned(aktprocsym) then
  745. begin
  746. if (((sp_static in aktprocsym^.symoptions) or
  747. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  748. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  749. or
  750. (po_staticmethod in p^.procdefinition^.procoptions) or
  751. ((p^.procdefinition^.proctypeoption=potype_constructor) and
  752. { if we call a constructor from anther constructor }
  753. { esi contains self }
  754. (assigned(p^.methodpointer))) or
  755. { ESI is loaded earlier }
  756. (po_classmethod in p^.procdefinition^.procoptions) then
  757. begin
  758. new(r);
  759. reset_reference(r^);
  760. r^.base:=R_ESI;
  761. end
  762. else
  763. begin
  764. new(r);
  765. reset_reference(r^);
  766. r^.base:=R_ESI;
  767. { this is one point where we need vmt_offset (PM) }
  768. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  769. {$ifndef noAllocEdi}
  770. getexplicitregister32(R_EDI);
  771. {$endif noAllocEdi}
  772. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  773. new(r);
  774. reset_reference(r^);
  775. r^.base:=R_EDI;
  776. end;
  777. end
  778. else
  779. { aktprocsym should be assigned, also in main program }
  780. internalerror(12345);
  781. {
  782. begin
  783. new(r);
  784. reset_reference(r^);
  785. r^.base:=R_ESI;
  786. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  787. new(r);
  788. reset_reference(r^);
  789. r^.base:=R_EDI;
  790. end;
  791. }
  792. if pprocdef(p^.procdefinition)^.extnumber=-1 then
  793. internalerror(44584);
  794. r^.offset:=pprocdef(p^.procdefinition)^._class^.vmtmethodoffset(pprocdef(p^.procdefinition)^.extnumber);
  795. {$ifndef TESTOBJEXT}
  796. if (cs_check_range in aktlocalswitches) then
  797. begin
  798. emit_reg(A_PUSH,S_L,r^.base);
  799. emitcall('FPC_CHECK_OBJECT');
  800. end;
  801. {$else TESTOBJEXT}
  802. if (cs_check_range in aktlocalswitches) then
  803. begin
  804. emit_sym(A_PUSH,S_L,
  805. newasmsymbol(pprocdef(p^.procdefinition)^._class^.vmt_mangledname));
  806. emit_reg(A_PUSH,S_L,r^.base);
  807. emitcall('FPC_CHECK_OBJECT_EXT');
  808. end;
  809. {$endif TESTOBJEXT}
  810. emit_ref(A_CALL,S_NO,r);
  811. {$ifndef noAllocEdi}
  812. ungetregister32(R_EDI);
  813. {$endif noAllocEdi}
  814. end
  815. else if not inlined then
  816. emitcall(pprocdef(p^.procdefinition)^.mangledname)
  817. else { inlined proc }
  818. { inlined code is in inlinecode }
  819. begin
  820. { set poinline again }
  821. {$ifdef INCLUDEOK}
  822. include(p^.procdefinition^.proccalloptions,pocall_inline);
  823. {$else}
  824. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions+[pocall_inline];
  825. {$endif}
  826. { process the inlinecode }
  827. secondpass(inlinecode);
  828. { free the args }
  829. ungetpersistanttemp(pprocdef(p^.procdefinition)^.parast^.address_fixup);
  830. end;
  831. end
  832. else
  833. { now procedure variable case }
  834. begin
  835. secondpass(p^.right);
  836. { procedure of object? }
  837. if (po_methodpointer in p^.procdefinition^.procoptions) then
  838. begin
  839. { method pointer can't be in a register }
  840. hregister:=R_NO;
  841. { do some hacking if we call a method pointer }
  842. { which is a class member }
  843. { else ESI is overwritten ! }
  844. if (p^.right^.location.reference.base=R_ESI) or
  845. (p^.right^.location.reference.index=R_ESI) then
  846. begin
  847. del_reference(p^.right^.location.reference);
  848. {$ifndef noAllocEdi}
  849. getexplicitregister32(R_EDI);
  850. {$endif noAllocEdi}
  851. emit_ref_reg(A_MOV,S_L,
  852. newreference(p^.right^.location.reference),R_EDI);
  853. hregister:=R_EDI;
  854. end;
  855. { load self, but not if it's already explicitly pushed }
  856. if not(po_containsself in p^.procdefinition^.procoptions) then
  857. begin
  858. { load ESI }
  859. inc(p^.right^.location.reference.offset,4);
  860. emit_ref_reg(A_MOV,S_L,
  861. newreference(p^.right^.location.reference),R_ESI);
  862. dec(p^.right^.location.reference.offset,4);
  863. { push self pointer }
  864. emit_reg(A_PUSH,S_L,R_ESI);
  865. end;
  866. if hregister=R_NO then
  867. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))
  868. else
  869. begin
  870. {$ifndef noAllocEdi}
  871. ungetregister32(hregister);
  872. {$else noAllocEdi}
  873. { the same code, the previous line is just to }
  874. { indicate EDI actually is deallocated if allocated }
  875. { above (JM) }
  876. ungetregister32(hregister);
  877. {$endif noAllocEdi}
  878. emit_reg(A_CALL,S_NO,hregister);
  879. end;
  880. del_reference(p^.right^.location.reference);
  881. end
  882. else
  883. begin
  884. case p^.right^.location.loc of
  885. LOC_REGISTER,LOC_CREGISTER:
  886. begin
  887. emit_reg(A_CALL,S_NO,p^.right^.location.register);
  888. ungetregister32(p^.right^.location.register);
  889. end
  890. else
  891. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference));
  892. del_reference(p^.right^.location.reference);
  893. end;
  894. end;
  895. end;
  896. { this was only for normal functions
  897. displaced here so we also get
  898. it to work for procvars PM }
  899. if (not inlined) and (pocall_clearstack in p^.procdefinition^.proccalloptions) then
  900. begin
  901. { consider the alignment with the rest (PM) }
  902. inc(pushedparasize,pop_size);
  903. pop_size:=0;
  904. { better than an add on all processors }
  905. if pushedparasize=4 then
  906. begin
  907. {$ifndef noAllocEdi}
  908. getexplicitregister32(R_EDI);
  909. {$endif noAllocEdi}
  910. emit_reg(A_POP,S_L,R_EDI);
  911. {$ifndef noAllocEdi}
  912. ungetregister32(R_EDI);
  913. {$endif noAllocEdi}
  914. end
  915. { the pentium has two pipes and pop reg is pairable }
  916. { but the registers must be different! }
  917. else if (pushedparasize=8) and
  918. not(cs_littlesize in aktglobalswitches) and
  919. (aktoptprocessor=ClassP5) and
  920. (procinfo^._class=nil) then
  921. begin
  922. {$ifndef noAllocEdi}
  923. getexplicitregister32(R_EDI);
  924. {$endif noAllocEdi}
  925. emit_reg(A_POP,S_L,R_EDI);
  926. {$ifndef noAllocEdi}
  927. ungetregister32(R_EDI);
  928. {$endif noAllocEdi}
  929. {$ifndef noAllocEdi}
  930. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  931. {$endif noAllocEdi}
  932. emit_reg(A_POP,S_L,R_ESI);
  933. {$ifndef noAllocEdi}
  934. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  935. {$endif noAllocEdi}
  936. end
  937. else if pushedparasize<>0 then
  938. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  939. end;
  940. dont_call:
  941. pushedparasize:=oldpushedparasize;
  942. unused:=unusedregisters;
  943. usablereg32:=usablecount;
  944. {$ifdef TEMPREGDEBUG}
  945. testregisters32;
  946. {$endif TEMPREGDEBUG}
  947. { a constructor could be a function with boolean result }
  948. { if calling constructor called fail we
  949. must jump directly to quickexitlabel PM
  950. but only if it is a call of an inherited constructor }
  951. if (p^.right=nil) and
  952. (p^.procdefinition^.proctypeoption=potype_constructor) and
  953. assigned(p^.methodpointer) and
  954. (p^.methodpointer^.treetype=typen) and
  955. (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  956. begin
  957. emitjmp(C_Z,faillabel);
  958. end;
  959. { handle function results }
  960. { structured results are easy to handle.... }
  961. { needed also when result_no_used !! }
  962. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  963. begin
  964. p^.location.loc:=LOC_MEM;
  965. p^.location.reference.symbol:=nil;
  966. p^.location.reference:=funcretref;
  967. end;
  968. { we have only to handle the result if it is used, but }
  969. { ansi/widestrings must be registered, so we can dispose them }
  970. if (p^.resulttype<>pdef(voiddef)) and (p^.return_value_used or
  971. is_ansistring(p^.resulttype) or is_widestring(p^.resulttype)) then
  972. begin
  973. { a contructor could be a function with boolean result }
  974. if (p^.right=nil) and
  975. (p^.procdefinition^.proctypeoption=potype_constructor) and
  976. { quick'n'dirty check if it is a class or an object }
  977. (p^.resulttype^.deftype=orddef) then
  978. begin
  979. { this fails if popsize > 0 PM }
  980. p^.location.loc:=LOC_FLAGS;
  981. p^.location.resflags:=F_NE;
  982. if extended_new then
  983. begin
  984. {$ifdef test_dest_loc}
  985. if dest_loc_known and (dest_loc_tree=p) then
  986. mov_reg_to_dest(p,S_L,R_EAX)
  987. else
  988. {$endif test_dest_loc}
  989. begin
  990. hregister:=getexplicitregister32(R_EAX);
  991. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  992. p^.location.register:=hregister;
  993. end;
  994. end;
  995. end
  996. { structed results are easy to handle.... }
  997. else if ret_in_param(p^.resulttype) then
  998. begin
  999. {p^.location.loc:=LOC_MEM;
  1000. stringdispose(p^.location.reference.symbol);
  1001. p^.location.reference:=funcretref;
  1002. already done above (PM) }
  1003. end
  1004. else
  1005. begin
  1006. if (p^.resulttype^.deftype in [orddef,enumdef]) then
  1007. begin
  1008. p^.location.loc:=LOC_REGISTER;
  1009. case p^.resulttype^.size of
  1010. 4 :
  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_L,R_EAX)
  1015. else
  1016. {$endif test_dest_loc}
  1017. begin
  1018. hregister:=getexplicitregister32(R_EAX);
  1019. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1020. p^.location.register:=hregister;
  1021. end;
  1022. end;
  1023. 1 :
  1024. begin
  1025. {$ifdef test_dest_loc}
  1026. if dest_loc_known and (dest_loc_tree=p) then
  1027. mov_reg_to_dest(p,S_B,R_AL)
  1028. else
  1029. {$endif test_dest_loc}
  1030. begin
  1031. hregister:=getexplicitregister32(R_EAX);
  1032. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1033. p^.location.register:=reg32toreg8(hregister);
  1034. end;
  1035. end;
  1036. 2 :
  1037. begin
  1038. {$ifdef test_dest_loc}
  1039. if dest_loc_known and (dest_loc_tree=p) then
  1040. mov_reg_to_dest(p,S_W,R_AX)
  1041. else
  1042. {$endif test_dest_loc}
  1043. begin
  1044. hregister:=getexplicitregister32(R_EAX);
  1045. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1046. p^.location.register:=reg32toreg16(hregister);
  1047. end;
  1048. end;
  1049. 8 :
  1050. begin
  1051. {$ifdef test_dest_loc}
  1052. {$error Don't know what to do here}
  1053. {$endif test_dest_loc}
  1054. hregister:=getexplicitregister32(R_EAX);
  1055. hregister2:=getexplicitregister32(R_EDX);
  1056. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1057. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  1058. p^.location.registerlow:=hregister;
  1059. p^.location.registerhigh:=hregister2;
  1060. end;
  1061. else internalerror(7);
  1062. end
  1063. end
  1064. else if (p^.resulttype^.deftype=floatdef) then
  1065. case pfloatdef(p^.resulttype)^.typ of
  1066. f32bit:
  1067. begin
  1068. p^.location.loc:=LOC_REGISTER;
  1069. {$ifdef test_dest_loc}
  1070. if dest_loc_known and (dest_loc_tree=p) then
  1071. mov_reg_to_dest(p,S_L,R_EAX)
  1072. else
  1073. {$endif test_dest_loc}
  1074. begin
  1075. hregister:=getexplicitregister32(R_EAX);
  1076. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1077. p^.location.register:=hregister;
  1078. end;
  1079. end;
  1080. else
  1081. begin
  1082. p^.location.loc:=LOC_FPU;
  1083. inc(fpuvaroffset);
  1084. end;
  1085. end
  1086. else if is_ansistring(p^.resulttype) or
  1087. is_widestring(p^.resulttype) then
  1088. begin
  1089. hregister:=getexplicitregister32(R_EAX);
  1090. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1091. gettempansistringreference(hr);
  1092. decrstringref(p^.resulttype,hr);
  1093. emit_reg_ref(A_MOV,S_L,hregister,
  1094. newreference(hr));
  1095. ungetregister32(hregister);
  1096. p^.location.loc:=LOC_MEM;
  1097. p^.location.reference:=hr;
  1098. end
  1099. else
  1100. begin
  1101. p^.location.loc:=LOC_REGISTER;
  1102. {$ifdef test_dest_loc}
  1103. if dest_loc_known and (dest_loc_tree=p) then
  1104. mov_reg_to_dest(p,S_L,R_EAX)
  1105. else
  1106. {$endif test_dest_loc}
  1107. begin
  1108. hregister:=getexplicitregister32(R_EAX);
  1109. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1110. p^.location.register:=hregister;
  1111. end;
  1112. end;
  1113. end;
  1114. end;
  1115. { perhaps i/o check ? }
  1116. if iolabel<>nil then
  1117. begin
  1118. emit_sym(A_PUSH,S_L,iolabel);
  1119. emitcall('FPC_IOCHECK');
  1120. end;
  1121. if pop_size>0 then
  1122. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1123. { restore registers }
  1124. popusedregisters(pushed);
  1125. { at last, restore instance pointer (SELF) }
  1126. if loadesi then
  1127. maybe_loadesi;
  1128. pp:=params;
  1129. while assigned(pp) do
  1130. begin
  1131. if assigned(pp^.left) then
  1132. begin
  1133. if (pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1134. ungetiftemp(pp^.left^.location.reference);
  1135. { process also all nodes of an array of const }
  1136. if pp^.left^.treetype=arrayconstructn then
  1137. begin
  1138. if assigned(pp^.left^.left) then
  1139. begin
  1140. hp:=pp^.left;
  1141. while assigned(hp) do
  1142. begin
  1143. if (hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1144. ungetiftemp(hp^.left^.location.reference);
  1145. hp:=hp^.right;
  1146. end;
  1147. end;
  1148. end;
  1149. end;
  1150. pp:=pp^.right;
  1151. end;
  1152. if inlined then
  1153. ungetpersistanttemp(inlinecode^.retoffset);
  1154. disposetree(params);
  1155. { from now on the result can be freed normally }
  1156. if inlined and ret_in_param(p^.resulttype) then
  1157. persistanttemptonormal(funcretref.offset);
  1158. { if return value is not used }
  1159. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1160. begin
  1161. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1162. begin
  1163. { data which must be finalized ? }
  1164. if (p^.resulttype^.needs_inittable) and
  1165. ( (p^.resulttype^.deftype<>objectdef) or
  1166. not(pobjectdef(p^.resulttype)^.is_class)) then
  1167. finalize(p^.resulttype,p^.location.reference,ret_in_param(p^.resulttype));
  1168. { release unused temp }
  1169. ungetiftemp(p^.location.reference)
  1170. end
  1171. else if p^.location.loc=LOC_FPU then
  1172. begin
  1173. { release FPU stack }
  1174. emit_reg(A_FSTP,S_NO,R_ST0);
  1175. {
  1176. dec(fpuvaroffset);
  1177. do NOT decrement as the increment before
  1178. is not called for unused results PM }
  1179. end;
  1180. end;
  1181. end;
  1182. {*****************************************************************************
  1183. SecondProcInlineN
  1184. *****************************************************************************}
  1185. procedure secondprocinline(var p : ptree);
  1186. var st : psymtable;
  1187. oldprocsym : pprocsym;
  1188. para_size : longint;
  1189. oldprocinfo : pprocinfo;
  1190. oldinlining_procedure,
  1191. nostackframe,make_global : boolean;
  1192. proc_names : tstringcontainer;
  1193. inlineentrycode,inlineexitcode : paasmoutput;
  1194. oldexitlabel,oldexit2label,oldquickexitlabel:Pasmlabel;
  1195. begin
  1196. oldinlining_procedure:=inlining_procedure;
  1197. oldexitlabel:=aktexitlabel;
  1198. oldexit2label:=aktexit2label;
  1199. oldquickexitlabel:=quickexitlabel;
  1200. getlabel(aktexitlabel);
  1201. getlabel(aktexit2label);
  1202. oldprocsym:=aktprocsym;
  1203. { save old procinfo }
  1204. getmem(oldprocinfo,sizeof(tprocinfo));
  1205. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1206. { we're inlining a procedure }
  1207. inlining_procedure:=true;
  1208. { set the return value }
  1209. aktprocsym:=p^.inlineprocsym;
  1210. procinfo^.returntype:=aktprocsym^.definition^.rettype;
  1211. procinfo^.return_offset:=p^.retoffset;
  1212. procinfo^.para_offset:=p^.para_offset;
  1213. { arg space has been filled by the parent secondcall }
  1214. st:=aktprocsym^.definition^.localst;
  1215. { set it to the same lexical level }
  1216. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1217. if st^.datasize>0 then
  1218. begin
  1219. st^.address_fixup:=gettempofsizepersistant(st^.datasize);
  1220. {$ifdef extdebug}
  1221. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1222. exprasmlist^.concat(new(pai_asm_comment,init(strpnew(
  1223. 'local symtable is at offset '+tostr(st^.address_fixup)))));
  1224. {$endif extdebug}
  1225. end;
  1226. {$ifdef extdebug}
  1227. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('Start of inlined proc'))));
  1228. {$endif extdebug}
  1229. { takes care of local data initialization }
  1230. inlineentrycode:=new(paasmoutput,init);
  1231. inlineexitcode:=new(paasmoutput,init);
  1232. proc_names.init;
  1233. para_size:=p^.para_size;
  1234. make_global:=false; { to avoid warning }
  1235. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  1236. exprasmlist^.concatlist(inlineentrycode);
  1237. secondpass(p^.inlinetree);
  1238. genexitcode(inlineexitcode,0,false,true);
  1239. exprasmlist^.concatlist(inlineexitcode);
  1240. {$ifdef extdebug}
  1241. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('End of inlined proc'))));
  1242. {$endif extdebug}
  1243. {we can free the local data now, reset also the fixup address }
  1244. if st^.datasize>0 then
  1245. begin
  1246. ungetpersistanttemp(st^.address_fixup);
  1247. st^.address_fixup:=0;
  1248. end;
  1249. { restore procinfo }
  1250. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1251. freemem(oldprocinfo,sizeof(tprocinfo));
  1252. { restore }
  1253. aktprocsym:=oldprocsym;
  1254. aktexitlabel:=oldexitlabel;
  1255. aktexit2label:=oldexit2label;
  1256. quickexitlabel:=oldquickexitlabel;
  1257. inlining_procedure:=oldinlining_procedure;
  1258. end;
  1259. end.
  1260. {
  1261. $Log$
  1262. Revision 1.118 2000-01-20 12:14:47 florian
  1263. * bug 793 fixed
  1264. Revision 1.117 2000/01/16 22:17:11 peter
  1265. * renamed call_offset to para_offset
  1266. Revision 1.116 2000/01/09 12:35:00 jonas
  1267. * changed edi allocation to use getexplicitregister32/ungetregister
  1268. (adapted tgeni386 a bit for this) and enabled it by default
  1269. * fixed very big and stupid bug of mine in cg386mat that broke the
  1270. include() code (and make cycle :( ) if you compiled without
  1271. -dnewoptimizations
  1272. Revision 1.115 2000/01/09 01:44:19 jonas
  1273. + (de)allocation info for EDI to fix reported bug on mailinglist.
  1274. Also some (de)allocation info for ESI added. Between -dallocEDI
  1275. because at this time of the night bugs could easily slip in ;)
  1276. Revision 1.114 2000/01/07 01:14:20 peter
  1277. * updated copyright to 2000
  1278. Revision 1.113 1999/12/22 01:01:46 peter
  1279. - removed freelabel()
  1280. * added undefined label detection in internal assembler, this prevents
  1281. a lot of ld crashes and wrong .o files
  1282. * .o files aren't written anymore if errors have occured
  1283. * inlining of assembler labels is now correct
  1284. Revision 1.112 1999/12/13 21:49:54 pierre
  1285. * bug in extdebugg code for inlined procedures
  1286. Revision 1.111 1999/11/30 10:40:42 peter
  1287. + ttype, tsymlist
  1288. Revision 1.110 1999/11/06 14:34:17 peter
  1289. * truncated log to 20 revs
  1290. Revision 1.109 1999/11/04 00:23:58 pierre
  1291. * fix for fpuvaroffset for unused return value
  1292. Revision 1.108 1999/10/26 12:30:40 peter
  1293. * const parameter is now checked
  1294. * better and generic check if a node can be used for assigning
  1295. * export fixes
  1296. * procvar equal works now (it never had worked at least from 0.99.8)
  1297. * defcoll changed to linkedlist with pparaitem so it can easily be
  1298. walked both directions
  1299. Revision 1.107 1999/10/08 15:40:47 pierre
  1300. * use and remember that C functions with complex data results use ret $4
  1301. Revision 1.106 1999/09/27 23:44:46 peter
  1302. * procinfo is now a pointer
  1303. * support for result setting in sub procedure
  1304. Revision 1.105 1999/09/26 13:26:02 florian
  1305. * exception patch of Romio nevertheless the excpetion handling
  1306. needs some corections regarding register saving
  1307. * gettempansistring is again a procedure
  1308. Revision 1.104 1999/09/16 11:34:46 pierre
  1309. * typo correction
  1310. Revision 1.103 1999/09/07 07:54:23 peter
  1311. * small array push to open array fixed, open array always needs addr
  1312. pushing
  1313. Revision 1.102 1999/08/25 11:59:39 jonas
  1314. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1315. Revision 1.101 1999/08/23 23:38:18 pierre
  1316. + TEMPREGDEBUG code added
  1317. Revision 1.100 1999/08/19 13:08:45 pierre
  1318. * emit_??? used
  1319. Revision 1.99 1999/08/09 22:19:47 peter
  1320. * classes vmt changed to only positive addresses
  1321. * sharedlib creation is working
  1322. Revision 1.98 1999/08/09 10:37:55 peter
  1323. * fixed pushing of self with methodpointer
  1324. Revision 1.97 1999/08/04 13:45:18 florian
  1325. + floating point register variables !!
  1326. * pairegalloc is now generated for register variables
  1327. Revision 1.96 1999/08/04 00:22:41 florian
  1328. * renamed i386asm and i386base to cpuasm and cpubase
  1329. Revision 1.95 1999/08/03 22:02:34 peter
  1330. * moved bitmask constants to sets
  1331. * some other type/const renamings
  1332. Revision 1.94 1999/07/06 21:48:09 florian
  1333. * a lot bug fixes:
  1334. - po_external isn't any longer necessary for procedure compatibility
  1335. - m_tp_procvar is in -Sd now available
  1336. - error messages of procedure variables improved
  1337. - return values with init./finalization fixed
  1338. - data types with init./finalization aren't any longer allowed in variant
  1339. record
  1340. Revision 1.93 1999/06/22 13:31:24 peter
  1341. * merged
  1342. Revision 1.92 1999/06/16 09:32:45 peter
  1343. * merged
  1344. Revision 1.91 1999/06/14 17:47:47 peter
  1345. * merged
  1346. Revision 1.90.2.3 1999/06/22 13:30:08 peter
  1347. * fixed return with packenum
  1348. }