n386cal.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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 bymethodpointer
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n386cal;
  19. {$i defines.inc}
  20. interface
  21. { $define AnsiStrRef}
  22. uses
  23. symdef,node,ncal;
  24. type
  25. ti386callparanode = class(tcallparanode)
  26. procedure secondcallparan(defcoll : TParaItem;
  27. push_from_left_to_right,inlined,is_cdecl : boolean;
  28. para_alignment,para_offset : longint);override;
  29. end;
  30. ti386callnode = class(tcallnode)
  31. procedure pass_2;override;
  32. end;
  33. ti386procinlinenode = class(tprocinlinenode)
  34. procedure pass_2;override;
  35. end;
  36. implementation
  37. uses
  38. {$ifdef delphi}
  39. sysutils,
  40. {$else}
  41. strings,
  42. {$endif}
  43. globtype,systems,
  44. cutils,cobjects,verbose,globals,
  45. symconst,symbase,symtype,symsym,symtable,aasm,types,
  46. {$ifdef GDB}
  47. gdb,
  48. {$endif GDB}
  49. hcodegen,temp_gen,pass_2,
  50. cpubase,cpuasm,
  51. nmem,nld,
  52. cgai386,tgcpu,n386ld,n386util,regvars;
  53. {*****************************************************************************
  54. TI386CALLPARANODE
  55. *****************************************************************************}
  56. procedure ti386callparanode.secondcallparan(defcoll : TParaItem;
  57. push_from_left_to_right,inlined,is_cdecl : boolean;para_alignment,para_offset : longint);
  58. procedure maybe_push_high;
  59. begin
  60. { open array ? }
  61. { defcoll.data can be nil for read/write }
  62. if assigned(defcoll.paratype.def) and
  63. assigned(hightree) then
  64. begin
  65. secondpass(hightree);
  66. { this is a longint anyway ! }
  67. push_value_para(hightree,inlined,false,para_offset,4);
  68. end;
  69. end;
  70. var
  71. otlabel,oflabel : pasmlabel;
  72. { temporary variables: }
  73. tempdeftype : tdeftype;
  74. r : preference;
  75. begin
  76. { set default para_alignment to target_os.stackalignment }
  77. if para_alignment=0 then
  78. para_alignment:=target_os.stackalignment;
  79. { push from left to right if specified }
  80. if push_from_left_to_right and assigned(right) then
  81. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  82. inlined,is_cdecl,para_alignment,para_offset);
  83. otlabel:=truelabel;
  84. oflabel:=falselabel;
  85. getlabel(truelabel);
  86. getlabel(falselabel);
  87. secondpass(left);
  88. { filter array constructor with c styled args }
  89. if is_array_constructor(left.resulttype) and (nf_cargs in left.flags) then
  90. begin
  91. { nothing, everything is already pushed }
  92. end
  93. { in codegen.handleread.. defcoll.data is set to nil }
  94. else if assigned(defcoll.paratype.def) and
  95. (defcoll.paratype.def^.deftype=formaldef) then
  96. begin
  97. { allow @var }
  98. inc(pushedparasize,4);
  99. if (left.nodetype=addrn) and
  100. (not(nf_procvarload in left.flags)) then
  101. begin
  102. { always a register }
  103. if inlined then
  104. begin
  105. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  106. emit_reg_ref(A_MOV,S_L,
  107. left.location.register,r);
  108. end
  109. else
  110. emit_reg(A_PUSH,S_L,left.location.register);
  111. ungetregister32(left.location.register);
  112. end
  113. else
  114. begin
  115. if not(left.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  116. CGMessage(type_e_mismatch)
  117. else
  118. begin
  119. if inlined then
  120. begin
  121. getexplicitregister32(R_EDI);
  122. emit_ref_reg(A_LEA,S_L,
  123. newreference(left.location.reference),R_EDI);
  124. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  125. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  126. ungetregister32(R_EDI);
  127. end
  128. else
  129. emitpushreferenceaddr(left.location.reference);
  130. del_reference(left.location.reference);
  131. end;
  132. end;
  133. end
  134. { handle call by reference parameter }
  135. else if (defcoll.paratyp in [vs_var,vs_out]) then
  136. begin
  137. if (left.location.loc<>LOC_REFERENCE) then
  138. CGMessage(cg_e_var_must_be_reference);
  139. maybe_push_high;
  140. if (defcoll.paratyp=vs_out) and
  141. assigned(defcoll.paratype.def) and
  142. not is_class(defcoll.paratype.def) and
  143. defcoll.paratype.def^.needs_inittable then
  144. finalize(defcoll.paratype.def,left.location.reference,false);
  145. inc(pushedparasize,4);
  146. if inlined then
  147. begin
  148. getexplicitregister32(R_EDI);
  149. emit_ref_reg(A_LEA,S_L,
  150. newreference(left.location.reference),R_EDI);
  151. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  152. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  153. ungetregister32(R_EDI);
  154. end
  155. else
  156. emitpushreferenceaddr(left.location.reference);
  157. del_reference(left.location.reference);
  158. end
  159. else
  160. begin
  161. tempdeftype:=resulttype^.deftype;
  162. if tempdeftype=filedef then
  163. CGMessage(cg_e_file_must_call_by_reference);
  164. { open array must always push the address, this is needed to
  165. also push addr of small open arrays and with cdecl functions (PFV) }
  166. if (
  167. assigned(defcoll.paratype.def) and
  168. (is_open_array(defcoll.paratype.def) or
  169. is_array_of_const(defcoll.paratype.def))
  170. ) or
  171. (
  172. push_addr_param(resulttype) and
  173. not is_cdecl
  174. ) then
  175. begin
  176. maybe_push_high;
  177. inc(pushedparasize,4);
  178. if inlined then
  179. begin
  180. getexplicitregister32(R_EDI);
  181. emit_ref_reg(A_LEA,S_L,
  182. newreference(left.location.reference),R_EDI);
  183. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  184. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  185. ungetregister32(R_EDI);
  186. end
  187. else
  188. emitpushreferenceaddr(left.location.reference);
  189. del_reference(left.location.reference);
  190. end
  191. else
  192. begin
  193. push_value_para(left,inlined,is_cdecl,
  194. para_offset,para_alignment);
  195. end;
  196. end;
  197. truelabel:=otlabel;
  198. falselabel:=oflabel;
  199. { push from right to left }
  200. if not push_from_left_to_right and assigned(right) then
  201. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  202. inlined,is_cdecl,para_alignment,para_offset);
  203. end;
  204. {*****************************************************************************
  205. TI386CALLNODE
  206. *****************************************************************************}
  207. procedure ti386callnode.pass_2;
  208. var
  209. unusedregisters : tregisterset;
  210. usablecount : byte;
  211. pushed : tpushed;
  212. hr,funcretref : treference;
  213. hregister,hregister2 : tregister;
  214. oldpushedparasize : longint;
  215. { true if ESI must be loaded again after the subroutine }
  216. loadesi : boolean;
  217. { true if a virtual method must be called directly }
  218. no_virtual_call : boolean;
  219. { true if we produce a con- or destrutor in a call }
  220. is_con_or_destructor : boolean;
  221. { true if a constructor is called again }
  222. extended_new : boolean;
  223. { adress returned from an I/O-error }
  224. iolabel : pasmlabel;
  225. { lexlevel count }
  226. i : longint;
  227. { help reference pointer }
  228. r : preference;
  229. hp : tnode;
  230. pp : tbinarynode;
  231. params : tnode;
  232. inlined : boolean;
  233. inlinecode : tprocinlinenode;
  234. para_alignment,
  235. para_offset : longint;
  236. { instruction for alignement correction }
  237. { corr : paicpu;}
  238. { we must pop this size also after !! }
  239. { must_pop : boolean; }
  240. pop_size : longint;
  241. push_size : longint;
  242. pop_esp : boolean;
  243. pop_allowed : boolean;
  244. regs_to_push : byte;
  245. constructorfailed : pasmlabel;
  246. label
  247. dont_call;
  248. begin
  249. reset_reference(location.reference);
  250. extended_new:=false;
  251. iolabel:=nil;
  252. inlinecode:=nil;
  253. inlined:=false;
  254. loadesi:=true;
  255. no_virtual_call:=false;
  256. unusedregisters:=unused;
  257. usablecount:=usablereg32;
  258. if ([pocall_cdecl,pocall_cppdecl,pocall_stdcall]*procdefinition^.proccalloptions)<>[] then
  259. para_alignment:=4
  260. else
  261. para_alignment:=target_os.stackalignment;
  262. if not assigned(procdefinition) then
  263. exit;
  264. { Deciding whether we may still need the parameters happens next (JM) }
  265. if assigned(left) then
  266. params:=left.getcopy
  267. else params := nil;
  268. if (pocall_inline in procdefinition^.proccalloptions) then
  269. begin
  270. inlined:=true;
  271. inlinecode:=tprocinlinenode(right);
  272. { set it to the same lexical level as the local symtable, becuase
  273. the para's are stored there }
  274. pprocdef(procdefinition)^.parast^.symtablelevel:=aktprocsym^.definition^.localst^.symtablelevel;
  275. if assigned(params) then
  276. inlinecode.para_offset:=gettempofsizepersistant(inlinecode.para_size);
  277. pprocdef(procdefinition)^.parast^.address_fixup:=inlinecode.para_offset;
  278. {$ifdef extdebug}
  279. Comment(V_debug,
  280. 'inlined parasymtable is at offset '
  281. +tostr(pprocdef(procdefinition)^.parast^.address_fixup));
  282. exprasmList.concat(Tai_asm_comment.Create(
  283. strpnew('inlined parasymtable is at offset '
  284. +tostr(pprocdef(procdefinition)^.parast^.address_fixup))));
  285. {$endif extdebug}
  286. { disable further inlining of the same proc
  287. in the args }
  288. exclude(procdefinition^.proccalloptions,pocall_inline);
  289. end;
  290. { only if no proc var }
  291. if inlined or
  292. not(assigned(right)) then
  293. is_con_or_destructor:=(procdefinition^.proctypeoption in [potype_constructor,potype_destructor]);
  294. { proc variables destroy all registers }
  295. if (inlined or
  296. (right=nil)) and
  297. { virtual methods too }
  298. not(po_virtualmethod in procdefinition^.procoptions) then
  299. begin
  300. if (cs_check_io in aktlocalswitches) and
  301. (po_iocheck in procdefinition^.procoptions) and
  302. not(po_iocheck in aktprocsym^.definition^.procoptions) then
  303. begin
  304. getaddrlabel(iolabel);
  305. emitlab(iolabel);
  306. end
  307. else
  308. iolabel:=nil;
  309. { save all used registers }
  310. regs_to_push := pprocdef(procdefinition)^.usedregisters;
  311. pushusedregisters(pushed,regs_to_push);
  312. { give used registers through }
  313. usedinproc:=usedinproc or pprocdef(procdefinition)^.usedregisters;
  314. end
  315. else
  316. begin
  317. regs_to_push := $ff;
  318. pushusedregisters(pushed,regs_to_push);
  319. usedinproc:=$ff;
  320. { no IO check for methods and procedure variables }
  321. iolabel:=nil;
  322. end;
  323. { generate the code for the parameter and push them }
  324. oldpushedparasize:=pushedparasize;
  325. pushedparasize:=0;
  326. pop_size:=0;
  327. { no inc esp for inlined procedure
  328. and for objects constructors PM }
  329. if (inlined or
  330. (right=nil)) and
  331. (procdefinition^.proctypeoption=potype_constructor) and
  332. { quick'n'dirty check if it is a class or an object }
  333. (resulttype^.deftype=orddef) then
  334. pop_allowed:=false
  335. else
  336. pop_allowed:=true;
  337. if pop_allowed then
  338. begin
  339. { Old pushedsize aligned on 4 ? }
  340. i:=oldpushedparasize and 3;
  341. if i>0 then
  342. inc(pop_size,4-i);
  343. { This parasize aligned on 4 ? }
  344. i:=procdefinition^.para_size(para_alignment) and 3;
  345. if i>0 then
  346. inc(pop_size,4-i);
  347. { insert the opcode and update pushedparasize }
  348. { never push 4 or more !! }
  349. pop_size:=pop_size mod 4;
  350. if pop_size>0 then
  351. begin
  352. inc(pushedparasize,pop_size);
  353. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  354. {$ifdef GDB}
  355. if (cs_debuginfo in aktmoduleswitches) and
  356. (exprasmList.first=exprasmList.last) then
  357. exprasmList.concat(Tai_force_line.Create);
  358. {$endif GDB}
  359. end;
  360. end;
  361. {
  362. if pop_allowed and (cs_align in aktglobalswitches) then
  363. begin
  364. pop_esp:=true;
  365. push_size:=procdefinition^.para_size(para_alignment);
  366. { !!!! here we have to take care of return type, self
  367. and nested procedures
  368. }
  369. inc(push_size,12);
  370. emit_reg_reg(A_MOV,S_L,R_ESP,R_EDI);
  371. if (push_size mod 8)=0 then
  372. emit_const_reg(A_AND,S_L,longint($fffffff8),R_ESP)
  373. else
  374. begin
  375. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  376. emit_const_reg(A_AND,S_L,longint($fffffff8),R_ESP);
  377. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  378. end;
  379. emit_reg(A_PUSH,S_L,R_EDI);
  380. end
  381. else
  382. }
  383. pop_esp:=false;
  384. if (resulttype<>pdef(voiddef)) and
  385. ret_in_param(resulttype) then
  386. begin
  387. funcretref.symbol:=nil;
  388. {$ifdef test_dest_loc}
  389. if dest_loc_known and (dest_loc_tree=p) and
  390. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  391. begin
  392. funcretref:=dest_loc.reference;
  393. if assigned(dest_loc.reference.symbol) then
  394. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  395. in_dest_loc:=true;
  396. end
  397. else
  398. {$endif test_dest_loc}
  399. if inlined then
  400. begin
  401. reset_reference(funcretref);
  402. funcretref.offset:=gettempofsizepersistant(procdefinition^.rettype.def^.size);
  403. funcretref.base:=procinfo^.framepointer;
  404. end
  405. else
  406. gettempofsizereference(procdefinition^.rettype.def^.size,funcretref);
  407. end;
  408. if assigned(params) then
  409. begin
  410. { be found elsewhere }
  411. if inlined then
  412. para_offset:=pprocdef(procdefinition)^.parast^.address_fixup+
  413. pprocdef(procdefinition)^.parast^.datasize
  414. else
  415. para_offset:=0;
  416. if not(inlined) and
  417. assigned(right) then
  418. tcallparanode(params).secondcallparan(TParaItem(pabstractprocdef(right.resulttype)^.Para.first),
  419. (pocall_leftright in procdefinition^.proccalloptions),inlined,
  420. (([pocall_cdecl,pocall_cppdecl]*procdefinition^.proccalloptions)<>[]),
  421. para_alignment,para_offset)
  422. else
  423. tcallparanode(params).secondcallparan(TParaItem(procdefinition^.Para.first),
  424. (pocall_leftright in procdefinition^.proccalloptions),inlined,
  425. (([pocall_cdecl,pocall_cppdecl]*procdefinition^.proccalloptions)<>[]),
  426. para_alignment,para_offset);
  427. end;
  428. if inlined then
  429. inlinecode.retoffset:=gettempofsizepersistant(4);
  430. if ret_in_param(resulttype) then
  431. begin
  432. { This must not be counted for C code
  433. complex return address is removed from stack
  434. by function itself ! }
  435. {$ifdef OLD_C_STACK}
  436. inc(pushedparasize,4); { lets try without it PM }
  437. {$endif not OLD_C_STACK}
  438. if inlined then
  439. begin
  440. {$ifndef noAllocEdi}
  441. getexplicitregister32(R_EDI);
  442. {$endif noAllocEdi}
  443. emit_ref_reg(A_LEA,S_L,
  444. newreference(funcretref),R_EDI);
  445. r:=new_reference(procinfo^.framepointer,inlinecode.retoffset);
  446. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  447. {$ifndef noAllocEdi}
  448. ungetregister32(R_EDI);
  449. {$endif noAllocEdi}
  450. end
  451. else
  452. emitpushreferenceaddr(funcretref);
  453. end;
  454. { procedure variable ? }
  455. if inlined or
  456. (right=nil) then
  457. begin
  458. { overloaded operator have no symtable }
  459. { push self }
  460. if assigned(symtableproc) and
  461. (symtableproc^.symtabletype=withsymtable) then
  462. begin
  463. { dirty trick to avoid the secondcall below }
  464. methodpointer:=ccallparanode.create(nil,nil);
  465. methodpointer.location.loc:=LOC_REGISTER;
  466. {$ifndef noAllocEDI}
  467. getexplicitregister32(R_ESI);
  468. {$endif noAllocEDI}
  469. methodpointer.location.register:=R_ESI;
  470. { ARGHHH this is wrong !!!
  471. if we can init from base class for a child
  472. class that the wrong VMT will be
  473. transfered to constructor !! }
  474. methodpointer.resulttype:=
  475. twithnode(pwithsymtable(symtableproc)^.withnode).left.resulttype;
  476. { make a reference }
  477. new(r);
  478. reset_reference(r^);
  479. { if assigned(ptree(pwithsymtable(symtable)^.withnode)^.pref) then
  480. begin
  481. r^:=ptree(pwithsymtable(symtable)^.withnode)^.pref^;
  482. end
  483. else
  484. begin
  485. r^.offset:=symtable^.datasize;
  486. r^.base:=procinfo^.framepointer;
  487. end; }
  488. r^:=twithnode(pwithsymtable(symtableproc)^.withnode).withreference^;
  489. if ((not(nf_islocal in twithnode(pwithsymtable(symtableproc)^.withnode).flags)) and
  490. (not pwithsymtable(symtableproc)^.direct_with)) or
  491. is_class_or_interface(methodpointer.resulttype) then
  492. emit_ref_reg(A_MOV,S_L,r,R_ESI)
  493. else
  494. emit_ref_reg(A_LEA,S_L,r,R_ESI);
  495. end;
  496. { push self }
  497. if assigned(symtableproc) and
  498. ((symtableproc^.symtabletype=objectsymtable) or
  499. (symtableproc^.symtabletype=withsymtable)) then
  500. begin
  501. if assigned(methodpointer) then
  502. begin
  503. {
  504. if methodpointer^.resulttype=classrefdef then
  505. begin
  506. two possibilities:
  507. 1. constructor
  508. 2. class method
  509. end
  510. else }
  511. begin
  512. case methodpointer.nodetype of
  513. typen:
  514. begin
  515. { direct call to inherited method }
  516. if (po_abstractmethod in procdefinition^.procoptions) then
  517. begin
  518. CGMessage(cg_e_cant_call_abstract_method);
  519. goto dont_call;
  520. end;
  521. { generate no virtual call }
  522. no_virtual_call:=true;
  523. if (sp_static in symtableprocentry^.symoptions) then
  524. begin
  525. { well lets put the VMT address directly into ESI }
  526. { it is kind of dirty but that is the simplest }
  527. { way to accept virtual static functions (PM) }
  528. loadesi:=true;
  529. { if no VMT just use $0 bug0214 PM }
  530. {$ifndef noAllocEDI}
  531. getexplicitregister32(R_ESI);
  532. {$endif noAllocEDI}
  533. if not(oo_has_vmt in pobjectdef(methodpointer.resulttype)^.objectoptions) then
  534. emit_const_reg(A_MOV,S_L,0,R_ESI)
  535. else
  536. begin
  537. emit_sym_ofs_reg(A_MOV,S_L,
  538. newasmsymbol(pobjectdef(methodpointer.resulttype)^.vmt_mangledname),
  539. 0,R_ESI);
  540. end;
  541. { emit_reg(A_PUSH,S_L,R_ESI);
  542. this is done below !! }
  543. end
  544. else
  545. { this is a member call, so ESI isn't modfied }
  546. loadesi:=false;
  547. { a class destructor needs a flag }
  548. if is_class(pobjectdef(methodpointer.resulttype)) and
  549. {assigned(aktprocsym) and
  550. (aktprocsym^.definition^.proctypeoption=potype_destructor)}
  551. (procdefinition^.proctypeoption=potype_destructor) then
  552. begin
  553. push_int(0);
  554. emit_reg(A_PUSH,S_L,R_ESI);
  555. end;
  556. if not(is_con_or_destructor and
  557. is_class(methodpointer.resulttype) and
  558. {assigned(aktprocsym) and
  559. (aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor])}
  560. (procdefinition^.proctypeoption in [potype_constructor,potype_destructor])
  561. ) then
  562. emit_reg(A_PUSH,S_L,R_ESI);
  563. { if an inherited con- or destructor should be }
  564. { called in a con- or destructor then a warning }
  565. { will be made }
  566. { con- and destructors need a pointer to the vmt }
  567. if is_con_or_destructor and
  568. is_object(methodpointer.resulttype) and
  569. assigned(aktprocsym) then
  570. begin
  571. if not(aktprocsym^.definition^.proctypeoption in
  572. [potype_constructor,potype_destructor]) then
  573. CGMessage(cg_w_member_cd_call_from_method);
  574. end;
  575. { class destructors get there flag above }
  576. { constructor flags ? }
  577. if is_con_or_destructor and
  578. not(
  579. is_class(methodpointer.resulttype) and
  580. assigned(aktprocsym) and
  581. (aktprocsym^.definition^.proctypeoption=potype_destructor)) then
  582. begin
  583. { a constructor needs also a flag }
  584. if is_class(methodpointer.resulttype) then
  585. push_int(0);
  586. push_int(0);
  587. end;
  588. end;
  589. hnewn:
  590. begin
  591. { extended syntax of new }
  592. { ESI must be zero }
  593. {$ifndef noAllocEDI}
  594. getexplicitregister32(R_ESI);
  595. {$endif noAllocEDI}
  596. emit_reg_reg(A_XOR,S_L,R_ESI,R_ESI);
  597. emit_reg(A_PUSH,S_L,R_ESI);
  598. { insert the vmt }
  599. emit_sym(A_PUSH,S_L,
  600. newasmsymbol(pobjectdef(methodpointer.resulttype)^.vmt_mangledname));
  601. extended_new:=true;
  602. end;
  603. hdisposen:
  604. begin
  605. secondpass(methodpointer);
  606. { destructor with extended syntax called from dispose }
  607. { hdisposen always deliver LOC_REFERENCE }
  608. {$ifndef noAllocEDI}
  609. getexplicitregister32(R_ESI);
  610. {$endif noAllocEDI}
  611. emit_ref_reg(A_LEA,S_L,
  612. newreference(methodpointer.location.reference),R_ESI);
  613. del_reference(methodpointer.location.reference);
  614. emit_reg(A_PUSH,S_L,R_ESI);
  615. emit_sym(A_PUSH,S_L,
  616. newasmsymbol(pobjectdef(methodpointer.resulttype)^.vmt_mangledname));
  617. end;
  618. else
  619. begin
  620. { call to an instance member }
  621. if (symtableproc^.symtabletype<>withsymtable) then
  622. begin
  623. secondpass(methodpointer);
  624. {$ifndef noAllocEDI}
  625. getexplicitregister32(R_ESI);
  626. {$endif noAllocEDI}
  627. case methodpointer.location.loc of
  628. LOC_CREGISTER,
  629. LOC_REGISTER:
  630. begin
  631. emit_reg_reg(A_MOV,S_L,methodpointer.location.register,R_ESI);
  632. ungetregister32(methodpointer.location.register);
  633. end;
  634. else
  635. begin
  636. if (methodpointer.resulttype^.deftype=classrefdef) or
  637. is_class_or_interface(methodpointer.resulttype) then
  638. emit_ref_reg(A_MOV,S_L,
  639. newreference(methodpointer.location.reference),R_ESI)
  640. else
  641. emit_ref_reg(A_LEA,S_L,
  642. newreference(methodpointer.location.reference),R_ESI);
  643. del_reference(methodpointer.location.reference);
  644. end;
  645. end;
  646. end;
  647. { when calling a class method, we have to load ESI with the VMT !
  648. But, not for a class method via self }
  649. if not(po_containsself in procdefinition^.procoptions) then
  650. begin
  651. if (po_classmethod in procdefinition^.procoptions) and
  652. not(methodpointer.resulttype^.deftype=classrefdef) then
  653. begin
  654. { class method needs current VMT }
  655. getexplicitregister32(R_ESI);
  656. new(r);
  657. reset_reference(r^);
  658. r^.base:=R_ESI;
  659. r^.offset:= pprocdef(procdefinition)^._class^.vmt_offset;
  660. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  661. end;
  662. { direct call to destructor: remove data }
  663. if (procdefinition^.proctypeoption=potype_destructor) and
  664. is_class(methodpointer.resulttype) then
  665. emit_const(A_PUSH,S_L,1);
  666. { direct call to class constructor, don't allocate memory }
  667. if (procdefinition^.proctypeoption=potype_constructor) and
  668. is_class(methodpointer.resulttype) then
  669. begin
  670. emit_const(A_PUSH,S_L,0);
  671. emit_const(A_PUSH,S_L,0);
  672. end
  673. else
  674. begin
  675. { constructor call via classreference => allocate memory }
  676. if (procdefinition^.proctypeoption=potype_constructor) and
  677. (methodpointer.resulttype^.deftype=classrefdef) and
  678. is_class(pclassrefdef(methodpointer.resulttype)^.pointertype.def) then
  679. emit_const(A_PUSH,S_L,1);
  680. emit_reg(A_PUSH,S_L,R_ESI);
  681. end;
  682. end;
  683. if is_con_or_destructor then
  684. begin
  685. { classes don't get a VMT pointer pushed }
  686. if is_object(methodpointer.resulttype) then
  687. begin
  688. if (procdefinition^.proctypeoption=potype_constructor) then
  689. begin
  690. { it's no bad idea, to insert the VMT }
  691. emit_sym(A_PUSH,S_L,newasmsymbol(
  692. pobjectdef(methodpointer.resulttype)^.vmt_mangledname));
  693. end
  694. { destructors haven't to dispose the instance, if this is }
  695. { a direct call }
  696. else
  697. push_int(0);
  698. end;
  699. end;
  700. end;
  701. end;
  702. end;
  703. end
  704. else
  705. begin
  706. if (po_classmethod in procdefinition^.procoptions) and
  707. not(
  708. assigned(aktprocsym) and
  709. (po_classmethod in aktprocsym^.definition^.procoptions)
  710. ) then
  711. begin
  712. { class method needs current VMT }
  713. getexplicitregister32(R_ESI);
  714. new(r);
  715. reset_reference(r^);
  716. r^.base:=R_ESI;
  717. r^.offset:= pprocdef(procdefinition)^._class^.vmt_offset;
  718. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  719. end
  720. else
  721. begin
  722. { member call, ESI isn't modified }
  723. loadesi:=false;
  724. end;
  725. { direct call to destructor: don't remove data! }
  726. if is_class(procinfo^._class) then
  727. begin
  728. if (procdefinition^.proctypeoption=potype_destructor) then
  729. begin
  730. emit_const(A_PUSH,S_L,0);
  731. emit_reg(A_PUSH,S_L,R_ESI);
  732. end
  733. else if (procdefinition^.proctypeoption=potype_constructor) then
  734. begin
  735. emit_const(A_PUSH,S_L,0);
  736. emit_const(A_PUSH,S_L,0);
  737. end
  738. else
  739. emit_reg(A_PUSH,S_L,R_ESI);
  740. end
  741. else if is_object(procinfo^._class) then
  742. begin
  743. emit_reg(A_PUSH,S_L,R_ESI);
  744. if is_con_or_destructor then
  745. begin
  746. if (procdefinition^.proctypeoption=potype_constructor) then
  747. begin
  748. { it's no bad idea, to insert the VMT }
  749. emit_sym(A_PUSH,S_L,newasmsymbol(
  750. procinfo^._class^.vmt_mangledname));
  751. end
  752. { destructors haven't to dispose the instance, if this is }
  753. { a direct call }
  754. else
  755. push_int(0);
  756. end;
  757. end
  758. else
  759. Internalerror(200006165);
  760. end;
  761. end;
  762. { call to BeforeDestruction? }
  763. if (procdefinition^.proctypeoption=potype_destructor) and
  764. assigned(methodpointer) and
  765. (methodpointer.nodetype<>typen) and
  766. is_class(pobjectdef(methodpointer.resulttype)) and
  767. (inlined or
  768. (right=nil)) then
  769. begin
  770. emit_reg(A_PUSH,S_L,R_ESI);
  771. new(r);
  772. reset_reference(r^);
  773. r^.base:=R_ESI;
  774. getexplicitregister32(R_EDI);
  775. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  776. new(r);
  777. reset_reference(r^);
  778. r^.offset:=72;
  779. r^.base:=R_EDI;
  780. emit_ref(A_CALL,S_NO,r);
  781. ungetregister32(R_EDI);
  782. end;
  783. { push base pointer ?}
  784. if (lexlevel>=normal_function_level) and assigned(pprocdef(procdefinition)^.parast) and
  785. ((pprocdef(procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  786. begin
  787. { if we call a nested function in a method, we must }
  788. { push also SELF! }
  789. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  790. { access }
  791. {
  792. begin
  793. loadesi:=false;
  794. emit_reg(A_PUSH,S_L,R_ESI);
  795. end;
  796. }
  797. if lexlevel=(pprocdef(procdefinition)^.parast^.symtablelevel) then
  798. begin
  799. new(r);
  800. reset_reference(r^);
  801. r^.offset:=procinfo^.framepointer_offset;
  802. r^.base:=procinfo^.framepointer;
  803. emit_ref(A_PUSH,S_L,r)
  804. end
  805. { this is only true if the difference is one !!
  806. but it cannot be more !! }
  807. else if (lexlevel=pprocdef(procdefinition)^.parast^.symtablelevel-1) then
  808. begin
  809. emit_reg(A_PUSH,S_L,procinfo^.framepointer)
  810. end
  811. else if (lexlevel>pprocdef(procdefinition)^.parast^.symtablelevel) then
  812. begin
  813. hregister:=getregister32;
  814. new(r);
  815. reset_reference(r^);
  816. r^.offset:=procinfo^.framepointer_offset;
  817. r^.base:=procinfo^.framepointer;
  818. emit_ref_reg(A_MOV,S_L,r,hregister);
  819. for i:=(pprocdef(procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  820. begin
  821. new(r);
  822. reset_reference(r^);
  823. {we should get the correct frame_pointer_offset at each level
  824. how can we do this !!! }
  825. r^.offset:=procinfo^.framepointer_offset;
  826. r^.base:=hregister;
  827. emit_ref_reg(A_MOV,S_L,r,hregister);
  828. end;
  829. emit_reg(A_PUSH,S_L,hregister);
  830. ungetregister32(hregister);
  831. end
  832. else
  833. internalerror(25000);
  834. end;
  835. saveregvars(regs_to_push);
  836. if (po_virtualmethod in procdefinition^.procoptions) and
  837. not(no_virtual_call) then
  838. begin
  839. { static functions contain the vmt_address in ESI }
  840. { also class methods }
  841. { Here it is quite tricky because it also depends }
  842. { on the methodpointer PM }
  843. getexplicitregister32(R_ESI);
  844. if assigned(aktprocsym) then
  845. begin
  846. if (((sp_static in aktprocsym^.symoptions) or
  847. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  848. ((methodpointer=nil) or (methodpointer.nodetype=typen)))
  849. or
  850. (po_staticmethod in procdefinition^.procoptions) or
  851. ((procdefinition^.proctypeoption=potype_constructor) and
  852. { esi contains the vmt if we call a constructor via a class ref }
  853. assigned(methodpointer) and
  854. (methodpointer.resulttype^.deftype=classrefdef)
  855. ) or
  856. { is_interface(pprocdef(procdefinition)^._class) or }
  857. { ESI is loaded earlier }
  858. (po_classmethod in procdefinition^.procoptions) then
  859. begin
  860. new(r);
  861. reset_reference(r^);
  862. r^.base:=R_ESI;
  863. end
  864. else
  865. begin
  866. new(r);
  867. reset_reference(r^);
  868. r^.base:=R_ESI;
  869. { this is one point where we need vmt_offset (PM) }
  870. r^.offset:= pprocdef(procdefinition)^._class^.vmt_offset;
  871. getexplicitregister32(R_EDI);
  872. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  873. new(r);
  874. reset_reference(r^);
  875. r^.base:=R_EDI;
  876. end;
  877. end
  878. else
  879. { aktprocsym should be assigned, also in main program }
  880. internalerror(12345);
  881. {
  882. begin
  883. new(r);
  884. reset_reference(r^);
  885. r^.base:=R_ESI;
  886. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  887. new(r);
  888. reset_reference(r^);
  889. r^.base:=R_EDI;
  890. end;
  891. }
  892. if pprocdef(procdefinition)^.extnumber=-1 then
  893. internalerror(44584);
  894. r^.offset:=pprocdef(procdefinition)^._class^.vmtmethodoffset(pprocdef(procdefinition)^.extnumber);
  895. if not(is_interface(pprocdef(procdefinition)^._class)) and
  896. not(is_cppclass(pprocdef(procdefinition)^._class)) then
  897. begin
  898. if (cs_check_object_ext in aktlocalswitches) then
  899. begin
  900. emit_sym(A_PUSH,S_L,
  901. newasmsymbol(pprocdef(procdefinition)^._class^.vmt_mangledname));
  902. emit_reg(A_PUSH,S_L,r^.base);
  903. emitcall('FPC_CHECK_OBJECT_EXT');
  904. end
  905. else if (cs_check_range in aktlocalswitches) then
  906. begin
  907. emit_reg(A_PUSH,S_L,r^.base);
  908. emitcall('FPC_CHECK_OBJECT');
  909. end;
  910. end;
  911. emit_ref(A_CALL,S_NO,r);
  912. {$ifndef noAllocEdi}
  913. ungetregister32(R_EDI);
  914. {$endif noAllocEdi}
  915. end
  916. else if not inlined then
  917. begin
  918. { We can call interrupts from within the smae code
  919. by just pushing the flags and CS PM }
  920. if (po_interrupt in procdefinition^.procoptions) then
  921. begin
  922. emit_none(A_PUSHF,S_L);
  923. emit_reg(A_PUSH,S_L,R_CS);
  924. end;
  925. emitcall(pprocdef(procdefinition)^.mangledname);
  926. end
  927. else { inlined proc }
  928. { inlined code is in inlinecode }
  929. begin
  930. { set poinline again }
  931. include(procdefinition^.proccalloptions,pocall_inline);
  932. { process the inlinecode }
  933. secondpass(inlinecode);
  934. { free the args }
  935. if pprocdef(procdefinition)^.parast^.datasize>0 then
  936. ungetpersistanttemp(pprocdef(procdefinition)^.parast^.address_fixup);
  937. end;
  938. end
  939. else
  940. { now procedure variable case }
  941. begin
  942. secondpass(right);
  943. if (po_interrupt in procdefinition^.procoptions) then
  944. begin
  945. emit_none(A_PUSHF,S_L);
  946. emit_reg(A_PUSH,S_L,R_CS);
  947. end;
  948. { procedure of object? }
  949. if (po_methodpointer in procdefinition^.procoptions) then
  950. begin
  951. { method pointer can't be in a register }
  952. hregister:=R_NO;
  953. { do some hacking if we call a method pointer }
  954. { which is a class member }
  955. { else ESI is overwritten ! }
  956. if (right.location.reference.base=R_ESI) or
  957. (right.location.reference.index=R_ESI) then
  958. begin
  959. del_reference(right.location.reference);
  960. getexplicitregister32(R_EDI);
  961. emit_ref_reg(A_MOV,S_L,
  962. newreference(right.location.reference),R_EDI);
  963. hregister:=R_EDI;
  964. end;
  965. { load self, but not if it's already explicitly pushed }
  966. if not(po_containsself in procdefinition^.procoptions) then
  967. begin
  968. { load ESI }
  969. inc(right.location.reference.offset,4);
  970. getexplicitregister32(R_ESI);
  971. emit_ref_reg(A_MOV,S_L,
  972. newreference(right.location.reference),R_ESI);
  973. dec(right.location.reference.offset,4);
  974. { push self pointer }
  975. emit_reg(A_PUSH,S_L,R_ESI);
  976. end;
  977. saveregvars($ff);
  978. if hregister=R_NO then
  979. emit_ref(A_CALL,S_NO,newreference(right.location.reference))
  980. else
  981. begin
  982. {$ifndef noAllocEdi}
  983. ungetregister32(hregister);
  984. {$else noAllocEdi}
  985. { the same code, the previous line is just to }
  986. { indicate EDI actually is deallocated if allocated }
  987. { above (JM) }
  988. ungetregister32(hregister);
  989. {$endif noAllocEdi}
  990. emit_reg(A_CALL,S_NO,hregister);
  991. end;
  992. del_reference(right.location.reference);
  993. end
  994. else
  995. begin
  996. saveregvars($ff);
  997. case right.location.loc of
  998. LOC_REGISTER,LOC_CREGISTER:
  999. begin
  1000. emit_reg(A_CALL,S_NO,right.location.register);
  1001. ungetregister32(right.location.register);
  1002. end
  1003. else
  1004. emit_ref(A_CALL,S_NO,newreference(right.location.reference));
  1005. del_reference(right.location.reference);
  1006. end;
  1007. end;
  1008. end;
  1009. { this was only for normal functions
  1010. displaced here so we also get
  1011. it to work for procvars PM }
  1012. if (not inlined) and (pocall_clearstack in procdefinition^.proccalloptions) then
  1013. begin
  1014. { we also add the pop_size which is included in pushedparasize }
  1015. pop_size:=0;
  1016. { better than an add on all processors }
  1017. if pushedparasize=4 then
  1018. begin
  1019. getexplicitregister32(R_EDI);
  1020. emit_reg(A_POP,S_L,R_EDI);
  1021. ungetregister32(R_EDI);
  1022. end
  1023. { the pentium has two pipes and pop reg is pairable }
  1024. { but the registers must be different! }
  1025. else if (pushedparasize=8) and
  1026. not(cs_littlesize in aktglobalswitches) and
  1027. (aktoptprocessor=ClassP5) and
  1028. (procinfo^._class=nil) then
  1029. begin
  1030. getexplicitregister32(R_EDI);
  1031. emit_reg(A_POP,S_L,R_EDI);
  1032. ungetregister32(R_EDI);
  1033. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1034. emit_reg(A_POP,S_L,R_ESI);
  1035. exprasmList.concat(Tairegalloc.Alloc(R_ESI));
  1036. end
  1037. else if pushedparasize<>0 then
  1038. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  1039. end;
  1040. if pop_esp then
  1041. emit_reg(A_POP,S_L,R_ESP);
  1042. dont_call:
  1043. pushedparasize:=oldpushedparasize;
  1044. unused:=unusedregisters;
  1045. usablereg32:=usablecount;
  1046. {$ifdef TEMPREGDEBUG}
  1047. testregisters32;
  1048. {$endif TEMPREGDEBUG}
  1049. { a constructor could be a function with boolean result }
  1050. { if calling constructor called fail we
  1051. must jump directly to quickexitlabel PM
  1052. but only if it is a call of an inherited constructor }
  1053. if (inlined or
  1054. (right=nil)) and
  1055. (procdefinition^.proctypeoption=potype_constructor) and
  1056. assigned(methodpointer) and
  1057. (methodpointer.nodetype=typen) and
  1058. (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1059. begin
  1060. emitjmp(C_Z,faillabel);
  1061. end;
  1062. { call to AfterConstruction? }
  1063. if is_class(resulttype) and
  1064. (inlined or
  1065. (right=nil)) and
  1066. (procdefinition^.proctypeoption=potype_constructor) and
  1067. assigned(methodpointer) and
  1068. (methodpointer.nodetype<>typen) then
  1069. begin
  1070. getlabel(constructorfailed);
  1071. emitjmp(C_Z,constructorfailed);
  1072. emit_reg(A_PUSH,S_L,R_ESI);
  1073. new(r);
  1074. reset_reference(r^);
  1075. r^.base:=R_ESI;
  1076. getexplicitregister32(R_EDI);
  1077. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  1078. new(r);
  1079. reset_reference(r^);
  1080. r^.offset:=68;
  1081. r^.base:=R_EDI;
  1082. emit_ref(A_CALL,S_NO,r);
  1083. ungetregister32(R_EDI);
  1084. exprasmList.concat(Tairegalloc.Alloc(R_EAX));
  1085. emitlab(constructorfailed);
  1086. emit_reg_reg(A_MOV,S_L,R_ESI,R_EAX);
  1087. end;
  1088. { handle function results }
  1089. { structured results are easy to handle.... }
  1090. { needed also when result_no_used !! }
  1091. if (resulttype<>pdef(voiddef)) and ret_in_param(resulttype) then
  1092. begin
  1093. location.loc:=LOC_MEM;
  1094. location.reference.symbol:=nil;
  1095. location.reference:=funcretref;
  1096. end;
  1097. { we have only to handle the result if it is used, but }
  1098. { ansi/widestrings must be registered, so we can dispose them }
  1099. if (resulttype<>pdef(voiddef)) and ((nf_return_value_used in flags) or
  1100. is_ansistring(resulttype) or is_widestring(resulttype)) then
  1101. begin
  1102. { a contructor could be a function with boolean result }
  1103. if (inlined or
  1104. (right=nil)) and
  1105. (procdefinition^.proctypeoption=potype_constructor) and
  1106. { quick'n'dirty check if it is a class or an object }
  1107. (resulttype^.deftype=orddef) then
  1108. begin
  1109. { this fails if popsize > 0 PM }
  1110. location.loc:=LOC_FLAGS;
  1111. location.resflags:=F_NE;
  1112. if extended_new then
  1113. begin
  1114. {$ifdef test_dest_loc}
  1115. if dest_loc_known and (dest_loc_tree=p) then
  1116. mov_reg_to_dest(p,S_L,R_EAX)
  1117. else
  1118. {$endif test_dest_loc}
  1119. begin
  1120. hregister:=getexplicitregister32(R_EAX);
  1121. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1122. location.register:=hregister;
  1123. end;
  1124. end;
  1125. end
  1126. { structed results are easy to handle.... }
  1127. else if ret_in_param(resulttype) then
  1128. begin
  1129. {location.loc:=LOC_MEM;
  1130. stringdispose(location.reference.symbol);
  1131. location.reference:=funcretref;
  1132. already done above (PM) }
  1133. end
  1134. else
  1135. begin
  1136. if (resulttype^.deftype in [orddef,enumdef]) then
  1137. begin
  1138. location.loc:=LOC_REGISTER;
  1139. case resulttype^.size of
  1140. 4 :
  1141. begin
  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. location.register:=hregister;
  1151. end;
  1152. end;
  1153. 1 :
  1154. begin
  1155. {$ifdef test_dest_loc}
  1156. if dest_loc_known and (dest_loc_tree=p) then
  1157. mov_reg_to_dest(p,S_B,R_AL)
  1158. else
  1159. {$endif test_dest_loc}
  1160. begin
  1161. hregister:=getexplicitregister32(R_EAX);
  1162. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1163. location.register:=reg32toreg8(hregister);
  1164. end;
  1165. end;
  1166. 2 :
  1167. begin
  1168. {$ifdef test_dest_loc}
  1169. if dest_loc_known and (dest_loc_tree=p) then
  1170. mov_reg_to_dest(p,S_W,R_AX)
  1171. else
  1172. {$endif test_dest_loc}
  1173. begin
  1174. hregister:=getexplicitregister32(R_EAX);
  1175. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1176. location.register:=reg32toreg16(hregister);
  1177. end;
  1178. end;
  1179. 8 :
  1180. begin
  1181. {$ifdef test_dest_loc}
  1182. {$error Don't know what to do here}
  1183. {$endif test_dest_loc}
  1184. if R_EDX in unused then
  1185. begin
  1186. hregister2:=getexplicitregister32(R_EDX);
  1187. hregister:=getexplicitregister32(R_EAX);
  1188. end
  1189. else
  1190. begin
  1191. hregister:=getexplicitregister32(R_EAX);
  1192. hregister2:=getexplicitregister32(R_EDX);
  1193. end;
  1194. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1195. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  1196. location.registerlow:=hregister;
  1197. location.registerhigh:=hregister2;
  1198. end;
  1199. else internalerror(7);
  1200. end
  1201. end
  1202. else if (resulttype^.deftype=floatdef) then
  1203. case pfloatdef(resulttype)^.typ of
  1204. f32bit:
  1205. begin
  1206. location.loc:=LOC_REGISTER;
  1207. {$ifdef test_dest_loc}
  1208. if dest_loc_known and (dest_loc_tree=p) then
  1209. mov_reg_to_dest(p,S_L,R_EAX)
  1210. else
  1211. {$endif test_dest_loc}
  1212. begin
  1213. hregister:=getexplicitregister32(R_EAX);
  1214. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1215. location.register:=hregister;
  1216. end;
  1217. end;
  1218. else
  1219. begin
  1220. location.loc:=LOC_FPU;
  1221. inc(fpuvaroffset);
  1222. end;
  1223. end
  1224. else if is_ansistring(resulttype) or
  1225. is_widestring(resulttype) then
  1226. begin
  1227. hregister:=getexplicitregister32(R_EAX);
  1228. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1229. gettempansistringreference(hr);
  1230. decrstringref(resulttype,hr);
  1231. emit_reg_ref(A_MOV,S_L,hregister,
  1232. newreference(hr));
  1233. ungetregister32(hregister);
  1234. location.loc:=LOC_MEM;
  1235. location.reference:=hr;
  1236. end
  1237. else
  1238. begin
  1239. location.loc:=LOC_REGISTER;
  1240. {$ifdef test_dest_loc}
  1241. if dest_loc_known and (dest_loc_tree=p) then
  1242. mov_reg_to_dest(p,S_L,R_EAX)
  1243. else
  1244. {$endif test_dest_loc}
  1245. begin
  1246. hregister:=getexplicitregister32(R_EAX);
  1247. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1248. location.register:=hregister;
  1249. end;
  1250. end;
  1251. end;
  1252. end;
  1253. { perhaps i/o check ? }
  1254. if iolabel<>nil then
  1255. begin
  1256. emit_sym(A_PUSH,S_L,iolabel);
  1257. emitcall('FPC_IOCHECK');
  1258. end;
  1259. if pop_size>0 then
  1260. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1261. { restore registers }
  1262. popusedregisters(pushed);
  1263. { at last, restore instance pointer (SELF) }
  1264. if loadesi then
  1265. maybe_loadesi;
  1266. pp:=tbinarynode(params);
  1267. while assigned(pp) do
  1268. begin
  1269. if assigned(pp.left) then
  1270. begin
  1271. if (pp.left.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1272. ungetiftemp(pp.left.location.reference);
  1273. { process also all nodes of an array of const }
  1274. if pp.left.nodetype=arrayconstructorn then
  1275. begin
  1276. if assigned(tarrayconstructornode(pp.left).left) then
  1277. begin
  1278. hp:=pp.left;
  1279. while assigned(hp) do
  1280. begin
  1281. if (tarrayconstructornode(tunarynode(hp).left).location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1282. ungetiftemp(tarrayconstructornode(hp).left.location.reference);
  1283. hp:=tbinarynode(hp).right;
  1284. end;
  1285. end;
  1286. end;
  1287. end;
  1288. pp:=tbinarynode(pp.right);
  1289. end;
  1290. if inlined then
  1291. ungetpersistanttemp(inlinecode.retoffset);
  1292. if assigned(params) then
  1293. params.free;
  1294. { from now on the result can be freed normally }
  1295. if inlined and ret_in_param(resulttype) then
  1296. persistanttemptonormal(funcretref.offset);
  1297. { if return value is not used }
  1298. if (not(nf_return_value_used in flags)) and (resulttype<>pdef(voiddef)) then
  1299. begin
  1300. if location.loc in [LOC_MEM,LOC_REFERENCE] then
  1301. begin
  1302. { data which must be finalized ? }
  1303. if (resulttype^.needs_inittable) then
  1304. finalize(resulttype,location.reference,false);
  1305. { release unused temp }
  1306. ungetiftemp(location.reference)
  1307. end
  1308. else if location.loc=LOC_FPU then
  1309. begin
  1310. { release FPU stack }
  1311. emit_reg(A_FSTP,S_NO,R_ST0);
  1312. {
  1313. dec(fpuvaroffset);
  1314. do NOT decrement as the increment before
  1315. is not called for unused results PM }
  1316. end;
  1317. end;
  1318. end;
  1319. {*****************************************************************************
  1320. TI386PROCINLINENODE
  1321. *****************************************************************************}
  1322. procedure ti386procinlinenode.pass_2;
  1323. var st : psymtable;
  1324. oldprocsym : pprocsym;
  1325. ps, i : longint;
  1326. tmpreg: tregister;
  1327. oldprocinfo : pprocinfo;
  1328. oldinlining_procedure,
  1329. nostackframe,make_global : boolean;
  1330. inlineentrycode,inlineexitcode : TAAsmoutput;
  1331. oldexitlabel,oldexit2label,oldquickexitlabel:Pasmlabel;
  1332. oldunused,oldusableregs : tregisterset;
  1333. oldc_usableregs : longint;
  1334. oldreg_pushes : regvar_longintarray;
  1335. oldregvar_loaded,
  1336. oldis_reg_var : regvar_booleanarray;
  1337. {$ifdef TEMPREGDEBUG}
  1338. oldreg_user : regvar_ptreearray;
  1339. oldreg_releaser : regvar_ptreearray;
  1340. {$endif TEMPREGDEBUG}
  1341. {$ifdef GDB}
  1342. startlabel,endlabel : pasmlabel;
  1343. pp : pchar;
  1344. mangled_length : longint;
  1345. {$endif GDB}
  1346. begin
  1347. { deallocate the registers used for the current procedure's regvars }
  1348. if assigned(aktprocsym^.definition^.regvarinfo) then
  1349. begin
  1350. with pregvarinfo(aktprocsym^.definition^.regvarinfo)^ do
  1351. for i := 1 to maxvarregs do
  1352. if assigned(regvars[i]) then
  1353. store_regvar(exprasmlist,regvars[i]^.reg);
  1354. oldunused := unused;
  1355. oldusableregs := usableregs;
  1356. oldc_usableregs := c_usableregs;
  1357. oldreg_pushes := reg_pushes;
  1358. oldis_reg_var := is_reg_var;
  1359. oldregvar_loaded := regvar_loaded;
  1360. {$ifdef TEMPREGDEBUG}
  1361. oldreg_user := reg_user;
  1362. oldreg_releaser := reg_releaser;
  1363. {$endif TEMPREGDEBUG}
  1364. { make sure the register allocator knows what the regvars in the }
  1365. { inlined code block are (JM) }
  1366. resetusableregisters;
  1367. clearregistercount;
  1368. cleartempgen;
  1369. if assigned(inlineprocsym^.definition^.regvarinfo) then
  1370. with pregvarinfo(inlineprocsym^.definition^.regvarinfo)^ do
  1371. for i := 1 to maxvarregs do
  1372. if assigned(regvars[i]) then
  1373. begin
  1374. case regsize(regvars[i]^.reg) of
  1375. S_B: tmpreg := reg8toreg32(regvars[i]^.reg);
  1376. S_W: tmpreg := reg16toreg32(regvars[i]^.reg);
  1377. S_L: tmpreg := regvars[i]^.reg;
  1378. end;
  1379. usableregs:=usableregs-[tmpreg];
  1380. is_reg_var[tmpreg]:=true;
  1381. dec(c_usableregs);
  1382. end;
  1383. end;
  1384. oldinlining_procedure:=inlining_procedure;
  1385. oldexitlabel:=aktexitlabel;
  1386. oldexit2label:=aktexit2label;
  1387. oldquickexitlabel:=quickexitlabel;
  1388. getlabel(aktexitlabel);
  1389. getlabel(aktexit2label);
  1390. oldprocsym:=aktprocsym;
  1391. { we're inlining a procedure }
  1392. inlining_procedure:=true;
  1393. { save old procinfo }
  1394. getmem(oldprocinfo,sizeof(tprocinfo));
  1395. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1396. { set the return value }
  1397. aktprocsym:=inlineprocsym;
  1398. procinfo^.returntype:=aktprocsym^.definition^.rettype;
  1399. procinfo^.return_offset:=retoffset;
  1400. procinfo^.para_offset:=para_offset;
  1401. { arg space has been filled by the parent secondcall }
  1402. st:=aktprocsym^.definition^.localst;
  1403. { set it to the same lexical level }
  1404. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1405. if st^.datasize>0 then
  1406. begin
  1407. st^.address_fixup:=gettempofsizepersistant(st^.datasize)+st^.datasize;
  1408. {$ifdef extdebug}
  1409. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1410. exprasmList.concat(Tai_asm_comment.Create(strpnew(
  1411. 'local symtable is at offset '+tostr(st^.address_fixup))));
  1412. {$endif extdebug}
  1413. end;
  1414. exprasmList.concat(Tai_Marker.Create(InlineStart));
  1415. {$ifdef extdebug}
  1416. exprasmList.concat(Tai_asm_comment.Create(strpnew('Start of inlined proc')));
  1417. {$endif extdebug}
  1418. {$ifdef GDB}
  1419. if (cs_debuginfo in aktmoduleswitches) then
  1420. begin
  1421. getaddrlabel(startlabel);
  1422. getaddrlabel(endlabel);
  1423. emitlab(startlabel);
  1424. inlineprocsym^.definition^.localst^.symtabletype:=inlinelocalsymtable;
  1425. inlineprocsym^.definition^.parast^.symtabletype:=inlineparasymtable;
  1426. { Here we must include the para and local symtable info }
  1427. inlineprocsym^.concatstabto(withdebuglist);
  1428. { set it back for savety }
  1429. inlineprocsym^.definition^.localst^.symtabletype:=localsymtable;
  1430. inlineprocsym^.definition^.parast^.symtabletype:=parasymtable;
  1431. mangled_length:=length(oldprocsym^.definition^.mangledname);
  1432. getmem(pp,mangled_length+50);
  1433. strpcopy(pp,'192,0,0,'+startlabel^.name);
  1434. if (target_os.use_function_relative_addresses) then
  1435. begin
  1436. strpcopy(strend(pp),'-');
  1437. strpcopy(strend(pp),oldprocsym^.definition^.mangledname);
  1438. end;
  1439. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1440. end;
  1441. {$endif GDB}
  1442. { takes care of local data initialization }
  1443. inlineentrycode:=TAAsmoutput.Create;
  1444. inlineexitcode:=TAAsmoutput.Create;
  1445. ps:=para_size;
  1446. make_global:=false; { to avoid warning }
  1447. genentrycode(inlineentrycode,make_global,0,ps,nostackframe,true);
  1448. if po_assembler in aktprocsym^.definition^.procoptions then
  1449. inlineentrycode.insert(Tai_marker.Create(asmblockstart));
  1450. exprasmList.concatlist(inlineentrycode);
  1451. secondpass(inlinetree);
  1452. genexitcode(inlineexitcode,0,false,true);
  1453. if po_assembler in aktprocsym^.definition^.procoptions then
  1454. inlineexitcode.concat(Tai_marker.Create(asmblockend));
  1455. exprasmList.concatlist(inlineexitcode);
  1456. inlineentrycode.free;
  1457. inlineexitcode.free;
  1458. {$ifdef extdebug}
  1459. exprasmList.concat(Tai_asm_comment.Create(strpnew('End of inlined proc')));
  1460. {$endif extdebug}
  1461. exprasmList.concat(Tai_Marker.Create(InlineEnd));
  1462. {we can free the local data now, reset also the fixup address }
  1463. if st^.datasize>0 then
  1464. begin
  1465. ungetpersistanttemp(st^.address_fixup-st^.datasize);
  1466. st^.address_fixup:=0;
  1467. end;
  1468. { restore procinfo }
  1469. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1470. freemem(oldprocinfo,sizeof(tprocinfo));
  1471. {$ifdef GDB}
  1472. if (cs_debuginfo in aktmoduleswitches) then
  1473. begin
  1474. emitlab(endlabel);
  1475. strpcopy(pp,'224,0,0,'+endlabel^.name);
  1476. if (target_os.use_function_relative_addresses) then
  1477. begin
  1478. strpcopy(strend(pp),'-');
  1479. strpcopy(strend(pp),oldprocsym^.definition^.mangledname);
  1480. end;
  1481. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1482. freemem(pp,mangled_length+50);
  1483. end;
  1484. {$endif GDB}
  1485. { restore }
  1486. aktprocsym:=oldprocsym;
  1487. aktexitlabel:=oldexitlabel;
  1488. aktexit2label:=oldexit2label;
  1489. quickexitlabel:=oldquickexitlabel;
  1490. inlining_procedure:=oldinlining_procedure;
  1491. { reallocate the registers used for the current procedure's regvars, }
  1492. { since they may have been used and then deallocated in the inlined }
  1493. { procedure (JM) }
  1494. if assigned(aktprocsym^.definition^.regvarinfo) then
  1495. begin
  1496. unused := oldunused;
  1497. usableregs := oldusableregs;
  1498. c_usableregs := oldc_usableregs;
  1499. reg_pushes := oldreg_pushes;
  1500. is_reg_var := oldis_reg_var;
  1501. regvar_loaded := oldregvar_loaded;
  1502. {$ifdef TEMPREGDEBUG}
  1503. reg_user := oldreg_user;
  1504. reg_releaser := oldreg_releaser;
  1505. {$endif TEMPREGDEBUG}
  1506. end;
  1507. end;
  1508. begin
  1509. ccallparanode:=ti386callparanode;
  1510. ccallnode:=ti386callnode;
  1511. cprocinlinenode:=ti386procinlinenode;
  1512. end.
  1513. {
  1514. $Log$
  1515. Revision 1.18 2001-01-27 21:29:35 florian
  1516. * behavior -Oa optimized
  1517. Revision 1.17 2001/01/08 21:46:46 peter
  1518. * don't push high value for open array with cdecl;external;
  1519. Revision 1.16 2000/12/25 00:07:32 peter
  1520. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1521. tlinkedlist objects)
  1522. Revision 1.15 2000/12/09 10:45:40 florian
  1523. * AfterConstructor isn't called anymore when a constructor failed
  1524. Revision 1.14 2000/12/07 17:19:46 jonas
  1525. * new constant handling: from now on, hex constants >$7fffffff are
  1526. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1527. and became $ffffffff80000000), all constants in the longint range
  1528. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1529. are cardinals and the rest are int64's.
  1530. * added lots of longint typecast to prevent range check errors in the
  1531. compiler and rtl
  1532. * type casts of symbolic ordinal constants are now preserved
  1533. * fixed bug where the original resulttype wasn't restored correctly
  1534. after doing a 64bit rangecheck
  1535. Revision 1.13 2000/12/05 11:44:33 jonas
  1536. + new integer regvar handling, should be much more efficient
  1537. Revision 1.12 2000/12/03 22:26:54 florian
  1538. * fixed web buzg 1275: problem with int64 functions results
  1539. Revision 1.11 2000/11/29 00:30:46 florian
  1540. * unused units removed from uses clause
  1541. * some changes for widestrings
  1542. Revision 1.10 2000/11/23 13:26:34 jonas
  1543. * fix for webbug 1066/1126
  1544. Revision 1.9 2000/11/22 15:12:06 jonas
  1545. * fixed inline-related problems (partially "merges")
  1546. Revision 1.8 2000/11/17 09:54:58 florian
  1547. * INT_CHECK_OBJECT_* isn't applied to interfaces anymore
  1548. Revision 1.7 2000/11/12 23:24:14 florian
  1549. * interfaces are basically running
  1550. Revision 1.6 2000/11/07 23:40:49 florian
  1551. + AfterConstruction and BeforeDestruction impemented
  1552. Revision 1.5 2000/11/06 23:15:01 peter
  1553. * added copyvaluepara call again
  1554. Revision 1.4 2000/11/04 14:25:23 florian
  1555. + merged Attila's changes for interfaces, not tested yet
  1556. Revision 1.3 2000/11/04 13:12:14 jonas
  1557. * check for nil pointers before calling getcopy
  1558. Revision 1.2 2000/10/31 22:02:56 peter
  1559. * symtable splitted, no real code changes
  1560. Revision 1.1 2000/10/15 09:33:31 peter
  1561. * moved n386*.pas to i386/ cpu_target dir
  1562. Revision 1.2 2000/10/14 10:14:48 peter
  1563. * moehrendorf oct 2000 rewrite
  1564. Revision 1.1 2000/10/10 17:31:56 florian
  1565. * initial revision
  1566. }