cg386cal.pas 66 KB

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