n386cal.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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. globtype,systems,
  39. cutils,verbose,globals,
  40. symconst,symbase,symsym,symtable,types,
  41. {$ifdef GDB}
  42. {$ifdef delphi}
  43. sysutils,
  44. {$else}
  45. strings,
  46. {$endif}
  47. gdb,
  48. {$endif GDB}
  49. cginfo,cgbase,pass_2,
  50. cpubase,aasmbase,aasmtai,aasmcpu,
  51. nmem,nld,ncnv,
  52. ncgutil,cga,cgobj,tgobj,regvars,rgobj,rgcpu,cg64f32,cgcpu;
  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. tmpreg : tregister;
  75. href : treference;
  76. begin
  77. { set default para_alignment to target_info.stackalignment }
  78. if para_alignment=0 then
  79. para_alignment:=aktalignment.paraalign;
  80. { push from left to right if specified }
  81. if push_from_left_to_right and assigned(right) then
  82. begin
  83. if (nf_varargs_para in flags) then
  84. tcallparanode(right).secondcallparan(defcoll,push_from_left_to_right,
  85. inlined,is_cdecl,para_alignment,para_offset)
  86. else
  87. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  88. inlined,is_cdecl,para_alignment,para_offset);
  89. end;
  90. otlabel:=truelabel;
  91. oflabel:=falselabel;
  92. getlabel(truelabel);
  93. getlabel(falselabel);
  94. secondpass(left);
  95. { handle varargs first, because defcoll is not valid }
  96. if (nf_varargs_para in flags) then
  97. begin
  98. if push_addr_param(left.resulttype.def) then
  99. begin
  100. inc(pushedparasize,4);
  101. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  102. location_release(exprasmlist,left.location);
  103. end
  104. else
  105. push_value_para(left,inlined,is_cdecl,para_offset,para_alignment);
  106. end
  107. { filter array constructor with c styled args }
  108. else if is_array_constructor(left.resulttype.def) and (nf_cargs in left.flags) then
  109. begin
  110. { nothing, everything is already pushed }
  111. end
  112. { in codegen.handleread.. defcoll.data is set to nil }
  113. else if assigned(defcoll.paratype.def) and
  114. (defcoll.paratype.def.deftype=formaldef) then
  115. begin
  116. { allow passing of a constant to a const formaldef }
  117. if (defcoll.paratyp=vs_const) and
  118. (left.location.loc=LOC_CONSTANT) then
  119. location_force_mem(exprasmlist,left.location);
  120. { allow @var }
  121. inc(pushedparasize,4);
  122. if (left.nodetype=addrn) and
  123. (not(nf_procvarload in left.flags)) then
  124. begin
  125. if inlined then
  126. begin
  127. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  128. cg.a_load_loc_ref(exprasmlist,left.location,href);
  129. end
  130. else
  131. cg.a_param_loc(exprasmlist,left.location,-1);
  132. location_release(exprasmlist,left.location);
  133. end
  134. else
  135. begin
  136. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  137. CGMessage(type_e_mismatch)
  138. else
  139. begin
  140. if inlined then
  141. begin
  142. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  143. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  144. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  145. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  146. cg.free_scratch_reg(exprasmlist,tmpreg);
  147. end
  148. else
  149. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  150. location_release(exprasmlist,left.location);
  151. end;
  152. end;
  153. end
  154. { handle call by reference parameter }
  155. else if (defcoll.paratyp in [vs_var,vs_out]) then
  156. begin
  157. if (left.location.loc<>LOC_REFERENCE) then
  158. begin
  159. { passing self to a var parameter is allowed in
  160. TP and delphi }
  161. if not((left.location.loc=LOC_CREFERENCE) and
  162. (left.nodetype=selfn)) then
  163. internalerror(200106041);
  164. end;
  165. maybe_push_high;
  166. if (defcoll.paratyp=vs_out) and
  167. assigned(defcoll.paratype.def) and
  168. not is_class(defcoll.paratype.def) and
  169. defcoll.paratype.def.needs_inittable then
  170. cg.g_finalize(exprasmlist,defcoll.paratype.def,left.location.reference,false);
  171. inc(pushedparasize,4);
  172. if inlined then
  173. begin
  174. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  175. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  176. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  177. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  178. cg.free_scratch_reg(exprasmlist,tmpreg);
  179. end
  180. else
  181. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  182. location_release(exprasmlist,left.location);
  183. end
  184. else
  185. begin
  186. tempdeftype:=resulttype.def.deftype;
  187. if tempdeftype=filedef then
  188. CGMessage(cg_e_file_must_call_by_reference);
  189. { open array must always push the address, this is needed to
  190. also push addr of small open arrays and with cdecl functions (PFV) }
  191. if (
  192. assigned(defcoll.paratype.def) and
  193. (is_open_array(defcoll.paratype.def) or
  194. is_array_of_const(defcoll.paratype.def))
  195. ) or
  196. (
  197. push_addr_param(resulttype.def) and
  198. not is_cdecl
  199. ) then
  200. begin
  201. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  202. begin
  203. { allow passing nil to a procvardef (methodpointer) }
  204. if (left.nodetype=typeconvn) and
  205. (left.resulttype.def.deftype=procvardef) and
  206. (ttypeconvnode(left).left.nodetype=niln) then
  207. begin
  208. tg.gettempofsizereference(exprasmlist,tcgsize2size[left.location.size],href);
  209. cg.a_load_loc_ref(exprasmlist,left.location,href);
  210. location_reset(left.location,LOC_REFERENCE,left.location.size);
  211. left.location.reference:=href;
  212. end
  213. else
  214. internalerror(200204011);
  215. end;
  216. maybe_push_high;
  217. inc(pushedparasize,4);
  218. if inlined then
  219. begin
  220. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  221. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  222. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  223. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  224. cg.free_scratch_reg(exprasmlist,tmpreg);
  225. end
  226. else
  227. cg.a_paramaddr_ref(exprasmlist,left.location.reference,-1);
  228. location_release(exprasmlist,left.location);
  229. end
  230. else
  231. begin
  232. push_value_para(left,inlined,is_cdecl,
  233. para_offset,para_alignment);
  234. end;
  235. end;
  236. truelabel:=otlabel;
  237. falselabel:=oflabel;
  238. { push from right to left }
  239. if not push_from_left_to_right and assigned(right) then
  240. begin
  241. if (nf_varargs_para in flags) then
  242. tcallparanode(right).secondcallparan(defcoll,push_from_left_to_right,
  243. inlined,is_cdecl,para_alignment,para_offset)
  244. else
  245. tcallparanode(right).secondcallparan(TParaItem(defcoll.next),push_from_left_to_right,
  246. inlined,is_cdecl,para_alignment,para_offset);
  247. end;
  248. end;
  249. {*****************************************************************************
  250. TI386CALLNODE
  251. *****************************************************************************}
  252. procedure ti386callnode.pass_2;
  253. var
  254. regs_to_push : tregisterset;
  255. unusedstate: pointer;
  256. pushed : tpushedsaved;
  257. funcretref,refcountedtemp : treference;
  258. tmpreg : tregister;
  259. hregister : tregister;
  260. oldpushedparasize : longint;
  261. { true if ESI must be loaded again after the subroutine }
  262. loadesi : boolean;
  263. { true if a virtual method must be called directly }
  264. no_virtual_call : boolean;
  265. { true if we produce a con- or destrutor in a call }
  266. is_con_or_destructor : boolean;
  267. { true if a constructor is called again }
  268. extended_new : boolean;
  269. { adress returned from an I/O-error }
  270. iolabel : tasmlabel;
  271. { lexlevel count }
  272. i : longint;
  273. { help reference pointer }
  274. href : treference;
  275. hrefvmt : treference;
  276. hp : tnode;
  277. pp : tbinarynode;
  278. params : tnode;
  279. inlined : boolean;
  280. inlinecode : tprocinlinenode;
  281. store_parast_fixup,
  282. para_alignment,
  283. para_offset : longint;
  284. cgsize : tcgsize;
  285. { instruction for alignement correction }
  286. { corr : paicpu;}
  287. { we must pop this size also after !! }
  288. { must_pop : boolean; }
  289. pop_size : longint;
  290. {$ifdef OPTALIGN}
  291. pop_esp : boolean;
  292. push_size : longint;
  293. {$endif OPTALIGN}
  294. pop_allowed : boolean;
  295. release_tmpreg : boolean;
  296. constructorfailed : tasmlabel;
  297. label
  298. dont_call;
  299. begin
  300. extended_new:=false;
  301. iolabel:=nil;
  302. inlinecode:=nil;
  303. inlined:=false;
  304. loadesi:=true;
  305. no_virtual_call:=false;
  306. rg.saveunusedstate(unusedstate);
  307. { if we allocate the temp. location for ansi- or widestrings }
  308. { already here, we avoid later a push/pop }
  309. if is_widestring(resulttype.def) then
  310. begin
  311. tg.gettempwidestringreference(exprasmlist,refcountedtemp);
  312. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp);
  313. end
  314. else if is_ansistring(resulttype.def) then
  315. begin
  316. tg.gettempansistringreference(exprasmlist,refcountedtemp);
  317. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp);
  318. end;
  319. if (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_stdcall]) then
  320. para_alignment:=4
  321. else
  322. para_alignment:=aktalignment.paraalign;
  323. if not assigned(procdefinition) then
  324. exit;
  325. { Deciding whether we may still need the parameters happens next (JM) }
  326. if assigned(left) then
  327. params:=left.getcopy
  328. else params := nil;
  329. if (procdefinition.proccalloption=pocall_inline) then
  330. begin
  331. inlined:=true;
  332. inlinecode:=tprocinlinenode(right);
  333. right:=nil;
  334. { set it to the same lexical level as the local symtable, becuase
  335. the para's are stored there }
  336. tprocdef(procdefinition).parast.symtablelevel:=aktprocdef.localst.symtablelevel;
  337. if assigned(params) then
  338. inlinecode.para_offset:=tg.gettempofsizepersistant(exprasmlist,inlinecode.para_size);
  339. store_parast_fixup:=tprocdef(procdefinition).parast.address_fixup;
  340. tprocdef(procdefinition).parast.address_fixup:=inlinecode.para_offset;
  341. {$ifdef extdebug}
  342. Comment(V_debug,
  343. 'inlined parasymtable is at offset '
  344. +tostr(tprocdef(procdefinition).parast.address_fixup));
  345. exprasmList.concat(Tai_asm_comment.Create(
  346. strpnew('inlined parasymtable is at offset '
  347. +tostr(tprocdef(procdefinition).parast.address_fixup))));
  348. {$endif extdebug}
  349. end;
  350. { only if no proc var }
  351. if inlined or
  352. not(assigned(right)) then
  353. is_con_or_destructor:=(procdefinition.proctypeoption in [potype_constructor,potype_destructor]);
  354. { proc variables destroy all registers }
  355. if (inlined or
  356. (right=nil)) and
  357. { virtual methods too }
  358. not(po_virtualmethod in procdefinition.procoptions) then
  359. begin
  360. if (cs_check_io in aktlocalswitches) and
  361. (po_iocheck in procdefinition.procoptions) and
  362. not(po_iocheck in aktprocdef.procoptions) then
  363. begin
  364. getaddrlabel(iolabel);
  365. cg.a_label(exprasmlist,iolabel);
  366. end
  367. else
  368. iolabel:=nil;
  369. { save all used registers }
  370. regs_to_push := tprocdef(procdefinition).usedregisters;
  371. rg.saveusedregisters(exprasmlist,pushed,regs_to_push);
  372. { give used registers through }
  373. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedregisters;
  374. end
  375. else
  376. begin
  377. regs_to_push := all_registers;
  378. rg.saveusedregisters(exprasmlist,pushed,regs_to_push);
  379. rg.usedinproc:=all_registers;
  380. { no IO check for methods and procedure variables }
  381. iolabel:=nil;
  382. end;
  383. { generate the code for the parameter and push them }
  384. oldpushedparasize:=pushedparasize;
  385. pushedparasize:=0;
  386. pop_size:=0;
  387. { no inc esp for inlined procedure
  388. and for objects constructors PM }
  389. if inlined or
  390. ((procdefinition.proctypeoption=potype_constructor) and
  391. { quick'n'dirty check if it is a class or an object }
  392. (resulttype.def.deftype=orddef)) then
  393. pop_allowed:=false
  394. else
  395. pop_allowed:=true;
  396. if pop_allowed then
  397. begin
  398. { Old pushedsize aligned on 4 ? }
  399. i:=oldpushedparasize and 3;
  400. if i>0 then
  401. inc(pop_size,4-i);
  402. { This parasize aligned on 4 ? }
  403. i:=procdefinition.para_size(para_alignment) and 3;
  404. if i>0 then
  405. inc(pop_size,4-i);
  406. { insert the opcode and update pushedparasize }
  407. { never push 4 or more !! }
  408. pop_size:=pop_size mod 4;
  409. if pop_size>0 then
  410. begin
  411. inc(pushedparasize,pop_size);
  412. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  413. {$ifdef GDB}
  414. if (cs_debuginfo in aktmoduleswitches) and
  415. (exprasmList.first=exprasmList.last) then
  416. exprasmList.concat(Tai_force_line.Create);
  417. {$endif GDB}
  418. end;
  419. end;
  420. {$ifdef OPTALIGN}
  421. if pop_allowed and (cs_align in aktglobalswitches) then
  422. begin
  423. pop_esp:=true;
  424. push_size:=procdefinition.para_size(para_alignment);
  425. { !!!! here we have to take care of return type, self
  426. and nested procedures
  427. }
  428. inc(push_size,12);
  429. emit_reg_reg(A_MOV,S_L,R_ESP,R_EDI);
  430. if (push_size mod 8)=0 then
  431. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP)
  432. else
  433. begin
  434. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  435. emit_const_reg(A_AND,S_L,$fffffff8,R_ESP);
  436. emit_const_reg(A_SUB,S_L,push_size,R_ESP);
  437. end;
  438. emit_reg(A_PUSH,S_L,R_EDI);
  439. end
  440. else
  441. pop_esp:=false;
  442. {$endif OPTALIGN}
  443. { Push parameters }
  444. if assigned(params) then
  445. begin
  446. { be found elsewhere }
  447. if inlined then
  448. para_offset:=tprocdef(procdefinition).parast.address_fixup+
  449. tprocdef(procdefinition).parast.datasize
  450. else
  451. para_offset:=0;
  452. if not(inlined) and
  453. assigned(right) then
  454. tcallparanode(params).secondcallparan(TParaItem(tabstractprocdef(right.resulttype.def).Para.first),
  455. (po_leftright in procdefinition.procoptions),inlined,
  456. (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl]),
  457. para_alignment,para_offset)
  458. else
  459. tcallparanode(params).secondcallparan(TParaItem(procdefinition.Para.first),
  460. (po_leftright in procdefinition.procoptions),inlined,
  461. (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl]),
  462. para_alignment,para_offset);
  463. end;
  464. { Allocate return value for inlined routines }
  465. if inlined then
  466. inlinecode.retoffset:=tg.gettempofsizepersistant(exprasmlist,Align(resulttype.def.size,aktalignment.paraalign));
  467. { Allocate return value when returned in argument }
  468. if ret_in_param(resulttype.def) then
  469. begin
  470. if assigned(funcretrefnode) then
  471. begin
  472. secondpass(funcretrefnode);
  473. if codegenerror then
  474. exit;
  475. if (funcretrefnode.location.loc<>LOC_REFERENCE) then
  476. internalerror(200204246);
  477. funcretref:=funcretrefnode.location.reference;
  478. end
  479. else
  480. begin
  481. if inlined then
  482. begin
  483. reference_reset(funcretref);
  484. funcretref.offset:=tg.gettempofsizepersistant(exprasmlist,resulttype.def.size);
  485. funcretref.base:=procinfo^.framepointer;
  486. {$ifdef extdebug}
  487. Comment(V_debug,'function return value is at offset '
  488. +tostr(funcretref.offset));
  489. exprasmlist.concat(tai_asm_comment.create(
  490. strpnew('function return value is at offset '
  491. +tostr(funcretref.offset))));
  492. {$endif extdebug}
  493. end
  494. else
  495. tg.gettempofsizereference(exprasmlist,resulttype.def.size,funcretref);
  496. end;
  497. { This must not be counted for C code
  498. complex return address is removed from stack
  499. by function itself ! }
  500. {$ifdef OLD_C_STACK}
  501. inc(pushedparasize,4); { lets try without it PM }
  502. {$endif not OLD_C_STACK}
  503. if inlined then
  504. begin
  505. hregister:=cg.get_scratch_reg_address(exprasmlist);
  506. cg.a_loadaddr_ref_reg(exprasmlist,funcretref,hregister);
  507. reference_reset_base(href,procinfo^.framepointer,inlinecode.retoffset);
  508. cg.a_load_reg_ref(exprasmlist,OS_ADDR,hregister,href);
  509. cg.free_scratch_reg(exprasmlist,hregister);
  510. end
  511. else
  512. cg.a_paramaddr_ref(exprasmlist,funcretref,-1);
  513. end;
  514. { procedure variable or normal function call ? }
  515. if inlined or
  516. (right=nil) then
  517. begin
  518. { Normal function call }
  519. { overloaded operator has no symtable }
  520. { push self }
  521. if assigned(symtableproc) and
  522. (symtableproc.symtabletype=withsymtable) then
  523. begin
  524. { dirty trick to avoid the secondcall below }
  525. methodpointer:=ccallparanode.create(nil,nil);
  526. location_reset(methodpointer.location,LOC_REGISTER,OS_ADDR);
  527. rg.getexplicitregisterint(exprasmlist,R_ESI);
  528. methodpointer.location.register:=R_ESI;
  529. { ARGHHH this is wrong !!!
  530. if we can init from base class for a child
  531. class that the wrong VMT will be
  532. transfered to constructor !! }
  533. methodpointer.resulttype:=
  534. twithnode(twithsymtable(symtableproc).withnode).left.resulttype;
  535. { make a reference }
  536. href:=twithnode(twithsymtable(symtableproc).withnode).withreference;
  537. if ((not(nf_islocal in twithnode(twithsymtable(symtableproc).withnode).flags)) and
  538. (not twithsymtable(symtableproc).direct_with)) or
  539. is_class_or_interface(methodpointer.resulttype.def) then
  540. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg)
  541. else
  542. cg.a_loadaddr_ref_reg(exprasmlist,href,self_pointer_reg);
  543. end;
  544. { push self }
  545. if assigned(symtableproc) and
  546. ((symtableproc.symtabletype=objectsymtable) or
  547. (symtableproc.symtabletype=withsymtable)) then
  548. begin
  549. if assigned(methodpointer) then
  550. begin
  551. {
  552. if methodpointer^.resulttype.def=classrefdef then
  553. begin
  554. two possibilities:
  555. 1. constructor
  556. 2. class method
  557. end
  558. else }
  559. begin
  560. case methodpointer.nodetype of
  561. typen:
  562. begin
  563. { direct call to inherited method }
  564. if (po_abstractmethod in procdefinition.procoptions) then
  565. begin
  566. CGMessage(cg_e_cant_call_abstract_method);
  567. goto dont_call;
  568. end;
  569. { generate no virtual call }
  570. no_virtual_call:=true;
  571. if (sp_static in symtableprocentry.symoptions) then
  572. begin
  573. { well lets put the VMT address directly into ESI }
  574. { it is kind of dirty but that is the simplest }
  575. { way to accept virtual static functions (PM) }
  576. loadesi:=true;
  577. { if no VMT just use $0 bug0214 PM }
  578. rg.getexplicitregisterint(exprasmlist,R_ESI);
  579. if not(oo_has_vmt in tobjectdef(methodpointer.resulttype.def).objectoptions) then
  580. cg.a_load_const_reg(exprasmlist,OS_ADDR,0,self_pointer_reg)
  581. else
  582. begin
  583. reference_reset_symbol(href,newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  584. cg.a_loadaddr_ref_reg(exprasmlist,href,self_pointer_reg);
  585. end;
  586. { emit_reg(A_PUSH,S_L,R_ESI);
  587. this is done below !! }
  588. end
  589. else
  590. { this is a member call, so ESI isn't modfied }
  591. loadesi:=false;
  592. { a class destructor needs a flag }
  593. if is_class(tobjectdef(methodpointer.resulttype.def)) and
  594. (procdefinition.proctypeoption=potype_destructor) then
  595. begin
  596. cg.a_param_const(exprasmlist,OS_ADDR,0,2);
  597. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  598. end;
  599. if not(is_con_or_destructor and
  600. is_class(methodpointer.resulttype.def) and
  601. (procdefinition.proctypeoption in [potype_constructor,potype_destructor])
  602. ) then
  603. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  604. { if an inherited con- or destructor should be }
  605. { called in a con- or destructor then a warning }
  606. { will be made }
  607. { con- and destructors need a pointer to the vmt }
  608. if is_con_or_destructor and
  609. is_object(methodpointer.resulttype.def) and
  610. assigned(aktprocdef) then
  611. begin
  612. if not(aktprocdef.proctypeoption in
  613. [potype_constructor,potype_destructor]) then
  614. CGMessage(cg_w_member_cd_call_from_method);
  615. end;
  616. { class destructors get there flag above }
  617. { constructor flags ? }
  618. if is_con_or_destructor and
  619. not(
  620. is_class(methodpointer.resulttype.def) and
  621. assigned(aktprocdef) and
  622. (aktprocdef.proctypeoption=potype_destructor)) then
  623. begin
  624. { a constructor needs also a flag }
  625. if is_class(methodpointer.resulttype.def) then
  626. cg.a_param_const(exprasmlist,OS_ADDR,0,2);
  627. cg.a_param_const(exprasmlist,OS_ADDR,0,1);
  628. end;
  629. end;
  630. hnewn:
  631. begin
  632. { extended syntax of new }
  633. { ESI must be zero }
  634. rg.getexplicitregisterint(exprasmlist,R_ESI);
  635. cg.a_load_const_reg(exprasmlist,OS_ADDR,0,self_pointer_reg);
  636. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,2);
  637. { insert the vmt }
  638. reference_reset_symbol(href,newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  639. cg.a_paramaddr_ref(exprasmlist,href,1);
  640. extended_new:=true;
  641. end;
  642. hdisposen:
  643. begin
  644. secondpass(methodpointer);
  645. { destructor with extended syntax called from dispose }
  646. { hdisposen always deliver LOC_REFERENCE }
  647. rg.getexplicitregisterint(exprasmlist,R_ESI);
  648. emit_ref_reg(A_LEA,S_L,methodpointer.location.reference,R_ESI);
  649. reference_release(exprasmlist,methodpointer.location.reference);
  650. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,2);
  651. reference_reset_symbol(href,newasmsymbol(tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  652. cg.a_paramaddr_ref(exprasmlist,href,1);
  653. end;
  654. else
  655. begin
  656. { call to an instance member }
  657. if (symtableproc.symtabletype<>withsymtable) then
  658. begin
  659. secondpass(methodpointer);
  660. rg.getexplicitregisterint(exprasmlist,R_ESI);
  661. case methodpointer.location.loc of
  662. LOC_CREGISTER,
  663. LOC_REGISTER:
  664. begin
  665. cg.a_load_reg_reg(exprasmlist,OS_ADDR,methodpointer.location.register,R_ESI);
  666. rg.ungetregisterint(exprasmlist,methodpointer.location.register);
  667. end;
  668. else
  669. begin
  670. if (methodpointer.resulttype.def.deftype=classrefdef) or
  671. is_class_or_interface(methodpointer.resulttype.def) then
  672. cg.a_load_ref_reg(exprasmlist,OS_ADDR,methodpointer.location.reference,R_ESI)
  673. else
  674. cg.a_loadaddr_ref_reg(exprasmlist,methodpointer.location.reference,R_ESI);
  675. reference_release(exprasmlist,methodpointer.location.reference);
  676. end;
  677. end;
  678. end;
  679. { when calling a class method, we have to load ESI with the VMT !
  680. But, not for a class method via self }
  681. if not(po_containsself in procdefinition.procoptions) then
  682. begin
  683. if (po_classmethod in procdefinition.procoptions) and
  684. not(methodpointer.resulttype.def.deftype=classrefdef) then
  685. begin
  686. { class method needs current VMT }
  687. rg.getexplicitregisterint(exprasmlist,R_ESI);
  688. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  689. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg);
  690. end;
  691. { direct call to destructor: remove data }
  692. if (procdefinition.proctypeoption=potype_destructor) and
  693. is_class(methodpointer.resulttype.def) then
  694. cg.a_param_const(exprasmlist,OS_INT,1,1);
  695. { direct call to class constructor, don't allocate memory }
  696. if (procdefinition.proctypeoption=potype_constructor) and
  697. is_class(methodpointer.resulttype.def) then
  698. begin
  699. cg.a_param_const(exprasmlist,OS_INT,0,2);
  700. cg.a_param_const(exprasmlist,OS_INT,0,1);
  701. end
  702. else
  703. begin
  704. { constructor call via classreference => allocate memory }
  705. if (procdefinition.proctypeoption=potype_constructor) and
  706. (methodpointer.resulttype.def.deftype=classrefdef) and
  707. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  708. cg.a_param_const(exprasmlist,OS_INT,1,1);
  709. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  710. end;
  711. end;
  712. if is_con_or_destructor then
  713. begin
  714. { classes don't get a VMT pointer pushed }
  715. if is_object(methodpointer.resulttype.def) then
  716. begin
  717. if (procdefinition.proctypeoption=potype_constructor) then
  718. begin
  719. { it's no bad idea, to insert the VMT }
  720. reference_reset_symbol(href,newasmsymbol(
  721. tobjectdef(methodpointer.resulttype.def).vmt_mangledname),0);
  722. cg.a_paramaddr_ref(exprasmlist,href,1);
  723. end
  724. { destructors haven't to dispose the instance, if this is }
  725. { a direct call }
  726. else
  727. cg.a_param_const(exprasmlist,OS_INT,0,1);
  728. end;
  729. end;
  730. end;
  731. end;
  732. end;
  733. end
  734. else
  735. begin
  736. if (po_classmethod in procdefinition.procoptions) and
  737. not(
  738. assigned(aktprocdef) and
  739. (po_classmethod in aktprocdef.procoptions)
  740. ) then
  741. begin
  742. { class method needs current VMT }
  743. rg.getexplicitregisterint(exprasmlist,R_ESI);
  744. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  745. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,R_ESI);
  746. end
  747. else
  748. begin
  749. { member call, ESI isn't modified }
  750. loadesi:=false;
  751. end;
  752. { direct call to destructor: don't remove data! }
  753. if is_class(procinfo^._class) then
  754. begin
  755. if (procdefinition.proctypeoption=potype_destructor) then
  756. begin
  757. cg.a_param_const(exprasmlist,OS_INT,0,2);
  758. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,1);
  759. end
  760. else if (procdefinition.proctypeoption=potype_constructor) then
  761. begin
  762. cg.a_param_const(exprasmlist,OS_INT,0,2);
  763. cg.a_param_const(exprasmlist,OS_INT,0,1);
  764. end
  765. else
  766. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,1);
  767. end
  768. else if is_object(procinfo^._class) then
  769. begin
  770. cg.a_param_reg(exprasmlist,OS_ADDR,R_ESI,1);
  771. if is_con_or_destructor then
  772. begin
  773. if (procdefinition.proctypeoption=potype_constructor) then
  774. begin
  775. { it's no bad idea, to insert the VMT }
  776. reference_reset_symbol(href,newasmsymbol(procinfo^._class.vmt_mangledname),0);
  777. cg.a_paramaddr_ref(exprasmlist,href,1);
  778. end
  779. { destructors haven't to dispose the instance, if this is }
  780. { a direct call }
  781. else
  782. cg.a_param_const(exprasmlist,OS_INT,0,1);
  783. end;
  784. end
  785. else
  786. Internalerror(200006165);
  787. end;
  788. end;
  789. { call to BeforeDestruction? }
  790. if (procdefinition.proctypeoption=potype_destructor) and
  791. assigned(methodpointer) and
  792. (methodpointer.nodetype<>typen) and
  793. is_class(tobjectdef(methodpointer.resulttype.def)) and
  794. (inlined or
  795. (right=nil)) then
  796. begin
  797. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  798. reference_reset_base(href,self_pointer_reg,0);
  799. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  800. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  801. reference_reset_base(href,tmpreg,72);
  802. cg.a_call_ref(exprasmlist,href);
  803. cg.free_scratch_reg(exprasmlist,tmpreg);
  804. end;
  805. { push base pointer ?}
  806. { never when inlining, since if necessary, the base pointer }
  807. { can/will be gottten from the current procedure's symtable }
  808. { (JM) }
  809. if not inlined then
  810. if (lexlevel>=normal_function_level) and assigned(tprocdef(procdefinition).parast) and
  811. ((tprocdef(procdefinition).parast.symtablelevel)>normal_function_level) then
  812. begin
  813. { if we call a nested function in a method, we must }
  814. { push also SELF! }
  815. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  816. { access }
  817. {
  818. begin
  819. loadesi:=false;
  820. emit_reg(A_PUSH,S_L,R_ESI);
  821. end;
  822. }
  823. if lexlevel=(tprocdef(procdefinition).parast.symtablelevel) then
  824. begin
  825. reference_reset_base(href,procinfo^.framepointer,procinfo^.framepointer_offset);
  826. cg.a_param_ref(exprasmlist,OS_ADDR,href,-1);
  827. end
  828. { this is only true if the difference is one !!
  829. but it cannot be more !! }
  830. else if (lexlevel=(tprocdef(procdefinition).parast.symtablelevel)-1) then
  831. begin
  832. cg.a_param_reg(exprasmlist,OS_ADDR,procinfo^.framepointer,-1);
  833. end
  834. else if (lexlevel>(tprocdef(procdefinition).parast.symtablelevel)) then
  835. begin
  836. hregister:=rg.getregisterint(exprasmlist);
  837. reference_reset_base(href,procinfo^.framepointer,procinfo^.framepointer_offset);
  838. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  839. for i:=(tprocdef(procdefinition).parast.symtablelevel) to lexlevel-1 do
  840. begin
  841. {we should get the correct frame_pointer_offset at each level
  842. how can we do this !!! }
  843. reference_reset_base(href,hregister,procinfo^.framepointer_offset);
  844. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  845. end;
  846. cg.a_param_reg(exprasmlist,OS_ADDR,hregister,-1);
  847. rg.ungetregisterint(exprasmlist,hregister);
  848. end
  849. else
  850. internalerror(25000);
  851. end;
  852. rg.saveregvars(exprasmlist,regs_to_push);
  853. if (po_virtualmethod in procdefinition.procoptions) and
  854. not(no_virtual_call) then
  855. begin
  856. { static functions contain the vmt_address in ESI }
  857. { also class methods }
  858. { Here it is quite tricky because it also depends }
  859. { on the methodpointer PM }
  860. release_tmpreg:=false;
  861. rg.getexplicitregisterint(exprasmlist,R_ESI);
  862. if assigned(aktprocdef) then
  863. begin
  864. if (((sp_static in aktprocdef.procsym.symoptions) or
  865. (po_classmethod in aktprocdef.procoptions)) and
  866. ((methodpointer=nil) or (methodpointer.nodetype=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.def.deftype=classrefdef)
  873. ) or
  874. { is_interface(tprocdef(procdefinition)._class) or }
  875. { ESI is loaded earlier }
  876. (po_classmethod in procdefinition.procoptions) then
  877. begin
  878. reference_reset_base(href,R_ESI,0);
  879. end
  880. else
  881. begin
  882. { this is one point where we need vmt_offset (PM) }
  883. reference_reset_base(href,R_ESI,tprocdef(procdefinition)._class.vmt_offset);
  884. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  885. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  886. reference_reset_base(href,tmpreg,0);
  887. release_tmpreg:=true;
  888. end;
  889. end
  890. else
  891. { aktprocdef should be assigned, also in main program }
  892. internalerror(12345);
  893. if tprocdef(procdefinition).extnumber=-1 then
  894. internalerror(44584);
  895. href.offset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  896. if not(is_interface(tprocdef(procdefinition)._class)) and
  897. not(is_cppclass(tprocdef(procdefinition)._class)) then
  898. begin
  899. if (cs_check_object in aktlocalswitches) then
  900. begin
  901. reference_reset_symbol(hrefvmt,newasmsymbol(tprocdef(procdefinition)._class.vmt_mangledname),0);
  902. cg.a_paramaddr_ref(exprasmlist,hrefvmt,2);
  903. cg.a_param_reg(exprasmlist,OS_ADDR,href.base,1);
  904. cg.a_call_name(exprasmlist,'FPC_CHECK_OBJECT_EXT');
  905. end
  906. else if (cs_check_range in aktlocalswitches) then
  907. begin
  908. cg.a_param_reg(exprasmlist,OS_ADDR,href.base,1);
  909. cg.a_call_name(exprasmlist,'FPC_CHECK_OBJECT');
  910. end;
  911. end;
  912. cg.a_call_ref(exprasmlist,href);
  913. if release_tmpreg then
  914. cg.free_scratch_reg(exprasmlist,tmpreg);
  915. end
  916. else if not inlined then
  917. begin
  918. { We can call interrupts from within the smae code
  919. by just pushing the flags and CS PM }
  920. if (po_interrupt in procdefinition.procoptions) then
  921. begin
  922. emit_none(A_PUSHF,S_L);
  923. emit_reg(A_PUSH,S_L,R_CS);
  924. end;
  925. cg.a_call_name(exprasmlist,tprocdef(procdefinition).mangledname);
  926. end
  927. else { inlined proc }
  928. { inlined code is in inlinecode }
  929. begin
  930. { process the inlinecode }
  931. secondpass(inlinecode);
  932. { free the args }
  933. if tprocdef(procdefinition).parast.datasize>0 then
  934. tg.ungetpersistanttemp(exprasmlist,tprocdef(procdefinition).parast.address_fixup);
  935. end;
  936. end
  937. else
  938. { now procedure variable case }
  939. begin
  940. secondpass(right);
  941. if (po_interrupt in procdefinition.procoptions) then
  942. begin
  943. emit_none(A_PUSHF,S_L);
  944. emit_reg(A_PUSH,S_L,R_CS);
  945. end;
  946. { procedure of object? }
  947. if (po_methodpointer in procdefinition.procoptions) then
  948. begin
  949. { method pointer can't be in a register }
  950. hregister:=R_NO;
  951. { do some hacking if we call a method pointer }
  952. { which is a class member }
  953. { else ESI is overwritten ! }
  954. if (right.location.reference.base=R_ESI) or
  955. (right.location.reference.index=R_ESI) then
  956. begin
  957. reference_release(exprasmlist,right.location.reference);
  958. hregister:=cg.get_scratch_reg_address(exprasmlist);
  959. cg.a_load_ref_reg(exprasmlist,OS_ADDR,right.location.reference,hregister);
  960. end;
  961. { load self, but not if it's already explicitly pushed }
  962. if not(po_containsself in procdefinition.procoptions) then
  963. begin
  964. { load ESI }
  965. href:=right.location.reference;
  966. inc(href.offset,4);
  967. rg.getexplicitregisterint(exprasmlist,R_ESI);
  968. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,self_pointer_reg);
  969. { push self pointer }
  970. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,-1);
  971. end;
  972. rg.saveregvars(exprasmlist,ALL_REGISTERS);
  973. if hregister<>R_NO then
  974. reference_reset_base(href,hregister,0)
  975. else
  976. href:=right.location.reference;
  977. cg.a_call_ref(exprasmlist,href);
  978. if hregister<>R_NO then
  979. cg.free_scratch_reg(exprasmlist,hregister);
  980. reference_release(exprasmlist,right.location.reference);
  981. end
  982. else
  983. begin
  984. rg.saveregvars(exprasmlist,ALL_REGISTERS);
  985. case right.location.loc of
  986. LOC_REGISTER,LOC_CREGISTER:
  987. reference_reset_base(href,right.location.register,0);
  988. LOC_REFERENCE,LOC_CREFERENCE :
  989. href:=right.location.reference;
  990. else
  991. internalerror(200203311);
  992. end;
  993. cg.a_call_ref(exprasmlist,href);
  994. location_release(exprasmlist,right.location);
  995. end;
  996. end;
  997. { this was only for normal functions
  998. displaced here so we also get
  999. it to work for procvars PM }
  1000. if (not inlined) and (po_clearstack in procdefinition.procoptions) then
  1001. begin
  1002. { we also add the pop_size which is included in pushedparasize }
  1003. pop_size:=0;
  1004. { better than an add on all processors }
  1005. if pushedparasize=4 then
  1006. begin
  1007. rg.getexplicitregisterint(exprasmlist,R_EDI);
  1008. emit_reg(A_POP,S_L,R_EDI);
  1009. rg.ungetregisterint(exprasmlist,R_EDI);
  1010. end
  1011. { the pentium has two pipes and pop reg is pairable }
  1012. { but the registers must be different! }
  1013. else if (pushedparasize=8) and
  1014. not(cs_littlesize in aktglobalswitches) and
  1015. (aktoptprocessor=ClassP5) and
  1016. (procinfo^._class=nil) then
  1017. begin
  1018. rg.getexplicitregisterint(exprasmlist,R_EDI);
  1019. emit_reg(A_POP,S_L,R_EDI);
  1020. rg.ungetregisterint(exprasmlist,R_EDI);
  1021. exprasmList.concat(tai_regalloc.Alloc(R_ESI));
  1022. emit_reg(A_POP,S_L,R_ESI);
  1023. exprasmList.concat(tai_regalloc.DeAlloc(R_ESI));
  1024. end
  1025. else if pushedparasize<>0 then
  1026. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  1027. end;
  1028. {$ifdef OPTALIGN}
  1029. if pop_esp then
  1030. emit_reg(A_POP,S_L,R_ESP);
  1031. {$endif OPTALIGN}
  1032. dont_call:
  1033. pushedparasize:=oldpushedparasize;
  1034. rg.restoreunusedstate(unusedstate);
  1035. {$ifdef TEMPREGDEBUG}
  1036. testregisters32;
  1037. {$endif TEMPREGDEBUG}
  1038. { a constructor could be a function with boolean result }
  1039. { if calling constructor called fail we
  1040. must jump directly to quickexitlabel PM
  1041. but only if it is a call of an inherited constructor }
  1042. if (inlined or
  1043. (right=nil)) and
  1044. (procdefinition.proctypeoption=potype_constructor) and
  1045. assigned(methodpointer) and
  1046. (methodpointer.nodetype=typen) and
  1047. (aktprocdef.proctypeoption=potype_constructor) then
  1048. begin
  1049. emitjmp(C_Z,faillabel);
  1050. end;
  1051. { call to AfterConstruction? }
  1052. if is_class(resulttype.def) and
  1053. (inlined or
  1054. (right=nil)) and
  1055. (procdefinition.proctypeoption=potype_constructor) and
  1056. assigned(methodpointer) and
  1057. (methodpointer.nodetype<>typen) then
  1058. begin
  1059. getlabel(constructorfailed);
  1060. emitjmp(C_Z,constructorfailed);
  1061. cg.a_param_reg(exprasmlist,OS_ADDR,self_pointer_reg,1);
  1062. reference_reset_base(href,self_pointer_reg,0);
  1063. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  1064. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,tmpreg);
  1065. reference_reset_base(href,tmpreg,68);
  1066. cg.a_call_ref(exprasmlist,href);
  1067. cg.free_scratch_reg(exprasmlist,tmpreg);
  1068. exprasmList.concat(tai_regalloc.Alloc(accumulator));
  1069. cg.a_label(exprasmlist,constructorfailed);
  1070. cg.a_load_reg_reg(exprasmlist,OS_ADDR,self_pointer_reg,accumulator);
  1071. end;
  1072. { handle function results }
  1073. if (not is_void(resulttype.def)) then
  1074. begin
  1075. { structured results are easy to handle.... }
  1076. { needed also when result_no_used !! }
  1077. if ret_in_param(resulttype.def) then
  1078. begin
  1079. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  1080. location.reference.symbol:=nil;
  1081. location.reference:=funcretref;
  1082. end
  1083. else
  1084. { ansi/widestrings must be registered, so we can dispose them }
  1085. if is_ansistring(resulttype.def) or
  1086. is_widestring(resulttype.def) then
  1087. begin
  1088. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  1089. location.reference:=refcountedtemp;
  1090. cg.a_reg_alloc(exprasmlist,accumulator);
  1091. cg.a_load_reg_ref(exprasmlist,OS_ADDR,accumulator,location.reference);
  1092. cg.a_reg_dealloc(exprasmlist,accumulator);
  1093. end
  1094. else
  1095. { we have only to handle the result if it is used }
  1096. if (nf_return_value_used in flags) then
  1097. begin
  1098. case resulttype.def.deftype of
  1099. enumdef,
  1100. orddef :
  1101. begin
  1102. cgsize:=def_cgsize(resulttype.def);
  1103. { an object constructor is a function with boolean result }
  1104. if (inlined or (right=nil)) and
  1105. (procdefinition.proctypeoption=potype_constructor) then
  1106. begin
  1107. if extended_new then
  1108. cgsize:=OS_INT
  1109. else
  1110. begin
  1111. cgsize:=OS_NO;
  1112. { this fails if popsize > 0 PM }
  1113. location_reset(location,LOC_FLAGS,OS_NO);
  1114. location.resflags:=F_NE;
  1115. end;
  1116. end;
  1117. if cgsize<>OS_NO then
  1118. begin
  1119. location_reset(location,LOC_REGISTER,cgsize);
  1120. cg.a_reg_alloc(exprasmlist,accumulator);
  1121. if cgsize in [OS_64,OS_S64] then
  1122. begin
  1123. cg.a_reg_alloc(exprasmlist,accumulatorhigh);
  1124. if accumulatorhigh in rg.unusedregsint then
  1125. begin
  1126. location.registerhigh:=rg.getexplicitregisterint(exprasmlist,accumulatorhigh);
  1127. location.registerlow:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1128. end
  1129. else
  1130. begin
  1131. location.registerhigh:=rg.getexplicitregisterint(exprasmlist,accumulatorhigh);
  1132. location.registerlow:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1133. end;
  1134. cg64.a_load64_reg_reg(exprasmlist,joinreg64(accumulator,accumulatorhigh),
  1135. location.register64);
  1136. end
  1137. else
  1138. begin
  1139. location.register:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1140. hregister:=rg.makeregsize(accumulator,cgsize);
  1141. location.register:=rg.makeregsize(location.register,cgsize);
  1142. cg.a_load_reg_reg(exprasmlist,cgsize,hregister,location.register);
  1143. end;
  1144. end;
  1145. end;
  1146. floatdef :
  1147. begin
  1148. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  1149. location.register:=R_ST;
  1150. inc(trgcpu(rg).fpuvaroffset);
  1151. end;
  1152. else
  1153. begin
  1154. location_reset(location,LOC_REGISTER,OS_INT);
  1155. location.register:=rg.getexplicitregisterint(exprasmlist,accumulator);
  1156. cg.a_load_reg_reg(exprasmlist,OS_INT,accumulator,location.register);
  1157. end;
  1158. end;
  1159. end;
  1160. end;
  1161. { perhaps i/o check ? }
  1162. if iolabel<>nil then
  1163. begin
  1164. reference_reset_symbol(href,iolabel,0);
  1165. cg.a_paramaddr_ref(exprasmlist,href,1);
  1166. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  1167. end;
  1168. if pop_size>0 then
  1169. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1170. { restore registers }
  1171. rg.restoreusedregisters(exprasmlist,pushed);
  1172. { at last, restore instance pointer (SELF) }
  1173. if loadesi then
  1174. cg.g_maybe_loadself(exprasmlist);
  1175. pp:=tbinarynode(params);
  1176. while assigned(pp) do
  1177. begin
  1178. if assigned(pp.left) then
  1179. begin
  1180. location_freetemp(exprasmlist,pp.left.location);
  1181. { process also all nodes of an array of const }
  1182. if pp.left.nodetype=arrayconstructorn then
  1183. begin
  1184. if assigned(tarrayconstructornode(pp.left).left) then
  1185. begin
  1186. hp:=pp.left;
  1187. while assigned(hp) do
  1188. begin
  1189. location_freetemp(exprasmlist,tarrayconstructornode(hp).left.location);
  1190. hp:=tarrayconstructornode(hp).right;
  1191. end;
  1192. end;
  1193. end;
  1194. end;
  1195. pp:=tbinarynode(pp.right);
  1196. end;
  1197. if inlined then
  1198. begin
  1199. tg.ungetpersistanttemp(exprasmlist,inlinecode.retoffset);
  1200. tprocdef(procdefinition).parast.address_fixup:=store_parast_fixup;
  1201. right:=inlinecode;
  1202. end;
  1203. if assigned(params) then
  1204. params.free;
  1205. { from now on the result can be freed normally }
  1206. if inlined and ret_in_param(resulttype.def) then
  1207. tg.persistanttemptonormal(funcretref.offset);
  1208. { if return value is not used }
  1209. if (not(nf_return_value_used in flags)) and (not is_void(resulttype.def)) then
  1210. begin
  1211. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  1212. begin
  1213. { data which must be finalized ? }
  1214. if (resulttype.def.needs_inittable) then
  1215. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  1216. { release unused temp }
  1217. tg.ungetiftemp(exprasmlist,location.reference)
  1218. end
  1219. else if location.loc=LOC_FPUREGISTER then
  1220. begin
  1221. { release FPU stack }
  1222. emit_reg(A_FSTP,S_NO,R_ST);
  1223. {
  1224. dec(trgcpu(rg).fpuvaroffset);
  1225. do NOT decrement as the increment before
  1226. is not called for unused results PM }
  1227. end;
  1228. end;
  1229. end;
  1230. {*****************************************************************************
  1231. TI386PROCINLINENODE
  1232. *****************************************************************************}
  1233. procedure ti386procinlinenode.pass_2;
  1234. var st : tsymtable;
  1235. oldprocdef : tprocdef;
  1236. ps, i : longint;
  1237. tmpreg: tregister;
  1238. oldprocinfo : pprocinfo;
  1239. oldinlining_procedure,
  1240. nostackframe,make_global : boolean;
  1241. inlineentrycode,inlineexitcode : TAAsmoutput;
  1242. oldexitlabel,oldexit2label,oldquickexitlabel:tasmlabel;
  1243. oldregstate: pointer;
  1244. {$ifdef GDB}
  1245. startlabel,endlabel : tasmlabel;
  1246. pp : pchar;
  1247. mangled_length : longint;
  1248. {$endif GDB}
  1249. begin
  1250. { deallocate the registers used for the current procedure's regvars }
  1251. if assigned(aktprocdef.regvarinfo) then
  1252. begin
  1253. with pregvarinfo(aktprocdef.regvarinfo)^ do
  1254. for i := 1 to maxvarregs do
  1255. if assigned(regvars[i]) then
  1256. store_regvar(exprasmlist,regvars[i].reg);
  1257. rg.saveStateForInline(oldregstate);
  1258. { make sure the register allocator knows what the regvars in the }
  1259. { inlined code block are (JM) }
  1260. rg.resetusableregisters;
  1261. rg.clearregistercount;
  1262. rg.cleartempgen;
  1263. if assigned(inlineprocdef.regvarinfo) then
  1264. with pregvarinfo(inlineprocdef.regvarinfo)^ do
  1265. for i := 1 to maxvarregs do
  1266. if assigned(regvars[i]) then
  1267. begin
  1268. tmpreg:=rg.makeregsize(regvars[i].reg,OS_INT);
  1269. rg.makeregvar(tmpreg);
  1270. end;
  1271. end;
  1272. oldinlining_procedure:=inlining_procedure;
  1273. oldexitlabel:=aktexitlabel;
  1274. oldexit2label:=aktexit2label;
  1275. oldquickexitlabel:=quickexitlabel;
  1276. getlabel(aktexitlabel);
  1277. getlabel(aktexit2label);
  1278. { we're inlining a procedure }
  1279. inlining_procedure:=true;
  1280. { save old procinfo }
  1281. oldprocdef:=aktprocdef;
  1282. getmem(oldprocinfo,sizeof(tprocinfo));
  1283. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1284. { set new procinfo }
  1285. aktprocdef:=inlineprocdef;
  1286. procinfo^.return_offset:=retoffset;
  1287. procinfo^.para_offset:=para_offset;
  1288. procinfo^.no_fast_exit:=false;
  1289. { arg space has been filled by the parent secondcall }
  1290. st:=aktprocdef.localst;
  1291. { set it to the same lexical level }
  1292. st.symtablelevel:=oldprocdef.localst.symtablelevel;
  1293. if st.datasize>0 then
  1294. begin
  1295. st.address_fixup:=tg.gettempofsizepersistant(exprasmlist,st.datasize)+st.datasize;
  1296. {$ifdef extdebug}
  1297. Comment(V_debug,'local symtable is at offset '+tostr(st.address_fixup));
  1298. exprasmList.concat(Tai_asm_comment.Create(strpnew(
  1299. 'local symtable is at offset '+tostr(st.address_fixup))));
  1300. {$endif extdebug}
  1301. end;
  1302. exprasmList.concat(Tai_Marker.Create(InlineStart));
  1303. {$ifdef extdebug}
  1304. exprasmList.concat(Tai_asm_comment.Create(strpnew('Start of inlined proc')));
  1305. {$endif extdebug}
  1306. {$ifdef GDB}
  1307. if (cs_debuginfo in aktmoduleswitches) then
  1308. begin
  1309. getaddrlabel(startlabel);
  1310. getaddrlabel(endlabel);
  1311. cg.a_label(exprasmlist,startlabel);
  1312. inlineprocdef.localst.symtabletype:=inlinelocalsymtable;
  1313. inlineprocdef.parast.symtabletype:=inlineparasymtable;
  1314. { Here we must include the para and local symtable info }
  1315. inlineprocdef.concatstabto(withdebuglist);
  1316. { set it back for safety }
  1317. inlineprocdef.localst.symtabletype:=localsymtable;
  1318. inlineprocdef.parast.symtabletype:=parasymtable;
  1319. mangled_length:=length(oldprocdef.mangledname);
  1320. getmem(pp,mangled_length+50);
  1321. strpcopy(pp,'192,0,0,'+startlabel.name);
  1322. if (target_info.use_function_relative_addresses) then
  1323. begin
  1324. strpcopy(strend(pp),'-');
  1325. strpcopy(strend(pp),oldprocdef.mangledname);
  1326. end;
  1327. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1328. end;
  1329. {$endif GDB}
  1330. { takes care of local data initialization }
  1331. inlineentrycode:=TAAsmoutput.Create;
  1332. inlineexitcode:=TAAsmoutput.Create;
  1333. ps:=para_size;
  1334. make_global:=false; { to avoid warning }
  1335. genentrycode(inlineentrycode,make_global,0,ps,nostackframe,true);
  1336. if po_assembler in aktprocdef.procoptions then
  1337. inlineentrycode.insert(Tai_marker.Create(asmblockstart));
  1338. exprasmList.concatlist(inlineentrycode);
  1339. secondpass(inlinetree);
  1340. genexitcode(inlineexitcode,0,false,true);
  1341. if po_assembler in aktprocdef.procoptions then
  1342. inlineexitcode.concat(Tai_marker.Create(asmblockend));
  1343. exprasmList.concatlist(inlineexitcode);
  1344. inlineentrycode.free;
  1345. inlineexitcode.free;
  1346. {$ifdef extdebug}
  1347. exprasmList.concat(Tai_asm_comment.Create(strpnew('End of inlined proc')));
  1348. {$endif extdebug}
  1349. exprasmList.concat(Tai_Marker.Create(InlineEnd));
  1350. {we can free the local data now, reset also the fixup address }
  1351. if st.datasize>0 then
  1352. begin
  1353. tg.ungetpersistanttemp(exprasmlist,st.address_fixup-st.datasize);
  1354. st.address_fixup:=0;
  1355. end;
  1356. { restore procinfo }
  1357. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1358. freemem(oldprocinfo,sizeof(tprocinfo));
  1359. {$ifdef GDB}
  1360. if (cs_debuginfo in aktmoduleswitches) then
  1361. begin
  1362. cg.a_label(exprasmlist,endlabel);
  1363. strpcopy(pp,'224,0,0,'+endlabel.name);
  1364. if (target_info.use_function_relative_addresses) then
  1365. begin
  1366. strpcopy(strend(pp),'-');
  1367. strpcopy(strend(pp),oldprocdef.mangledname);
  1368. end;
  1369. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1370. freemem(pp,mangled_length+50);
  1371. end;
  1372. {$endif GDB}
  1373. { restore }
  1374. aktprocdef:=oldprocdef;
  1375. aktexitlabel:=oldexitlabel;
  1376. aktexit2label:=oldexit2label;
  1377. quickexitlabel:=oldquickexitlabel;
  1378. inlining_procedure:=oldinlining_procedure;
  1379. { reallocate the registers used for the current procedure's regvars, }
  1380. { since they may have been used and then deallocated in the inlined }
  1381. { procedure (JM) }
  1382. if assigned(aktprocdef.regvarinfo) then
  1383. begin
  1384. rg.restoreStateAfterInline(oldregstate);
  1385. end;
  1386. end;
  1387. begin
  1388. ccallparanode:=ti386callparanode;
  1389. ccallnode:=ti386callnode;
  1390. cprocinlinenode:=ti386procinlinenode;
  1391. end.
  1392. {
  1393. $Log$
  1394. Revision 1.56 2002-07-01 18:46:31 peter
  1395. * internal linker
  1396. * reorganized aasm layer
  1397. Revision 1.55 2002/07/01 16:23:56 peter
  1398. * cg64 patch
  1399. * basics for currency
  1400. * asnode updates for class and interface (not finished)
  1401. Revision 1.54 2002/05/20 13:30:40 carl
  1402. * bugfix of hdisponen (base must be set, not index)
  1403. * more portability fixes
  1404. Revision 1.53 2002/05/18 13:34:23 peter
  1405. * readded missing revisions
  1406. Revision 1.52 2002/05/16 19:46:51 carl
  1407. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1408. + try to fix temp allocation (still in ifdef)
  1409. + generic constructor calls
  1410. + start of tassembler / tmodulebase class cleanup
  1411. Revision 1.50 2002/05/13 19:54:38 peter
  1412. * removed n386ld and n386util units
  1413. * maybe_save/maybe_restore added instead of the old maybe_push
  1414. Revision 1.49 2002/05/12 16:53:17 peter
  1415. * moved entry and exitcode to ncgutil and cgobj
  1416. * foreach gets extra argument for passing local data to the
  1417. iterator function
  1418. * -CR checks also class typecasts at runtime by changing them
  1419. into as
  1420. * fixed compiler to cycle with the -CR option
  1421. * fixed stabs with elf writer, finally the global variables can
  1422. be watched
  1423. * removed a lot of routines from cga unit and replaced them by
  1424. calls to cgobj
  1425. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1426. u32bit then the other is typecasted also to u32bit without giving
  1427. a rangecheck warning/error.
  1428. * fixed pascal calling method with reversing also the high tree in
  1429. the parast, detected by tcalcst3 test
  1430. Revision 1.48 2002/04/25 20:16:40 peter
  1431. * moved more routines from cga/n386util
  1432. Revision 1.47 2002/04/21 19:02:07 peter
  1433. * removed newn and disposen nodes, the code is now directly
  1434. inlined from pexpr
  1435. * -an option that will write the secondpass nodes to the .s file, this
  1436. requires EXTDEBUG define to actually write the info
  1437. * fixed various internal errors and crashes due recent code changes
  1438. Revision 1.46 2002/04/21 15:34:25 carl
  1439. * changeregsize -> rg.makeregsize
  1440. Revision 1.45 2002/04/15 19:44:21 peter
  1441. * fixed stackcheck that would be called recursively when a stack
  1442. error was found
  1443. * generic changeregsize(reg,size) for i386 register resizing
  1444. * removed some more routines from cga unit
  1445. * fixed returnvalue handling
  1446. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1447. Revision 1.44 2002/04/04 19:06:10 peter
  1448. * removed unused units
  1449. * use tlocation.size in cg.a_*loc*() routines
  1450. Revision 1.43 2002/04/02 17:11:35 peter
  1451. * tlocation,treference update
  1452. * LOC_CONSTANT added for better constant handling
  1453. * secondadd splitted in multiple routines
  1454. * location_force_reg added for loading a location to a register
  1455. of a specified size
  1456. * secondassignment parses now first the right and then the left node
  1457. (this is compatible with Kylix). This saves a lot of push/pop especially
  1458. with string operations
  1459. * adapted some routines to use the new cg methods
  1460. Revision 1.42 2002/03/31 20:26:38 jonas
  1461. + a_loadfpu_* and a_loadmm_* methods in tcg
  1462. * register allocation is now handled by a class and is mostly processor
  1463. independent (+rgobj.pas and i386/rgcpu.pas)
  1464. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1465. * some small improvements and fixes to the optimizer
  1466. * some register allocation fixes
  1467. * some fpuvaroffset fixes in the unary minus node
  1468. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1469. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1470. also better optimizable)
  1471. * fixed and optimized register saving/restoring for new/dispose nodes
  1472. * LOC_FPU locations now also require their "register" field to be set to
  1473. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1474. - list field removed of the tnode class because it's not used currently
  1475. and can cause hard-to-find bugs
  1476. Revision 1.41 2002/03/04 19:10:13 peter
  1477. * removed compiler warnings
  1478. }