n386cal.pas 71 KB

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