n386cal.pas 72 KB

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