ncgcal.pas 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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 ncgcal;
  19. {$i fpcdefs.inc}
  20. interface
  21. { $define AnsiStrRef}
  22. uses
  23. cpubase,
  24. globtype,
  25. symdef,node,ncal;
  26. type
  27. tcgcallparanode = class(tcallparanode)
  28. procedure secondcallparan(push_from_left_to_right:boolean;calloption:tproccalloption;
  29. para_alignment,para_offset : longint);override;
  30. end;
  31. tcgcallnode = class(tcallnode)
  32. private
  33. procedure release_para_temps;
  34. procedure normal_pass_2;
  35. procedure inlined_pass_2;
  36. protected
  37. refcountedtemp : treference;
  38. procedure handle_return_value;
  39. {# This routine is used to push the current frame pointer
  40. on the stack. This is used in nested routines where the
  41. value of the frame pointer is always pushed as an extra
  42. parameter.
  43. The default handling is the standard handling used on
  44. most stack based machines, where the frame pointer is
  45. the first invisible parameter.
  46. }
  47. function align_parasize:longint;virtual;
  48. procedure pop_parasize(pop_size:longint);virtual;
  49. procedure push_framepointer;virtual;
  50. procedure extra_interrupt_code;virtual;
  51. public
  52. procedure pass_2;override;
  53. end;
  54. implementation
  55. uses
  56. systems,
  57. cutils,verbose,globals,
  58. symconst,symbase,symsym,symtable,defutil,paramgr,
  59. {$ifdef GDB}
  60. {$ifdef delphi}
  61. sysutils,
  62. {$else}
  63. strings,
  64. {$endif}
  65. gdb,
  66. {$endif GDB}
  67. cginfo,cgbase,pass_2,
  68. cpuinfo,aasmbase,aasmtai,aasmcpu,
  69. nbas,nmem,nld,ncnv,
  70. {$ifdef x86}
  71. cga,
  72. {$endif x86}
  73. {$ifdef cpu64bit}
  74. cg64f64,
  75. {$else cpu64bit}
  76. cg64f32,
  77. {$endif cpu64bit}
  78. {$ifdef powerpc}
  79. cpupi,
  80. {$endif powerpc}
  81. ncgutil,cgobj,tgobj,regvars,rgobj,rgcpu;
  82. var
  83. { Current callnode, this is needed for having a link
  84. between the callparanodes and the callnode they belong to }
  85. aktcallnode : tcallnode;
  86. {*****************************************************************************
  87. TCGCALLPARANODE
  88. *****************************************************************************}
  89. procedure tcgcallparanode.secondcallparan(push_from_left_to_right:boolean;calloption:tproccalloption;para_alignment,para_offset : longint);
  90. var
  91. otlabel,
  92. oflabel : tasmlabel;
  93. tmpreg : tregister;
  94. href : treference;
  95. begin
  96. if not(assigned(paraitem.paratype.def) or
  97. assigned(paraitem.parasym)) then
  98. internalerror(200304242);
  99. { set default para_alignment to target_info.stackalignment }
  100. if para_alignment=0 then
  101. para_alignment:=aktalignment.paraalign;
  102. { push from left to right if specified }
  103. if push_from_left_to_right and assigned(right) then
  104. begin
  105. tcallparanode(right).secondcallparan(push_from_left_to_right,
  106. calloption,para_alignment,para_offset);
  107. end;
  108. otlabel:=truelabel;
  109. oflabel:=falselabel;
  110. objectlibrary.getlabel(truelabel);
  111. objectlibrary.getlabel(falselabel);
  112. secondpass(left);
  113. { handle varargs first, because defcoll is not valid }
  114. if (nf_varargs_para in flags) then
  115. begin
  116. if paramanager.push_addr_param(left.resulttype.def,calloption) then
  117. begin
  118. inc(pushedparasize,POINTER_SIZE);
  119. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  120. location_release(exprasmlist,left.location);
  121. end
  122. else
  123. push_value_para(exprasmlist,left,calloption,para_offset,para_alignment,paraitem.paraloc);
  124. end
  125. { hidden parameters }
  126. else if paraitem.is_hidden then
  127. begin
  128. { don't push a node that already generated a pointer type
  129. by address for implicit hidden parameters }
  130. if (vo_is_funcret in tvarsym(paraitem.parasym).varoptions) or
  131. (not(left.resulttype.def.deftype in [pointerdef,classrefdef]) and
  132. paramanager.push_addr_param(paraitem.paratype.def,calloption)) then
  133. begin
  134. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  135. internalerror(200305071);
  136. inc(pushedparasize,POINTER_SIZE);
  137. if calloption=pocall_inline then
  138. begin
  139. {$ifdef newra}
  140. tmpreg:=rg.getaddressregister(exprasmlist);
  141. {$else}
  142. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  143. {$endif}
  144. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  145. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  146. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  147. {$ifdef newra}
  148. rg.ungetregisterint(exprasmlist,tmpreg);
  149. {$else}
  150. cg.free_scratch_reg(exprasmlist,tmpreg);
  151. {$endif}
  152. end
  153. else
  154. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  155. location_release(exprasmlist,left.location);
  156. end
  157. else
  158. begin
  159. push_value_para(exprasmlist,left,calloption,
  160. para_offset,para_alignment,paraitem.paraloc);
  161. end;
  162. end
  163. { filter array of const c styled args }
  164. else if is_array_of_const(left.resulttype.def) and (nf_cargs in left.flags) then
  165. begin
  166. { nothing, everything is already pushed }
  167. end
  168. { in codegen.handleread.. paraitem.data is set to nil }
  169. else if assigned(paraitem.paratype.def) and
  170. (paraitem.paratype.def.deftype=formaldef) then
  171. begin
  172. { allow passing of a constant to a const formaldef }
  173. if (tvarsym(paraitem.parasym).varspez=vs_const) and
  174. (left.location.loc=LOC_CONSTANT) then
  175. location_force_mem(exprasmlist,left.location);
  176. { allow @var }
  177. inc(pushedparasize,POINTER_SIZE);
  178. if (left.nodetype=addrn) and
  179. (not(nf_procvarload in left.flags)) then
  180. begin
  181. if calloption=pocall_inline then
  182. begin
  183. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  184. cg.a_load_loc_ref(exprasmlist,left.location,href);
  185. end
  186. else
  187. cg.a_param_loc(exprasmlist,left.location,paraitem.paraloc);
  188. location_release(exprasmlist,left.location);
  189. end
  190. else
  191. begin
  192. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  193. internalerror(200304235);
  194. if calloption=pocall_inline then
  195. begin
  196. {$ifdef newra}
  197. tmpreg:=rg.getaddressregister(exprasmlist);
  198. {$else}
  199. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  200. {$endif newra}
  201. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  202. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  203. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  204. {$ifdef newra}
  205. rg.ungetregisterint(exprasmlist,tmpreg);
  206. {$else}
  207. cg.free_scratch_reg(exprasmlist,tmpreg);
  208. {$endif}
  209. end
  210. else
  211. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  212. location_release(exprasmlist,left.location);
  213. end;
  214. end
  215. { handle call by reference parameter }
  216. else if (paraitem.paratyp in [vs_var,vs_out]) then
  217. begin
  218. if (left.location.loc<>LOC_REFERENCE) then
  219. begin
  220. { passing self to a var parameter is allowed in
  221. TP and delphi }
  222. if not((left.location.loc=LOC_CREFERENCE) and
  223. is_self_node(left)) then
  224. internalerror(200106041);
  225. end;
  226. if (paraitem.paratyp=vs_out) and
  227. assigned(paraitem.paratype.def) and
  228. not is_class(paraitem.paratype.def) and
  229. paraitem.paratype.def.needs_inittable then
  230. cg.g_finalize(exprasmlist,paraitem.paratype.def,left.location.reference,false);
  231. inc(pushedparasize,POINTER_SIZE);
  232. if calloption=pocall_inline then
  233. begin
  234. {$ifdef newra}
  235. tmpreg:=rg.getaddressregister(exprasmlist);
  236. {$else}
  237. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  238. {$endif}
  239. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  240. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  241. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  242. {$ifdef newra}
  243. rg.ungetregisterint(exprasmlist,tmpreg);
  244. {$else}
  245. cg.free_scratch_reg(exprasmlist,tmpreg);
  246. {$endif}
  247. end
  248. else
  249. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  250. location_release(exprasmlist,left.location);
  251. end
  252. else
  253. begin
  254. { don't push a node that already generated a pointer type
  255. by address for implicit hidden parameters }
  256. if (not(
  257. paraitem.is_hidden and
  258. (left.resulttype.def.deftype in [pointerdef,classrefdef])
  259. ) and
  260. paramanager.push_addr_param(paraitem.paratype.def,calloption)) then
  261. begin
  262. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  263. begin
  264. { allow passing nil to a procvardef (methodpointer) }
  265. if (left.nodetype=typeconvn) and
  266. (left.resulttype.def.deftype=procvardef) and
  267. (ttypeconvnode(left).left.nodetype=niln) then
  268. begin
  269. tg.GetTemp(exprasmlist,tcgsize2size[left.location.size],tt_normal,href);
  270. if not (left.location.size in [OS_64,OS_S64]) then
  271. cg.a_load_loc_ref(exprasmlist,left.location,href)
  272. else
  273. cg64.a_load64_loc_ref(exprasmlist,left.location,href);
  274. location_reset(left.location,LOC_REFERENCE,left.location.size);
  275. left.location.reference:=href;
  276. end
  277. else
  278. internalerror(200204011);
  279. end;
  280. inc(pushedparasize,POINTER_SIZE);
  281. if calloption=pocall_inline then
  282. begin
  283. {$ifdef newra}
  284. tmpreg:=rg.getaddressregister(exprasmlist);
  285. {$else}
  286. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  287. {$endif}
  288. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  289. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  290. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  291. {$ifdef newra}
  292. rg.ungetregisterint(exprasmlist,tmpreg);
  293. {$else}
  294. cg.free_scratch_reg(exprasmlist,tmpreg);
  295. {$endif}
  296. end
  297. else
  298. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  299. location_release(exprasmlist,left.location);
  300. end
  301. else
  302. begin
  303. push_value_para(exprasmlist,left,calloption,
  304. para_offset,para_alignment,paraitem.paraloc);
  305. end;
  306. end;
  307. truelabel:=otlabel;
  308. falselabel:=oflabel;
  309. { update return location in callnode when this is the function
  310. result }
  311. if (vo_is_funcret in tvarsym(paraitem.parasym).varoptions) then
  312. begin
  313. location_copy(aktcallnode.location,left.location);
  314. end;
  315. { push from right to left }
  316. if not push_from_left_to_right and assigned(right) then
  317. begin
  318. tcallparanode(right).secondcallparan(push_from_left_to_right,
  319. calloption,para_alignment,para_offset);
  320. end;
  321. end;
  322. {*****************************************************************************
  323. TCGCALLNODE
  324. *****************************************************************************}
  325. procedure tcgcallnode.extra_interrupt_code;
  326. begin
  327. end;
  328. function tcgcallnode.align_parasize:longint;
  329. begin
  330. result:=0;
  331. end;
  332. procedure tcgcallnode.pop_parasize(pop_size:longint);
  333. begin
  334. end;
  335. procedure tcgcallnode.push_framepointer;
  336. var
  337. href : treference;
  338. hregister : tregister;
  339. i : integer;
  340. begin
  341. { this routine is itself not nested }
  342. if current_procdef.parast.symtablelevel=(tprocdef(procdefinition).parast.symtablelevel) then
  343. begin
  344. reference_reset_base(href,current_procinfo.framepointer,current_procinfo.framepointer_offset);
  345. cg.a_param_ref(exprasmlist,OS_ADDR,href,paramanager.getintparaloc(1));
  346. end
  347. { one nesting level }
  348. else if (current_procdef.parast.symtablelevel=(tprocdef(procdefinition).parast.symtablelevel)-1) then
  349. begin
  350. cg.a_param_reg(exprasmlist,OS_ADDR,current_procinfo.framepointer,paramanager.getintparaloc(1));
  351. end
  352. { very complex nesting level ... }
  353. else if (current_procdef.parast.symtablelevel>(tprocdef(procdefinition).parast.symtablelevel)) then
  354. begin
  355. hregister:=rg.getaddressregister(exprasmlist);
  356. reference_reset_base(href,current_procinfo.framepointer,current_procinfo.framepointer_offset);
  357. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  358. i:=current_procdef.parast.symtablelevel;
  359. while (i>tprocdef(procdefinition).parast.symtablelevel) do
  360. begin
  361. reference_reset_base(href,hregister,current_procinfo.framepointer_offset);
  362. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  363. dec(i);
  364. end;
  365. cg.a_param_reg(exprasmlist,OS_ADDR,hregister,paramanager.getintparaloc(1));
  366. rg.ungetaddressregister(exprasmlist,hregister);
  367. end;
  368. end;
  369. procedure tcgcallnode.handle_return_value;
  370. var
  371. cgsize : tcgsize;
  372. r,hregister : tregister;
  373. nr:Tnewregister;
  374. begin
  375. { structured results are easy to handle.... }
  376. { needed also when result_no_used !! }
  377. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  378. begin
  379. { Location should be setup by the funcret para }
  380. if location.loc<>LOC_REFERENCE then
  381. internalerror(200304241);
  382. end
  383. else
  384. { ansi/widestrings must be registered, so we can dispose them }
  385. if is_ansistring(resulttype.def) or
  386. is_widestring(resulttype.def) then
  387. begin
  388. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  389. location.reference:=refcountedtemp;
  390. r.enum:=accumulator;
  391. cg.a_reg_alloc(exprasmlist,r);
  392. cg.a_load_reg_ref(exprasmlist,OS_ADDR,r,location.reference);
  393. cg.a_reg_dealloc(exprasmlist,r);
  394. end
  395. else
  396. { we have only to handle the result if it is used }
  397. if (nf_return_value_used in flags) then
  398. begin
  399. if (resulttype.def.deftype=floatdef) then
  400. begin
  401. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  402. {$ifdef cpufpemu}
  403. if cs_fp_emulation in aktmoduleswitches then
  404. location.register.enum := accumulator
  405. else
  406. {$endif cpufpemu}
  407. location.register.enum:=FPU_RESULT_REG;
  408. {$ifdef x86}
  409. inc(trgcpu(rg).fpuvaroffset);
  410. {$else x86}
  411. hregister := rg.getregisterfpu(exprasmlist);
  412. cg.a_loadfpu_reg_reg(exprasmlist,location.register,hregister);
  413. location.register := hregister;
  414. {$endif x86}
  415. end
  416. else
  417. begin
  418. cgsize:=def_cgsize(resulttype.def);
  419. { an object constructor is a function with pointer result }
  420. if (procdefinition.proctypeoption=potype_constructor) then
  421. cgsize:=OS_ADDR;
  422. if cgsize<>OS_NO then
  423. begin
  424. location_reset(location,LOC_REGISTER,cgsize);
  425. {$ifndef cpu64bit}
  426. if cgsize in [OS_64,OS_S64] then
  427. begin
  428. {Move the function result to free registers, preferably the
  429. accumulator/accumulatorhigh, so no move is necessary.}
  430. r.enum:=R_INTREGISTER;
  431. r.number:=NR_ACCUMULATOR;
  432. hregister.enum:=R_INTREGISTER;
  433. hregister.number:=NR_ACCUMULATORHIGH;
  434. {$ifdef newra}
  435. rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATOR);
  436. rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATORHIGH);
  437. rg.ungetregisterint(exprasmlist,r);
  438. rg.ungetregisterint(exprasmlist,hregister);
  439. location.registerlow:=rg.getregisterint(exprasmlist,OS_INT);
  440. location.registerhigh:=rg.getregisterint(exprasmlist,OS_INT);
  441. {$else newra}
  442. if RS_ACCUMULATOR in rg.unusedregsint then
  443. location.registerlow:=rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATOR)
  444. else
  445. cg.a_reg_alloc(exprasmlist,r);
  446. if RS_ACCUMULATORHIGH in rg.unusedregsint then
  447. location.registerhigh:=rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATORHIGH)
  448. else
  449. cg.a_reg_alloc(exprasmlist,hregister);
  450. { do this after both low,high are allocated, else it is possible that
  451. low will be loaded in the register that still contains high }
  452. if location.registerlow.number=NR_NO then
  453. location.registerlow:=rg.getregisterint(exprasmlist,OS_INT);
  454. if location.registerhigh.number=NR_NO then
  455. location.registerhigh:=rg.getregisterint(exprasmlist,OS_INT);
  456. {$endif newra}
  457. cg64.a_load64_reg_reg(exprasmlist,joinreg64(r,hregister),
  458. location.register64);
  459. end
  460. else
  461. {$endif cpu64bit}
  462. begin
  463. {Move the function result to a free register, preferably the
  464. accumulator, so no move is necessary.}
  465. nr:=RS_ACCUMULATOR shl 8 or cgsize2subreg(cgsize);
  466. r.enum:=R_INTREGISTER;
  467. r.number:=nr;
  468. {$ifdef newra}
  469. rg.getexplicitregisterint(exprasmlist,nr);
  470. rg.ungetregisterint(exprasmlist,r);
  471. location.register:=rg.getregisterint(exprasmlist,cgsize);
  472. {$else newra}
  473. cg.a_reg_alloc(exprasmlist,r);
  474. if RS_ACCUMULATOR in rg.unusedregsint then
  475. location.register:=rg.getexplicitregisterint(exprasmlist,nr)
  476. else
  477. location.register:=rg.getregisterint(exprasmlist,cgsize);
  478. {$endif newra}
  479. cg.a_load_reg_reg(exprasmlist,cgsize,cgsize,r,location.register);
  480. end;
  481. end
  482. else
  483. begin
  484. if resulttype.def.size>0 then
  485. internalerror(200305131);
  486. end;
  487. end;
  488. end
  489. else
  490. location_reset(location,LOC_VOID,OS_NO);
  491. end;
  492. procedure tcgcallnode.release_para_temps;
  493. var
  494. hp : tnode;
  495. ppn : tcallparanode;
  496. begin
  497. { Release temps from parameters }
  498. ppn:=tcallparanode(left);
  499. while assigned(ppn) do
  500. begin
  501. if assigned(ppn.left) then
  502. begin
  503. { don't release the funcret temp }
  504. if not(vo_is_funcret in tvarsym(ppn.paraitem.parasym).varoptions) then
  505. location_freetemp(exprasmlist,ppn.left.location);
  506. { process also all nodes of an array of const }
  507. if ppn.left.nodetype=arrayconstructorn then
  508. begin
  509. if assigned(tarrayconstructornode(ppn.left).left) then
  510. begin
  511. hp:=ppn.left;
  512. while assigned(hp) do
  513. begin
  514. location_freetemp(exprasmlist,tarrayconstructornode(hp).left.location);
  515. hp:=tarrayconstructornode(hp).right;
  516. end;
  517. end;
  518. end;
  519. end;
  520. ppn:=tcallparanode(ppn.right);
  521. end;
  522. end;
  523. procedure tcgcallnode.normal_pass_2;
  524. var
  525. regs_to_push_int : Tsupregset;
  526. regs_to_push_other : tregisterset;
  527. unusedstate: pointer;
  528. pushedother : tpushedsavedother;
  529. pushedint : tpushedsavedint;
  530. oldpushedparasize : longint;
  531. { adress returned from an I/O-error }
  532. iolabel : tasmlabel;
  533. { help reference pointer }
  534. href : treference;
  535. pushedregs : tmaybesave;
  536. para_alignment,
  537. pop_size : longint;
  538. accreg : tregister;
  539. oldaktcallnode : tcallnode;
  540. begin
  541. if not assigned(procdefinition) then
  542. internalerror(200305264);
  543. iolabel:=nil;
  544. rg.saveunusedstate(unusedstate);
  545. { if we allocate the temp. location for ansi- or widestrings }
  546. { already here, we avoid later a push/pop }
  547. if is_widestring(resulttype.def) then
  548. begin
  549. tg.GetTemp(exprasmlist,pointer_size,tt_widestring,refcountedtemp);
  550. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  551. end
  552. else if is_ansistring(resulttype.def) then
  553. begin
  554. tg.GetTemp(exprasmlist,pointer_size,tt_ansistring,refcountedtemp);
  555. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  556. end;
  557. if (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_stdcall]) then
  558. para_alignment:=4
  559. else
  560. para_alignment:=aktalignment.paraalign;
  561. { proc variables destroy all registers }
  562. if (right=nil) and
  563. { virtual methods too }
  564. not(po_virtualmethod in procdefinition.procoptions) then
  565. begin
  566. if (cs_check_io in aktlocalswitches) and
  567. (po_iocheck in procdefinition.procoptions) and
  568. not(po_iocheck in current_procdef.procoptions) then
  569. begin
  570. objectlibrary.getaddrlabel(iolabel);
  571. cg.a_label(exprasmlist,iolabel);
  572. end
  573. else
  574. iolabel:=nil;
  575. { save all used registers and possible registers
  576. used for the return value }
  577. regs_to_push_int := tprocdef(procdefinition).usedintregisters;
  578. regs_to_push_other := tprocdef(procdefinition).usedotherregisters;
  579. if (not is_void(resulttype.def)) and
  580. (not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption)) then
  581. begin
  582. include(regs_to_push_int,RS_ACCUMULATOR);
  583. {$ifndef cpu64bit}
  584. if resulttype.def.size>sizeof(aword) then
  585. include(regs_to_push_int,RS_ACCUMULATORHIGH);
  586. {$endif cpu64bit}
  587. end;
  588. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  589. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  590. { on the ppc, ever procedure saves the non-volatile registers it uses itself }
  591. { and must make sure it saves its volatile registers before doing a call }
  592. {$ifdef i386}
  593. { give used registers through }
  594. rg.usedintinproc:=rg.usedintinproc + tprocdef(procdefinition).usedintregisters;
  595. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedotherregisters;
  596. {$endif i386}
  597. end
  598. else
  599. begin
  600. regs_to_push_int := all_intregisters;
  601. regs_to_push_other := all_registers;
  602. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  603. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  604. {$ifdef i386}
  605. rg.usedinproc:=all_registers;
  606. {$endif i386}
  607. { no IO check for methods and procedure variables }
  608. iolabel:=nil;
  609. end;
  610. { Initialize for pushing the parameters }
  611. oldpushedparasize:=pushedparasize;
  612. pushedparasize:=0;
  613. { Align stack if required }
  614. pop_size:=align_parasize;
  615. { Push parameters }
  616. oldaktcallnode:=aktcallnode;
  617. aktcallnode:=self;
  618. { process procvar. Done here already, because otherwise it may }
  619. { destroy registers containing a parameter for the actual }
  620. { function call (e.g. if it's a function, its result will }
  621. { overwrite r3, which contains the first parameter) (JM) }
  622. if assigned(right) then
  623. secondpass(right);
  624. if (po_virtualmethod in procdefinition.procoptions) and
  625. assigned(methodpointer) then
  626. begin
  627. secondpass(methodpointer);
  628. location_force_reg(exprasmlist,methodpointer.location,OS_ADDR,false);
  629. { virtual methods require an index }
  630. if tprocdef(procdefinition).extnumber=-1 then
  631. internalerror(200304021);
  632. { VMT should already be loaded in a register }
  633. if methodpointer.location.register.number=NR_NO then
  634. internalerror(200304022);
  635. { test validity of VMT }
  636. if not(is_interface(tprocdef(procdefinition)._class)) and
  637. not(is_cppclass(tprocdef(procdefinition)._class)) then
  638. cg.g_maybe_testvmt(exprasmlist,methodpointer.location.register,tprocdef(procdefinition)._class);
  639. end;
  640. if assigned(left) then
  641. begin
  642. {$ifndef newra}
  643. if assigned(right) then
  644. maybe_save(exprasmlist,left.registers32,right.location,pushedregs)
  645. else
  646. if assigned(methodpointer) then
  647. maybe_save(exprasmlist,left.registers32,methodpointer.location,pushedregs);
  648. {$endif}
  649. tcallparanode(left).secondcallparan(
  650. (po_leftright in procdefinition.procoptions),procdefinition.proccalloption,
  651. para_alignment,0);
  652. {$ifndef newra}
  653. if assigned(right) then
  654. maybe_restore(exprasmlist,right.location,pushedregs)
  655. else
  656. if assigned(methodpointer) then
  657. maybe_restore(exprasmlist,methodpointer.location,pushedregs);
  658. {$endif newra}
  659. end;
  660. aktcallnode:=oldaktcallnode;
  661. { procedure variable or normal function call ? }
  662. if (right=nil) then
  663. begin
  664. { push base pointer ?}
  665. if (current_procdef.parast.symtablelevel>=normal_function_level) and
  666. assigned(tprocdef(procdefinition).parast) and
  667. ((tprocdef(procdefinition).parast.symtablelevel)>normal_function_level) then
  668. push_framepointer;
  669. rg.saveintregvars(exprasmlist,regs_to_push_int);
  670. rg.saveotherregvars(exprasmlist,regs_to_push_other);
  671. if (po_virtualmethod in procdefinition.procoptions) and
  672. assigned(methodpointer) then
  673. begin
  674. { call method }
  675. reference_reset_base(href,methodpointer.location.register,
  676. tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber));
  677. cg.a_call_ref(exprasmlist,href);
  678. { release vmt register }
  679. rg.ungetaddressregister(exprasmlist,methodpointer.location.register);
  680. end
  681. else
  682. begin
  683. { Calling interrupt from the same code requires some
  684. extra code }
  685. if (po_interrupt in procdefinition.procoptions) then
  686. extra_interrupt_code;
  687. cg.a_call_name(exprasmlist,tprocdef(procdefinition).mangledname);
  688. end;
  689. end
  690. else
  691. { now procedure variable case }
  692. begin
  693. { Calling interrupt from the same code requires some
  694. extra code }
  695. if (po_interrupt in procdefinition.procoptions) then
  696. extra_interrupt_code;
  697. rg.saveintregvars(exprasmlist,ALL_INTREGISTERS);
  698. rg.saveotherregvars(exprasmlist,ALL_REGISTERS);
  699. cg.a_call_loc(exprasmlist,right.location);
  700. location_release(exprasmlist,right.location);
  701. location_freetemp(exprasmlist,right.location);
  702. end;
  703. { Need to remove the parameters from the stack? }
  704. if (po_clearstack in procdefinition.procoptions) then
  705. begin
  706. { the old pop_size was already included in pushedparasize }
  707. pop_size:=pushedparasize;
  708. { for Cdecl functions we don't need to pop the funcret when it
  709. was pushed by para }
  710. if paramanager.ret_in_param(procdefinition.rettype.def,procdefinition.proccalloption) then
  711. dec(pop_size,POINTER_SIZE);
  712. end;
  713. { Remove parameters/alignment from the stack }
  714. if pop_size>0 then
  715. pop_parasize(pop_size);
  716. {$ifdef powerpc}
  717. { this calculation must be done in pass_1 anyway, so don't worry }
  718. if tppcprocinfo(current_procinfo).maxpushedparasize<pushedparasize then
  719. tppcprocinfo(current_procinfo).maxpushedparasize:=pushedparasize;
  720. {$endif powerpc}
  721. { Restore }
  722. pushedparasize:=oldpushedparasize;
  723. rg.restoreunusedstate(unusedstate);
  724. {$ifdef TEMPREGDEBUG}
  725. testregisters32;
  726. {$endif TEMPREGDEBUG}
  727. { handle function results }
  728. if (not is_void(resulttype.def)) then
  729. handle_return_value
  730. else
  731. location_reset(location,LOC_VOID,OS_NO);
  732. { perhaps i/o check ? }
  733. if iolabel<>nil then
  734. begin
  735. reference_reset_symbol(href,iolabel,0);
  736. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  737. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  738. end;
  739. { restore registers }
  740. rg.restoreusedotherregisters(exprasmlist,pushedother);
  741. rg.restoreusedintregisters(exprasmlist,pushedint);
  742. { release temps of paras }
  743. release_para_temps;
  744. { if return value is not used }
  745. if (not(nf_return_value_used in flags)) and (not is_void(resulttype.def)) then
  746. begin
  747. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  748. begin
  749. { data which must be finalized ? }
  750. if (resulttype.def.needs_inittable) then
  751. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  752. { release unused temp }
  753. tg.ungetiftemp(exprasmlist,location.reference)
  754. end
  755. else if location.loc=LOC_FPUREGISTER then
  756. begin
  757. {$ifdef x86}
  758. { release FPU stack }
  759. accreg.enum:=FPU_RESULT_REG;
  760. emit_reg(A_FSTP,S_NO,accreg);
  761. {
  762. dec(trgcpu(rg).fpuvaroffset);
  763. do NOT decrement as the increment before
  764. is not called for unused results PM }
  765. {$endif x86}
  766. end;
  767. end;
  768. end;
  769. procedure tcgcallnode.inlined_pass_2;
  770. var
  771. regs_to_push_int : Tsupregset;
  772. regs_to_push_other : tregisterset;
  773. unusedstate: pointer;
  774. pushedother : tpushedsavedother;
  775. pushedint : tpushedsavedint;
  776. oldpushedparasize : longint;
  777. { adress returned from an I/O-error }
  778. iolabel : tasmlabel;
  779. { help reference pointer }
  780. href : treference;
  781. pushedregs : tmaybesave;
  782. accreg : tregister;
  783. oldaktcallnode : tcallnode;
  784. oldprocdef : tprocdef;
  785. i : longint;
  786. oldprocinfo : tprocinfo;
  787. oldinlining_procedure : boolean;
  788. inlineentrycode,inlineexitcode : TAAsmoutput;
  789. oldexitlabel:tasmlabel;
  790. oldregstate: pointer;
  791. old_local_fixup,
  792. old_para_fixup : longint;
  793. pararef,
  794. localsref : treference;
  795. {$ifdef GDB}
  796. startlabel,endlabel : tasmlabel;
  797. pp : pchar;
  798. mangled_length : longint;
  799. {$endif GDB}
  800. begin
  801. if not(assigned(procdefinition) and (procdefinition.deftype=procdef)) then
  802. internalerror(200305262);
  803. oldinlining_procedure:=inlining_procedure;
  804. oldexitlabel:=aktexitlabel;
  805. oldprocdef:=current_procdef;
  806. oldprocinfo:=current_procinfo;
  807. objectlibrary.getlabel(aktexitlabel);
  808. { we're inlining a procedure }
  809. inlining_procedure:=true;
  810. { deallocate the registers used for the current procedure's regvars }
  811. if assigned(current_procdef.regvarinfo) then
  812. begin
  813. with pregvarinfo(current_procdef.regvarinfo)^ do
  814. for i := 1 to maxvarregs do
  815. if assigned(regvars[i]) then
  816. store_regvar(exprasmlist,regvars[i].reg);
  817. rg.saveStateForInline(oldregstate);
  818. { make sure the register allocator knows what the regvars in the }
  819. { inlined code block are (JM) }
  820. rg.resetusableregisters;
  821. rg.clearregistercount;
  822. {$ifndef newra}
  823. rg.cleartempgen;
  824. {$endif}
  825. if assigned(tprocdef(procdefinition).regvarinfo) then
  826. with pregvarinfo(tprocdef(procdefinition).regvarinfo)^ do
  827. for i := 1 to maxvarregs do
  828. if assigned(regvars[i]) then
  829. begin
  830. {Fix me!!}
  831. {tmpreg:=rg.makeregsize(regvars[i].reg,OS_INT);
  832. rg.makeregvar(tmpreg);}
  833. internalerror(200301232);
  834. end;
  835. end;
  836. { create temp procinfo }
  837. current_procinfo:=cprocinfo.create(nil);
  838. current_procinfo.procdef:=tprocdef(procdefinition);
  839. current_procdef:=current_procinfo.procdef;
  840. { Localsymtable }
  841. current_procdef.localst.symtablelevel:=oldprocdef.localst.symtablelevel;
  842. if current_procdef.localst.datasize>0 then
  843. begin
  844. old_local_fixup:=current_procdef.localst.address_fixup;
  845. tg.GetTemp(exprasmlist,current_procdef.localst.datasize,tt_persistent,localsref);
  846. if tg.direction>0 then
  847. current_procdef.localst.address_fixup:=localsref.offset
  848. else
  849. current_procdef.localst.address_fixup:=localsref.offset+current_procdef.localst.datasize;
  850. {$ifdef extdebug}
  851. Comment(V_debug,'inlined local symtable ('+tostr(current_procdef.localst.datasize)+' bytes) is at offset '+tostr(current_procdef.localst.address_fixup));
  852. exprasmList.concat(tai_comment.Create(strpnew(
  853. 'inlined local symtable ('+tostr(current_procdef.localst.datasize)+' bytes) is at offset '+tostr(current_procdef.localst.address_fixup))));
  854. {$endif extdebug}
  855. end;
  856. { Parasymtable }
  857. current_procdef.parast.symtablelevel:=oldprocdef.localst.symtablelevel;
  858. if current_procdef.parast.datasize>0 then
  859. begin
  860. old_para_fixup:=current_procdef.parast.address_fixup;
  861. tg.GetTemp(exprasmlist,current_procdef.parast.datasize,tt_persistent,pararef);
  862. current_procdef.parast.address_fixup:=pararef.offset;
  863. {$ifdef extdebug}
  864. Comment(V_debug,'inlined para symtable ('+tostr(current_procdef.parast.datasize)+' bytes) is at offset '+tostr(current_procdef.parast.address_fixup));
  865. exprasmList.concat(tai_comment.Create(strpnew(
  866. 'inlined para symtable ('+tostr(current_procdef.parast.datasize)+' bytes) is at offset '+tostr(current_procdef.parast.address_fixup))));
  867. {$endif extdebug}
  868. end;
  869. { Calculate offsets }
  870. current_procinfo.after_header;
  871. exprasmList.concat(Tai_Marker.Create(InlineStart));
  872. {$ifdef extdebug}
  873. exprasmList.concat(tai_comment.Create(strpnew('Start of inlined proc')));
  874. {$endif extdebug}
  875. {$ifdef GDB}
  876. if (cs_debuginfo in aktmoduleswitches) then
  877. begin
  878. objectlibrary.getaddrlabel(startlabel);
  879. objectlibrary.getaddrlabel(endlabel);
  880. cg.a_label(exprasmlist,startlabel);
  881. tprocdef(procdefinition).localst.symtabletype:=inlinelocalsymtable;
  882. procdefinition.parast.symtabletype:=inlineparasymtable;
  883. { Here we must include the para and local symtable info }
  884. procdefinition.concatstabto(withdebuglist);
  885. { set it back for safety }
  886. tprocdef(procdefinition).localst.symtabletype:=localsymtable;
  887. procdefinition.parast.symtabletype:=parasymtable;
  888. mangled_length:=length(oldprocdef.mangledname);
  889. getmem(pp,mangled_length+50);
  890. strpcopy(pp,'192,0,0,'+startlabel.name);
  891. if (target_info.use_function_relative_addresses) then
  892. begin
  893. strpcopy(strend(pp),'-');
  894. strpcopy(strend(pp),oldprocdef.mangledname);
  895. end;
  896. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  897. end;
  898. {$endif GDB}
  899. iolabel:=nil;
  900. rg.saveunusedstate(unusedstate);
  901. { if we allocate the temp. location for ansi- or widestrings }
  902. { already here, we avoid later a push/pop }
  903. if is_widestring(resulttype.def) then
  904. begin
  905. tg.GetTemp(exprasmlist,pointer_size,tt_widestring,refcountedtemp);
  906. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  907. end
  908. else if is_ansistring(resulttype.def) then
  909. begin
  910. tg.GetTemp(exprasmlist,pointer_size,tt_ansistring,refcountedtemp);
  911. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  912. end;
  913. if (cs_check_io in aktlocalswitches) and
  914. (po_iocheck in procdefinition.procoptions) and
  915. not(po_iocheck in current_procdef.procoptions) then
  916. begin
  917. objectlibrary.getaddrlabel(iolabel);
  918. cg.a_label(exprasmlist,iolabel);
  919. end
  920. else
  921. iolabel:=nil;
  922. { save all used registers and possible registers
  923. used for the return value }
  924. regs_to_push_int := tprocdef(procdefinition).usedintregisters;
  925. regs_to_push_other := tprocdef(procdefinition).usedotherregisters;
  926. if (not is_void(resulttype.def)) and
  927. (not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption)) then
  928. begin
  929. include(regs_to_push_int,RS_ACCUMULATOR);
  930. {$ifndef cpu64bit}
  931. if resulttype.def.size>sizeof(aword) then
  932. include(regs_to_push_int,RS_ACCUMULATORHIGH);
  933. {$endif cpu64bit}
  934. end;
  935. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  936. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  937. {$ifdef i386}
  938. { give used registers through }
  939. rg.usedintinproc:=rg.usedintinproc + tprocdef(procdefinition).usedintregisters;
  940. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedotherregisters;
  941. {$endif i386}
  942. { Initialize for pushing the parameters }
  943. oldpushedparasize:=pushedparasize;
  944. pushedparasize:=0;
  945. { Push parameters }
  946. oldaktcallnode:=aktcallnode;
  947. aktcallnode:=self;
  948. if assigned(left) then
  949. begin
  950. {$ifndef newra}
  951. if assigned(right) then
  952. maybe_save(exprasmlist,left.registers32,right.location,pushedregs)
  953. else
  954. if assigned(methodpointer) then
  955. maybe_save(exprasmlist,left.registers32,methodpointer.location,pushedregs);
  956. {$endif}
  957. { we push from right to left, so start with parameters at the end of
  958. the parameter block }
  959. tcallparanode(left).secondcallparan(
  960. (po_leftright in procdefinition.procoptions),procdefinition.proccalloption,
  961. 0,procdefinition.parast.address_fixup+procdefinition.parast.datasize);
  962. {$ifndef newra}
  963. if assigned(right) then
  964. maybe_restore(exprasmlist,right.location,pushedregs)
  965. else
  966. if assigned(methodpointer) then
  967. maybe_restore(exprasmlist,methodpointer.location,pushedregs);
  968. {$endif newra}
  969. end;
  970. aktcallnode:=oldaktcallnode;
  971. rg.saveintregvars(exprasmlist,regs_to_push_int);
  972. rg.saveotherregvars(exprasmlist,regs_to_push_other);
  973. { takes care of local data initialization }
  974. inlineentrycode:=TAAsmoutput.Create;
  975. inlineexitcode:=TAAsmoutput.Create;
  976. geninlineentrycode(inlineentrycode,0);
  977. if po_assembler in current_procdef.procoptions then
  978. inlineentrycode.insert(Tai_marker.Create(asmblockstart));
  979. exprasmList.concatlist(inlineentrycode);
  980. { process the inline code }
  981. secondpass(inlinecode);
  982. {$ifdef powerpc}
  983. { this calculation must be done in pass_1 anyway, so don't worry }
  984. if tppcprocinfo(current_procinfo).maxpushedparasize<pushedparasize then
  985. tppcprocinfo(current_procinfo).maxpushedparasize:=pushedparasize;
  986. {$endif powerpc}
  987. { Restore }
  988. pushedparasize:=oldpushedparasize;
  989. rg.restoreunusedstate(unusedstate);
  990. {$ifdef TEMPREGDEBUG}
  991. testregisters32;
  992. {$endif TEMPREGDEBUG}
  993. geninlineexitcode(inlineexitcode,true);
  994. if po_assembler in current_procdef.procoptions then
  995. inlineexitcode.concat(Tai_marker.Create(asmblockend));
  996. exprasmList.concatlist(inlineexitcode);
  997. inlineentrycode.free;
  998. inlineexitcode.free;
  999. {$ifdef extdebug}
  1000. exprasmList.concat(tai_comment.Create(strpnew('End of inlined proc')));
  1001. {$endif extdebug}
  1002. exprasmList.concat(Tai_Marker.Create(InlineEnd));
  1003. {we can free the local data now, reset also the fixup address }
  1004. if current_procdef.localst.datasize>0 then
  1005. begin
  1006. tg.UnGetTemp(exprasmlist,localsref);
  1007. current_procdef.localst.address_fixup:=old_local_fixup;
  1008. end;
  1009. {we can free the para data now, reset also the fixup address }
  1010. if current_procdef.parast.datasize>0 then
  1011. begin
  1012. tg.UnGetTemp(exprasmlist,pararef);
  1013. current_procdef.parast.address_fixup:=old_para_fixup;
  1014. end;
  1015. { free return reference }
  1016. if (resulttype.def.size>0) then
  1017. begin
  1018. { from now on the result can be freed normally }
  1019. // if assigned(funcretnode) and
  1020. // paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  1021. // tg.ChangeTempType(exprasmlist,funcretnode.location.reference,tt_normal);
  1022. end;
  1023. { handle function results }
  1024. if (not is_void(resulttype.def)) then
  1025. handle_return_value
  1026. else
  1027. location_reset(location,LOC_VOID,OS_NO);
  1028. { perhaps i/o check ? }
  1029. if iolabel<>nil then
  1030. begin
  1031. reference_reset_symbol(href,iolabel,0);
  1032. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  1033. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  1034. end;
  1035. { restore registers }
  1036. rg.restoreusedotherregisters(exprasmlist,pushedother);
  1037. rg.restoreusedintregisters(exprasmlist,pushedint);
  1038. { release temps of paras }
  1039. release_para_temps;
  1040. { if return value is not used }
  1041. if (not is_void(resulttype.def)) and
  1042. (not(nf_return_value_used in flags)) then
  1043. begin
  1044. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  1045. begin
  1046. { data which must be finalized ? }
  1047. if (resulttype.def.needs_inittable) then
  1048. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  1049. { release unused temp }
  1050. tg.ungetiftemp(exprasmlist,location.reference)
  1051. end
  1052. else if location.loc=LOC_FPUREGISTER then
  1053. begin
  1054. {$ifdef x86}
  1055. { release FPU stack }
  1056. accreg.enum:=FPU_RESULT_REG;
  1057. emit_reg(A_FSTP,S_NO,accreg);
  1058. {
  1059. dec(trgcpu(rg).fpuvaroffset);
  1060. do NOT decrement as the increment before
  1061. is not called for unused results PM }
  1062. {$endif x86}
  1063. end;
  1064. end;
  1065. { release procinfo }
  1066. current_procinfo.free;
  1067. current_procinfo:=oldprocinfo;
  1068. {$ifdef GDB}
  1069. if (cs_debuginfo in aktmoduleswitches) then
  1070. begin
  1071. cg.a_label(exprasmlist,endlabel);
  1072. strpcopy(pp,'224,0,0,'+endlabel.name);
  1073. if (target_info.use_function_relative_addresses) then
  1074. begin
  1075. strpcopy(strend(pp),'-');
  1076. strpcopy(strend(pp),oldprocdef.mangledname);
  1077. end;
  1078. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1079. freemem(pp,mangled_length+50);
  1080. end;
  1081. {$endif GDB}
  1082. { restore }
  1083. current_procdef:=oldprocdef;
  1084. aktexitlabel:=oldexitlabel;
  1085. inlining_procedure:=oldinlining_procedure;
  1086. { reallocate the registers used for the current procedure's regvars, }
  1087. { since they may have been used and then deallocated in the inlined }
  1088. { procedure (JM) }
  1089. if assigned(current_procdef.regvarinfo) then
  1090. rg.restoreStateAfterInline(oldregstate);
  1091. end;
  1092. procedure tcgcallnode.pass_2;
  1093. begin
  1094. if assigned(inlinecode) then
  1095. inlined_pass_2
  1096. else
  1097. normal_pass_2;
  1098. end;
  1099. begin
  1100. ccallparanode:=tcgcallparanode;
  1101. ccallnode:=tcgcallnode;
  1102. end.
  1103. {
  1104. $Log$
  1105. Revision 1.76 2003-05-28 23:58:18 jonas
  1106. * added missing initialization of rg.usedint{in,by}proc
  1107. * ppc now also saves/restores used fpu registers
  1108. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  1109. i386
  1110. Revision 1.75 2003/05/26 21:17:17 peter
  1111. * procinlinenode removed
  1112. * aktexit2label removed, fast exit removed
  1113. + tcallnode.inlined_pass_2 added
  1114. Revision 1.74 2003/05/25 11:34:17 peter
  1115. * methodpointer self pushing fixed
  1116. Revision 1.73 2003/05/25 08:59:16 peter
  1117. * inline fixes
  1118. Revision 1.72 2003/05/24 13:36:54 jonas
  1119. * save fpu results in a normal fpu register on non-x86 processors
  1120. Revision 1.71 2003/05/23 19:35:50 jonas
  1121. - undid previous commit, it was wrong
  1122. Revision 1.70 2003/05/23 19:11:58 jonas
  1123. * fixed tests for whether a certain int register is unused
  1124. Revision 1.69 2003/05/23 18:01:56 jonas
  1125. * fixed ppc compiler
  1126. Revision 1.68 2003/05/23 14:27:35 peter
  1127. * remove some unit dependencies
  1128. * current_procinfo changes to store more info
  1129. Revision 1.67 2003/05/17 13:30:08 jonas
  1130. * changed tt_persistant to tt_persistent :)
  1131. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1132. temps, but a ttemptype, so you can also create ansistring temps etc
  1133. Revision 1.66 2003/05/16 14:33:31 peter
  1134. * regvar fixes
  1135. Revision 1.65 2003/05/15 18:58:53 peter
  1136. * removed selfpointer_offset, vmtpointer_offset
  1137. * tvarsym.adjusted_address
  1138. * address in localsymtable is now in the real direction
  1139. * removed some obsolete globals
  1140. Revision 1.64 2003/05/14 19:36:54 jonas
  1141. * patch from Peter for int64 function results
  1142. Revision 1.63 2003/05/13 19:14:41 peter
  1143. * failn removed
  1144. * inherited result code check moven to pexpr
  1145. Revision 1.62 2003/05/13 15:18:18 peter
  1146. * generate code for procvar first before pushing parameters. Made
  1147. the already existing code for powerpc available for all platforms
  1148. Revision 1.61 2003/05/12 18:17:55 jonas
  1149. * moved fpc_check_object call earlier for the ppc, so it can't destroy
  1150. already-loaded parameter registers
  1151. Revision 1.60 2003/05/11 21:48:38 jonas
  1152. * fixed procvar bug on the ppc (load procvar before loading para's,
  1153. because the procvar may otherwise destroy the already loaded paras)
  1154. Revision 1.59 2003/05/09 17:47:02 peter
  1155. * self moved to hidden parameter
  1156. * removed hdisposen,hnewn,selfn
  1157. Revision 1.58 2003/05/05 14:53:16 peter
  1158. * vs_hidden replaced by is_hidden boolean
  1159. Revision 1.57 2003/04/30 20:53:32 florian
  1160. * error when address of an abstract method is taken
  1161. * fixed some x86-64 problems
  1162. * merged some more x86-64 and i386 code
  1163. Revision 1.56 2003/04/29 07:28:52 michael
  1164. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1165. Revision 1.55 2003/04/27 11:21:33 peter
  1166. * aktprocdef renamed to current_procdef
  1167. * procinfo renamed to current_procinfo
  1168. * procinfo will now be stored in current_module so it can be
  1169. cleaned up properly
  1170. * gen_main_procsym changed to create_main_proc and release_main_proc
  1171. to also generate a tprocinfo structure
  1172. * fixed unit implicit initfinal
  1173. Revision 1.54 2003/04/27 07:29:50 peter
  1174. * current_procdef cleanup, current_procdef is now always nil when parsing
  1175. a new procdef declaration
  1176. * aktprocsym removed
  1177. * lexlevel removed, use symtable.symtablelevel instead
  1178. * implicit init/final code uses the normal genentry/genexit
  1179. * funcret state checking updated for new funcret handling
  1180. Revision 1.53 2003/04/25 20:59:33 peter
  1181. * removed funcretn,funcretsym, function result is now in varsym
  1182. and aliases for result and function name are added using absolutesym
  1183. * vs_hidden parameter for funcret passed in parameter
  1184. * vs_hidden fixes
  1185. * writenode changed to printnode and released from extdebug
  1186. * -vp option added to generate a tree.log with the nodetree
  1187. * nicer printnode for statements, callnode
  1188. Revision 1.52 2003/04/25 08:25:26 daniel
  1189. * Ifdefs around a lot of calls to cleartempgen
  1190. * Fixed registers that are allocated but not freed in several nodes
  1191. * Tweak to register allocator to cause less spills
  1192. * 8-bit registers now interfere with esi,edi and ebp
  1193. Compiler can now compile rtl successfully when using new register
  1194. allocator
  1195. Revision 1.51 2003/04/22 23:50:22 peter
  1196. * firstpass uses expectloc
  1197. * checks if there are differences between the expectloc and
  1198. location.loc from secondpass in EXTDEBUG
  1199. Revision 1.50 2003/04/22 14:33:38 peter
  1200. * removed some notes/hints
  1201. Revision 1.49 2003/04/22 13:47:08 peter
  1202. * fixed C style array of const
  1203. * fixed C array passing
  1204. * fixed left to right with high parameters
  1205. Revision 1.48 2003/04/22 10:09:34 daniel
  1206. + Implemented the actual register allocator
  1207. + Scratch registers unavailable when new register allocator used
  1208. + maybe_save/maybe_restore unavailable when new register allocator used
  1209. Revision 1.47 2003/04/22 09:49:44 peter
  1210. * do not load self when calling a non-inherited class constructor
  1211. Revision 1.46 2003/04/21 20:03:32 peter
  1212. * forgot to copy vmtrefaddr to selfrefaddr when self=vmt
  1213. Revision 1.45 2003/04/21 13:53:16 jonas
  1214. - removed copying of all paras when secondpassing a callnode (this used
  1215. to be necessary for inlinign support, but currently the whole inlined
  1216. procedure is already copied in advance). Note that the compiler crashes
  1217. when compiling ucomplex with -dTEST_INLINE (also after fixing the
  1218. syntax errors), but that was also the case before this change.
  1219. Revision 1.44 2003/04/10 17:57:52 peter
  1220. * vs_hidden released
  1221. Revision 1.43 2003/04/06 21:11:23 olle
  1222. * changed newasmsymbol to newasmsymboldata for data symbols
  1223. Revision 1.42 2003/04/04 15:38:56 peter
  1224. * moved generic code from n386cal to ncgcal, i386 now also
  1225. uses the generic ncgcal
  1226. Revision 1.41 2003/03/28 19:16:56 peter
  1227. * generic constructor working for i386
  1228. * remove fixed self register
  1229. * esi added as address register for i386
  1230. Revision 1.40 2003/03/06 11:35:50 daniel
  1231. * Fixed internalerror 7843 issue
  1232. Revision 1.39 2003/02/19 22:00:14 daniel
  1233. * Code generator converted to new register notation
  1234. - Horribily outdated todo.txt removed
  1235. Revision 1.38 2003/02/15 22:17:38 carl
  1236. * bugfix of FPU emulation code
  1237. Revision 1.37 2003/02/12 22:10:07 carl
  1238. * load_frame_pointer is now generic
  1239. * change fpu emulation routine names
  1240. Revision 1.36 2003/01/30 21:46:57 peter
  1241. * self fixes for static methods (merged)
  1242. Revision 1.35 2003/01/22 20:45:15 mazen
  1243. * making math code in RTL compiling.
  1244. *NB : This does NOT mean necessary that it will generate correct code!
  1245. Revision 1.34 2003/01/17 12:03:45 daniel
  1246. * Optalign conditional code adapted to record Tregister
  1247. Revision 1.33 2003/01/08 18:43:56 daniel
  1248. * Tregister changed into a record
  1249. Revision 1.32 2002/12/15 22:50:00 florian
  1250. + some stuff for the new hidden parameter handling added
  1251. Revision 1.31 2002/12/15 21:30:12 florian
  1252. * tcallnode.paraitem introduced, all references to defcoll removed
  1253. Revision 1.30 2002/11/27 20:04:39 peter
  1254. * cdecl array of const fixes
  1255. Revision 1.29 2002/11/25 17:43:17 peter
  1256. * splitted defbase in defutil,symutil,defcmp
  1257. * merged isconvertable and is_equal into compare_defs(_ext)
  1258. * made operator search faster by walking the list only once
  1259. Revision 1.28 2002/11/18 17:31:54 peter
  1260. * pass proccalloption to ret_in_xxx and push_xxx functions
  1261. Revision 1.27 2002/11/16 15:34:30 florian
  1262. * generic location for float results
  1263. Revision 1.26 2002/11/15 01:58:51 peter
  1264. * merged changes from 1.0.7 up to 04-11
  1265. - -V option for generating bug report tracing
  1266. - more tracing for option parsing
  1267. - errors for cdecl and high()
  1268. - win32 import stabs
  1269. - win32 records<=8 are returned in eax:edx (turned off by default)
  1270. - heaptrc update
  1271. - more info for temp management in .s file with EXTDEBUG
  1272. Revision 1.25 2002/10/05 12:43:25 carl
  1273. * fixes for Delphi 6 compilation
  1274. (warning : Some features do not work under Delphi)
  1275. Revision 1.24 2002/09/30 07:00:45 florian
  1276. * fixes to common code to get the alpha compiler compiled applied
  1277. Revision 1.23 2002/09/17 18:54:02 jonas
  1278. * a_load_reg_reg() now has two size parameters: source and dest. This
  1279. allows some optimizations on architectures that don't encode the
  1280. register size in the register name.
  1281. Revision 1.22 2002/09/07 15:25:02 peter
  1282. * old logs removed and tabs fixed
  1283. Revision 1.21 2002/09/07 11:50:02 jonas
  1284. * fixed small regalloction info bug
  1285. Revision 1.20 2002/09/02 11:25:20 florian
  1286. * fixed generic procedure variable calling
  1287. Revision 1.19 2002/09/01 21:04:48 florian
  1288. * several powerpc related stuff fixed
  1289. Revision 1.18 2002/09/01 18:43:27 peter
  1290. * include accumulator in regs_to_push list
  1291. Revision 1.17 2002/09/01 12:13:00 peter
  1292. * use a_call_reg
  1293. * ungetiftemp for procvar of object temp
  1294. Revision 1.16 2002/08/25 19:25:18 peter
  1295. * sym.insert_in_data removed
  1296. * symtable.insertvardata/insertconstdata added
  1297. * removed insert_in_data call from symtable.insert, it needs to be
  1298. called separatly. This allows to deref the address calculation
  1299. * procedures now calculate the parast addresses after the procedure
  1300. directives are parsed. This fixes the cdecl parast problem
  1301. * push_addr_param has an extra argument that specifies if cdecl is used
  1302. or not
  1303. Revision 1.15 2002/08/23 16:14:48 peter
  1304. * tempgen cleanup
  1305. * tt_noreuse temp type added that will be used in genentrycode
  1306. Revision 1.14 2002/08/20 16:55:38 peter
  1307. * don't write (stabs)line info when inlining a procedure
  1308. Revision 1.13 2002/08/19 19:36:42 peter
  1309. * More fixes for cross unit inlining, all tnodes are now implemented
  1310. * Moved pocall_internconst to po_internconst because it is not a
  1311. calling type at all and it conflicted when inlining of these small
  1312. functions was requested
  1313. Revision 1.12 2002/08/18 20:06:23 peter
  1314. * inlining is now also allowed in interface
  1315. * renamed write/load to ppuwrite/ppuload
  1316. * tnode storing in ppu
  1317. * nld,ncon,nbas are already updated for storing in ppu
  1318. Revision 1.11 2002/08/17 22:09:44 florian
  1319. * result type handling in tcgcal.pass_2 overhauled
  1320. * better tnode.dowrite
  1321. * some ppc stuff fixed
  1322. Revision 1.10 2002/08/17 09:23:35 florian
  1323. * first part of procinfo rewrite
  1324. Revision 1.9 2002/08/13 21:40:55 florian
  1325. * more fixes for ppc calling conventions
  1326. Revision 1.8 2002/08/13 18:01:51 carl
  1327. * rename swatoperands to swapoperands
  1328. + m68k first compilable version (still needs a lot of testing):
  1329. assembler generator, system information , inline
  1330. assembler reader.
  1331. Revision 1.7 2002/08/12 15:08:39 carl
  1332. + stab register indexes for powerpc (moved from gdb to cpubase)
  1333. + tprocessor enumeration moved to cpuinfo
  1334. + linker in target_info is now a class
  1335. * many many updates for m68k (will soon start to compile)
  1336. - removed some ifdef or correct them for correct cpu
  1337. Revision 1.6 2002/08/11 14:32:26 peter
  1338. * renamed current_library to objectlibrary
  1339. Revision 1.5 2002/08/11 13:24:11 peter
  1340. * saving of asmsymbols in ppu supported
  1341. * asmsymbollist global is removed and moved into a new class
  1342. tasmlibrarydata that will hold the info of a .a file which
  1343. corresponds with a single module. Added librarydata to tmodule
  1344. to keep the library info stored for the module. In the future the
  1345. objectfiles will also be stored to the tasmlibrarydata class
  1346. * all getlabel/newasmsymbol and friends are moved to the new class
  1347. Revision 1.4 2002/08/06 20:55:20 florian
  1348. * first part of ppc calling conventions fix
  1349. Revision 1.3 2002/07/20 11:57:53 florian
  1350. * types.pas renamed to defbase.pas because D6 contains a types
  1351. unit so this would conflicts if D6 programms are compiled
  1352. + Willamette/SSE2 instructions to assembler added
  1353. Revision 1.2 2002/07/13 19:38:43 florian
  1354. * some more generic calling stuff fixed
  1355. }