cg386cal.pas 67 KB

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