n386cal.pas 70 KB

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